This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_authentication.py
More file actions
256 lines (211 loc) · 9.65 KB
/
test_authentication.py
File metadata and controls
256 lines (211 loc) · 9.65 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
from datetime import datetime, timedelta
from http.cookies import SimpleCookie
from unittest.mock import AsyncMock, call, patch
import pytest
from django.conf import settings
from django.test import TestCase, override_settings
from django.urls import ResolverMatch
from rest_framework.exceptions import AuthenticationFailed, PermissionDenied
from rest_framework.test import APIRequestFactory
from shared.django_apps.codecov_auth.tests.factories import (
OwnerFactory,
UserFactory,
UserTokenFactory,
)
from shared.django_apps.core.tests.factories import RepositoryFactory
from codecov_auth.authentication import (
InternalTokenAuthentication,
SuperTokenAuthentication,
UserTokenAuthentication,
)
from codecov_auth.authentication.types import (
InternalToken,
InternalUser,
SuperToken,
SuperUser,
)
# Using the standard RequestFactory API to create a form POST request
def set_resolver_match(request, kwargs={}):
match = ResolverMatch(func=lambda: None, args=(), kwargs=kwargs)
request.resolver_match = match
class UserTokenAuthenticationTests(TestCase):
def test_bearer_token_auth(self):
user_token = UserTokenFactory()
request_factory = APIRequestFactory()
request = request_factory.get(
"", HTTP_AUTHORIZATION=f"Bearer {user_token.token}"
)
authenticator = UserTokenAuthentication()
result = authenticator.authenticate(request)
assert result == (user_token.owner, user_token)
def test_bearer_token_auth_invalid_token(self):
request_factory = APIRequestFactory()
request = request_factory.get(
"", HTTP_AUTHORIZATION="Bearer 8f9bc6cb-fd14-43bc-bbb5-be1e7c948f34"
)
authenticator = UserTokenAuthentication()
with pytest.raises(AuthenticationFailed):
authenticator.authenticate(request)
def test_token_not_uuid(self):
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION="Bearer hello_world")
authenticator = UserTokenAuthentication()
with pytest.raises(AuthenticationFailed):
authenticator.authenticate(request)
def test_bearer_token_auth_expired_token(self):
user_token = UserTokenFactory(valid_until=datetime.now() - timedelta(seconds=1))
request_factory = APIRequestFactory()
request = request_factory.get(
"", HTTP_AUTHORIZATION=f"Bearer {user_token.token}"
)
authenticator = UserTokenAuthentication()
with pytest.raises(AuthenticationFailed):
authenticator.authenticate(request)
def test_bearer_token_auth_malformed_header(self):
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION="wrong")
authenticator = UserTokenAuthentication()
result = authenticator.authenticate(request)
assert result is None
def test_bearer_token_auth_no_authorization_header(self):
request_factory = APIRequestFactory()
request = request_factory.get("")
authenticator = UserTokenAuthentication()
result = authenticator.authenticate(request)
assert result is None
class SuperTokenAuthenticationTests(TestCase):
@override_settings(SUPER_API_TOKEN="17603a9e-0463-45e1-883e-d649fccf4ae8")
def test_bearer_token_auth_if_token_is_super_token(self):
super_token = "17603a9e-0463-45e1-883e-d649fccf4ae8"
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {super_token}")
authenticator = SuperTokenAuthentication()
result = authenticator.authenticate(request)
assert isinstance(result[0], SuperUser)
assert isinstance(result[1], SuperToken)
assert result[1].token == super_token
@override_settings(SUPER_API_TOKEN="17603a9e-0463-45e1-883e-d649fccf4ae8")
def test_bearer_token_auth_invalid_super_token(self):
super_token = "0ae68e58-79f8-4341-9531-55aada05a251"
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {super_token}")
authenticator = SuperTokenAuthentication()
result = authenticator.authenticate(request)
assert result is None
def test_bearer_token_default_token_envar(self):
super_token = "0ae68e58-79f8-4341-9531-55aada05a251"
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {super_token}")
authenticator = SuperTokenAuthentication()
result = authenticator.authenticate(request)
assert result is None
def test_bearer_token_default_token_envar_and_same_string_as_header(self):
super_token = settings.SUPER_API_TOKEN
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {super_token}")
authenticator = SuperTokenAuthentication()
with pytest.raises(
AuthenticationFailed,
match="Invalid token header. Token string should not contain spaces.",
):
authenticator.authenticate(request)
class InternalTokenAuthenticationTests(TestCase):
@override_settings(CODECOV_INTERNAL_TOKEN="17603a9e-0463-45e1-883e-d649fccf4ae8")
def test_bearer_token_auth_if_token_is_internal_token(self):
internal_token = "17603a9e-0463-45e1-883e-d649fccf4ae8"
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {internal_token}")
authenticator = InternalTokenAuthentication()
result = authenticator.authenticate(request)
assert isinstance(result[0], InternalUser)
assert isinstance(result[1], InternalToken)
assert result[1].token == internal_token
@override_settings(CODECOV_INTERNAL_TOKEN="17603a9e-0463-45e1-883e-d649fccf4ae8")
def test_bearer_token_auth_if_token_is_not_internal_token(self):
internal_token = "random_token"
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {internal_token}")
authenticator = InternalTokenAuthentication()
with pytest.raises(
AuthenticationFailed,
match="Invalid token",
):
authenticator.authenticate(request)
def test_bearer_token_default_token_envar_and_same_string_as_header(self):
internal_token = settings.CODECOV_INTERNAL_TOKEN
request_factory = APIRequestFactory()
request = request_factory.get("", HTTP_AUTHORIZATION=f"Bearer {internal_token}")
authenticator = InternalTokenAuthentication()
with pytest.raises(
AuthenticationFailed,
match="Invalid token header. Token string should not contain spaces.",
):
authenticator.authenticate(request)
class ImpersonationTests(TestCase):
def setUp(self):
self.owner_to_impersonate = OwnerFactory(
username="impersonateme", service="github", user=UserFactory(is_staff=False)
)
self.staff_user = UserFactory(is_staff=True)
self.non_staff_user = UserFactory(is_staff=False)
self.client.cookies = SimpleCookie({"staff_user": self.owner_to_impersonate.pk})
def test_impersonation(self):
self.client.force_login(user=self.staff_user)
res = self.client.post(
"/graphql/gh",
{"query": "{ me { user { username } } }"},
content_type="application/json",
)
assert res.json()["data"]["me"] == {"user": {"username": "impersonateme"}}
@patch("core.commands.repository.repository.RepositoryCommands.fetch_repository")
def test_impersonation_with_okta(
self, mock_call_to_fetch_repository, new_callable=AsyncMock
):
repo = RepositoryFactory(author=self.owner_to_impersonate, private=True)
query_repositories = """{ owner(username: "%s") { repository(name: "%s") { ... on Repository { name } } } }"""
query = query_repositories % (repo.author.username, repo.name)
# not impersonating
del self.client.cookies["staff_user"]
self.client.force_login(user=self.owner_to_impersonate.user)
self.client.post(
"/graphql/gh",
{"query": query},
content_type="application/json",
)
# impersonating, same query
self.client.cookies = SimpleCookie({"staff_user": self.owner_to_impersonate.pk})
self.client.force_login(user=self.staff_user)
self.client.post(
"/graphql/gh",
{"query": query},
content_type="application/json",
)
mock_call_to_fetch_repository.assert_has_calls(
[
call(
self.owner_to_impersonate,
repo.name,
[],
exclude_okta_enforced_repos=True,
needs_coverage=False,
needs_commits=False,
),
call(
self.owner_to_impersonate,
repo.name,
[],
exclude_okta_enforced_repos=False,
needs_coverage=False,
needs_commits=False,
),
]
)
def test_impersonation_non_staff(self):
self.client.force_login(user=self.non_staff_user)
with pytest.raises(PermissionDenied):
self.client.get("/")
def test_impersonation_invalid_user(self):
self.client.cookies = SimpleCookie({"staff_user": 9999})
self.client.force_login(user=self.staff_user)
with pytest.raises(AuthenticationFailed):
self.client.get("/")