def test_with_all_filters(self): test_helpers.make_user_admin(self.user_profile) user_profile_1 = test_helpers.create_user(self.organization, last_name='abc_1') user_profile_4 = test_helpers.create_user(self.organization, last_name='bac_2') user_profile_3 = test_helpers.create_user(self.organization, last_name='abc_3') user_profile_2 = test_helpers.create_user(self.organization, last_name='abc_2') url = reverse('org-access') url = url + '?sort=last_name&order=DESC&size=2&query=abc' response = self.client.get(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) users_sorted = sorted( [user_profile_1, user_profile_3, user_profile_2], key=lambda user_profile: user_profile.user.last_name, reverse=True) user_1 = users_sorted[0] user_org_access_1 = UserOrganizationAccess.objects.get(user=user_1) user_2 = users_sorted[1] user_org_access_2 = UserOrganizationAccess.objects.get(user=user_2) expected_response = { 'organization': { 'id': str(self.organization.uuid), 'name': self.organization.name, 'type': self.organization.type, 'contact_no': self.organization.contact_no, 'address': None }, 'users': [ { 'id': str(user_1.uuid), 'old_id': None, 'title': '', 'first_name': user_1.user.first_name, 'last_name': user_1.user.last_name, 'username': user_1.user.username, 'contact_no': user_1.contact_no, 'email': user_1.user.email, 'user_role': user_org_access_1.user_role }, { 'id': str(user_2.uuid), 'old_id': None, 'title': '', 'first_name': user_2.user.first_name, 'last_name': user_2.user.last_name, 'username': user_2.user.username, 'contact_no': user_2.contact_no, 'email': user_2.user.email, 'user_role': user_org_access_2.user_role }, ] } self.assertJSONEqual(str(response.content, encoding='utf8'), expected_response)
def test_returns_staff_data(self): test_helpers.make_user_admin(self.user_profile) user_oa_mock = MagicMock(name='user_org_mock', organization=self.organization) user_1 = test_helpers.create_user(self.organization) user_1_oa_mock = MagicMock(name='user_1_org_access', organization=self.organization) self.user_org_access_ds_mock.get_user_org_access_by_user_profile.return_value = user_oa_mock self.user_org_access_ds_mock.get_user_org_access_by_user_id.return_value = user_1_oa_mock user_details_ser_mock = MagicMock(name='user_details_ser_mock') user_details_ser_class_mock = self.patch_class( 'user_auth.views.user_profile_views.UserDetailsResponseSerializer', user_details_ser_mock) serializer_mock = MagicMock(name='serializer_mock', data=1) user_details_ser_class_mock.return_value = serializer_mock request = MagicMock(name='request', user=self.user_profile.user) response = GetStaffView().get(request, user_1.uuid) self.assertEqual(response.status_code, status.HTTP_200_OK) self.user_org_access_ds_mock.get_user_org_access_by_user_profile.assert_called_once_with( self.user_profile) self.user_org_access_ds_mock.get_user_org_access_by_user_id.assert_called_once_with( user_1.uuid) user_details_ser_class_mock.assert_called_once_with( {'user': user_1_oa_mock}) self.assertEqual(response.data, serializer_mock.data)
def test_returns_if_userID_is_passed(self): "Returns information for requested userID if user exists" url = reverse('get-user-for-id') user_profile = test_helpers.create_user(self.organization) payload = { 'userID': str(user_profile.uuid) } response = self.client.post(url, json.dumps(payload), "application/json", **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) user = user_profile.user user_org_access = UserOrganizationAccess.objects.get(user=user_profile) expected_response = { 'userID': str(user_profile.uuid), 'firstName': user.first_name, 'lastName': user.last_name, 'username': user.username, 'primaryContact': user_profile.contact_no, 'addressID': None, 'email': user.email, 'roles': [ {'orgID': str(self.organization.uuid), 'org': self.organization.name, 'role': user_org_access.user_role } ] } self.assertJSONEqual(str(response.content, encoding='utf8'), expected_response)
def test_with_query_filter(self): test_helpers.make_user_admin(self.user_profile) user_profile_1 = test_helpers.create_user(self.organization, first_name='Maximus') url = reverse('org-access') url = url + '?query=Max' response = self.client.get(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) user_1 = user_profile_1 user_org_access_1 = UserOrganizationAccess.objects.get(user=user_1) expected_response = { 'organization': { 'id': str(self.organization.uuid), 'name': self.organization.name, 'type': self.organization.type, 'contact_no': self.organization.contact_no, 'address': None }, 'users': [{ 'id': str(user_1.uuid), 'old_id': None, 'title': '', 'first_name': user_1.user.first_name, 'last_name': user_1.user.last_name, 'username': user_1.user.username, 'contact_no': user_1.contact_no, 'email': user_1.user.email, 'user_role': user_org_access_1.user_role }] } self.assertJSONEqual(str(response.content, encoding='utf8'), expected_response)
def test_deletes_user(self): """Soft deletes the user""" test_helpers.make_user_admin(self.user_profile) user_profile = test_helpers.create_user(self.organization) url = reverse('delete-staff', args=[user_profile.uuid]) response = self.client.delete(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) user_profile = UserProfile.all_objects.get(uuid=user_profile.uuid) self.assertTrue(user_profile.is_deleted) self.assertFalse(user_profile.user.is_active)
def test_checks_for_same_org(self): """Raises error if user organizations don't match """ test_helpers.make_user_admin(self.user_profile) org = test_helpers.create_organization() user_profile = test_helpers.create_user(org) url = reverse('delete-staff', args=[user_profile.uuid]) response = self.client.delete(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['error_code'], errors.USER_NOT_EXIST)
def test_raises_error(self): test_helpers.make_user_admin(self.user_profile) org = test_helpers.create_organization() user_profile = test_helpers.create_user(org) url = reverse('get-staff', args=[user_profile.uuid]) response = self.client.get(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['error_code'], errors.USER_NOT_EXIST)
def test_for_size(self): size = 2 test_helpers.make_user_admin(self.user_profile) test_helpers.create_user(self.organization) test_helpers.create_user(self.organization) user_org_mock = MagicMock(name='user_org_mock', organization=self.organization) base_accesses = MagicMock(name='base_accesses') access_1 = MagicMock() access_2 = MagicMock() access_3 = MagicMock() ordered_accesses = [access_1, access_2, access_3] self.user_org_access_ds_mock.get_user_org_access_by_user_profile.return_value = user_org_mock self.user_org_access_ds_mock.get_user_org_access_for_org.return_value = base_accesses base_accesses.order_by.return_value = ordered_accesses self.admin_user_resp_serializer_mock.data = 1 request = MagicMock(name='request', user=self.user_profile.user, query_params={'size': size}) request.GET.getlist.return_value = None response = UserOrganizationView().get(request) self.assertEqual(response.status_code, status.HTTP_200_OK) self.user_org_access_ds_mock.get_user_org_access_by_user_profile.assert_called_once_with( self.user_profile) select_related_fields = ('user', 'user__user') self.user_org_access_ds_mock.get_user_org_access_for_org.assert_called_once_with( self.organization, select_related_fields) base_accesses.order_by.assert_called_once_with( query_to_db_field_map['first_name']) size_limited_access = ordered_accesses[:size] self.admin_user_response_ser_class.assert_called_once_with({ 'organization': self.organization, 'users': size_limited_access }) self.assertEqual(response.data, self.admin_user_resp_serializer_mock.data)
def test_with_no_filters(self): """Returns results sorted by first name if no params are passed""" test_helpers.make_user_admin(self.user_profile) user_profile_1 = test_helpers.create_user(self.organization) url = reverse('org-access') response = self.client.get(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) users_sorted = sorted( [self.user_profile, user_profile_1], key=lambda user_profile: user_profile.user.first_name) user_1 = users_sorted[0] user_org_access_1 = UserOrganizationAccess.objects.get(user=user_1) user_2 = users_sorted[1] user_org_access_2 = UserOrganizationAccess.objects.get(user=user_2) expected_response = { 'organization': { 'id': str(self.organization.uuid), 'name': self.organization.name, 'type': self.organization.type, 'contact_no': self.organization.contact_no, 'address': None }, 'users': [ { 'id': str(user_1.uuid), 'old_id': None, 'title': '', 'first_name': user_1.user.first_name, 'last_name': user_1.user.last_name, 'username': user_1.user.username, 'contact_no': user_1.contact_no, 'email': user_1.user.email, 'user_role': user_org_access_1.user_role }, { 'id': str(user_2.uuid), 'old_id': None, 'title': '', 'first_name': user_2.user.first_name, 'last_name': user_2.user.last_name, 'username': user_2.user.username, 'contact_no': user_2.contact_no, 'email': user_2.user.email, 'user_role': user_org_access_2.user_role }, ] } self.assertJSONEqual(str(response.content, encoding='utf8'), expected_response)
def test_checks_for_same_org(self): """Raises error if user organizations don't match """ test_helpers.make_user_admin(self.user_profile) org = test_helpers.create_organization() user_profile = test_helpers.create_user(org) url = reverse('update-staff', args=[user_profile.uuid]) payload = { 'user': { 'firstName': 'bla', } } response = self.client.put(url, json.dumps(payload), "application/json", **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['error_code'], errors.USER_NOT_EXIST)
def test_success_response(self): test_helpers.make_user_admin(self.user_profile) user_profile = test_helpers.create_user(self.organization) url = reverse('get-staff', args=[user_profile.uuid]) response = self.client.get(url, **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = { 'user': { 'id': str(user_profile.uuid), 'old_id': None, 'first_name': user_profile.user.first_name, 'last_name': user_profile.user.last_name, 'username': user_profile.user.username, 'email': user_profile.user.email, 'title': user_profile.title, 'contact_no': user_profile.contact_no, 'user_role': 'user_role' } } self.assertJSONEqual(str(response.content, encoding='utf8'), expected_response)
def test_updates_requested_profile(self): """Updates profile and makes call to pubnub service""" test_helpers.make_user_admin(self.user_profile) user_profile = test_helpers.create_user(self.organization) url = reverse('update-staff', args=[user_profile.uuid]) payload = { 'user': { 'firstName': 'new_first_name', 'lastName': 'new_last_name', 'email': 'new_email', 'password': '******', 'phone': 'new_phone', 'role': 'new_role' } } self.pubnub_service_mock.get_organization_channel.return_value = 'channel' self.pubnub_service_mock.get_user_update_message.return_value = 'user_update_message' response = self.client.put(url, json.dumps(payload), "application/json", **self.get_base_headers()) self.assertEqual(response.status_code, status.HTTP_200_OK) user_profile = UserProfile.objects.get(uuid=user_profile.uuid) expected_user_data = { 'first_name': payload['user']['firstName'], 'last_name': payload['user']['lastName'], 'email': payload['user']['email'], 'password': payload['user']['password'], 'contact_no': payload['user']['phone'], 'role': payload['user']['role'] } utils.compare_user(self, user_profile.user, expected_user_data) utils.compare_user_profile(self, user_profile, expected_user_data) self.pubnub_service_mock.get_organization_channel.assert_called_with(self.organization) self.pubnub_service_mock.get_user_update_message.assert_called_with(user_profile) self.pubnub_service_mock.publish.assert_called_with('channel', 'user_update_message')