-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_handler.py
117 lines (102 loc) · 3.33 KB
/
test_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import boto3
import pytest
from botocore.exceptions import ClientError
from handler import call
from moto import mock_s3
from moto import mock_dynamodb2
BUCKET = "some-bucket"
KEY = "incoming/transaction-0001.txt"
BODY = "Hello All!"
TXNS_TABLE = "my-transactions-table"
## Test Setup Functions
from contextlib import contextmanager
@contextmanager
def do_test_setup():
with mock_s3():
with mock_dynamodb2():
set_up_s3()
set_up_dynamodb()
yield
def set_up_s3():
conn = boto3.resource('s3', region_name='us-east-1')
conn.create_bucket(Bucket=BUCKET)
boto3.client('s3', region_name='us-east-1').put_object(Bucket=BUCKET, Key=KEY, Body=BODY)
def set_up_dynamodb():
client = boto3.client('dynamodb', region_name='us-east-1')
client.create_table(
AttributeDefinitions=[
{
'AttributeName': 'transaction_id',
'AttributeType': 'N'
},
],
KeySchema=[
{
'AttributeName': 'transaction_id',
'KeyType': 'HASH'
}
],
TableName=TXNS_TABLE,
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)
## Tests
def test_handler_moves_incoming_object_to_processed():
with do_test_setup():
call(s3_object_created_event(BUCKET, KEY), None)
conn = boto3.resource('s3', region_name='us-east-1')
assert_object_doesnt_exist(conn, BUCKET, KEY)
obj = conn.Object(BUCKET, "processed/transaction-0001.txt").get()
assert obj['Body'].read() == b'Hello All!'
def test_handler_adds_record_in_dynamo_db_about_object():
with do_test_setup():
call(s3_object_created_event(BUCKET, KEY), None)
table = boto3.resource('dynamodb', region_name='us-east-1').Table(TXNS_TABLE)
item = table.get_item(Key={'transaction_id': '0001'})['Item']
assert item['body'] == 'Hello All!'
## Helpers
def assert_object_doesnt_exist(conn, bucket_name, key):
with pytest.raises(ClientError) as e_info:
conn.Object(bucket_name, key).get()
assert e_info.response['Error']['Code'] == 'NoSuchKey'
def s3_object_created_event(bucket_name, key):
return {
"Records": [
{
"eventVersion": "2.0",
"eventTime": "1970-01-01T00:00:00.000Z",
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"s3": {
"configurationId": "testConfigRule",
"object": {
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901",
"key": key,
"size": 1024
},
"bucket": {
"arn": "bucketarn",
"name": bucket_name,
"ownerIdentity": {
"principalId": "EXAMPLE"
}
},
"s3SchemaVersion": "1.0"
},
"responseElements": {
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
"x-amz-request-id": "EXAMPLE123456789"
},
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}