Skip to content

Commit b170d72

Browse files
committed
Update tests to check with and without oidc_claim_scope.
1 parent 8db6ab7 commit b170d72

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

tests/conftest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,43 @@ def oidc_tokens(oauth2_settings, application, test_user, client):
158158
id_token=token_data["id_token"],
159159
oauth2_settings=oauth2_settings,
160160
)
161+
162+
163+
@pytest.fixture
164+
def oidc_email_scope_tokens(oauth2_settings, application, test_user, client):
165+
oauth2_settings.update(presets.OIDC_SETTINGS_EMAIL_SCOPE)
166+
client.force_login(test_user)
167+
auth_rsp = client.post(
168+
reverse("oauth2_provider:authorize"),
169+
data={
170+
"client_id": application.client_id,
171+
"state": "random_state_string",
172+
"scope": "openid email",
173+
"redirect_uri": "http://example.org",
174+
"response_type": "code",
175+
"allow": True,
176+
},
177+
)
178+
assert auth_rsp.status_code == 302
179+
code = parse_qs(urlparse(auth_rsp["Location"]).query)["code"]
180+
client.logout()
181+
token_rsp = client.post(
182+
reverse("oauth2_provider:token"),
183+
data={
184+
"grant_type": "authorization_code",
185+
"code": code,
186+
"redirect_uri": "http://example.org",
187+
"client_id": application.client_id,
188+
"client_secret": CLEARTEXT_SECRET,
189+
"scope": "openid email",
190+
},
191+
)
192+
assert token_rsp.status_code == 200
193+
token_data = token_rsp.json()
194+
return SimpleNamespace(
195+
user=test_user,
196+
application=application,
197+
access_token=token_data["access_token"],
198+
id_token=token_data["id_token"],
199+
oauth2_settings=oauth2_settings,
200+
)

tests/presets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
}
2323
OIDC_SETTINGS_RO = deepcopy(OIDC_SETTINGS_RW)
2424
OIDC_SETTINGS_RO["DEFAULT_SCOPES"] = ["read"]
25+
OIDC_SETTINGS_EMAIL_SCOPE = deepcopy(OIDC_SETTINGS_RW)
26+
OIDC_SETTINGS_EMAIL_SCOPE["SCOPES"].update({"email": "return email address"})
2527
OIDC_SETTINGS_HS256_ONLY = deepcopy(OIDC_SETTINGS_RW)
2628
del OIDC_SETTINGS_HS256_ONLY["OIDC_RSA_PRIVATE_KEY"]
2729
REST_FRAMEWORK_SCOPES = {

tests/test_oidc_views.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def claim_user_email(request):
160160
@pytest.mark.django_db
161161
def test_userinfo_endpoint_custom_claims_callable(oidc_tokens, client, oauth2_settings):
162162
class CustomValidator(OAuth2Validator):
163+
oidc_claim_scope = None
164+
163165
def get_additional_claims(self):
164166
return {
165167
"username": claim_user_email,
@@ -183,9 +185,38 @@ def get_additional_claims(self):
183185
assert data["email"] == EXAMPLE_EMAIL
184186

185187

188+
@pytest.mark.django_db
189+
def test_userinfo_endpoint_custom_claims_email_scope_callable(
190+
oidc_email_scope_tokens, client, oauth2_settings
191+
):
192+
class CustomValidator(OAuth2Validator):
193+
def get_additional_claims(self):
194+
return {
195+
"username": claim_user_email,
196+
"email": claim_user_email,
197+
}
198+
199+
oidc_email_scope_tokens.oauth2_settings.OAUTH2_VALIDATOR_CLASS = CustomValidator
200+
auth_header = "Bearer %s" % oidc_email_scope_tokens.access_token
201+
rsp = client.get(
202+
reverse("oauth2_provider:user-info"),
203+
HTTP_AUTHORIZATION=auth_header,
204+
)
205+
data = rsp.json()
206+
assert "sub" in data
207+
assert data["sub"] == str(oidc_email_scope_tokens.user.pk)
208+
209+
assert "username" not in data
210+
211+
assert "email" in data
212+
assert data["email"] == EXAMPLE_EMAIL
213+
214+
186215
@pytest.mark.django_db
187216
def test_userinfo_endpoint_custom_claims_plain(oidc_tokens, client, oauth2_settings):
188217
class CustomValidator(OAuth2Validator):
218+
oidc_claim_scope = None
219+
189220
def get_additional_claims(self, request):
190221
return {
191222
"username": EXAMPLE_EMAIL,
@@ -207,3 +238,28 @@ def get_additional_claims(self, request):
207238

208239
assert "email" in data
209240
assert data["email"] == EXAMPLE_EMAIL
241+
242+
243+
@pytest.mark.django_db
244+
def test_userinfo_endpoint_custom_claims_email_scopeplain(oidc_email_scope_tokens, client, oauth2_settings):
245+
class CustomValidator(OAuth2Validator):
246+
def get_additional_claims(self, request):
247+
return {
248+
"username": EXAMPLE_EMAIL,
249+
"email": EXAMPLE_EMAIL,
250+
}
251+
252+
oidc_email_scope_tokens.oauth2_settings.OAUTH2_VALIDATOR_CLASS = CustomValidator
253+
auth_header = "Bearer %s" % oidc_email_scope_tokens.access_token
254+
rsp = client.get(
255+
reverse("oauth2_provider:user-info"),
256+
HTTP_AUTHORIZATION=auth_header,
257+
)
258+
data = rsp.json()
259+
assert "sub" in data
260+
assert data["sub"] == str(oidc_email_scope_tokens.user.pk)
261+
262+
assert "username" not in data
263+
264+
assert "email" in data
265+
assert data["email"] == EXAMPLE_EMAIL

0 commit comments

Comments
 (0)