From a86aa4b392d5fea717e7e33d9a997f5d75d91ba1 Mon Sep 17 00:00:00 2001 From: Puneeth Chaganti Date: Wed, 23 Nov 2016 15:34:24 +0530 Subject: [PATCH] [#2374] Add tests to ensure no duplicate employments When an Employment with no group is created, the group is set to 'Users' in the `post_save` method. But, if there is already a similar Employment for that user, the `post_save` fails because there's a uniqueness constraint on (user, organisation, group) --- akvo/rsr/tests/rest/test_user.py | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 akvo/rsr/tests/rest/test_user.py diff --git a/akvo/rsr/tests/rest/test_user.py b/akvo/rsr/tests/rest/test_user.py new file mode 100644 index 0000000000..39bec06665 --- /dev/null +++ b/akvo/rsr/tests/rest/test_user.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- + +""" +Akvo RSR is covered by the GNU Affero General Public License. + +See more details in the license.txt file located at the root folder of the Akvo RSR module. +For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >. +""" + +from __future__ import print_function + +from django.conf import settings +from django.test import TransactionTestCase, Client + +from akvo.rsr.models import Employment, Organisation, User +from akvo.utils import check_auth_groups + + +# NOTE: Since these tests actually trigger some integrity errors and we want to +# see if they are handled correctly, we use a TransactionTestCase instead of +# TestCase, since wrapping the tests in a transaction isn't desirable. + +class UserTestCase(TransactionTestCase): + """Tests REST endpoints in views/user.py.""" + + def setUp(self): + check_auth_groups(settings.REQUIRED_AUTH_GROUPS) + self.c = Client(HTTP_HOST=settings.RSR_DOMAIN) + self.org = Organisation.objects.create(name='akvo', long_name='akvo foundation') + self.user_password = 'password' + self.user = self._create_user('abc@example.com', self.user_password) + self.c.login(username=self.user.username, + password=self.user_password) + + def test_request_organisation_once(self): + # Given + self.c.login(username=self.user.username, + password=self.user_password) + data = {'organisation': self.org.id} + pk = self.user.id + + # When + response = self.c.post( + '/rest/v1/user/{}/request_organisation/?format=json'.format(pk), data + ) + + # Then + self.assertEqual(response.status_code, 200) + employment = Employment.objects.get(user=self.user, organisation_id=self.org.id) + self.assertEqual(employment.group.name, 'Users') + + def test_request_organisation_twice(self): + # Given + data = {'organisation': self.org.id} + pk = self.user.id + self.c.post('/rest/v1/user/{}/request_organisation/?format=json'.format(pk), data) + + # When + response = self.c.post( + '/rest/v1/user/{}/request_organisation/?format=json'.format(pk), data + ) + + # Then + self.assertEqual(response.status_code, 409) + employment = Employment.objects.get(user=self.user, organisation_id=self.org.id) + self.assertEqual(employment.group.name, 'Users') + + def _create_user(self, email, password, is_active=True): + """Create a user with the given email and password.""" + + user = User.objects.create(email=email, username=email, is_active=is_active) + user.set_password(password) + user.save() + + return user