Skip to content

Commit 73e9393

Browse files
modified test to check if the data is written to audit
1 parent 7555020 commit 73e9393

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

tests/integration/conftest.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,6 @@ def firehose_client(boto3_session: Session, localstack: URL) -> BaseClient:
102102
return boto3_session.client("firehose", endpoint_url=str(localstack))
103103

104104

105-
@pytest.fixture(autouse=True)
106-
def firehose_delivery_stream(firehose_client: BaseClient) -> dict[str, Any]:
107-
return firehose_client.create_delivery_stream(DeliveryStreamName="eligibility-signposting-audit-stream")
108-
109-
110105
@pytest.fixture(scope="session")
111106
def iam_role(iam_client: BaseClient) -> Generator[str]:
112107
role_name = "LambdaExecutionRole"
@@ -391,13 +386,38 @@ def rules_bucket(s3_client: BaseClient) -> Generator[BucketName]:
391386

392387

393388
@pytest.fixture(scope="session")
394-
def audit_bucket(s3_client: BaseClient) -> Generator[BucketName]:
389+
def audit_bucket(s3_client: BaseClient) -> Generator[BucketName, None, None]:
395390
bucket_name = BucketName(os.getenv("AUDIT_BUCKET_NAME", "test-audit-bucket"))
396-
s3_client.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": AWS_REGION})
391+
s3_client.create_bucket(
392+
Bucket=bucket_name,
393+
CreateBucketConfiguration={"LocationConstraint": AWS_REGION}
394+
)
397395
yield bucket_name
396+
397+
# Delete all objects in the bucket before deletion
398+
objects = s3_client.list_objects_v2(Bucket=bucket_name).get("Contents", [])
399+
for obj in objects:
400+
s3_client.delete_object(Bucket=bucket_name, Key=obj["Key"])
398401
s3_client.delete_bucket(Bucket=bucket_name)
399402

400403

404+
@pytest.fixture(autouse=True)
405+
def firehose_delivery_stream(firehose_client: BaseClient, audit_bucket: BucketName) -> dict[str, Any]:
406+
return firehose_client.create_delivery_stream(
407+
DeliveryStreamName="eligibility-signposting-audit-stream",
408+
DeliveryStreamType="DirectPut",
409+
ExtendedS3DestinationConfiguration={
410+
"BucketARN": f"arn:aws:s3:::{audit_bucket}",
411+
"RoleARN": "arn:aws:iam::000000000000:role/firehose_delivery_role",
412+
"Prefix": "audit-logs/",
413+
"BufferingHints": {
414+
"SizeInMBs": 1,
415+
"IntervalInSeconds": 60
416+
},
417+
"CompressionFormat": "UNCOMPRESSED",
418+
}
419+
)
420+
401421
@pytest.fixture(scope="class")
402422
def campaign_config(s3_client: BaseClient, rules_bucket: BucketName) -> Generator[rules.CampaignConfig]:
403423
campaign: rules.CampaignConfig = rule.CampaignConfigFactory.build(

tests/integration/lambda/test_app_running_as_lambda.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
from brunns.matchers.data import json_matching as is_json_that
1212
from brunns.matchers.response import is_response
1313
from faker import Faker
14-
from hamcrest import assert_that, contains_exactly, contains_string, has_entries, has_item, has_key
14+
from hamcrest import assert_that, contains_exactly, contains_string, has_entries, has_item, has_key, any_of, starts_with
1515
from yarl import URL
1616

1717
from eligibility_signposting_api.model.eligibility import NHSNumber
1818
from eligibility_signposting_api.model.rules import CampaignConfig
19+
from eligibility_signposting_api.repos.campaign_repo import BucketName
1920

2021
logger = logging.getLogger(__name__)
2122

@@ -157,6 +158,8 @@ def test_given_nhs_number_in_path_matches_with_nhs_number_in_headers(
157158
lambda_client: BaseClient, # noqa:ARG001
158159
persisted_person: NHSNumber,
159160
campaign_config: CampaignConfig, # noqa:ARG001
161+
s3_client: BaseClient,
162+
audit_bucket: BucketName,
160163
api_gateway_endpoint: URL,
161164
):
162165
# Given
@@ -176,6 +179,12 @@ def test_given_nhs_number_in_path_matches_with_nhs_number_in_headers(
176179
is_response().with_status_code(HTTPStatus.OK).and_body(is_json_that(has_key("processedSuggestions"))),
177180
)
178181

182+
objects = s3_client.list_objects_v2(Bucket=audit_bucket).get("Contents", [])
183+
object_keys = [obj["Key"] for obj in objects]
184+
latest_key = sorted(object_keys)[-1]
185+
audit_data = json.loads(s3_client.get_object(Bucket=audit_bucket, Key=latest_key)["Body"].read())
186+
assert_that(audit_data, has_entries(test_audit="check if audit works"))
187+
179188

180189
def test_given_nhs_number_in_path_does_not_match_with_nhs_number_in_headers_results_in_error_response(
181190
lambda_client: BaseClient, # noqa:ARG001

0 commit comments

Comments
 (0)