|
| 1 | +from V2.source.entry_points.flask_api import create_app |
| 2 | +import json |
| 3 | +import pytest |
| 4 | +import typing |
| 5 | +from flask.testing import FlaskClient |
| 6 | +from http import HTTPStatus |
| 7 | +from faker import Faker |
| 8 | +import shutil |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def client(): |
| 13 | + app = create_app({'TESTING': True}) |
| 14 | + with app.test_client() as client: |
| 15 | + yield client |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def activities_json(tmpdir_factory): |
| 20 | + temporary_directory = tmpdir_factory.mktemp("tmp") |
| 21 | + json_file = temporary_directory.join("activities.json") |
| 22 | + activities = [ |
| 23 | + { |
| 24 | + 'id': 'c61a4a49-3364-49a3-a7f7-0c5f2d15072b', |
| 25 | + 'name': 'Development', |
| 26 | + 'description': 'Development', |
| 27 | + 'deleted': 'b4327ba6-9f96-49ee-a9ac-3c1edf525172', |
| 28 | + 'status': None, |
| 29 | + 'tenant_id': 'cc925a5d-9644-4a4f-8d99-0bee49aadd05', |
| 30 | + }, |
| 31 | + { |
| 32 | + 'id': '94ec92e2-a500-4700-a9f6-e41eb7b5507c', |
| 33 | + 'name': 'Management', |
| 34 | + 'description': None, |
| 35 | + 'deleted': '7cf6efe5-a221-4fe4-b94f-8945127a489a', |
| 36 | + 'status': None, |
| 37 | + 'tenant_id': 'cc925a5d-9644-4a4f-8d99-0bee49aadd05', |
| 38 | + }, |
| 39 | + { |
| 40 | + 'id': 'd45c770a-b1a0-4bd8-a713-22c01a23e41b', |
| 41 | + 'name': 'Operations', |
| 42 | + 'description': 'Operation activities performed.', |
| 43 | + 'deleted': '7cf6efe5-a221-4fe4-b94f-8945127a489a', |
| 44 | + 'status': 'active', |
| 45 | + 'tenant_id': 'cc925a5d-9644-4a4f-8d99-0bee49aadd05', |
| 46 | + }, |
| 47 | + ] |
| 48 | + |
| 49 | + with open(json_file, 'w') as outfile: |
| 50 | + json.dump(activities, outfile) |
| 51 | + |
| 52 | + with open(json_file) as outfile: |
| 53 | + activities_json = json.load(outfile) |
| 54 | + |
| 55 | + yield activities_json |
| 56 | + shutil.rmtree(temporary_directory) |
| 57 | + |
| 58 | + |
| 59 | +def test_test__activity_endpoint__returns_all_activities( |
| 60 | + client: FlaskClient, activities_json: typing.List[dict] |
| 61 | +): |
| 62 | + response = client.get("/activities/") |
| 63 | + json_data = json.loads(response.data) |
| 64 | + |
| 65 | + assert response.status_code == HTTPStatus.OK |
| 66 | + assert json_data == activities_json |
| 67 | + |
| 68 | + |
| 69 | +def test__activity_endpoint__returns_an_activity__when_activity_matches_its_id( |
| 70 | + client: FlaskClient, activities_json: typing.List[dict] |
| 71 | +): |
| 72 | + response = client.get("/activities/%s" % activities_json[0]['id']) |
| 73 | + json_data = json.loads(response.data) |
| 74 | + |
| 75 | + assert response.status_code == HTTPStatus.OK |
| 76 | + assert json_data == activities_json[0] |
| 77 | + |
| 78 | + |
| 79 | +def test__activity_endpoint__returns_a_not_found_status__when_no_activity_matches_its_id( |
| 80 | + client: FlaskClient, |
| 81 | +): |
| 82 | + response = client.get("/activities/%s" % Faker().uuid4()) |
| 83 | + json_data = json.loads(response.data) |
| 84 | + |
| 85 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 86 | + assert json_data['message'] == 'Activity not found' |
0 commit comments