Skip to content

Commit

Permalink
Fix timing issues in test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jobec committed Mar 2, 2019
1 parent 43faae0 commit 24f5f77
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"email": "email"},
"BOOLEAN_CLAIM_MAPPING": {"is_staff": "user_is_staff",
"is_superuser": "user_is_superuser"},
"LOGIN_EXEMPT_URLS": ["^context_processor/$"]
"CONFIG_RELOAD_INTERVAL": 0, # Always reload settings
}

LOGIN_URL = "django_auth_adfs:login"
Expand Down
7 changes: 3 additions & 4 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_group_claim(self):
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(len(user.groups.all()), 0)

@mock_adfs("2016")
@mock_adfs("2016", empty_keys=True)
def test_empty_keys(self):
backend = AdfsAuthCodeBackend()
with patch("django_auth_adfs.config.provider_config.signing_keys", []):
Expand Down Expand Up @@ -152,7 +152,6 @@ def test_oauth_redir_2012(self):
redir = urlparse(response["Location"])
qs = parse_qs(redir.query)
sq_expected = {
'scope': ['openid'],
'client_id': ['your-configured-client-id'],
'state': ['L3Rlc3Qv'],
'response_type': ['code'],
Expand All @@ -170,7 +169,7 @@ def test_oauth_redir_2016(self):
self.assertEqual(response.status_code, 302)
redir = urlparse(response["Location"])
qs = parse_qs(redir.query)
sq_expected = {
qs_expected = {
'scope': ['openid'],
'client_id': ['your-configured-client-id'],
'state': ['L3Rlc3Qv'],
Expand All @@ -181,7 +180,7 @@ def test_oauth_redir_2016(self):
self.assertEqual(redir.scheme, 'https')
self.assertEqual(redir.hostname, 'adfs.example.com')
self.assertEqual(redir.path.rstrip("/"), '/adfs/oauth2/authorize')
self.assertEqual(qs, sq_expected)
self.assertEqual(qs, qs_expected)

@mock_adfs("azure")
def test_oauth_redir_azure(self):
Expand Down
56 changes: 30 additions & 26 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import time
from datetime import datetime, tzinfo, timedelta
from functools import partial

import jwt
import responses
Expand Down Expand Up @@ -108,29 +109,32 @@ def do_build_access_token(request, issuer):
return 200, [], json.dumps(response)


def build_openid_keys(request):
keys = {
"keys": [
{
"kty": "RSA",
"use": "sig",
"kid": "dummythumbprint",
"x5t": "dummythumbprint",
"n": "somebase64encodedmodulus",
"e": "somebase64encodedexponent",
"x5c": [base64.b64encode(signing_cert_a).decode(), ]
},
{
"kty": "RSA",
"use": "sig",
"kid": "dummythumbprint",
"x5t": "dummythumbprint",
"n": "somebase64encodedmodulus",
"e": "somebase64encodedexponent",
"x5c": [base64.b64encode(signing_cert_b).decode(), ]
},
]
}
def build_openid_keys(request, empty_keys=False):
if empty_keys:
keys = {"keys": []}
else:
keys = {
"keys": [
{
"kty": "RSA",
"use": "sig",
"kid": "dummythumbprint",
"x5t": "dummythumbprint",
"n": "somebase64encodedmodulus",
"e": "somebase64encodedexponent",
"x5c": [base64.b64encode(signing_cert_a).decode(), ]
},
{
"kty": "RSA",
"use": "sig",
"kid": "dummythumbprint",
"x5t": "dummythumbprint",
"n": "somebase64encodedmodulus",
"e": "somebase64encodedexponent",
"x5c": [base64.b64encode(signing_cert_b).decode(), ]
},
]
}
return 200, [], json.dumps(keys)


Expand All @@ -142,7 +146,7 @@ def build_adfs_meta(request):
return 200, [], data


def mock_adfs(adfs_version):
def mock_adfs(adfs_version, empty_keys=False):
if adfs_version not in ["2012", "2016", "azure"]:
raise NotImplementedError("This version of ADFS is not implemented")

Expand All @@ -167,7 +171,7 @@ def wrapper(*original_args, **original_kwargs):
)
rsps.add_callback(
rsps.GET, openid_keys,
callback=build_openid_keys,
callback=partial(build_openid_keys, empty_keys=empty_keys),
content_type='application/json',
)
elif adfs_version == "azure":
Expand All @@ -177,7 +181,7 @@ def wrapper(*original_args, **original_kwargs):
)
rsps.add_callback(
rsps.GET, openid_keys,
callback=build_openid_keys,
callback=partial(build_openid_keys, empty_keys=empty_keys),
content_type='application/json',
)
else:
Expand Down

0 comments on commit 24f5f77

Please sign in to comment.