-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_infra.py
232 lines (198 loc) · 8.43 KB
/
test_infra.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import pytest
import boto3
import requests
import localstack.sdk.aws
import json
import time
@pytest.fixture(scope='module')
def api_endpoint():
apigateway_client = boto3.client('apigateway', endpoint_url='http://localhost:4566')
lambda_client = boto3.client('lambda', endpoint_url='http://localhost:4566')
API_NAME = 'QuizAPI'
response = apigateway_client.get_rest_apis()
api_list = response.get('items', [])
api = next((item for item in api_list if item['name'] == API_NAME), None)
if not api:
raise Exception(f"API {API_NAME} not found.")
API_ID = api['id']
API_ENDPOINT = f"http://localhost:4566/_aws/execute-api/{API_ID}/test"
print(f"API Endpoint: {API_ENDPOINT}")
time.sleep(2)
return API_ENDPOINT
def test_quiz_workflow(api_endpoint):
create_quiz_payload = {
"Title": "Sample Quiz",
"Visibility": "Public",
"EnableTimer": True,
"TimerSeconds": 10,
"Questions": [
{
"QuestionText": "What is the capital of France?",
"Options": ["A. Berlin", "B. London", "C. Madrid", "D. Paris"],
"CorrectAnswer": "D. Paris",
"Trivia": "Paris is known as the City of Light."
},
{
"QuestionText": "Who wrote Hamlet?",
"Options": ["A. Dickens", "B. Shakespeare", "C. Twain", "D. Hemingway"],
"CorrectAnswer": "B. Shakespeare",
"Trivia": "Shakespeare is often called England's national poet."
},
{
"QuestionText": "What is the largest planet in our solar system?",
"Options": ["A. Earth", "B. Mars", "C. Jupiter", "D. Saturn"],
"CorrectAnswer": "C. Jupiter",
"Trivia": "Jupiter is so large that all the other planets in the solar system could fit inside it."
},
{
"QuestionText": "Which element has the chemical symbol 'O'?",
"Options": ["A. Gold", "B. Oxygen", "C. Silver", "D. Iron"],
"CorrectAnswer": "B. Oxygen",
"Trivia": "Oxygen makes up about 21% of the Earth's atmosphere."
},
{
"QuestionText": "In which year did World War II end?",
"Options": ["A. 1943", "B. 1945", "C. 1947", "D. 1950"],
"CorrectAnswer": "B. 1945",
"Trivia": "The war ended with the surrender of Japan on September 2, 1945."
}
]
}
response = requests.post(
f"{api_endpoint}/createquiz",
headers={"Content-Type": "application/json"},
data=json.dumps(create_quiz_payload)
)
assert response.status_code == 200
quiz_creation_response = response.json()
assert 'QuizID' in quiz_creation_response
quiz_id = quiz_creation_response['QuizID']
print(f"Quiz created with ID: {quiz_id}")
response = requests.get(f"{api_endpoint}/listquizzes")
assert response.status_code == 200
quizzes_list = response.json().get('Quizzes', [])
quiz_titles = [quiz['Title'] for quiz in quizzes_list]
assert "Sample Quiz" in quiz_titles
response = requests.get(f"{api_endpoint}/getquiz?quiz_id={quiz_id}")
assert response.status_code == 200
quiz_details = response.json()
assert quiz_details['Title'] == "Sample Quiz"
assert len(quiz_details['Questions']) == 5
submissions = []
users = [
{
"Username": "user1",
"Answers": {
"0": {"Answer": "D. Paris", "TimeTaken": 8},
"1": {"Answer": "B. Shakespeare", "TimeTaken": 5},
"2": {"Answer": "C. Jupiter", "TimeTaken": 6},
"3": {"Answer": "B. Oxygen", "TimeTaken": 7},
"4": {"Answer": "B. 1945", "TimeTaken": 9}
}
},
{
"Username": "user2",
"Email": "[email protected]",
"Answers": {
"0": {"Answer": "D. Paris", "TimeTaken": 7},
"1": {"Answer": "B. Shakespeare", "TimeTaken": 6},
"2": {"Answer": "D. Saturn", "TimeTaken": 5}, # Incorrect
"3": {"Answer": "B. Oxygen", "TimeTaken": 8},
"4": {"Answer": "B. 1945", "TimeTaken": 10}
}
},
{
"Username": "user3",
"Answers": {
"0": {"Answer": "A. Berlin", "TimeTaken": 9}, # Incorrect
"1": {"Answer": "D. Hemingway", "TimeTaken": 4}, # Incorrect
"2": {"Answer": "C. Jupiter", "TimeTaken": 11}, # Exceeds time
"3": {"Answer": "B. Oxygen", "TimeTaken": 12}, # Exceeds time
"4": {"Answer": "B. 1945", "TimeTaken": 13} # Exceeds time
}
}
]
for user in users:
submission_payload = {
"Username": user["Username"],
"QuizID": quiz_id,
"Answers": user["Answers"]
}
if "Email" in user:
submission_payload["Email"] = user["Email"]
response = requests.post(
f"{api_endpoint}/submitquiz",
headers={"Content-Type": "application/json"},
data=json.dumps(submission_payload)
)
assert response.status_code == 200
submission_response = response.json()
assert 'SubmissionID' in submission_response
submissions.append({
"Username": user["Username"],
"SubmissionID": submission_response["SubmissionID"]
})
print(f"{user['Username']} submitted quiz with SubmissionID: {submission_response['SubmissionID']}")
time.sleep(5)
response = requests.get(f"{api_endpoint}/getleaderboard?quiz_id={quiz_id}&top=3")
assert response.status_code == 200
leaderboard = response.json()
assert len(leaderboard) == 3
expected_scores = {
"user1": None,
"user2": None,
"user3": None
}
max_score = 100
timer_seconds = 10
correct_answers = ["D. Paris", "B. Shakespeare", "C. Jupiter", "B. Oxygen", "B. 1945"]
def calculate_user_score(user_answers):
score = 0
for idx, correct_answer in enumerate(correct_answers):
user_answer = user_answers[str(idx)]["Answer"]
time_taken = user_answers[str(idx)]["TimeTaken"]
if user_answer == correct_answer and time_taken <= timer_seconds:
question_score = max_score * (1 - (time_taken / timer_seconds))
score += max(0, question_score)
else:
pass
return score
for user in users:
username = user["Username"]
expected_scores[username] = calculate_user_score(user["Answers"])
for entry in leaderboard:
username = entry["Username"]
actual_score = entry["Score"]
expected_score = expected_scores[username]
assert actual_score == pytest.approx(expected_score, abs=0.01)
print(f"{username} - Expected Score: {expected_score}, Actual Score: {actual_score}")
for submission in submissions:
response = requests.get(f"{api_endpoint}/getsubmission?submission_id={submission['SubmissionID']}")
assert response.status_code == 200
submission_data = response.json()
assert submission_data['Username'] == submission['Username']
assert submission_data['QuizID'] == quiz_id
assert 'Score' in submission_data
assert 'UserAnswers' in submission_data
expected_score = expected_scores[submission['Username']]
actual_score = submission_data['Score']
assert actual_score == pytest.approx(expected_score, abs=0.01)
print(f"Verified submission for {submission['Username']} with Score: {actual_score}")
client = localstack.sdk.aws.AWSClient()
sender_email = "[email protected]"
messages = client.get_ses_messages(email_filter=sender_email)
email_found = False
for message in messages:
if message.source == sender_email:
email_found = True
assert hasattr(message, 'id')
assert hasattr(message, 'region')
assert hasattr(message, 'timestamp')
assert hasattr(message, 'destination')
assert hasattr(message, 'subject')
assert hasattr(message, 'body')
body = message.body
assert hasattr(body, 'html_part')
html_content = body.html_part
print(f"Email content: {html_content}")
assert email_found, f"No email found sent from {sender_email}"