-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathtest_viewset.py
More file actions
268 lines (217 loc) · 7.99 KB
/
test_viewset.py
File metadata and controls
268 lines (217 loc) · 7.99 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
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
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
from organisations.models import Organisation
from users.models import FFAdminUser
def test_create_master_api_key__valid_data__returns_key_in_response(
admin_user: FFAdminUser, admin_client: APIClient, organisation: Organisation
) -> None:
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
data = {"name": "test_key", "organisation": organisation}
# When
response = admin_client.post(url, data=data)
# Then
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["key"] is not None
assert response.json()["is_admin"] is True
assert response.json()["created_by"] == admin_user.id
def test_create_master_api_key__non_admin_without_rbac__returns_400( # type: ignore[no-untyped-def]
admin_client, organisation, settings
):
# Given
settings.IS_RBAC_INSTALLED = False
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
data = {"name": "test_key", "organisation": organisation, "is_admin": False}
# When
response = admin_client.post(url, data=data)
# Then
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["is_admin"] == [
"RBAC is not installed, cannot create non-admin key"
]
def test_delete_master_api_key__existing_key__returns_204( # type: ignore[no-untyped-def]
admin_client, organisation, admin_master_api_key_prefix
): # noqa: E501
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-detail",
args=[organisation, admin_master_api_key_prefix],
)
# When
response = admin_client.delete(url)
# Then
assert response.status_code == status.HTTP_204_NO_CONTENT
def test_list_master_api_keys__active_key_exists__returns_key(
admin_client: APIClient,
organisation: int,
admin_master_api_key_prefix: str,
) -> None:
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
# When
response = admin_client.get(url)
# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 1
assert response.json()["results"][0]["prefix"] == admin_master_api_key_prefix
assert response.json()["results"][0]["has_expired"] is False
def test_list_master_api_keys__when_expired__returns_has_expired_true(
admin_client: APIClient,
organisation: Organisation,
expired_api_key_prefix: str,
) -> None:
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
# When
response = admin_client.get(url)
# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 1
assert response.json()["results"][0]["prefix"] == expired_api_key_prefix
assert response.json()["results"][0]["has_expired"] is True
def test_retrieve_master_api_key__existing_key__returns_key_details( # type: ignore[no-untyped-def]
admin_client, organisation, admin_master_api_key_prefix
):
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-detail",
args=[organisation, admin_master_api_key_prefix],
)
# When
response = admin_client.get(url)
# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["prefix"] == admin_master_api_key_prefix
def test_update_master_api_key__rbac_installed__updates_fields( # type: ignore[no-untyped-def]
admin_client, organisation, admin_master_api_key_prefix, settings
):
# Given
settings.IS_RBAC_INSTALLED = True
url = reverse(
"api-v1:organisations:organisation-master-api-keys-detail",
args=[organisation, admin_master_api_key_prefix],
)
new_name = "updated_test_key"
data = {
"prefix": admin_master_api_key_prefix,
"revoked": True,
"organisation": organisation,
"name": new_name,
"is_admin": False,
}
# When
response = admin_client.put(url, data=data)
# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["prefix"] == admin_master_api_key_prefix
assert response.json()["revoked"] is True
assert response.json()["name"] == new_name
assert response.json()["is_admin"] is False
def test_update_master_api_key__rbac_not_installed__returns_400( # type: ignore[no-untyped-def]
admin_client, organisation, admin_master_api_key_prefix, settings
):
# Given
settings.IS_RBAC_INSTALLED = False
url = reverse(
"api-v1:organisations:organisation-master-api-keys-detail",
args=[organisation, admin_master_api_key_prefix],
)
new_name = "updated_test_key"
data = {
"prefix": admin_master_api_key_prefix,
"organisation": organisation,
"name": new_name,
"is_admin": False,
}
# When
response = admin_client.put(url, data=data)
# Then
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["is_admin"] == [
"RBAC is not installed, cannot create non-admin key"
]
def test_list_master_api_keys__non_admin_user__returns_403( # type: ignore[no-untyped-def]
non_admin_client, organisation
):
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
# When
response = non_admin_client.get(url)
# Then
assert response.status_code == status.HTTP_403_FORBIDDEN
def test_create_master_api_key__wrong_org_in_body__uses_url_org( # type: ignore[no-untyped-def]
admin_client, organisation
):
# Given
list_create_url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
name = "test_key"
data = {"name": name, "organisation": 999}
# When
create_response = admin_client.post(list_create_url, data=data)
# Then
assert create_response.status_code == status.HTTP_201_CREATED
key = create_response.json()["key"]
assert key is not None
# and
# the key exists in the organisation provided in the URL
list_response = admin_client.get(list_create_url)
assert list_response.status_code == status.HTTP_200_OK
list_response_json = list_response.json()
assert list_response_json["count"] == 1
assert list_response_json["results"][0]["name"] == name
assert key.startswith(list_response_json["results"][0]["prefix"])
def test_delete_master_api_key__after_deletion__not_listed_and_unusable(
admin_client: APIClient,
organisation: int,
admin_master_api_key_client: APIClient,
admin_master_api_key_prefix: str,
) -> None:
# Given
# the relevant URLs
list_url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
detail_url = reverse(
"api-v1:organisations:organisation-master-api-keys-detail",
args=[organisation, admin_master_api_key_prefix],
)
list_projects_url = "%s?organisation=%s" % (
reverse("api-v1:projects:project-list"),
organisation,
)
# and we verify that before deletion, the master api key authenticated client
# can retrieve the projects for the organisation
valid_response = admin_master_api_key_client.get(list_projects_url)
assert valid_response.status_code == 200
# When
# we delete the api key
delete_response = admin_client.delete(detail_url)
assert delete_response.status_code == status.HTTP_204_NO_CONTENT
# Then
# It is not returned in the list response
list_response = admin_client.get(list_url)
assert list_response.json()["count"] == 0
# And
# it cannot be used to authenticate with the API anymore
invalid_response = admin_master_api_key_client.get(list_projects_url)
assert invalid_response.status_code == 401