|
| 1 | +import os |
| 2 | +from contextlib import contextmanager |
| 3 | +from datetime import datetime |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | +from domain.core.cpm_system_id import ProductId |
| 8 | +from domain.core.enum import Status |
| 9 | +from domain.core.root import Root |
| 10 | +from domain.repository.cpm_product_repository import CpmProductRepository |
| 11 | +from domain.repository.errors import ItemNotFound |
| 12 | +from domain.repository.product_team_repository import ProductTeamRepository |
| 13 | + |
| 14 | +from test_helpers.dynamodb import mock_table_cpm |
| 15 | +from test_helpers.sample_data import CPM_PRODUCT_TEAM_NO_ID |
| 16 | +from test_helpers.uuid import consistent_uuid |
| 17 | + |
| 18 | +TABLE_NAME = "hiya" |
| 19 | + |
| 20 | +ODS_CODE = "F5H1R" |
| 21 | +PRODUCT_TEAM_ID = consistent_uuid(1) |
| 22 | +PRODUCT_ID = "P.AAA-AAA" |
| 23 | +PRODUCT_NAME = "My Product" |
| 24 | +VERSION = 1 |
| 25 | + |
| 26 | + |
| 27 | +@contextmanager |
| 28 | +def mock_lambda(): |
| 29 | + org = Root.create_ods_organisation(ods_code=CPM_PRODUCT_TEAM_NO_ID["ods_code"]) |
| 30 | + product_team = org.create_product_team( |
| 31 | + name=CPM_PRODUCT_TEAM_NO_ID["name"], keys=CPM_PRODUCT_TEAM_NO_ID["keys"] |
| 32 | + ) |
| 33 | + |
| 34 | + with mock_table_cpm(table_name=TABLE_NAME) as client, mock.patch.dict( |
| 35 | + os.environ, |
| 36 | + {"DYNAMODB_TABLE": TABLE_NAME, "AWS_DEFAULT_REGION": "eu-west-2"}, |
| 37 | + clear=True, |
| 38 | + ): |
| 39 | + product_team_repo = ProductTeamRepository( |
| 40 | + table_name=TABLE_NAME, dynamodb_client=client |
| 41 | + ) |
| 42 | + product_team_repo.write(entity=product_team) |
| 43 | + |
| 44 | + product = product_team.create_cpm_product( |
| 45 | + name=PRODUCT_NAME, product_id=PRODUCT_ID |
| 46 | + ) |
| 47 | + product_repo = CpmProductRepository( |
| 48 | + table_name=TABLE_NAME, dynamodb_client=client |
| 49 | + ) |
| 50 | + product_repo.write(entity=product) |
| 51 | + |
| 52 | + import api.deleteCpmProduct.index as index |
| 53 | + |
| 54 | + index.cache["DYNAMODB_CLIENT"] = client |
| 55 | + |
| 56 | + yield index, product_team |
| 57 | + |
| 58 | + |
| 59 | +def test_index(): |
| 60 | + with mock_lambda() as (index, product_team): |
| 61 | + # Execute the lambda |
| 62 | + response = index.handler( |
| 63 | + event={ |
| 64 | + "headers": {"version": VERSION}, |
| 65 | + "pathParameters": { |
| 66 | + "product_team_id": product_team.id, |
| 67 | + "product_id": PRODUCT_ID, |
| 68 | + }, |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + # Validate that the response indicates that a resource was deleted |
| 73 | + assert response["statusCode"] == 200 |
| 74 | + assert ( |
| 75 | + response["body"] |
| 76 | + == '{"code": "200", "message": "P.AAA-AAA has been deleted."}' |
| 77 | + ) |
| 78 | + |
| 79 | + # Retrieve the created resource |
| 80 | + repo = CpmProductRepository( |
| 81 | + table_name=TABLE_NAME, dynamodb_client=index.cache["DYNAMODB_CLIENT"] |
| 82 | + ) |
| 83 | + with pytest.raises(ItemNotFound): |
| 84 | + repo.read(product_team_id=product_team.id, id=PRODUCT_ID) |
| 85 | + |
| 86 | + deleted_product = repo.read( |
| 87 | + product_team_id=product_team.id, id=PRODUCT_ID, status="inactive" |
| 88 | + ).dict() |
| 89 | + |
| 90 | + # Sense checks on the deleted resource |
| 91 | + created_on = deleted_product.pop("created_on") |
| 92 | + updated_on = deleted_product.pop("updated_on") |
| 93 | + deleted_on = deleted_product.pop("deleted_on") |
| 94 | + assert created_on < updated_on |
| 95 | + assert updated_on == deleted_on |
| 96 | + assert isinstance(created_on, datetime) |
| 97 | + assert isinstance(updated_on, datetime) |
| 98 | + assert isinstance(deleted_on, datetime) |
| 99 | + |
| 100 | + assert ProductId.validate_cpm_system_id(deleted_product.pop("id")) |
| 101 | + assert deleted_product == { |
| 102 | + "name": PRODUCT_NAME, |
| 103 | + "ods_code": ODS_CODE, |
| 104 | + "product_team_id": product_team.id, |
| 105 | + "status": Status.INACTIVE, |
| 106 | + "keys": [], |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + ["path_parameters", "error_code", "status_code"], |
| 112 | + [ |
| 113 | + ( |
| 114 | + {"product_team_id": PRODUCT_TEAM_ID}, |
| 115 | + "MISSING_VALUE", |
| 116 | + 400, |
| 117 | + ), |
| 118 | + ( |
| 119 | + {"product_id": PRODUCT_ID}, |
| 120 | + "MISSING_VALUE", |
| 121 | + 400, |
| 122 | + ), |
| 123 | + ( |
| 124 | + { |
| 125 | + "product_team_id": PRODUCT_TEAM_ID, |
| 126 | + "product_id": PRODUCT_ID, |
| 127 | + "extra_forbidden_param": "foo", |
| 128 | + }, |
| 129 | + "VALIDATION_ERROR", |
| 130 | + 400, |
| 131 | + ), |
| 132 | + ( |
| 133 | + {"product_team_id": "does-not-exist", "product_id": PRODUCT_ID}, |
| 134 | + "RESOURCE_NOT_FOUND", |
| 135 | + 404, |
| 136 | + ), |
| 137 | + ( |
| 138 | + {"product_team_id": PRODUCT_TEAM_ID, "product_id": "does-not-exist"}, |
| 139 | + "RESOURCE_NOT_FOUND", |
| 140 | + 404, |
| 141 | + ), |
| 142 | + ], |
| 143 | +) |
| 144 | +def test_incoming_errors(path_parameters, error_code, status_code): |
| 145 | + with mock_lambda() as (index, product_team): |
| 146 | + # Execute the lambda |
| 147 | + response = index.handler( |
| 148 | + event={ |
| 149 | + "headers": {"version": VERSION}, |
| 150 | + "pathParameters": path_parameters, |
| 151 | + } |
| 152 | + ) |
| 153 | + |
| 154 | + # Validate that the response indicates that the expected error |
| 155 | + assert response["statusCode"] == status_code |
| 156 | + assert error_code in response["body"] |
0 commit comments