Skip to content

Commit

Permalink
Merge pull request EGCETSII#29 from Decide-Part-Rota/Rota1-010
Browse files Browse the repository at this point in the history
Rota1-010
  • Loading branch information
fraorober authored Nov 24, 2022
2 parents 37efa48 + 871afc6 commit 35d8517
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 14 deletions.
121 changes: 118 additions & 3 deletions decide/census/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import random
from django.contrib.auth.models import User
from django.test import TestCase
from django.contrib.auth.models import User, AnonymousUser
from django.test import RequestFactory
from rest_framework.test import APIClient

from .models import Census
from voting.models import Voting, Question, QuestionOption
from base import mods
from base.tests import BaseTestCase
from .views import add_to_census, remove_from_census

from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware


class CensusTestCase(BaseTestCase):
Expand Down Expand Up @@ -72,4 +77,114 @@ def test_destroy_voter(self):
data = {'voters': [1]}
response = self.client.delete('/census/{}/'.format(1), data, format='json')
self.assertEqual(response.status_code, 204)
self.assertEqual(0, Census.objects.count())
self.assertEqual(0, Census.objects.count())


class CensusAddRemove(BaseTestCase):
def setUp(self):
super().setUp()

self.q = Question(desc='test question')
self.q.save()
for i in range(5):
self.opt = QuestionOption(question=self.q, option='option {}'.format(i+1))
self.opt.save()
self.v = Voting(name='test voting', question=self.q)
self.v.save()

self.voter = User(username='test_user')
self.voter.save()

user_admin = User.objects.get(username="admin")
self.census = Census(voting_id=self.v.id, voter_id=user_admin.id)
self.census.save()

self.factory = RequestFactory()
self.sm = SessionMiddleware()
self.mm = MessageMiddleware()

def tearDown(self):
super().tearDown()
self.census = None

self.q = None
self.opt = None
self.v = None
self.voter = None

self.factory = None
self.sm = None
self.mm = None

def test_create_census_from_gui(self):
self.user = AnonymousUser()
data = {'voting-select': self.v.id, 'user-select': self.voter.id}
request = self.factory.post('/census/add/add_to_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = add_to_census(request)
self.assertEqual(response.status_code, 401)

user_admin = User.objects.get(username="admin")
self.user = user_admin
existing_censuss = Census.objects.count()
data = {'voting-select': self.v.id, 'user-select': self.voter.id}
request = self.factory.post('/census/add/add_to_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = add_to_census(request)
self.assertEqual(response.status_code, 201)
self.assertEqual(existing_censuss+1, Census.objects.count())
self.assertTrue(Census.objects.all().filter(voting_id=self.v.id, voter_id=self.voter.id).exists())

user_admin = User.objects.get(username="admin")
self.user = user_admin
existing_censuss = Census.objects.count()
data = {'voting-select': self.v.id, 'user-select': self.voter.id}
request = self.factory.post('/census/add/add_to_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = add_to_census(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(existing_censuss, Census.objects.count())


def test_delete_census_from_gui(self):
user_admin = User.objects.get(username="admin")

self.user = AnonymousUser()
data = {'voting-select': self.v.id, 'user-select': user_admin.id}
request = self.factory.post('/census/remove/remove_from_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = remove_from_census(request)
self.assertEqual(response.status_code, 401)


self.user = user_admin
existing_censuss = Census.objects.count()
data = {'voting-select': self.v.id, 'user-select': user_admin.id}
request = self.factory.post('/census/remove/remove_from_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = remove_from_census(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(existing_censuss-1, Census.objects.count())
self.assertFalse(Census.objects.all().filter(voting_id=self.v.id, voter_id=user_admin.id).exists())

user_admin = User.objects.get(username="admin")
self.user = user_admin
existing_censuss = Census.objects.count()
data = {'voting-select': self.v.id, 'user-select': user_admin.id}
request = self.factory.post('/census/add/add_to_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = remove_from_census(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(existing_censuss, Census.objects.count())
15 changes: 5 additions & 10 deletions decide/census/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
HTTP_409_CONFLICT as ST_409
)
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.template import loader
from voting.models import Voting
from django.contrib.auth.models import User
Expand Down Expand Up @@ -86,19 +84,16 @@ def add_to_census(request):
census_by_voting = None

status_code=404
if census_by_voting == None:
if census_by_voting is None:
census = Census(voting_id=voting_id, voter_id=user_id)
census.save()
messages.success(request, "User added to the voting correctly")
status_code=ST_201

else:
messages.info(request, "The user was already assigned to the voting")
status_code = 200


return HttpResponse(template.render({}, request), status=status_code)

return HttpResponse(template.render({}, request), status=status_code)
else:
messages.error(request, "You must be a staff member to access this page")
return HttpResponse(template.render({}, request), status=ST_401)
Expand All @@ -120,8 +115,8 @@ def census_remove(request):
return HttpResponse(template.render({'remove': True}, request), status=ST_401)

def remove_from_census(request):
template = loader.get_template("result_page.html")
if request.user.is_staff:
template = loader.get_template("result_page.html")
if request.user.is_staff:
voting_id = request.POST['voting-select']
user_id = request.POST['user-select']
try:
Expand All @@ -130,7 +125,7 @@ def remove_from_census(request):
census_by_voting = None

status_code=404
if census_by_voting != None:
if census_by_voting is not None:
census_by_voting.delete()
messages.success(request, "User removed from the voting correctly")
status_code = 200
Expand Down
2 changes: 1 addition & 1 deletion decide/local_settings.gactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
}

# number of bits for the key, all auths should use the same number of bits
KEYBITS = 256
KEYBITS = 256

0 comments on commit 35d8517

Please sign in to comment.