-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathtest_permission_model.py
More file actions
56 lines (42 loc) · 1.68 KB
/
Copy pathtest_permission_model.py
File metadata and controls
56 lines (42 loc) · 1.68 KB
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
import pytest
from rest_framework.test import APIClient
@pytest.fixture
def api_client():
return APIClient()
@pytest.mark.django_db
def test_normal_user_cannot_access_non_query_api(api_client, normal_user):
api_client.force_authenticate(user=normal_user)
response = api_client.get("/api/v1/instance/")
assert response.status_code == 403
@pytest.mark.django_db
def test_superuser_can_access_non_query_api(api_client, super_user):
api_client.force_authenticate(user=super_user)
response = api_client.get("/api/v1/instance/")
assert response.status_code == 200
@pytest.mark.django_db
def test_normal_user_can_access_query_api(api_client, normal_user, monkeypatch):
expected = {"status": 0, "msg": "ok", "data": [{"id": 1, "instance_name": "ins"}]}
monkeypatch.setattr(
"sql_api.api_sqlquery.list_user_accessible_instances",
lambda **kwargs: expected,
)
api_client.force_authenticate(user=normal_user)
response = api_client.get("/api/v1/sqlquery/instances/")
assert response.status_code == 200
assert response.json() == expected
@pytest.mark.django_db
def test_normal_user_can_access_table_locator(api_client, normal_user, monkeypatch):
monkeypatch.setattr("sql_api.api_instance.user_instances", lambda user: [])
monkeypatch.setattr(
"sql_api.api_instance.resolve_table_instances", lambda **kwargs: []
)
api_client.force_authenticate(user=normal_user)
response = api_client.post(
"/api/v1/instance/table-instances/",
{"table_name": "orders"},
format="json",
)
assert response.status_code == 200
body = response.json()
assert body["status"] == 0
assert body["count"] == 0