-
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.
LITE-29286 Create upload list and retrieve endpoints
- Loading branch information
Showing
5 changed files
with
215 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import List | ||
|
||
from fastapi import Depends, status | ||
from connect.eaas.core.decorators import router | ||
from connect.eaas.core.inject.synchronous import get_installation | ||
from connect_extension_utils.db.models import get_db, VerboseBaseSession | ||
|
||
from connect_bi_reporter.uploads.api.schemas import map_to_upload_schema, UploadSchema | ||
from connect_bi_reporter.uploads.services import get_upload_or_404, get_uploads_or_404 | ||
|
||
|
||
class UploadsWebAppMixin: | ||
|
||
@router.get( | ||
'/feeds/{feed_id}/uploads/{upload_id}', | ||
summary='Returns the require Upload related to a Feed', | ||
response_model=UploadSchema, | ||
status_code=status.HTTP_200_OK, | ||
) | ||
def get_upload( | ||
self, | ||
feed_id: str, | ||
upload_id: str, | ||
db: VerboseBaseSession = Depends(get_db), | ||
installation: dict = Depends(get_installation), | ||
): | ||
return map_to_upload_schema(get_upload_or_404(db, installation, feed_id, upload_id)) | ||
|
||
@router.get( | ||
'/feeds/{feed_id}/uploads', | ||
summary='Returns all Uploads related to a Feed', | ||
response_model=List[UploadSchema], | ||
status_code=status.HTTP_200_OK, | ||
) | ||
def get_uploads( | ||
self, | ||
feed_id: str, | ||
db: VerboseBaseSession = Depends(get_db), | ||
installation: dict = Depends(get_installation), | ||
): | ||
return [ | ||
map_to_upload_schema(upload) for upload in get_uploads_or_404(db, installation, feed_id) | ||
] |
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,124 @@ | ||
def test_list_uploads(installation, feed_factory, api_client, connect_auth_header, upload_factory): | ||
uploads = [] | ||
invalid_feed = feed_factory(account_id='other-account') | ||
valid_feed = feed_factory(account_id=installation['owner']['id']) | ||
upload_factory(feed_id=invalid_feed.id) | ||
|
||
for _ in range(5): | ||
uploads.append(upload_factory(feed_id=valid_feed.id, name='Name', size=600)) | ||
|
||
response = api_client.get( | ||
f'/api/feeds/{valid_feed.id}/uploads', | ||
installation=installation, | ||
headers={'connect-auth': connect_auth_header}, | ||
) | ||
|
||
assert response.status_code == 200 | ||
|
||
response_data = response.json() | ||
|
||
assert len(response_data) == len(uploads) | ||
|
||
for upload, response_upload in zip(uploads, response_data): | ||
assert response_upload['id'] == upload.id | ||
assert response_upload['name'] == upload.name | ||
assert response_upload['report'] == {'id': upload.report_id} | ||
assert response_upload['feed'] == {'id': valid_feed.id} | ||
assert response_upload['size'] == upload.size | ||
assert response_upload['status'] == upload.status | ||
events = response_upload['events'] | ||
assert events['created']['at'] is not None | ||
assert events['updated']['at'] is not None | ||
|
||
|
||
def test_list_uploads_feed_not_found( | ||
installation, | ||
feed_factory, | ||
api_client, | ||
connect_auth_header, | ||
upload_factory, | ||
): | ||
uploads = [] | ||
valid_feed = feed_factory(account_id=installation['owner']['id']) | ||
for _ in range(5): | ||
uploads.append(upload_factory(feed_id=valid_feed.id, name='Name', size=600)) | ||
|
||
response = api_client.get( | ||
'/api/feeds/NOT-FOUND/uploads', | ||
installation=installation, | ||
headers={'connect-auth': connect_auth_header}, | ||
) | ||
|
||
assert response.status_code == 404 | ||
response_data = response.json() | ||
assert response_data['error_code'] == 'NFND_000' | ||
assert response_data['errors'][0] == 'Object `NOT-FOUND` not found.' | ||
|
||
|
||
def test_get_upload(installation, feed_factory, api_client, connect_auth_header, upload_factory): | ||
feed = feed_factory(account_id=installation['owner']['id']) | ||
upload = upload_factory(feed_id=feed.id, name='Test', size=600) | ||
|
||
response = api_client.get( | ||
f'/api/feeds/{feed.id}/uploads/{upload.id}', | ||
installation=installation, | ||
headers={'connect-auth': connect_auth_header}, | ||
) | ||
|
||
assert response.status_code == 200 | ||
|
||
response_data = response.json() | ||
|
||
assert response_data['id'] == upload.id | ||
assert response_data['name'] == upload.name | ||
assert response_data['report'] == {'id': upload.report_id} | ||
assert response_data['feed'] == {'id': feed.id} | ||
assert response_data['size'] == upload.size | ||
assert response_data['status'] == upload.status | ||
events = response_data['events'] | ||
assert events['created']['at'] is not None | ||
assert events['updated']['at'] is not None | ||
|
||
|
||
def test_get_upload_feed_not_found( | ||
installation, | ||
feed_factory, | ||
api_client, | ||
connect_auth_header, | ||
upload_factory, | ||
): | ||
feed = feed_factory(account_id=installation['owner']['id']) | ||
upload = upload_factory(feed_id=feed.id, name='Test', size=600) | ||
|
||
response = api_client.get( | ||
f'/api/feeds/FEED-NOT-FOUND/uploads/{upload.id}', | ||
installation=installation, | ||
headers={'connect-auth': connect_auth_header}, | ||
) | ||
|
||
assert response.status_code == 404 | ||
response_data = response.json() | ||
assert response_data['error_code'] == 'NFND_000' | ||
assert response_data['errors'][0] == 'Object `FEED-NOT-FOUND` not found.' | ||
|
||
|
||
def test_get_upload_upload_not_found( | ||
installation, | ||
feed_factory, | ||
api_client, | ||
connect_auth_header, | ||
upload_factory, | ||
): | ||
feed = feed_factory(account_id=installation['owner']['id']) | ||
upload_factory(feed_id=feed.id, name='Test', size=600) | ||
|
||
response = api_client.get( | ||
f'/api/feeds/{feed.id}/uploads/UPLOAD-NOT-FOUND', | ||
installation=installation, | ||
headers={'connect-auth': connect_auth_header}, | ||
) | ||
|
||
assert response.status_code == 404 | ||
response_data = response.json() | ||
assert response_data['error_code'] == 'NFND_000' | ||
assert response_data['errors'][0] == 'Object `UPLOAD-NOT-FOUND` not found.' |