Skip to content

Commit

Permalink
[#2374] Add tests to ensure no duplicate employments
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
punchagan committed Nov 23, 2016
1 parent c35391c commit a86aa4b
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions akvo/rsr/tests/rest/test_user.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a86aa4b

Please sign in to comment.