Skip to content

Commit

Permalink
tests: verify deprecation message for AzureAdOAuthenticator.user_grou…
Browse files Browse the repository at this point in the history
…ps_claim
  • Loading branch information
consideRatio committed Sep 3, 2024
1 parent 6618811 commit c16bb8c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion oauthenticator/azuread.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _auth_state_groups_key_default(self):
key = f"{self.user_auth_state_key}.{self.user_groups_claim}"
cls = self.__class__.__name__
self.log.warning(
f"{cls}.user_groups_claim is deprecated in OAuthenticator 17. Use {cls}.auth_state_groups_key={key!r}"
f"{cls}.user_groups_claim is deprecated in OAuthenticator 17. Use {cls}.auth_state_groups_key = {key!r}"
)
return key

Expand Down
47 changes: 46 additions & 1 deletion oauthenticator/tests/test_azuread.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""test azure ad"""

import json
import logging
import os
import re
import time
Expand All @@ -9,7 +10,7 @@

import jwt
import pytest
from pytest import fixture, mark
from pytest import fixture, mark, raises
from traitlets.config import Config

from ..azuread import AzureAdOAuthenticator
Expand Down Expand Up @@ -236,3 +237,47 @@ async def test_tenant_id_from_env():
with mock.patch.dict(os.environ, {"AAD_TENANT_ID": tenant_id}):
aad = AzureAdOAuthenticator()
assert aad.tenant_id == tenant_id


@mark.parametrize(
"test_variation_id,class_config,expect_config,expect_loglevel,expect_message",
[
(
"user_groups_claim",
{"user_groups_claim": "groups", "manage_groups": True},
{"auth_state_groups_key": "user.groups"},
logging.WARNING,
"AzureAdOAuthenticator.user_groups_claim is deprecated in OAuthenticator 17. Use AzureAdOAuthenticator.auth_state_groups_key = 'user.groups'",
),
],
)
async def test_deprecated_config(
caplog,
test_variation_id,
class_config,
expect_config,
expect_loglevel,
expect_message,
):
"""
Tests that a warning is emitted when using a deprecated config and that
configuring the old config ends up configuring the new config.
"""
print(f"Running test variation id {test_variation_id}")
c = Config()
c.AzureAdOAuthenticator = Config(class_config)

test_logger = logging.getLogger('testlog')
if expect_loglevel == logging.ERROR:
with raises(ValueError, match=expect_message):
AzureAdOAuthenticator(config=c, log=test_logger)
else:
authenticator = AzureAdOAuthenticator(config=c, log=test_logger)
for key, value in expect_config.items():
assert getattr(authenticator, key) == value

captured_log_tuples = caplog.record_tuples
print(captured_log_tuples)

expected_log_tuple = (test_logger.name, expect_loglevel, expect_message)
assert expected_log_tuple in captured_log_tuples

0 comments on commit c16bb8c

Please sign in to comment.