from django.contrib.auth.models import Group, Permission, User from django.test import TestCase, Client from cof.models import CofProfile from gestion.tests import create_profile def create_cofprofile(username): p = create_profile(username) return CofProfile.objects.create(profile=p) def create_buro(username, password=None): buro_group = Group.objects.get(name="cof_buro") members_group = Group.objects.get(name="cof_members") u = User.objects.create_user(username=username, password=password) u.groups.add(buro_group, members_group) return u class PatchPerms: """ Setup permissions since there are not set correctly after the migrations. Issue fixed by commit b68590ffd7 """ @classmethod def setUpTestData(cls): print("PatchPerms: remove me!") buro_group = Group.objects.get(name="cof_buro") members_group = Group.objects.get(name="cof_members") member_p = Permission.objects.get( content_type__app_label="cof", codename="member" ) buro_p = Permission.objects.get( content_type__app_label="cof", codename="buro" ) buro_group.permissions.add(buro_p) members_group.permissions.add(member_p) class TestCofProfile(PatchPerms, TestCase): def test_str(self): cofp = create_cofprofile('foo') self.assertFalse(cofp.profile.user.has_perm('cof.member')) # adding/removing a user from the group should impact the # permission group = Group.objects.get(name='cof_members') cofp.profile.user.groups.add(group) cofp.save() cofp = CofProfile.objects.first() self.assertTrue(cofp.profile.user.has_perm('cof.member')) class TestRegistration(PatchPerms, TestCase): def setUp(self): self.buro = create_buro("foo", password="foofoo") self.other = User.objects.create(username="bar") def test_get_view(self): client = Client() client.login(username="foo", password="foofoo") urls = [ "/cof/registration", "/cof/registration/form/", "/cof/registration/form/bar?user_type=normal", "/cof/registration/form/baz?user_type=clipper", ] for url in urls: self.assertEqual(200, client.get(url).status_code)