-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtests.py
258 lines (184 loc) · 7.36 KB
/
tests.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'''
Integration tests for the PostgREST sessions example.
'''
import os
import subprocess
import time
import requests
import pytest
BASE_URL = os.environ.get('EXAMPLEAPP_URI', 'http://localhost:3000/')
DB_URI = os.environ.get('EXAMPLEAPP_TESTS_DB_URI')
@pytest.mark.skipif(DB_URI is None,
reason='$EXAMPLEAPP_TESTS_DB_URI required for running db tests')
def test_db():
'Run tests that are defined in the database schema.'
result = subprocess.run(
['psql', DB_URI, '-c', 'select tests.run();'],
capture_output=True,
check=True,
)
test_results = result.stdout.decode('utf-8').split('\n')
for line in test_results:
assert not line.strip().startswith('not ok')
@pytest.fixture(scope='session')
def alice_email():
'Fixture for a user email.'
return f'alice-{time.time()}@test.org'
@pytest.fixture(scope='session')
def alice_account(alice_email):
'Fixture for a user account.'
session = requests.Session()
resp = session.post(f'{BASE_URL}rpc/register', json={
'email': alice_email,
'name': 'Alice',
'password': 'alicesecret',
})
assert resp.status_code == 200
@pytest.fixture()
def alice_session(alice_email, alice_account):
'Fixture for a logged in web session'
session = requests.Session()
resp = session.post(f'{BASE_URL}rpc/login', json={
'email': alice_email,
'password': 'alicesecret',
})
assert resp.status_code == 200
return session
@pytest.fixture(scope='session')
def bob_email():
'Fixture for a user email.'
return f'bob-{time.time()}@test.org'
@pytest.fixture(scope='session')
def bob_account(bob_email):
'Fixture for a user account.'
session = requests.Session()
resp = session.post(f'{BASE_URL}rpc/register', json={
'email': bob_email,
'name': 'Bob',
'password': 'bobsecret',
})
assert resp.status_code == 200
@pytest.fixture()
def bob_session(bob_email, bob_account):
'Fixture for a logged in web session'
session = requests.Session()
resp = session.post(f'{BASE_URL}rpc/login', json={
'email': bob_email,
'password': 'bobsecret',
})
assert resp.status_code == 200
return session
def test_register():
'Registering as an anonymous user should succeed.'
session = requests.Session()
resp = session.post(f'{BASE_URL}rpc/register', json={
'email': f'registrationtest-{time.time()}@test.org',
'name': 'Registration Test',
'password': 'registrationsecret',
})
assert resp.status_code == 200
assert resp.cookies.get('session_token')
def test_register_repeated(alice_session):
'Registering when already logged in should not be allowed.'
resp = alice_session.post(f'{BASE_URL}rpc/register', json={
'email': f'registrationtest-{time.time()}@test.org',
'name': 'Registration Test',
'password': 'registrationsecret',
})
assert resp.status_code == 401
assert resp.cookies.get('session_token') is None
def test_login(alice_account, alice_email):
'Logging in with valid credentials should succeed.'
session = requests.Session()
resp = session.post(f'{BASE_URL}rpc/login', json={
'email': alice_email,
'password': 'alicesecret',
})
assert resp.status_code == 200
assert resp.cookies.get('session_token')
def test_login_repeated(alice_email, alice_session):
'A logged in user should not be able to log in again.'
resp = alice_session.post(f'{BASE_URL}rpc/login', json={
'email': alice_email,
'password': 'alicesecret',
})
assert resp.status_code == 401
def test_login_wrong_email(alice_account, alice_email):
'Logins with wrong emails should fail.'
resp = requests.post(f'{BASE_URL}rpc/login', json={
'email': 'wrong_' + alice_email,
'password': 'alicesecret',
})
assert resp.status_code == 401
def test_login_wrong_password(alice_account, alice_email):
'Logins with wrong passwords should fail.'
resp = requests.post(f'{BASE_URL}rpc/login', json={
'email': alice_email,
'password': 'wrong_alicesecret',
})
assert resp.status_code == 401
def test_current_user(alice_session):
'Logged in users should be able to access information on their own account.'
resp = requests.get(f'{BASE_URL}rpc/current_user')
assert resp.status_code == 401
resp = alice_session.get(f'{BASE_URL}rpc/current_user')
assert resp.status_code == 200
assert resp.json()[0]['name'] == 'Alice'
def test_logout(alice_session):
'Logging out should be possible for logged in users and change permissions.'
resp = alice_session.get(f'{BASE_URL}rpc/current_user')
assert resp.status_code == 200
resp = alice_session.post(f'{BASE_URL}rpc/logout')
assert resp.status_code == 200
resp = alice_session.get(f'{BASE_URL}rpc/current_user')
assert resp.status_code == 401
resp = alice_session.post(f'{BASE_URL}rpc/logout')
assert resp.status_code == 401
def test_refresh_session(alice_session):
'Refreshing a session should succeed and set a new cookie.'
resp = alice_session.post(f'{BASE_URL}rpc/refresh_session')
assert resp.status_code == 200
assert resp.cookies.get('session_token')
def test_users(alice_session):
'Logged in users should be able to get a listing of users.'
current_user = alice_session.get(f'{BASE_URL}rpc/current_user').json()[0]
resp = alice_session.get(f'{BASE_URL}users')
assert resp.status_code == 200
assert current_user['user_id'] in (user['user_id'] for user in resp.json())
def test_create_todo(alice_session):
'A user should be able to create todo items.'
current_user = alice_session.get(f'{BASE_URL}rpc/current_user').json()[0]
resp = alice_session.post(f'{BASE_URL}todos', json={
'user_id': current_user['user_id'],
'description': 'Test todo',
}, headers={'Prefer': 'return=representation'})
assert resp.status_code == 201
assert resp.json()[0]['description'] == 'Test todo'
def test_todo_visibility(alice_session, bob_session):
'A user should only be able to see his own and public todos.'
current_user = alice_session.get(f'{BASE_URL}rpc/current_user').json()[0]
todo_desc = f'Alice test todo {time.time()}'
resp = alice_session.post(f'{BASE_URL}todos', json={
'user_id': current_user['user_id'],
'description': todo_desc,
}, headers={'Prefer': 'return=representation'})
assert resp.status_code == 201
# Create a new todo item
resp = alice_session.get(f'{BASE_URL}todos')
assert todo_desc in (todo['description'] for todo in resp.json())
# The todo item should not be visible for other users
resp = bob_session.get(f'{BASE_URL}todos')
assert todo_desc not in (todo['description'] for todo in resp.json())
# Set all todo items from alice to be public
resp = alice_session.patch(f'{BASE_URL}todos', json={'public': True})
assert resp.status_code == 204
# Other users should now be able to see it
resp = bob_session.get(f'{BASE_URL}todos')
assert todo_desc in (todo['description'] for todo in resp.json())
def test_create_todo_anonymous(alice_session):
'Anonymous users should not be able to create todo items.'
resp = alice_session.post(f'{BASE_URL}todos', json={
'user_id': 1,
'description': 'Test todo',
})
assert resp.status_code == 401