def test_get_user_external(self, requests_mock): ds_response_mock = mock.Mock() ds_response_mock.status_code = 200 ds_response_mock.json.return_value = DS_DATA requests_mock.get.return_value = ds_response_mock requests_mock.codes = requests.codes request = self.request_factory.get('/api/1/users') force_authenticate(request, user=self.user) view = authdata.views.QueryView.as_view() self.user.external_source = 'dreamschool' self.user.external_id = 123 self.user.save() attr = f.UserAttributeFactory(user=self.user) response = view(request, username=self.user.username) self.assertEqual(response.status_code, 200) response.data['roles'] = list(response.data['roles']) expected = DS_EXPECTED expected['attributes'] = [{ 'name': attr.attribute.name, 'value': attr.value }] self.assertEqual(response.data, expected)
def test_userattribute(self): o = f.UserAttributeFactory(attribute__name=u'Ääkkösattribute', value=u'Ääkkösvalue') self.assertTrue(o) self.assertTrue(o.value) self.assertTrue(o.created) self.assertTrue(o.modified) self.assertIn(u'Ääkkösattribute', unicode(o)) self.assertIn(u'Ääkkösvalue', unicode(o))
def test_get_user_fetch_no_attribute_binding(self, requests_mock): ds_response_mock = mock.Mock() ds_response_mock.status_code = 200 ds_response_mock.json.return_value = DS_DATA requests_mock.codes = requests.codes requests_mock.get.return_value = ds_response_mock self.client.force_authenticate(user=self.user) f.UserAttributeFactory(user=self.user, attribute__name='foo', value='bar') result = self.client.get('/api/1/user?foo=123&zao=zup') self.assertEqual(result.status_code, 404)
def test_get_object_with_attributes(self, requests_mock): ds_response_mock = mock.Mock() ds_response_mock.status_code = 200 ds_response_mock.json.return_value = DS_DATA requests_mock.codes = requests.codes requests_mock.get.return_value = ds_response_mock self.client.force_authenticate(user=self.user) f.UserAttributeFactory(user=self.user, attribute__name='foo', value='bar') result = self.client.get('/api/1/user?dreamschool=123&foo=bar&zao=zup') self.assertEqual(result.status_code, 404) # RR 200 -> 404
def test_attribute_data_no_request(self): obj = serializers.UserSerializer() self.assertTrue(obj) user_obj = f.UserFactory() user_attribute_obj = f.UserAttributeFactory(user=user_obj) data = obj.attribute_data(user_obj) self.assertEqual(len(data), 1) d = {} d['name'] = user_attribute_obj.attribute.name d['value'] = user_attribute_obj.value self.assertEqual(data[0], d)
def test_attribute_data_with_request(self): obj = serializers.UserSerializer() self.assertTrue(obj) obj.context['request'] = Mock() obj.context['request'].user = Mock() obj.context['request'].user.username = u'foo' user_obj = f.UserFactory() user_attribute_obj = f.UserAttributeFactory(user=user_obj, data_source__name=u'foo') data = obj.attribute_data(user_obj) self.assertEqual(len(data), 1) d = {} d['name'] = user_attribute_obj.attribute.name d['value'] = user_attribute_obj.value self.assertEqual(data[0], d)
def test_delete(self): user_attr = f.UserAttributeFactory() result = self.client.delete('/api/1/userattribute/%s/' % user_attr.pk) self.assertEqual(result.status_code, 204)
def test_get_object_no_attributes(self, request_mock): self.client.force_authenticate(user=self.user) f.UserAttributeFactory(attribute__name='foo', value='bar') result = self.client.get('/api/1/user') self.assertEqual(result.status_code, 404)