|
11 | 11 | from codecov_auth.models import Owner
|
12 | 12 |
|
13 | 13 |
|
14 |
| -def test_get_github_redirect(client): |
| 14 | +def _get_state_from_redis(mock_redis): |
| 15 | + key_redis = mock_redis.keys("*")[0].decode() |
| 16 | + return key_redis.replace("oauth-state-", "") |
| 17 | + |
| 18 | + |
| 19 | +def test_get_github_redirect(client, mock_redis): |
15 | 20 | url = reverse("github-login")
|
16 | 21 | res = client.get(url)
|
| 22 | + state = _get_state_from_redis(mock_redis) |
17 | 23 | assert res.status_code == 302
|
18 | 24 | assert (
|
19 | 25 | res.url
|
20 |
| - == "https://github.com/login/oauth/authorize?response_type=code&scope=user%3Aemail%2Cread%3Aorg%2Crepo%3Astatus%2Cwrite%3Arepo_hook&client_id=3d44be0e772666136a13" |
| 26 | + == f"https://github.com/login/oauth/authorize?response_type=code&scope=user%3Aemail%2Cread%3Aorg%2Crepo%3Astatus%2Cwrite%3Arepo_hook&client_id=3d44be0e772666136a13&state={state}" |
21 | 27 | )
|
22 | 28 |
|
23 | 29 |
|
24 |
| -def test_get_github_redirect_with_ghpr_cookie(client, settings): |
| 30 | +def test_get_github_redirect_with_ghpr_cookie(client, mock_redis, settings): |
25 | 31 | settings.COOKIES_DOMAIN = ".simple.site"
|
26 | 32 | client.cookies = SimpleCookie({"ghpr": "true"})
|
27 | 33 | url = reverse("github-login")
|
28 | 34 | res = client.get(url)
|
| 35 | + state = _get_state_from_redis(mock_redis) |
29 | 36 | assert res.status_code == 302
|
30 | 37 | assert (
|
31 | 38 | res.url
|
32 |
| - == "https://github.com/login/oauth/authorize?response_type=code&scope=user%3Aemail%2Cread%3Aorg%2Crepo%3Astatus%2Cwrite%3Arepo_hook%2Crepo&client_id=3d44be0e772666136a13" |
| 39 | + == f"https://github.com/login/oauth/authorize?response_type=code&scope=user%3Aemail%2Cread%3Aorg%2Crepo%3Astatus%2Cwrite%3Arepo_hook%2Crepo&client_id=3d44be0e772666136a13&state={state}" |
33 | 40 | )
|
34 | 41 | assert "ghpr" in res.cookies
|
35 | 42 | ghpr_cooke = res.cookies["ghpr"]
|
36 | 43 | assert ghpr_cooke.value == "true"
|
37 | 44 | assert ghpr_cooke.get("domain") == ".simple.site"
|
38 | 45 |
|
39 | 46 |
|
40 |
| -def test_get_github_redirect_with_private_url(client, settings): |
| 47 | +def test_get_github_redirect_with_private_url(client, mock_redis, settings): |
41 | 48 | settings.COOKIES_DOMAIN = ".simple.site"
|
42 | 49 | url = reverse("github-login")
|
43 | 50 | res = client.get(url, {"private": "true"})
|
| 51 | + state = _get_state_from_redis(mock_redis) |
44 | 52 | assert res.status_code == 302
|
45 | 53 | assert (
|
46 | 54 | res.url
|
47 |
| - == "https://github.com/login/oauth/authorize?response_type=code&scope=user%3Aemail%2Cread%3Aorg%2Crepo%3Astatus%2Cwrite%3Arepo_hook%2Crepo&client_id=3d44be0e772666136a13" |
| 55 | + == f"https://github.com/login/oauth/authorize?response_type=code&scope=user%3Aemail%2Cread%3Aorg%2Crepo%3Astatus%2Cwrite%3Arepo_hook%2Crepo&client_id=3d44be0e772666136a13&state={state}" |
48 | 56 | )
|
49 | 57 | assert "ghpr" in res.cookies
|
50 | 58 | ghpr_cooke = res.cookies["ghpr"]
|
@@ -118,8 +126,10 @@ async def is_student(*args, **kwargs):
|
118 | 126 | as_tuple=mocker.MagicMock(return_value=("a", "b"))
|
119 | 127 | ),
|
120 | 128 | )
|
| 129 | + |
121 | 130 | url = reverse("github-login")
|
122 |
| - res = client.get(url, {"code": "aaaaaaa"}) |
| 131 | + mock_redis.setex("oauth-state-abc", 300, "http://localhost:3000/gh") |
| 132 | + res = client.get(url, {"code": "aaaaaaa", "state": "abc"}) |
123 | 133 | assert res.status_code == 302
|
124 | 134 | assert "github-token" in res.cookies
|
125 | 135 | assert "github-username" in res.cookies
|
@@ -181,15 +191,25 @@ def test_get_github_already_with_code_github_error(
|
181 | 191 | async def helper_func(*args, **kwargs):
|
182 | 192 | raise TorngitClientGeneralError(403, "response", "message")
|
183 | 193 |
|
| 194 | + mock_redis.setex("oauth-state-abc", 300, "http://localhost:3000/gh") |
| 195 | + |
184 | 196 | mocker.patch.object(Github, "get_authenticated_user", side_effect=helper_func)
|
185 | 197 | url = reverse("github-login")
|
186 |
| - res = client.get(url, {"code": "aaaaaaa"}) |
| 198 | + res = client.get(url, {"code": "aaaaaaa", "state": "abc"}) |
187 | 199 | assert res.status_code == 302
|
188 | 200 | assert "github-token" not in res.cookies
|
189 | 201 | assert "github-username" not in res.cookies
|
190 | 202 | assert res.url == "/"
|
191 | 203 |
|
192 | 204 |
|
| 205 | +def test_state_not_known(client, mocker, db, mock_redis, settings): |
| 206 | + url = reverse("github-login") |
| 207 | + res = client.get(url, {"code": "aaaaaaa", "state": "doesnt exist"}) |
| 208 | + assert res.status_code == 400 |
| 209 | + assert "github-token" not in res.cookies |
| 210 | + assert "github-username" not in res.cookies |
| 211 | + |
| 212 | + |
193 | 213 | def test_get_github_already_with_code_with_email(
|
194 | 214 | client, mocker, db, mock_redis, settings
|
195 | 215 | ):
|
@@ -226,8 +246,9 @@ async def is_student(*args, **kwargs):
|
226 | 246 | as_tuple=mocker.MagicMock(return_value=("a", "b"))
|
227 | 247 | ),
|
228 | 248 | )
|
| 249 | + mock_redis.setex("oauth-state-abc", 300, "http://localhost:3000/gh") |
229 | 250 | url = reverse("github-login")
|
230 |
| - res = client.get(url, {"code": "aaaaaaa"}) |
| 251 | + res = client.get(url, {"code": "aaaaaaa", "state": "abc"}) |
231 | 252 | assert res.status_code == 302
|
232 | 253 | assert "github-token" in res.cookies
|
233 | 254 | assert "github-username" in res.cookies
|
@@ -281,8 +302,9 @@ async def is_student(*args, **kwargs):
|
281 | 302 | as_tuple=mocker.MagicMock(return_value=("a", "b"))
|
282 | 303 | ),
|
283 | 304 | )
|
| 305 | + mock_redis.setex("oauth-state-abc", 300, "http://localhost:3000/gh") |
284 | 306 | url = reverse("github-login")
|
285 |
| - res = client.get(url, {"code": "aaaaaaa"}) |
| 307 | + res = client.get(url, {"code": "aaaaaaa", "state": "abc"}) |
286 | 308 | assert res.status_code == 302
|
287 | 309 | assert "github-token" in res.cookies
|
288 | 310 | assert "github-username" in res.cookies
|
@@ -345,7 +367,8 @@ async def is_student(*args, **kwargs):
|
345 | 367 | ),
|
346 | 368 | )
|
347 | 369 | url = reverse("github-login")
|
348 |
| - res = client.get(url, {"code": "aaaaaaa"}) |
| 370 | + mock_redis.setex("oauth-state-abc", 300, "http://localhost:3000/gh") |
| 371 | + res = client.get(url, {"code": "aaaaaaa", "state": "abc"}) |
349 | 372 | assert res.status_code == 302
|
350 | 373 | assert "github-token" in res.cookies
|
351 | 374 | assert "github-username" in res.cookies
|
|
0 commit comments