示例#1
0
    def test__build_response(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford = get_user_adapter()(ford, self.factory.get('/fake/request'))
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
        )
        abernathy = get_user_adapter()(abernathy,
                                       self.factory.get('/fake/request'))

        qs = get_user_model().objects.all()
        mixin = views.FilterMixin()
        mixin.scim_adapter = get_user_adapter()
        resp = mixin._build_response(self.factory.get('/fake/request'), qs, 1,
                                     5)

        result = json.loads(resp.content.decode())
        expected = {
            "schemas": [constants.SchemaURI.LIST_RESPONSE],
            "totalResults": 2,
            "itemsPerPage": 5,
            "startIndex": 1,
            'Resources': [
                ford.to_dict(),
                abernathy.to_dict(),
            ],
        }
        self.assertEqual(expected, result)
示例#2
0
    def test_put(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )

        url = reverse('scim:users', kwargs={'uuid': ford.id})
        data = get_user_adapter()(ford, self.request).to_dict()
        data['userName'] = '******'
        data['name'] = {'givenName': 'Bobby'}
        data['emails'] = [{'value': '*****@*****.**', 'primary': True}]
        body = json.dumps(data)
        resp = self.client.put(url,
                               body,
                               content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 200, resp.content.decode())

        # test object
        ford.refresh_from_db()
        self.assertEqual(ford.first_name, 'Bobby')
        self.assertEqual(ford.last_name, '')
        self.assertEqual(ford.username, 'updatedrford')
        self.assertEqual(ford.email, '*****@*****.**')

        # test response
        result = json.loads(resp.content.decode())
        ford = get_user_adapter()(ford, self.request)
        self.assertEqual(result, ford.to_dict())
示例#3
0
    def test__build_response(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford = get_user_adapter()(ford)
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
        )
        abernathy = get_user_adapter()(abernathy)

        qs = get_user_model().objects.all()
        mixin = views.FilterMixin()
        mixin.scim_adapter = get_user_adapter()
        resp = mixin._build_response(None, qs, 1, 5)

        result = json.loads(resp.content.decode())
        expected = {
            "schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
            "totalResults": 2,
            "itemsPerPage": 5,
            "startIndex": 1,
            'Resources': [
                ford.to_dict(),
                abernathy.to_dict(),
            ],
        }
        self.assertEqual(expected, result)
示例#4
0
    def test_post(self):
        url = reverse('scim:users')
        data = {
            'schemas': ['urn:ietf:params:scim:schemas:core:2.0:User'],
            'userName': '******',
            'name': {
                'givenName': 'Elsie',
                'familyName': 'Hughes',
            },
            'password': '******',
            'emails': [{
                'value': '*****@*****.**',
                'primary': True
            }],
        }
        body = json.dumps(data)
        resp = self.client.post(url,
                                body,
                                content_type='application/scim+json')
        self.assertEqual(resp.status_code, 201, resp.content.decode())

        # test object
        elsie = get_user_model().objects.get(username='******')
        self.assertEqual(elsie.first_name, 'Elsie')
        self.assertEqual(elsie.last_name, 'Hughes')
        self.assertEqual(elsie.email, '*****@*****.**')

        # test response
        elsie = get_user_adapter()(elsie)
        result = json.loads(resp.content.decode())
        self.assertEqual(result, elsie.to_dict())
        self.assertEqual(resp['Location'], elsie.location)
示例#5
0
    def test_handle_add_complex_1(self):
        operations = [{
            "op": "Add",
            "path":
            "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department",
            "value": "Design"
        }]

        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = ((
            'department', None,
            'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'),
                    'Design', operations[0])

        with unittest.mock.patch(
                'django_scim.adapters.SCIMUser.handle_add') as handler:
            ford.handle_operations(operations)
            handler.assert_called_with(*expected)
示例#6
0
    def test_post(self):
        url = reverse('scim:users')
        data = {
            'schemas': [constants.SchemaURI.USER],
            'userName': '******',
            'name': {
                'givenName': 'Elsie',
                'familyName': 'Hughes',
            },
            'password': '******',
            'emails': [{
                'value': '*****@*****.**',
                'primary': True
            }],
        }
        body = json.dumps(data)
        resp = self.client.post(url,
                                body,
                                content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 201, resp.content.decode())

        # test object
        elsie = get_user_model().objects.get(username='******')
        self.assertEqual(elsie.first_name, 'Elsie')
        self.assertEqual(elsie.last_name, 'Hughes')
        self.assertEqual(elsie.email, '*****@*****.**')

        # test response
        elsie = get_user_adapter()(elsie, self.request)
        result = json.loads(resp.content.decode())
        self.assertEqual(result, elsie.to_dict())
        self.assertEqual(resp['Location'], elsie.location)
示例#7
0
    def test_handle_add_complex_2(self):
        operations = [
            {
                "op": "Add",
                "path": "addresses[type eq \"work\"].country",
                "value": "Sector 9"
            }
        ]

        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = (
            ('addresses', 'country', None),
            'Sector 9',
            operations[0]
        )

        with unittest.mock.patch('django_scim.adapters.SCIMUser.handle_add') as handler:
            ford.handle_operations(operations)
            path_obj, value, op_dict = handler.call_args[0]
            self.assertEqual(path_obj.filter, 'addresses[type eq \"work\"].country eq ""')
            self.assertEqual(
                list(path_obj),
                [('addresses', 'type', None), ('addresses', 'country', None)]
            )
            self.assertEqual(value, 'Sector 9')
            self.assertEqual(op_dict, operations[0])
示例#8
0
    def test_get_all_users_with_extra_model_exclude_kwargs(self, func):
        """
        Test GET /Users with extra model exclude filters.
        """
        # define kwargs returned by a call to get_extra_exclude_kwargs
        func.return_value = {'is_active': True}

        get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
            is_active=False,
        )
        abernathy = get_user_adapter()(abernathy, self.request)

        url = reverse('scim:users')
        resp = self.client.get(url, content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 200, resp.content.decode())
        result = json.loads(resp.content.decode())

        expected = {
            "schemas": [constants.SchemaURI.LIST_RESPONSE],
            "totalResults": 1,
            "itemsPerPage": 50,
            "startIndex": 1,
            'Resources': [
                abernathy.to_dict(),
            ],
        }
        self.assertEqual(expected, result)
示例#9
0
    def test_handle_add_simple(self):
        operations = [
            {
                "op": "Add",
                "path": "externalId",
                "value": "Robert.Ford"
            },
        ]

        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = (
            ('externalId', None, None),
            'Robert.Ford',
            operations[0]
        )

        with unittest.mock.patch('django_scim.adapters.SCIMUser.handle_add') as handler:
            ford.handle_operations(operations)
            call_args = handler.call_args[0]
            self.assertIsInstance(call_args[0], AttrPath)
            self.assertEqual(call_args[0].first_path, expected[0])
            self.assertEqual(call_args[1], expected[1])
            self.assertEqual(call_args[2], expected[2])
示例#10
0
    def test_handle_add_complex_3(self):
        operations = [{
            "op": "Add",
            "path": 'members[value eq "6784"]',
            "value": "[]"
        }]

        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = (('addresses', 'country', None), 'Sector 9', operations[0])

        with unittest.mock.patch(
                'django_scim.adapters.SCIMUser.handle_add') as handler:
            ford.handle_operations(operations)
            path_obj, value, op_dict = handler.call_args[0]
            self.assertEqual(path_obj.path, 'members[value eq "6784"]')
            self.assertEqual(path_obj.attr_paths, [('members', 'value', None),
                                                   ('members', None, None)])
            self.assertEqual(value, '[]')
            self.assertEqual(op_dict, operations[0])
示例#11
0
    def test_search_for_user_with_username_filter_with_value(self):
        """
        Test POST /Users/.search/?filter=userName eq "rford"
        """
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford = get_user_adapter()(ford, self.request)

        url = reverse('scim:users-search')
        body = json.dumps({
            'schemas': [constants.SchemaURI.SERACH_REQUEST],
            'filter': 'userName eq "rford"',
        })
        resp = self.client.post(url,
                                body,
                                content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 200, resp.content.decode())
        location = urljoin(get_base_scim_location_getter()(), '/scim/v2/')
        location = urljoin(location, 'Users/.search')
        self.assertEqual(resp['Location'], location)

        result = json.loads(resp.content.decode())
        expected = {
            "schemas": [constants.SchemaURI.LIST_RESPONSE],
            "totalResults": 1,
            "itemsPerPage": 50,
            "startIndex": 1,
            "Resources": [
                ford.to_dict(),
            ]
        }
        self.assertEqual(expected, result)
示例#12
0
 def test_get_single(self):
     url = reverse('scim:resource-types', kwargs={'uuid': 'User'})
     resp = self.client.get(url)
     self.assertEqual(resp.status_code, 200, resp.content.decode())
     expected = get_user_adapter().resource_type_dict()
     result = json.loads(resp.content.decode())
     self.assertEqual(expected, result)
示例#13
0
    def test_display_name(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford = get_user_adapter()(ford)

        self.assertEqual(ford.display_name, 'Robert Ford')
示例#14
0
    def test_emails(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford)

        self.assertEqual(ford.emails, [{
            'primary': True,
            'value': '*****@*****.**'
        }])
示例#15
0
 def test_get_all(self):
     url = reverse('scim:resource-types')
     resp = self.client.get(url)
     self.assertEqual(resp.status_code, 200, resp.content.decode())
     user_type = get_user_adapter().resource_type_dict()
     group_type = get_group_adapter().resource_type_dict()
     key = lambda o: o.get('id')
     expected = {
         'schemas': ['urn:ietf:params:scim:api:messages:2.0:ListResponse'],
         'Resources': list(sorted((user_type, group_type), key=key)),
     }
     result = json.loads(resp.content.decode())
     self.assertEqual(expected, result)
示例#16
0
 def test_get_all(self):
     url = reverse('scim:resource-types')
     resp = self.client.get(url)
     self.assertEqual(resp.status_code, 200, resp.content.decode())
     user_type = get_user_adapter().resource_type_dict()
     group_type = get_group_adapter().resource_type_dict()
     key = lambda o: o.get('id')  # noqa: 731
     expected = {
         'schemas': [constants.SchemaURI.LIST_RESPONSE],
         'Resources': list(sorted((user_type, group_type), key=key)),
     }
     result = json.loads(resp.content.decode())
     self.assertEqual(expected, result)
示例#17
0
    def test_get_all_users(self):
        """
        Test GET /Users
        """
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford = get_user_adapter()(ford)
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
        )
        abernathy = get_user_adapter()(abernathy)

        url = reverse('scim:users')
        resp = self.client.get(url, content_type='application/scim+json')
        self.assertEqual(resp.status_code, 200, resp.content.decode())

        result = json.loads(resp.content.decode())
        expected = {
            "schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
            "totalResults":
            3,
            "itemsPerPage":
            50,
            "startIndex":
            1,
            'Resources': [
                get_user_adapter()(self.user).to_dict(),
                ford.to_dict(),
                abernathy.to_dict(),
            ],
        }
        self.assertEqual(expected, result)
示例#18
0
    def test_get_all_users(self):
        """
        Test GET /Users
        """
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford = get_user_adapter()(ford, self.request)
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
        )
        abernathy = get_user_adapter()(abernathy, self.request)

        url = reverse('scim:users')
        resp = self.client.get(url, content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 200, resp.content.decode())

        result = json.loads(resp.content.decode())
        expected = {
            "schemas": [constants.SchemaURI.LIST_RESPONSE],
            "totalResults":
            3,
            "itemsPerPage":
            50,
            "startIndex":
            1,
            'Resources': [
                get_user_adapter()(self.user, self.request).to_dict(),
                ford.to_dict(),
                abernathy.to_dict(),
            ],
        }
        self.assertEqual(expected, result)
示例#19
0
    def test_to_dict(self):
        behavior = get_group_model().objects.create(name='Behavior Group', )
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
            scim_external_id='Anthony.Hopkins',
        )
        ford.scim_groups.add(behavior)

        expected = {
            'schemas': [constants.SchemaURI.USER],
            'userName':
            '******',
            'meta': {
                'resourceType': 'User',
                'lastModified': ford.date_joined.isoformat(),
                'location': u'https://localhost/scim/v2/Users/1',
                'created': ford.date_joined.isoformat(),
            },
            'displayName':
            u'Robert Ford',
            'name': {
                'givenName': 'Robert',
                'familyName': 'Ford',
                'formatted': 'Robert Ford',
            },
            'groups': [{
                'display': u'Behavior Group',
                'value': '1',
                '$ref': u'https://localhost/scim/v2/Groups/1'
            }],
            'active':
            True,
            'id':
            '1',
            'emails': [{
                'primary': True,
                'value': '*****@*****.**'
            }],
            'externalId':
            'Anthony.Hopkins',
        }

        ford = get_user_adapter()(ford, self.request)
        self.assertEqual(ford.to_dict(), expected)
示例#20
0
    def test_groups(self):
        behavior = get_group_model().objects.create(name='Behavior Group', )
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford.groups.add(behavior)
        ford = get_user_adapter()(ford)

        expected = [{
            'display': u'Behavior Group',
            'value': '1',
            '$ref': u'https://localhost/scim/v2/Groups/1',
        }]

        self.assertEqual(ford.groups, expected)
示例#21
0
    def test_patch_remove(self):
        behavior = get_group_model().objects.create(name='Behavior Group', )
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford.groups.add(behavior)
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
        )
        abernathy.groups.add(behavior)
        scim_abernathy = get_user_adapter()(abernathy)

        data = {
            'schemas': ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
            'Operations': [{
                'op':
                'remove',
                'path':
                'members',
                'value': [
                    {
                        'value': ford.id
                    },
                    {
                        'value': abernathy.id
                    },
                ]
            }]
        }
        data = json.dumps(data)

        url = reverse('scim:groups', kwargs={'uuid': behavior.id})
        resp = self.client.patch(url,
                                 data=data,
                                 content_type='application/scim+json')
        self.assertEqual(resp.status_code, 200, resp.content.decode())

        result = json.loads(resp.content.decode())
        expected = get_group_adapter()(behavior).to_dict()
        self.assertEqual(expected, result)

        self.assertEqual(behavior.user_set.count(), 0)
示例#22
0
    def test_get_user_by_id(self):
        """
        Test GET /Users/{id}
        """
        # create user
        ford = get_user_model().objects.create(first_name='Robert',
                                               last_name='Ford')
        ford = get_user_adapter()(ford, self.request)

        url = reverse('scim:users', kwargs={'uuid': ford.id})
        resp = self.client.get(url, content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 200, resp.content.decode())
        self.assertEqual(resp['Location'], ford.location)

        result = json.loads(resp.content.decode())
        expected = ford.to_dict()
        self.assertEqual(expected, result)
示例#23
0
    def test_meta(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )

        expected = {
            'resourceType': 'User',
            'lastModified': ford.date_joined.isoformat(),
            'location': u'https://localhost/scim/v2/Users/1',
            'created': ford.date_joined.isoformat(),
        }

        ford = get_user_adapter()(ford)
        self.assertEqual(ford.meta, expected)
示例#24
0
    def test_to_dict(self):
        behavior = get_group_model().objects.create(name='Behavior Group', )
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford.groups.add(behavior)

        expected = {
            'schemas': ['urn:ietf:params:scim:schemas:core:2.0:User'],
            'userName':
            '******',
            'meta': {
                'resourceType': 'User',
                'lastModified': ford.date_joined.isoformat(),
                'location': u'https://localhost/scim/v2/Users/1',
                'created': ford.date_joined.isoformat(),
            },
            'displayName':
            u'Robert Ford',
            'name': {
                'givenName': 'Robert',
                'familyName': 'Ford',
            },
            'groups': [{
                'display': u'Behavior Group',
                'value': '1',
                '$ref': u'https://localhost/scim/v2/Groups/1'
            }],
            'active':
            True,
            'id':
            '1',
            'emails': [{
                'primary': True,
                'value': '*****@*****.**'
            }],
        }

        ford = get_user_adapter()(ford)
        self.assertEqual(ford.to_dict(), expected)
示例#25
0
    def test_put_invalid_active_value(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )

        url = reverse('scim:users', kwargs={'uuid': ford.id})
        data = get_user_adapter()(ford, self.request).to_dict()
        data['active'] = 'False'
        body = json.dumps(data)
        resp = self.client.put(url,
                               body,
                               content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 400, resp.content.decode())
        result = json.loads(resp.content.decode())
        self.assertEqual(result['detail'],
                         '"active" should be of type "bool". Got type "str"')
示例#26
0
    def test_patch_add(self):
        behavior = get_group_model().objects.create(name='Behavior Group', )
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
        )
        ford.scim_groups.add(behavior)
        abernathy = get_user_model().objects.create(
            first_name='Dolores',
            last_name='Abernathy',
            username='******',
        )
        scim_abernathy = get_user_adapter()(abernathy, self.request)

        data = {
            'schemas': [constants.SchemaURI.PATCH_OP],
            'Operations': [{
                'op': 'add',
                'path': 'members',
                'value': [{
                    'value': scim_abernathy.id
                }]
            }]
        }
        data = json.dumps(data)

        url = reverse('scim:groups', kwargs={'uuid': behavior.id})
        resp = self.client.patch(url,
                                 data=data,
                                 content_type=constants.SCIM_CONTENT_TYPE)
        self.assertEqual(resp.status_code, 200, resp.content.decode())

        result = json.loads(resp.content.decode())
        expected = get_group_adapter()(behavior, self.request).to_dict()
        self.assertEqual(expected, result)

        self.assertEqual(behavior.user_set.count(), 2)
示例#27
0
    def test_resource_type_dict(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = {
            'endpoint': u'/scim/v2/Users',
            'description': 'User Account',
            'name': 'User',
            'meta': {
                'resourceType': 'ResourceType',
                'location': u'https://localhost/scim/v2/ResourceTypes/User'
            },
            'schemas': [constants.SchemaURI.RESOURCE_TYPE],
            'id': 'User',
            'schema': constants.SchemaURI.USER,
        }

        self.assertEqual(ford.resource_type_dict(), expected)
示例#28
0
    def test_handle_replace_complex(self):
        operations = [
            {
                "op": "Replace",
                "path": "name.givenName",
                "value": "Robert"
            },
        ]

        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = (('name', 'givenName', None), 'Robert', operations[0])

        with unittest.mock.patch(
                'django_scim.adapters.SCIMUser.handle_replace') as handler:
            ford.handle_operations(operations)
            handler.assert_called_with(*expected)
示例#29
0
    def test_handle_add_simple(self):
        operations = [
            {
                "op": "Add",
                "path": "externalId",
                "value": "Robert.Ford"
            },
        ]

        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford, self.request)

        expected = (('externalId', None, None), 'Robert.Ford', operations[0])

        with unittest.mock.patch(
                'django_scim.adapters.SCIMUser.handle_add') as handler:
            ford.handle_operations(operations)
            handler.assert_called_with(*expected)
示例#30
0
    def test_resource_type_dict(self):
        ford = get_user_model().objects.create(
            first_name='Robert',
            last_name='Ford',
            username='******',
            email='*****@*****.**',
        )
        ford = get_user_adapter()(ford)

        expected = {
            'endpoint': u'/scim/v2/Users',
            'description': 'User Account',
            'name': 'User',
            'meta': {
                'resourceType': 'ResourceType',
                'location': u'https://localhost/scim/v2/ResourceTypes/User'
            },
            'schemas': ['urn:ietf:params:scim:schemas:core:2.0:ResourceType'],
            'id': 'User',
            'schema': 'urn:ietf:params:scim:schemas:core:2.0:User'
        }

        self.assertEqual(ford.resource_type_dict(), expected)