authentik.sources.oauth.tests.test_property_mappings
Apple Type tests
1"""Apple Type tests""" 2 3from copy import deepcopy 4 5from django.contrib.auth.models import AnonymousUser 6from django.test import TestCase 7 8from authentik.core.expression.exceptions import PropertyMappingExpressionException 9from authentik.core.tests.utils import RequestFactory 10from authentik.events.models import Event, EventAction 11from authentik.lib.generators import generate_id 12from authentik.sources.oauth.models import OAuthSource, OAuthSourcePropertyMapping 13from authentik.sources.oauth.views.callback import OAuthSourceFlowManager 14 15INFO = { 16 "sub": "83692", 17 "name": "Alice Adams", 18 "email": "alice@example.com", 19 "department": "Engineering", 20 "birthdate": "1975-12-31", 21 "nickname": "foo", 22} 23IDENTIFIER = INFO["sub"] 24 25 26class TestPropertyMappings(TestCase): 27 """OAuth Source tests""" 28 29 def setUp(self): 30 self.source = OAuthSource.objects.create( 31 name="test", 32 slug="test", 33 provider_type="openidconnect", 34 authorization_url="", 35 profile_url="", 36 consumer_key=generate_id(), 37 ) 38 self.request_factory = RequestFactory() 39 40 def test_user_base_properties(self): 41 """Test user base properties""" 42 properties = self.source.get_base_user_properties(info=INFO) 43 self.assertEqual( 44 properties, 45 { 46 "email": "alice@example.com", 47 "groups": [], 48 "name": "Alice Adams", 49 "username": "foo", 50 }, 51 ) 52 53 def test_group_base_properties(self): 54 """Test group base properties""" 55 info = deepcopy(INFO) 56 info["groups"] = ["group 1", "group 2"] 57 properties = self.source.get_base_user_properties(info=info) 58 self.assertEqual(properties["groups"], ["group 1", "group 2"]) 59 for group_id in info["groups"]: 60 properties = self.source.get_base_group_properties(info=info, group_id=group_id) 61 self.assertEqual(properties, {"name": group_id}) 62 63 def test_user_property_mappings(self): 64 self.source.user_property_mappings.add( 65 OAuthSourcePropertyMapping.objects.create( 66 name="test", 67 expression="return {'attributes': {'department': info.get('department')}}", 68 ) 69 ) 70 request = self.request_factory.get("/", user=AnonymousUser()) 71 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": INFO}, {}) 72 self.assertEqual( 73 flow_manager.user_properties, 74 { 75 "attributes": { 76 "department": "Engineering", 77 }, 78 "email": "alice@example.com", 79 "name": "Alice Adams", 80 "username": "foo", 81 "path": self.source.get_user_path(), 82 }, 83 ) 84 85 def test_user_property_mappings_non_dict(self): 86 """Test that a mapping returning a non-dict raises a configuration error""" 87 self.source.user_property_mappings.add( 88 OAuthSourcePropertyMapping.objects.create( 89 name="test", 90 expression="return ['foo']", 91 ) 92 ) 93 request = self.request_factory.get("/", user=AnonymousUser()) 94 with self.assertRaises(PropertyMappingExpressionException): 95 OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": INFO}, {}) 96 self.assertTrue( 97 Event.objects.filter( 98 action=EventAction.CONFIGURATION_ERROR, 99 context__message="Failed to evaluate property mapping: 'test'", 100 ).exists() 101 ) 102 103 def test_grup_property_mappings(self): 104 info = deepcopy(INFO) 105 info["groups"] = ["group 1", "group 2"] 106 self.source.group_property_mappings.add( 107 OAuthSourcePropertyMapping.objects.create( 108 name="test", 109 expression="return {'attributes': {'id': group_id}}", 110 ) 111 ) 112 request = self.request_factory.get("/", user=AnonymousUser()) 113 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": info}, {}) 114 self.assertEqual( 115 flow_manager.groups_properties, 116 { 117 "group 1": { 118 "name": "group 1", 119 "attributes": { 120 "id": "group 1", 121 }, 122 }, 123 "group 2": { 124 "name": "group 2", 125 "attributes": { 126 "id": "group 2", 127 }, 128 }, 129 }, 130 ) 131 132 def test_group_property_mappings_with_object_groups(self): 133 """An object-shaped `groups` entry is skipped instead of aborting the flow. 134 135 Added in #25195 asserting the `TypeError` that #25191 is about; the 136 identifier is still unusable as a key, so the entry is dropped rather 137 than mapped. 138 """ 139 info = deepcopy(INFO) 140 info["groups"] = [ 141 {"id": "group-1", "name": "Admins"}, 142 ] 143 144 request = self.request_factory.get("/", user=AnonymousUser()) 145 146 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": info}, {}) 147 self.assertEqual(flow_manager.groups_properties, {})
INFO =
{'sub': '83692', 'name': 'Alice Adams', 'email': 'alice@example.com', 'department': 'Engineering', 'birthdate': '1975-12-31', 'nickname': 'foo'}
IDENTIFIER =
'83692'
class
TestPropertyMappings(django.test.testcases.TestCase):
27class TestPropertyMappings(TestCase): 28 """OAuth Source tests""" 29 30 def setUp(self): 31 self.source = OAuthSource.objects.create( 32 name="test", 33 slug="test", 34 provider_type="openidconnect", 35 authorization_url="", 36 profile_url="", 37 consumer_key=generate_id(), 38 ) 39 self.request_factory = RequestFactory() 40 41 def test_user_base_properties(self): 42 """Test user base properties""" 43 properties = self.source.get_base_user_properties(info=INFO) 44 self.assertEqual( 45 properties, 46 { 47 "email": "alice@example.com", 48 "groups": [], 49 "name": "Alice Adams", 50 "username": "foo", 51 }, 52 ) 53 54 def test_group_base_properties(self): 55 """Test group base properties""" 56 info = deepcopy(INFO) 57 info["groups"] = ["group 1", "group 2"] 58 properties = self.source.get_base_user_properties(info=info) 59 self.assertEqual(properties["groups"], ["group 1", "group 2"]) 60 for group_id in info["groups"]: 61 properties = self.source.get_base_group_properties(info=info, group_id=group_id) 62 self.assertEqual(properties, {"name": group_id}) 63 64 def test_user_property_mappings(self): 65 self.source.user_property_mappings.add( 66 OAuthSourcePropertyMapping.objects.create( 67 name="test", 68 expression="return {'attributes': {'department': info.get('department')}}", 69 ) 70 ) 71 request = self.request_factory.get("/", user=AnonymousUser()) 72 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": INFO}, {}) 73 self.assertEqual( 74 flow_manager.user_properties, 75 { 76 "attributes": { 77 "department": "Engineering", 78 }, 79 "email": "alice@example.com", 80 "name": "Alice Adams", 81 "username": "foo", 82 "path": self.source.get_user_path(), 83 }, 84 ) 85 86 def test_user_property_mappings_non_dict(self): 87 """Test that a mapping returning a non-dict raises a configuration error""" 88 self.source.user_property_mappings.add( 89 OAuthSourcePropertyMapping.objects.create( 90 name="test", 91 expression="return ['foo']", 92 ) 93 ) 94 request = self.request_factory.get("/", user=AnonymousUser()) 95 with self.assertRaises(PropertyMappingExpressionException): 96 OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": INFO}, {}) 97 self.assertTrue( 98 Event.objects.filter( 99 action=EventAction.CONFIGURATION_ERROR, 100 context__message="Failed to evaluate property mapping: 'test'", 101 ).exists() 102 ) 103 104 def test_grup_property_mappings(self): 105 info = deepcopy(INFO) 106 info["groups"] = ["group 1", "group 2"] 107 self.source.group_property_mappings.add( 108 OAuthSourcePropertyMapping.objects.create( 109 name="test", 110 expression="return {'attributes': {'id': group_id}}", 111 ) 112 ) 113 request = self.request_factory.get("/", user=AnonymousUser()) 114 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": info}, {}) 115 self.assertEqual( 116 flow_manager.groups_properties, 117 { 118 "group 1": { 119 "name": "group 1", 120 "attributes": { 121 "id": "group 1", 122 }, 123 }, 124 "group 2": { 125 "name": "group 2", 126 "attributes": { 127 "id": "group 2", 128 }, 129 }, 130 }, 131 ) 132 133 def test_group_property_mappings_with_object_groups(self): 134 """An object-shaped `groups` entry is skipped instead of aborting the flow. 135 136 Added in #25195 asserting the `TypeError` that #25191 is about; the 137 identifier is still unusable as a key, so the entry is dropped rather 138 than mapped. 139 """ 140 info = deepcopy(INFO) 141 info["groups"] = [ 142 {"id": "group-1", "name": "Admins"}, 143 ] 144 145 request = self.request_factory.get("/", user=AnonymousUser()) 146 147 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": info}, {}) 148 self.assertEqual(flow_manager.groups_properties, {})
OAuth Source tests
def
setUp(self):
30 def setUp(self): 31 self.source = OAuthSource.objects.create( 32 name="test", 33 slug="test", 34 provider_type="openidconnect", 35 authorization_url="", 36 profile_url="", 37 consumer_key=generate_id(), 38 ) 39 self.request_factory = RequestFactory()
Hook method for setting up the test fixture before exercising it.
def
test_user_base_properties(self):
41 def test_user_base_properties(self): 42 """Test user base properties""" 43 properties = self.source.get_base_user_properties(info=INFO) 44 self.assertEqual( 45 properties, 46 { 47 "email": "alice@example.com", 48 "groups": [], 49 "name": "Alice Adams", 50 "username": "foo", 51 }, 52 )
Test user base properties
def
test_group_base_properties(self):
54 def test_group_base_properties(self): 55 """Test group base properties""" 56 info = deepcopy(INFO) 57 info["groups"] = ["group 1", "group 2"] 58 properties = self.source.get_base_user_properties(info=info) 59 self.assertEqual(properties["groups"], ["group 1", "group 2"]) 60 for group_id in info["groups"]: 61 properties = self.source.get_base_group_properties(info=info, group_id=group_id) 62 self.assertEqual(properties, {"name": group_id})
Test group base properties
def
test_user_property_mappings(self):
64 def test_user_property_mappings(self): 65 self.source.user_property_mappings.add( 66 OAuthSourcePropertyMapping.objects.create( 67 name="test", 68 expression="return {'attributes': {'department': info.get('department')}}", 69 ) 70 ) 71 request = self.request_factory.get("/", user=AnonymousUser()) 72 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": INFO}, {}) 73 self.assertEqual( 74 flow_manager.user_properties, 75 { 76 "attributes": { 77 "department": "Engineering", 78 }, 79 "email": "alice@example.com", 80 "name": "Alice Adams", 81 "username": "foo", 82 "path": self.source.get_user_path(), 83 }, 84 )
def
test_user_property_mappings_non_dict(self):
86 def test_user_property_mappings_non_dict(self): 87 """Test that a mapping returning a non-dict raises a configuration error""" 88 self.source.user_property_mappings.add( 89 OAuthSourcePropertyMapping.objects.create( 90 name="test", 91 expression="return ['foo']", 92 ) 93 ) 94 request = self.request_factory.get("/", user=AnonymousUser()) 95 with self.assertRaises(PropertyMappingExpressionException): 96 OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": INFO}, {}) 97 self.assertTrue( 98 Event.objects.filter( 99 action=EventAction.CONFIGURATION_ERROR, 100 context__message="Failed to evaluate property mapping: 'test'", 101 ).exists() 102 )
Test that a mapping returning a non-dict raises a configuration error
def
test_grup_property_mappings(self):
104 def test_grup_property_mappings(self): 105 info = deepcopy(INFO) 106 info["groups"] = ["group 1", "group 2"] 107 self.source.group_property_mappings.add( 108 OAuthSourcePropertyMapping.objects.create( 109 name="test", 110 expression="return {'attributes': {'id': group_id}}", 111 ) 112 ) 113 request = self.request_factory.get("/", user=AnonymousUser()) 114 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": info}, {}) 115 self.assertEqual( 116 flow_manager.groups_properties, 117 { 118 "group 1": { 119 "name": "group 1", 120 "attributes": { 121 "id": "group 1", 122 }, 123 }, 124 "group 2": { 125 "name": "group 2", 126 "attributes": { 127 "id": "group 2", 128 }, 129 }, 130 }, 131 )
def
test_group_property_mappings_with_object_groups(self):
133 def test_group_property_mappings_with_object_groups(self): 134 """An object-shaped `groups` entry is skipped instead of aborting the flow. 135 136 Added in #25195 asserting the `TypeError` that #25191 is about; the 137 identifier is still unusable as a key, so the entry is dropped rather 138 than mapped. 139 """ 140 info = deepcopy(INFO) 141 info["groups"] = [ 142 {"id": "group-1", "name": "Admins"}, 143 ] 144 145 request = self.request_factory.get("/", user=AnonymousUser()) 146 147 flow_manager = OAuthSourceFlowManager(self.source, request, IDENTIFIER, {"info": info}, {}) 148 self.assertEqual(flow_manager.groups_properties, {})
An object-shaped groups entry is skipped instead of aborting the flow.
Added in #25195 asserting the TypeError that #25191 is about; the
identifier is still unusable as a key, so the entry is dropped rather
than mapped.