-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: restructured test config, add test to media endpoint" (#8)
* hotfix: update gitignore * hotfix: update docker-compose file - remove env-files config * hotfix: structured and enhancement project (#3) * hotfix: update readme project * feat: add new environment variable * feat: add package beanie-odm * fix: remove unsing config in docker-compose file * feat: update botoclient function * feat: cleanup error codes and add news * feat: remove unusing functions and update utils functionnal * feat: define bucket policy config * test: add tests for endpoints and functional (#5) * test: restructured test config, add test to media endpoint (#7) * test: add tests for endpoints and functional * test: restructured test config, add test to media endpoint
- Loading branch information
1 parent
0dcbeab
commit 1ff458a
Showing
7 changed files
with
183 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from src.config.database import shutdown_db_client, startup_db_client | ||
|
||
|
||
@pytest.mark.asyncio | ||
@mock.patch("src.config.database.init_beanie", return_value=None) | ||
async def test_startup_db_client(mock_init_beanie, fixture_client_mongo, mock_app_instance, fixture_models): | ||
await startup_db_client(app=mock_app_instance, models=[fixture_models.Bucket, fixture_models.Media]) | ||
|
||
assert mock_app_instance.mongo_db_client is not None | ||
assert fixture_client_mongo.is_mongos is True | ||
|
||
""" | ||
mock_init_beanie.assert_called_once_with( | ||
database=mock_app_instance.mongo_db_client[settings.MONGO_DB], | ||
document_models=[fixture_models.Bucket, fixture_models.Media], | ||
multiprocessing_mode=True, | ||
) | ||
""" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_shutdown_db_client(mock_app_instance): | ||
mock_app_instance.mongo_db_client = mock.AsyncMock() | ||
await shutdown_db_client(app=mock_app_instance) | ||
mock_app_instance.mongo_db_client.close.assert_called_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import pytest | ||
from starlette import status | ||
from src.common.error_codes import SfsErrorCodes | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_all_media_without_data(http_client_api): | ||
response = await http_client_api.get("/media") | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
assert response.json() == {"items": [], "total": 0, "page": 1, "size": None, "pages": 0} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_media_with_data(http_client_api, default_media): | ||
response = await http_client_api.get("/media") | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
assert response.json()["total"] >= 1 | ||
assert response.json()["items"][0]["bucket_name"] == default_media.bucket_name | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_media_filter(http_client_api, default_media, fake_data): | ||
# Test filter by bucket_name | ||
response = await http_client_api.get("/media", params={"bucket_name": f"{default_media.bucket_name}"}) | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
assert response.json()["items"][0]["bucket_name"] == default_media.bucket_name | ||
|
||
# Test filter by description | ||
response = await http_client_api.get("/media", params={"tags.tag": "value"}) | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
assert response.json()["items"][0]["tags"] == default_media.tags | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_media_url(http_client_api, default_media): | ||
response = await http_client_api.get(f"/media/{default_media.bucket_name}/{default_media.filename}") | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
assert response.json()["url"] == default_media.url | ||
|
||
|
||
@pytest.mark.skip | ||
@pytest.mark.asyncio | ||
async def test_get_media_url_download(http_client_api, default_media): | ||
response = await http_client_api.get( | ||
f"/media/{default_media.bucket_name}/{default_media.filename}", params={"download": True} | ||
) | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
assert response.json()["url"] == default_media.url | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_media_view(http_client_api): | ||
response = await http_client_api.get("/media/filename") | ||
assert response.status_code == status.HTTP_200_OK, response.text | ||
|
||
|
||
@pytest.mark.skip | ||
@pytest.mark.asyncio | ||
async def test_upload_media_success(http_client_api, default_bucket, fake_data): | ||
response = await http_client_api.post( | ||
"/media", | ||
data={"bucket_name": default_bucket.bucket_name, "tags": '{"tag": "value"}'}, | ||
files={"file": ("test.txt", fake_data.text().encode("utf-8"))}, | ||
) | ||
assert response.status_code == status.HTTP_201_CREATED, response.text | ||
assert response.json()["bucket_name"] == default_bucket.bucket_name | ||
assert response.json()["tags"] == {"tag": "value"} | ||
assert response.json()["url"] is not None | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_upload_media_invalid_tags(http_client_api, default_bucket, fake_data): | ||
response = await http_client_api.post( | ||
"/media", | ||
data={"bucket_name": default_bucket.bucket_name, "tags": "invalid"}, | ||
files={"file": ("test.txt", fake_data.text().encode("utf-8"))}, | ||
) | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.text | ||
assert response.json()["error_code"] == SfsErrorCodes.SFS_INVALID_TAGS | ||
assert response.json()["error_message"] == "Invalid JSON string for tags." | ||
|
||
|
||
@pytest.mark.skip | ||
@pytest.mark.asyncio | ||
async def test_upload_media_invalid_bucket_name(http_client_api, fake_data): | ||
response = await http_client_api.post( | ||
"/media", | ||
data={"bucket_name": "invalid", "tags": '{"tag": "value"}'}, | ||
files={"file": ("test.txt", fake_data.text().encode("utf-8"))}, | ||
) | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.text | ||
assert response.json()["error_code"] == SfsErrorCodes.SFS_BUCKET_NOT_FOUND | ||
assert response.json()["error_message"] == "Bucket not found." | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_upload_media_invalid_file(http_client_api, default_bucket): | ||
response = await http_client_api.post( | ||
"/media", | ||
data={"bucket_name": default_bucket.bucket_name, "tags": "{'tag': 'value'}"}, | ||
files={"file": ("fooooo", "foooo")}, | ||
) | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.text | ||
assert response.json()["error_code"] == SfsErrorCodes.SFS_INVALID_TAGS | ||
assert response.json()["error_message"] == "Invalid JSON string for tags." | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_media(http_client_api, default_media): | ||
|
||
bucket_name, filename = default_media.bucket_name, default_media.filename | ||
|
||
response = await http_client_api.delete(f"/media/{bucket_name}/{filename}") | ||
assert response.status_code == status.HTTP_204_NO_CONTENT, response.text |