-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathtest_happy_db_setups.py
396 lines (339 loc) · 12.2 KB
/
test_happy_db_setups.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import pytest
import requests
SERVICE_HOST = 'http://mathesar-api-test-service:8000'
EXTERNAL_HOST = 'mathesar-test-user-db'
RPC_ENDPOINT = f'{SERVICE_HOST}/api/rpc/v0/'
@pytest.fixture(scope="module")
def admin_session():
login_payload = {"username": "admin", "password": "password"}
s = requests.Session()
s.get(f'{SERVICE_HOST}/auth/login/')
s.headers['X-CSRFToken'] = s.cookies['csrftoken']
s.post(f'{SERVICE_HOST}/auth/login/', data=login_payload)
s.headers['X-CSRFToken'] = s.cookies['csrftoken']
return s
@pytest.fixture(scope="module")
def admin_rpc_call(admin_session):
def _admin_rpc_request(function, **kwargs):
response = admin_session.post(
RPC_ENDPOINT,
json={
"jsonrpc": "2.0",
"method": function,
"params": kwargs,
"id": 0,
}
).json()
return response['result']
return _admin_rpc_request
@pytest.fixture(scope="module")
def intern_session():
# This function handles Django's auto-password-reset flow
# NOTE: This will only work after the admin adds `intern` as a user!
init_login_payload = {"username": "intern", "password": "password"}
s = requests.Session()
s.get(f'{SERVICE_HOST}/auth/login/')
s.headers['X-CSRFToken'] = s.cookies['csrftoken']
s.post(f'{SERVICE_HOST}/auth/login/', data=init_login_payload)
s.headers['X-CSRFToken'] = s.cookies['csrftoken']
reset_payload = {
"new_password1": "myinternpass1234",
"new_password2": "myinternpass1234"
}
s.post(f'{SERVICE_HOST}/auth/password_reset_confirm/', data=reset_payload)
s.headers['X-CSRFToken'] = s.cookies['csrftoken']
new_login_payload = {"username": "intern", "password": "myinternpass1234"}
s.post(f'{SERVICE_HOST}/auth/login/', data=new_login_payload)
s.headers['X-CSRFToken'] = s.cookies['csrftoken']
return s
@pytest.fixture(scope="module")
def intern_rpc_call(intern_session):
def _intern_rpc_request(function, **kwargs):
response = intern_session.post(
RPC_ENDPOINT,
json={
"jsonrpc": "2.0",
"method": function,
"params": kwargs,
"id": 0,
}
).json()
return response['result']
return _intern_rpc_request
def test_empty_db_list(admin_rpc_call):
db_list = admin_rpc_call('databases.configured.list')
assert db_list == []
def test_create_mathesar_db_internal(admin_rpc_call):
global internal_db_id
global internal_server_id
result = admin_rpc_call(
'databases.setup.create_new',
database='mathesar',
sample_data=['library_management']
)
assert set(result.keys()) == set(['configured_role', 'database', 'server'])
internal_db = result['database']
internal_server = result['server']
assert internal_db['name'] == 'mathesar'
assert internal_db['needs_upgrade_attention'] is False
internal_db_id = internal_db['id']
internal_server_id = internal_server['id']
def test_connect_mathesar_db_external(admin_rpc_call):
global external_db_id
result = admin_rpc_call(
'databases.setup.connect_existing',
host=EXTERNAL_HOST,
port=5432,
database='my_data',
role='data_admin',
password='data1234',
)
assert set(result.keys()) == set(['configured_role', 'database', 'server'])
external_db = result['database']
assert external_db['name'] == 'my_data'
assert external_db['needs_upgrade_attention'] is False
assert result['configured_role']['name'] == 'data_admin'
external_db_id = external_db['id']
def test_list_databases_has_upgrade_status(admin_rpc_call):
result = admin_rpc_call('databases.configured.list')
assert all(d['needs_upgrade_attention'] is False for d in result)
def test_batch_sql_update_no_error(admin_session):
admin_session.post(
RPC_ENDPOINT,
json=[
{
"jsonrpc": "2.0",
"method": "databases.upgrade_sql",
"id": "0",
"params": {"database_id": internal_db_id}
},
{
"jsonrpc": "2.0",
"method": "databases.upgrade_sql",
"id": "2",
"params": {"database_id": external_db_id}
},
]
)
def test_get_current_role(admin_rpc_call):
global mathesar_role_oid
result = admin_rpc_call(
'roles.get_current_role',
database_id=internal_db_id,
)
assert result['current_role']['super'] is True
mathesar_role_oid = result['current_role']['oid']
# Now the scenario proper starts. The admin will add an `intern` user to
# Mathesar, an `intern` role to the internal `mathesar` DB, then grant
# `CONNECT` to that role on the DB, `USAGE` on "Library Management", and
# `SELECT` on "Books". We'll make sure the intern has the correct
# privileges at each step.
#
# We'll follow along with the API calls made by the front end.
def test_add_user(admin_rpc_call):
global intern_user_id
before_users = admin_rpc_call('users.list')
assert len(before_users) == 1
intern_add = {
'display_language': 'en',
'email': 'intern@example.com',
'is_superuser': False,
'password': 'password',
'username': 'intern',
}
admin_rpc_call('users.add', user_def=intern_add)
after_users = admin_rpc_call('users.list')
assert len(after_users) == 2
intern_user_id = [
u['id'] for u in after_users if u['username'] == 'intern'
][0]
def test_intern_no_databases(intern_rpc_call):
db_list = intern_rpc_call('databases.configured.list')
assert db_list == []
def test_list_configured_roles(admin_rpc_call):
# The only role at this point should be the initial `mathesar` role.
result = admin_rpc_call(
'roles.configured.list',
server_id=internal_server_id,
)
assert len(result) == 1
def test_add_role(admin_rpc_call):
global intern_role_oid
result = admin_rpc_call(
'roles.add',
database_id=internal_db_id,
login=True,
rolename='intern',
password='internpass'
)
intern_role_oid = result['oid']
def test_configure_role(admin_rpc_call):
global intern_configured_role_id
result = admin_rpc_call(
'roles.configured.add',
name='intern',
password='internpass',
server_id=internal_server_id,
)
assert 'password' not in result.keys()
intern_configured_role_id = result['id']
def test_add_collaborator(admin_rpc_call):
before_collaborators = admin_rpc_call(
'collaborators.list',
database_id=internal_db_id,
)
# Should only have the admin so far
assert len(before_collaborators) == 1
admin_rpc_call(
'collaborators.add',
configured_role_id=intern_configured_role_id,
database_id=internal_db_id,
user_id=intern_user_id,
)
after_collaborators = admin_rpc_call(
'collaborators.list',
database_id=internal_db_id,
)
assert len(after_collaborators) == 2
intern_collab_definition = [
c for c in after_collaborators if c['user_id'] == intern_user_id
][0]
assert intern_collab_definition['database_id'] == internal_db_id
assert intern_collab_definition['configured_role_id'] == intern_configured_role_id
def test_intern_has_internal_database(intern_rpc_call):
db_list = intern_rpc_call('databases.configured.list')
assert len(db_list) == 1
assert db_list[0]['id'] == internal_db_id
def test_database_privileges_add(admin_rpc_call):
before_result = admin_rpc_call(
'databases.privileges.list_direct',
database_id=internal_db_id,
)
# `intern` shouldn't have any privileges yet.
assert intern_role_oid not in [r['role_oid'] for r in before_result]
after_result = admin_rpc_call(
'databases.privileges.replace_for_roles',
database_id=internal_db_id,
privileges=[{'direct': ['CONNECT'], 'role_oid': intern_role_oid}]
)
# `intern` should have `CONNECT` after this point.
assert [
d['direct'] for d in after_result if d['role_oid'] == intern_role_oid
][0] == ['CONNECT']
def test_schemas_list(admin_rpc_call):
global library_management_oid
result = admin_rpc_call(
'schemas.list',
database_id=1
)
# Should have `public` and `Library Management`
assert len(result) == 2
library_management_oid = [
s['oid'] for s in result if s['name'] == 'Library Management'
][0]
def test_tables_list(admin_rpc_call):
global books_oid
result = admin_rpc_call(
'tables.list',
database_id=1,
schema_oid=library_management_oid
)
assert len(result) == 7
books_oid = [t['oid'] for t in result if t['name'] == 'Books'][0]
def test_intern_cannot_access_library_schema_tables(intern_session):
response = intern_session.post(
RPC_ENDPOINT,
json={
"jsonrpc": "2.0",
"method": 'records.list',
"params": {
"table_oid": books_oid,
"database_id": internal_db_id,
"limit": 20
},
"id": 0,
}
).json()
assert response['error']['code'] == -30101 # InsufficientPrivilege
assert "permission denied for schema" in response['error']['message']
def test_schema_privileges_add(admin_rpc_call):
before_result = admin_rpc_call(
'schemas.privileges.list_direct',
database_id=internal_db_id,
schema_oid=library_management_oid,
)
# `intern` should not have any schema privileges yet.
assert intern_role_oid not in [r['role_oid'] for r in before_result]
after_result = admin_rpc_call(
'schemas.privileges.replace_for_roles',
database_id=internal_db_id,
schema_oid=library_management_oid,
privileges=[{'direct': ['USAGE'], 'role_oid': intern_role_oid}],
)
assert [
d['direct'] for d in after_result if d['role_oid'] == intern_role_oid
][0] == ['USAGE']
def test_intern_still_cannot_access_books_table_data(intern_session):
response = intern_session.post(
RPC_ENDPOINT,
json={
"jsonrpc": "2.0",
"method": 'records.list',
"params": {
"table_oid": books_oid,
"database_id": internal_db_id,
"limit": 20
},
"id": 0,
}
).json()
assert response['error']['code'] == -30101 # InsufficientPrivilege
assert "permission denied for table" in response['error']['message']
def test_table_privileges_add(admin_rpc_call):
before_result = admin_rpc_call(
'tables.privileges.list_direct',
table_oid=books_oid,
database_id=internal_db_id,
)
# `intern` should not have any schema privileges yet.
assert intern_role_oid not in [r['role_oid'] for r in before_result]
after_result = admin_rpc_call(
'tables.privileges.replace_for_roles',
table_oid=books_oid,
database_id=internal_db_id,
privileges=[{'direct': ['SELECT'], 'role_oid': intern_role_oid}],
)
assert [
d['direct'] for d in after_result if d['role_oid'] == intern_role_oid
][0] == ['SELECT']
def test_intern_can_now_access_books_table(intern_rpc_call):
records = intern_rpc_call(
'records.list',
table_oid=books_oid,
database_id=internal_db_id,
limit=20,
)
assert records['count'] == 1410
assert len(records['results']) == 20
def test_disconnect_fails_when_dependencies(admin_session):
response = admin_session.post(
RPC_ENDPOINT,
json={
"jsonrpc": "2.0",
"method": "databases.configured.disconnect",
"params": {
"database_id": internal_db_id,
},
"id": 0,
},
).json()
assert response['error']['code'] == -30035 # DependentObjectsStillExist
def test_disconnect_succeeds_when_no_dependencies(admin_rpc_call):
admin_rpc_call(
"databases.configured.disconnect",
database_id=internal_db_id,
schemas_to_remove=["msar", "__msar"]
)
result = admin_rpc_call('databases.configured.list')
assert len(result) == 1
assert internal_db_id not in [d["id"] for d in result]