-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathminitwit_sim_api_test.py
198 lines (154 loc) · 5.82 KB
/
minitwit_sim_api_test.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
import os
import json
import base64
import sqlite3
import requests
from pathlib import Path
from contextlib import closing
BASE_URL = 'http://127.0.0.1:5001'
DATABASE = "/tmp/minitwit.db"
USERNAME = 'simulator'
PWD = 'super_safe!'
CREDENTIALS = ':'.join([USERNAME, PWD]).encode('ascii')
ENCODED_CREDENTIALS = base64.b64encode(CREDENTIALS).decode()
HEADERS = {'Connection': 'close',
'Content-Type': 'application/json',
f'Authorization': f'Basic {ENCODED_CREDENTIALS}'}
def init_db():
"""Creates the database tables."""
with closing(sqlite3.connect(DATABASE)) as db:
with open("schema.sql") as fp:
db.cursor().executescript(fp.read())
db.commit()
# Empty the database and initialize the schema again
Path(DATABASE).unlink()
init_db()
def test_latest():
# post something to update LATEST
url = f"{BASE_URL}/register"
data = {'username': 'test', 'email': 'test@test', 'pwd': 'foo'}
params = {'latest': 1337}
response = requests.post(url, data=json.dumps(data),
params=params, headers=HEADERS)
assert response.ok
# verify that latest was updated
url = f'{BASE_URL}/latest'
response = requests.get(url, headers=HEADERS)
assert response.ok
assert response.json()['latest'] == 1337
def test_register():
username = 'a'
email = 'a@a.a'
pwd = 'a'
data = {'username': username, 'email': email, 'pwd': pwd}
params = {'latest': 1}
response = requests.post(f'{BASE_URL}/register',
data=json.dumps(data), headers=HEADERS, params=params)
assert response.ok
# TODO: add another assertion that it is really there
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 1
def test_create_msg():
username = 'a'
data = {'content': 'Blub!'}
url = f'{BASE_URL}/msgs/{username}'
params = {'latest': 2}
response = requests.post(url, data=json.dumps(data),
headers=HEADERS, params=params)
assert response.ok
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 2
def test_get_latest_user_msgs():
username = 'a'
query = {'no': 20, 'latest': 3}
url = f'{BASE_URL}/msgs/{username}'
response = requests.get(url, headers=HEADERS, params=query)
assert response.status_code == 200
got_it_earlier = False
for msg in response.json():
if msg['content'] == 'Blub!' and msg['user'] == username:
got_it_earlier = True
assert got_it_earlier
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 3
def test_get_latest_msgs():
username = 'a'
query = {'no': 20, 'latest': 4}
url = f'{BASE_URL}/msgs'
response = requests.get(url, headers=HEADERS, params=query)
assert response.status_code == 200
got_it_earlier = False
for msg in response.json():
if msg['content'] == 'Blub!' and msg['user'] == username:
got_it_earlier = True
assert got_it_earlier
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 4
def test_register_b():
username = 'b'
email = 'b@b.b'
pwd = 'b'
data = {'username': username, 'email': email, 'pwd': pwd}
params = {'latest': 5}
response = requests.post(f'{BASE_URL}/register', data=json.dumps(data),
headers=HEADERS, params=params)
assert response.ok
# TODO: add another assertion that it is really there
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 5
def test_register_c():
username = 'c'
email = 'c@c.c'
pwd = 'c'
data = {'username': username, 'email': email, 'pwd': pwd}
params = {'latest': 6}
response = requests.post(f'{BASE_URL}/register', data=json.dumps(data),
headers=HEADERS, params=params)
assert response.ok
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 6
def test_follow_user():
username = 'a'
url = f'{BASE_URL}/fllws/{username}'
data = {'follow': 'b'}
params = {'latest': 7}
response = requests.post(url, data=json.dumps(data),
headers=HEADERS, params=params)
assert response.ok
data = {'follow': 'c'}
params = {'latest': 8}
response = requests.post(url, data=json.dumps(data),
headers=HEADERS, params=params)
assert response.ok
query = {'no': 20, 'latest': 9}
response = requests.get(url, headers=HEADERS, params=query)
assert response.ok
json_data = response.json()
assert "b" in json_data["follows"]
assert "c" in json_data["follows"]
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 9
def test_a_unfollows_b():
username = 'a'
url = f'{BASE_URL}/fllws/{username}'
# first send unfollow command
data = {'unfollow': 'b'}
params = {'latest': 10}
response = requests.post(url, data=json.dumps(data),
headers=HEADERS, params=params)
assert response.ok
# then verify that b is no longer in follows list
query = {'no': 20, 'latest': 11}
response = requests.get(url, params=query, headers=HEADERS)
assert response.ok
assert 'b' not in response.json()['follows']
# verify that latest was updated
response = requests.get(f'{BASE_URL}/latest', headers=HEADERS)
assert response.json()['latest'] == 11