1+ import boto3
2+ import json
3+ import pytest
4+ import time
5+ import os
6+
7+ @pytest .fixture
8+ def lambda_client ():
9+ return boto3 .client ('lambda' )
10+
11+ @pytest .fixture
12+ def s3_client ():
13+ return boto3 .client ('s3' )
14+
15+ @pytest .fixture
16+ def logs_client ():
17+ return boto3 .client ('logs' )
18+
19+ @pytest .fixture (scope = 'session' )
20+ def cleanup ():
21+ # Create a new S3 client for cleanup
22+ s3_client = boto3 .client ('s3' )
23+
24+ yield
25+ # Cleanup code will be executed after all tests have finished
26+
27+ # Delete test.pdf from the source bucket
28+ source_bucket = 'EXAMPLE-BUCKET'
29+ source_file_key = 'test.pdf'
30+ s3_client .delete_object (Bucket = source_bucket , Key = source_file_key )
31+ print (f"\n Deleted { source_file_key } from { source_bucket } " )
32+
33+ # Delete test_encrypted.pdf from the destination bucket
34+ destination_bucket = 'EXAMPLE-BUCKET-encrypted'
35+ destination_file_key = 'test_encrypted.pdf'
36+ s3_client .delete_object (Bucket = destination_bucket , Key = destination_file_key )
37+ print (f"Deleted { destination_file_key } from { destination_bucket } " )
38+
39+
40+ @pytest .mark .order (1 )
41+ def test_source_bucket_available (s3_client ):
42+ s3_bucket_name = 'EXAMPLE-BUCKET'
43+ file_name = 'test.pdf'
44+ file_path = os .path .join (os .path .dirname (__file__ ), file_name )
45+
46+ file_uploaded = False
47+ try :
48+ s3_client .upload_file (file_path , s3_bucket_name , file_name )
49+ file_uploaded = True
50+ except :
51+ print ("Error: couldn't upload file" )
52+
53+ assert file_uploaded , "Could not upload file to S3 bucket"
54+
55+
56+
57+ @pytest .mark .order (2 )
58+ def test_lambda_invoked (logs_client ):
59+
60+ # Wait for a few seconds to make sure the logs are available
61+ time .sleep (5 )
62+
63+ # Get the latest log stream for the specified log group
64+ log_streams = logs_client .describe_log_streams (
65+ logGroupName = '/aws/lambda/EncryptPDF' ,
66+ orderBy = 'LastEventTime' ,
67+ descending = True ,
68+ limit = 1
69+ )
70+
71+ latest_log_stream_name = log_streams ['logStreams' ][0 ]['logStreamName' ]
72+
73+ # Retrieve the log events from the latest log stream
74+ log_events = logs_client .get_log_events (
75+ logGroupName = '/aws/lambda/EncryptPDF' ,
76+ logStreamName = latest_log_stream_name
77+ )
78+
79+ success_found = False
80+ for event in log_events ['events' ]:
81+ message = json .loads (event ['message' ])
82+ status = message .get ('record' , {}).get ('status' )
83+ if status == 'success' :
84+ success_found = True
85+ break
86+
87+ assert success_found , "Lambda function execution did not report 'success' status in logs."
88+
89+ @pytest .mark .order (3 )
90+ def test_encrypted_file_in_bucket (s3_client ):
91+ # Specify the destination S3 bucket and the expected converted file key
92+ destination_bucket = 'EXAMPLE-BUCKET-encrypted'
93+ converted_file_key = 'test_encrypted.pdf'
94+
95+ try :
96+ # Attempt to retrieve the metadata of the converted file from the destination S3 bucket
97+ s3_client .head_object (Bucket = destination_bucket , Key = converted_file_key )
98+ except s3_client .exceptions .ClientError as e :
99+ # If the file is not found, the test will fail
100+ pytest .fail (f"Converted file '{ converted_file_key } ' not found in the destination bucket: { str (e )} " )
101+
102+ def test_cleanup (cleanup ):
103+ # This test uses the cleanup fixture and will be executed last
104+ pass
0 commit comments