def test_add_voter_custom_fail_not_staff(self): census_before = len(Census.objects.all().values_list('voting_id', flat=True)) admin = User(email='*****@*****.**', password='******') admin.is_staff = False admin.save() user1 = User(email='*****@*****.**', password='******', city='sevilla', sex='M', birthdate=date(year=2000, month=1, day=1)) user1.save() self.client.force_login(admin) voting = Voting.objects.get(pk=40) response = self.client.post( '/census/addCustomCensus', { 'voting': voting.pk, 'city': 'sevilla', 'sex': 'M', 'age_initial_range': '22/12/1990', 'age_final_range_election': '22/12/2020' }) census_after = len(Census.objects.all().values_list('voting_id', flat=True)) self.assertEqual(response.status_code, 302) self.assertTrue(census_after == census_before)
def test_add_voters_sex_fail_invalid_voting_id(self): census_before = len(Census.objects.all().values_list('voting_id', flat=True)) user1 = User(email='*****@*****.**', password='******', city='Madrid', sex='M') user1.save() user2 = User(email='*****@*****.**', password='******', city="Lugo", sex="M") user2.save() user3 = User(email='*****@*****.**', password='******', city="Palencia", sex="W") user3.save() admin = User(email='*****@*****.**', password='******') admin.is_staff = True admin.save() voting_id = "88" self.client.force_login(admin) response2 = self.client.get( '/census/addAllBySex/?voting_id={}&sex={}'.format(voting_id, 'M')) census_after = len(Census.objects.all().values_list('voting_id', flat=True)) self.assertEqual(response2.status_code, 302) self.assertTrue(census_before == census_after)
def test_add_voters_age_fail_voting_started(self): census_before = len(Census.objects.all().values_list('voting_id', flat=True)) admin = User(email='*****@*****.**', password='******') admin.is_staff = True admin.save() user1 = User(email='*****@*****.**', password='******', city='sevilla', sex='M', birthdate=date(year=2000, month=1, day=1)) user1.save() voting_id = "40" voting = Voting.objects.get(pk=40) voting.start_date = timezone.now() voting.save() self.client.force_login(admin) response2 = self.client.get( '/census/addAllByAge/?voting_id={}&younger={}&older={}'.format( voting_id, 100, 0)) census_after = len(Census.objects.all().values_list('voting_id', flat=True)) self.assertEqual(response2.status_code, 302) self.assertTrue(census_after == census_before)
def test_add_voters_registered(self): census_before = len(Census.objects.all().values_list('voting_id', flat=True)) admin = User(email='*****@*****.**', password='******') admin.is_staff = True admin.save() voting_id = "40" self.client.force_login(admin) response2 = self.client.get( '/census/addAllRegistered/?voting_id={}'.format(voting_id)) census_after = len(Census.objects.all().values_list('voting_id', flat=True)) self.assertEqual(response2.status_code, 302) self.assertTrue(census_before < census_after)
def mutate(cls, root, info, **kwargs): if not isinstance(info.context.user, User): raise UnauthorizedException() if not info.context.user.is_superuser: raise ForbiddenException() user = User() create_kwargs = dict(kwargs) del create_kwargs['password'] for key, value in create_kwargs.items(): setattr(user, key, value) user.set_password(kwargs.get('password')) user.is_staff = user.is_superuser user.save() return CreateUser(user=user)
def test_add_voters_city_fail_not_staff(self): census_before = len(Census.objects.all().values_list('voting_id', flat=True)) user1 = User(email='*****@*****.**', password='******', city='sevilla', sex='M') user1.save() user2 = User(email='*****@*****.**', password='******', city="Murcia", sex="N") user2.save() user3 = User(email='*****@*****.**', password='******', city="Albacete", sex="W") user3.save() admin = User(email='*****@*****.**', password='******') admin.is_staff = False admin.save() voting_id = "40" self.client.force_login(admin) response2 = self.client.get( '/census/addAllInCity/?voting_id={}&city={}'.format( voting_id, 'Sevilla')) census_after = len(Census.objects.all().values_list('voting_id', flat=True)) self.assertEqual(response2.status_code, 302) self.assertTrue(census_before == census_after)
def test_add_voters_registered_fail_votingId(self): census_before = len(Census.objects.all().values_list('voting_id', flat=True)) user1 = User(email='*****@*****.**', password='******', city='sevilla', sex='M') user1.save() admin = User(email='*****@*****.**', password='******') admin.is_staff = True admin.save() self.client.force_login(admin) voting_id = "90" response2 = self.client.get( '/census/addAllRegistered/?voting_id={}'.format(voting_id)) census_after = len(Census.objects.all().values_list('voting_id', flat=True)) self.assertEqual(response2.status_code, 302) self.assertTrue(census_before == census_after)