-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_handler.py
159 lines (132 loc) · 4.72 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import time
import pytest
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
from handler import get_online_users
from handler import get_recent_logs
from handler import add_log_entry
from handler import get_online_users_handler
from handler import default_message_handler
from handler import get_recent_logs_handler
from handler import add_log_entry_handler
from handler import new_log_stream_handler
dynamodb = boto3.resource("dynamodb")
CONNECTIONS_TABLE = "photonranch-logstream-connections-dev"
LOGS_TABLE = "photonranch-observatory-logs-dev"
def test_get_recent_logs():
site = "tst"
timestamp_max_age = 0
recent_logs = get_recent_logs(site, timestamp_max_age)
assert isinstance(recent_logs, list)
def test_add_log_entry():
# This is the sample content to add
entry = {
"site": "tst",
"log_message": "This message is a result of tests running in the backend.",
"log_level": "debug",
"timestamp": int(time.time())
}
# The function we're testing
add_log_entry(entry)
# Try and retrieve the message we added
table = dynamodb.Table(LOGS_TABLE)
response = table.query(
KeyConditionExpression=Key('site').eq(entry["site"]) \
& Key('timestamp').eq(entry["timestamp"])
)
response_obj = response["Items"][0]
# Duplicate the format of the original submission and compare.
retrieved_object = {
"site": response_obj["site"],
"log_message": response_obj["message"],
"log_level": response_obj["log_level"],
"timestamp": response_obj["timestamp"],
}
assert retrieved_object == entry
######################################################
########## Test Handler Functions ############
######################################################
def test_get_online_users_handler():
event = {
"queryStringParameters": {
"site": "tst",
}
}
context = {}
response = get_online_users_handler(event, context)
assert response["statusCode"] == 200
def test_default_message_handler():
event = {}
context = {}
response = default_message_handler(event, context)
assert response["statusCode"] == 400
def test_get_recent_logs_handler():
event = {
"queryStringParameters": {
"after_time": 0,
"site": "tst",
}
}
context = {}
response = get_recent_logs_handler(event, context)
assert response["statusCode"] == 200
def test_add_log_entry_handler():
entry = {
"site": "tst",
"log_message": "This message is a result of tests running in the backend.",
"log_level": "debug",
"timestamp": int(time.time())
}
event = { "body": json.dumps(entry) }
context = {}
# The function we're testing
add_log_entry_handler(event,context)
# Try and retrieve the message we added
table = dynamodb.Table(LOGS_TABLE)
response = table.query(
KeyConditionExpression=Key('site').eq(entry["site"]) \
& Key('timestamp').eq(entry["timestamp"])
)
response_obj = response["Items"][0]
# Duplicate the format of the original submission and compare.
retrieved_object = {
"site": response_obj["site"],
"log_message": response_obj["message"],
"log_level": response_obj["log_level"],
"timestamp": response_obj["timestamp"],
}
assert retrieved_object == entry
def test_new_log_stream_handler():
# sample event:
creation_timestamp = int(time.time())
event = {
'Records': [
{
'eventID': '699f8495963197b9b17d91b2ec0946f0',
'eventName': 'INSERT',
'eventVersion': '1.1',
'eventSource': 'aws:dynamodb',
'awsRegion': 'us-east-1',
'dynamodb': {
'ApproximateCreationDateTime': creation_timestamp,
'Keys': {
'site': {'S': 'tst'},
'timestamp': {'N': str(creation_timestamp)}
},
'NewImage': {
'site': {'S': 'tst'},
'message': {'S': 'first log'},
'timestamp': {'N': str(creation_timestamp)}
},
'SequenceNumber': '100000000001547440520',
'SizeBytes': 60,
'StreamViewType': 'NEW_IMAGE'
},
'eventSourceARN': 'arn:aws:dynamodb:us-east-1:306389350997:table/photonranch-observatory-logs/stream/2020-10-21T21:47:47.149'
}
]
}
context = {}
new_log_stream_handler(event, context)
assert True # acceptable as long as there are no exceptions.