forked from melli-labs/python-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_melli.py
241 lines (202 loc) Β· 8.36 KB
/
test_melli.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
233
234
235
236
237
238
239
240
241
import pytest
from httpx import AsyncClient
from melli import app
@pytest.fixture()
async def async_client():
async_client = AsyncClient(app=app, base_url="http://test")
yield async_client
await async_client.aclose()
@pytest.mark.asyncio
async def test_task1(async_client):
response = await async_client.get("/task1/greet/Jasmin")
assert response.status_code == 200
assert response.json() == "Hallo Jasmin, ich bin Melli."
response = await async_client.get("/task1/greet/Stefan", params={"language": "en"})
assert response.status_code == 200
assert response.json() == "Hello Stefan, I am Melli."
response = await async_client.get("/task1/greet/Hans", params={"language": "es"})
assert response.status_code == 200
assert response.json() == "Hola Hans, soy Melli."
response = await async_client.get("/task1/greet/Ben", params={"language": "ita"})
assert response.status_code == 200
assert response.json() == f"Hallo Ben, leider spreche ich nicht 'ita'!"
@pytest.mark.depends(on=["test_task1"])
def test_task1_success_message():
print(
" π Congratulations! You solved the first task."
" Go to `/task2` to solve the next one."
)
@pytest.mark.asyncio
async def test_task2(async_client):
data = {"company_name": "Melli", "is_future_unicorn": True}
response = await async_client.post("/task2/camelize", json=data)
assert response.status_code == 200
assert response.json() == {
"companyName": "Melli",
"isFutureUnicorn": True,
}
@pytest.mark.depends(on=["test_task2"])
def test_task2_success_message():
print(
" π Wow, keep going! Let's see if you solve the next one too."
" You can find it at `/task3`."
)
@pytest.mark.asyncio
class TestTask3:
async def test_call_friend(self, async_client):
response = await async_client.post(
"/task3/action",
json={"username": "Matthias", "action": "Call my friend Sahar."},
)
assert response.status_code == 200
assert response.json() == {
"message": "π€ Calling Sahar ...",
}
response = await async_client.post(
"/task3/action",
json={"username": "Matthias", "action": "Can you call Hans?"},
)
assert response.status_code == 200
assert response.json() == {
"message": "π€ Calling Hans ...",
}
response = await async_client.post(
"/task3/action",
json={
"username": "Matthias",
"action": "I haven't spoken to Franziska in a long time. Can you call her?",
},
)
assert response.status_code == 200
assert response.json() == {
"message": "π€ Calling Franziska ...",
}
response = await async_client.post(
"/task3/action",
json={
"username": "Stefan",
"action": "Can you call Ben for me?",
},
)
assert response.status_code == 200
assert response.json() == {
"message": "π€ Calling Ben ...",
}
async def test_call_unknown(self, async_client):
response = await async_client.post(
"/task3/action",
json={"username": "Stefan", "action": "Call my friend Christian."},
)
assert response.status_code == 200
assert response.json() == {
"message": "Stefan, I can't find this person in your contacts.",
}
async def test_reminder(self, async_client):
response = await async_client.post(
"/task3/action",
json={
"username": "Stefan",
"action": "Remind me to book the theater tickets.",
},
)
assert response.status_code == 200
assert response.json() == {
"message": "π Alright, I will remind you!",
}
async def test_timer(self, async_client):
response = await async_client.post(
"/task3/action",
json={"username": "Matthias", "action": "Set a timer for eight minutes!"},
)
assert response.status_code == 200
assert response.json() == {
"message": "β° Alright, the timer is set!",
}
async def test_unknown_action(self, async_client):
response = await async_client.post(
"/task3/action",
json={"username": "Stefan", "action": "What is the meaning of life?"},
)
assert response.status_code == 200
assert response.json() == {
"message": "π Sorry , but I can't help with that!",
}
async def test_unknown_user(self, async_client):
response = await async_client.post(
"/task3/action",
json={"username": "Felix", "action": "Call my friend Leo."},
)
assert response.status_code == 200
assert response.json() == {
"message": "Hi Felix, I don't know you yet. But I would love to meet you!",
}
response = await async_client.post(
"/task3/action",
json={
"username": "Ben",
"action": "Hey Melli, remind me to rewrite our PHP backend in Rust π¦!",
},
)
assert response.status_code == 200
assert response.json() == {
"message": "Hi Ben, I don't know you yet. But I would love to meet you!",
}
@pytest.mark.depends(on=["TestTask3"])
def test_task3_success_message():
print(
" β This was really hard, congratulations! You're awesome π!"
" If you really wanna impress us, there is an optional bonus task at `/task4`."
" But if you're short on time you can already create a pull request at"
" https://github.com/melli-labs/python-challenge!"
)
@pytest.mark.bonus
@pytest.mark.asyncio
class TestTask4:
stefan = {"username": "stefan", "password": "decent-espresso-by-john-buckmann"}
felix = {"username": "felix", "password": "elm>javascript"}
@pytest.fixture()
async def token_stefan(self, async_client):
response = await async_client.post("/task4/token", data=self.stefan)
assert response.status_code == 200
return response.json()
@pytest.fixture()
async def token_felix(self, async_client):
response = await async_client.post("/task4/token", data=self.felix)
assert response.status_code == 200
return response.json()
async def test_login_registered_user(self, token_stefan, token_felix):
for token in token_stefan, token_felix:
assert isinstance(token, dict)
assert "token_type" in token and "access_token" in token
async def test_login_registered_user_wrong_password(self, async_client):
stefan_wrong_password = self.stefan | {"password": "wrong_password"}
response = await async_client.post("/task4/token", data=stefan_wrong_password)
assert response.status_code == 401
assert response.json() == {"detail": "Incorrect username or password"}
async def test_login_unregistered_user(self, async_client):
response = await async_client.post(
"/task4/token", data={"username": "hacker", "password": "123456"}
)
assert response.status_code == 401
assert response.json() == {"detail": "Incorrect username or password"}
async def test_read_own_secret(self, async_client, token_stefan):
response = await async_client.get(
"/task4/users/stefan/secret",
headers={"Authorization": f"Bearer {token_stefan['access_token']}"},
)
assert response.status_code == 200
assert response.json() == "I love pressure-profiled espresso β!"
async def test_read_other_user_secret(self, async_client, token_felix):
response = await async_client.get(
"/task4/users/stefan/secret",
headers={"Authorization": f"Bearer {token_felix['access_token']}"},
)
assert response.status_code == 403
assert response.json() == {"detail": "Don't spy on other user!"}
@pytest.mark.depends(on=["TestTask4"])
def test_task4_success_message():
print(
" π€© Amazing! This is really impressive. Here is your prize π!"
" We would love to get in touch with you π―. Therefore, create"
" a pull request at https://github.com/melli-labs/python-challenge!"
)