Skip to content

Commit 55f9b68

Browse files
authored
Enable OIDC tests
This also converts the existing tests into pytest tests
1 parent 3a1f6f2 commit 55f9b68

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ module = [
6767
"src.MCPClient.lib.clientScripts.validate_file",
6868
"tests.archivematicaCommon.test_execute_functions",
6969
"tests.dashboard.fpr.test_views",
70+
"tests.dashboard.test_oidc",
7071
"tests.MCPClient.conftest",
7172
"tests.MCPClient.test_characterize_file",
7273
"tests.MCPClient.test_has_packages",

tests/dashboard/test_oidc.py

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,55 @@
11
import pytest
2+
import pytest_django
23
from components.accounts.backends import CustomOIDCBackend
3-
from django.conf import settings
4-
from django.test import TestCase
5-
6-
7-
@pytest.mark.skipif(
8-
not settings.OIDC_AUTHENTICATION, reason="tests will only pass if OIDC is enabled"
9-
)
10-
class TestOIDC(TestCase):
11-
def test_create_user(self):
12-
backend = CustomOIDCBackend()
13-
user = backend.create_user(
14-
{"email": "[email protected]", "first_name": "Test", "last_name": "User"}
15-
)
16-
17-
user.refresh_from_db()
18-
assert user.first_name == "Test"
19-
assert user.last_name == "User"
20-
assert user.email == "[email protected]"
21-
assert user.username == "[email protected]"
22-
assert user.api_key
23-
24-
def test_get_userinfo(self):
25-
# Encoded at https://www.jsonwebtoken.io/
26-
# {"email": "[email protected]"}
27-
id_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InRlc3RAZXhhbXBsZS5jb20iLCJqdGkiOiI1M2QyMzUzMy04NDk0LTQyZWQtYTJiZC03Mzc2MjNmMjUzZjciLCJpYXQiOjE1NzMwMzE4NDQsImV4cCI6MTU3MzAzNTQ0NH0.m3nHgvj_DyVJMcW5eyYuUss1Y0PNzJV2O3bX0b_DCmI"
28-
# {"given_name": "Test", "family_name": "User"}
29-
access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJnaXZlbl9uYW1lIjoiVGVzdCIsImZhbWlseV9uYW1lIjoiVXNlciIsImp0aSI6ImRhZjIwNTNiLWE4MTgtNDE1Yy1hM2Y1LTkxYWVhMTMxYjljZCIsImlhdCI6MTU3MzAzMTk3OSwiZXhwIjoxNTczMDM1NTc5fQ.cGcmt7d9IuKndvrqPpAH3Dvb3KyCOMqixUWgS7sg8r4"
30-
31-
backend = CustomOIDCBackend()
32-
info = backend.get_userinfo(
33-
access_token=access_token, id_token=id_token, verified_id=None
34-
)
35-
assert info["email"] == "[email protected]"
36-
assert info["first_name"] == "Test"
37-
assert info["last_name"] == "User"
4+
5+
6+
@pytest.fixture
7+
def settings(
8+
settings: pytest_django.fixtures.SettingsWrapper,
9+
) -> pytest_django.fixtures.SettingsWrapper:
10+
settings.OIDC_OP_TOKEN_ENDPOINT = "https://example.com/token"
11+
settings.OIDC_OP_USER_ENDPOINT = "https://example.com/user"
12+
settings.OIDC_RP_CLIENT_ID = "rp_client_id"
13+
settings.OIDC_RP_CLIENT_SECRET = "rp_client_secret"
14+
settings.OIDC_ACCESS_ATTRIBUTE_MAP = {
15+
"given_name": "first_name",
16+
"family_name": "last_name",
17+
}
18+
settings.OIDC_ID_ATTRIBUTE_MAP = {"email": "email"}
19+
settings.OIDC_USERNAME_ALGO = lambda email: email
20+
21+
return settings
22+
23+
24+
@pytest.mark.django_db
25+
def test_create_user(settings: pytest_django.fixtures.SettingsWrapper) -> None:
26+
backend = CustomOIDCBackend()
27+
28+
user = backend.create_user(
29+
{"email": "[email protected]", "first_name": "Test", "last_name": "User"}
30+
)
31+
32+
user.refresh_from_db()
33+
assert user.first_name == "Test"
34+
assert user.last_name == "User"
35+
assert user.email == "[email protected]"
36+
assert user.username == "[email protected]"
37+
assert user.api_key
38+
39+
40+
@pytest.mark.django_db
41+
def test_get_userinfo(settings: pytest_django.fixtures.SettingsWrapper) -> None:
42+
# Encoded at https://www.jsonwebtoken.io/
43+
# {"email": "[email protected]"}
44+
id_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InRlc3RAZXhhbXBsZS5jb20iLCJqdGkiOiI1M2QyMzUzMy04NDk0LTQyZWQtYTJiZC03Mzc2MjNmMjUzZjciLCJpYXQiOjE1NzMwMzE4NDQsImV4cCI6MTU3MzAzNTQ0NH0.m3nHgvj_DyVJMcW5eyYuUss1Y0PNzJV2O3bX0b_DCmI"
45+
# {"given_name": "Test", "family_name": "User"}
46+
access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJnaXZlbl9uYW1lIjoiVGVzdCIsImZhbWlseV9uYW1lIjoiVXNlciIsImp0aSI6ImRhZjIwNTNiLWE4MTgtNDE1Yy1hM2Y1LTkxYWVhMTMxYjljZCIsImlhdCI6MTU3MzAzMTk3OSwiZXhwIjoxNTczMDM1NTc5fQ.cGcmt7d9IuKndvrqPpAH3Dvb3KyCOMqixUWgS7sg8r4"
47+
backend = CustomOIDCBackend()
48+
49+
info = backend.get_userinfo(
50+
access_token=access_token, id_token=id_token, verified_id=None
51+
)
52+
53+
assert info["email"] == "[email protected]"
54+
assert info["first_name"] == "Test"
55+
assert info["last_name"] == "User"

0 commit comments

Comments
 (0)