From df6223f583011f3139fba2ac7a3e2a82363c408b Mon Sep 17 00:00:00 2001 From: Fraborcar1 Date: Sun, 11 Dec 2022 17:13:53 +0100 Subject: [PATCH 1/6] test: Added test Graphics Added new tests to prove Graphics --- decide/visualizer/tests.py | 190 +++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/decide/visualizer/tests.py b/decide/visualizer/tests.py index 7ce503c2dd..0f98e13dee 100644 --- a/decide/visualizer/tests.py +++ b/decide/visualizer/tests.py @@ -1,3 +1,193 @@ +import random +import itertools +from django.utils import timezone +from django.conf import settings from django.test import TestCase +from django.contrib.staticfiles.testing import StaticLiveServerTestCase + +from base import mods +from base.tests import BaseTestCase +from mixnet.mixcrypt import ElGamal +from mixnet.mixcrypt import MixCrypt +from mixnet.models import Auth +from voting.models import Voting, Question, QuestionOption +from census.models import Census +from django.contrib.auth.models import User + +from selenium import webdriver +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.common.keys import Keys + # Create your tests here. + +class GraphicsTestCases(BaseTestCase): + def setUp(self): + + super().setUp() + + def tearDown(self): + super().tearDown() + + def encrypt_msg(self, msg, v, bits=settings.KEYBITS): + pk = v.pub_key + p, g, y = (pk.p, pk.g, pk.y) + k = MixCrypt(bits=bits) + k.k = ElGamal.construct((p, g, y)) + return k.encrypt(msg) + + def create_voting(self): + q = Question(desc='Tipos de helados') + q.save() + + opt1 = QuestionOption(question=q, option='Chocolate') + opt1.save() + opt2 = QuestionOption(question=q, option='Vainilla') + opt2.save() + opt3 = QuestionOption(question=q, option='Frambuesa') + opt3.save() + + v = Voting(name='Helado', question=q) + v.save() + + a, _ = Auth.objects.get_or_create(url=settings.BASEURL, + defaults={'me': True, 'name': 'test auth'}) + a.save() + v.auths.add(a) + + return v + + def create_voters(self, v): + for i in range(25): + u, _ = User.objects.get_or_create(username='testvoter{}'.format(i)) + u.is_active = True + u.save() + c = Census(voter_id=u.id, voting_id=v.id) + c.save() + + def get_or_create_user(self, pk): + user, _ = User.objects.get_or_create(pk=pk) + user.username = 'user{}'.format(pk) + user.set_password('qwerty') + user.save() + return user + + def store_votes(self, v): + voters = list(Census.objects.filter(voting_id=v.id)) + voter = voters.pop() + + clear = {} + for opt in v.question.options.all(): + clear[opt.number] = 0 + for i in range(random.randint(0, 5)): + a, b = self.encrypt_msg(opt.number, v) + data = { + 'voting': v.id, + 'voter': voter.voter_id, + 'vote': { 'a': a, 'b': b }, + } + clear[opt.number] += 1 + user = self.get_or_create_user(voter.voter_id) + self.login(user=user.username) + voter = voters.pop() + mods.post('store', json=data) + return clear + + + def test_correct_access_graphics(self): + v = self.create_voting() + self.create_voters(v) + + v.create_pubkey() + v.start_date = timezone.now() + v.save() + + clear = self.store_votes(v) + + self.login() # set token + v.tally_votes(self.token) + + #Se comprueba que se puede acceder a las gráficas de dicha votación + response = self.client.post('/graphics/{}'.format(v.pk), format='json') + self.assertEquals(response.status_code,200) + + def test_graphic_template_correct(self): + v = self.create_voting() + self.create_voters(v) + + v.create_pubkey() + v.start_date = timezone.now() + v.save() + + clear = self.store_votes(v) + + self.login() # set token + v.tally_votes(self.token) + + + #Se comprueba que el template se cargó correctamente + response = self.client.post('/graphics/{}'.format(v.pk), format='json') + self.assertTemplateUsed(response, "graphics.html") + +class SeleniumGraphics(StaticLiveServerTestCase): + + def setUp(self): + self.base = BaseTestCase() + self.base.setUp() + + options = webdriver.ChromeOptions() + options.headless = True + self.driver = webdriver.Chrome(options=options) + + super().setUp() + + def tearDown(self): + super().tearDown() + self.driver.quit() + + self.base.tearDown() + + def encrypt_msg(self, msg, v, bits=settings.KEYBITS): + pk = v.pub_key + p, g, y = (pk.p, pk.g, pk.y) + k = MixCrypt(bits=bits) + k.k = ElGamal.construct((p, g, y)) + return k.encrypt(msg) + + def create_voting(self): + q = Question(desc='Tipos de helados') + q.save() + + opt1 = QuestionOption(question=q, option='Chocolate') + opt1.save() + opt2 = QuestionOption(question=q, option='Vainilla') + opt2.save() + opt3 = QuestionOption(question=q, option='Frambuesa') + opt3.save() + + v = Voting(name='Helado', question=q) + v.save() + + a, _ = Auth.objects.get_or_create(url=settings.BASEURL, + defaults={'me': True, 'name': 'test auth'}) + a.save() + v.auths.add(a) + + return v + + def test_exist_graphic_bars(self): + v = self.create_voting() + + self.driver.get('{}/graphics/{}'.format(self.live_server_url, v.pk)) + tituloPag = self.driver.find_element(By.XPATH, '//div[@class="logo"]').text + graficaBarras = self.driver.find_elements(By.ID, 'grafic') + self.assertTrue(len(graficaBarras)==1) + self.assertEquals(tituloPag, "Votaciones para 'Helado'") + + + + + + From 93fa8da5d80fa1aa3da94c2ff60519babb09cd42 Mon Sep 17 00:00:00 2001 From: Fraborcar1 Date: Sun, 11 Dec 2022 17:40:44 +0100 Subject: [PATCH 2/6] fix: Add selenium as requirement This commit adds selenium in requirements.txt so that the workflow works and run the tests --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8bc743735b..92bbb49b7f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,8 +9,8 @@ django-rest-swagger==2.2.0 coverage==4.5.2 django-nose==1.4.6 jsonnet==0.12.1 +selenium django-allauth==0.51.0 django-verify-email==2.0.3 python-decouple==3.6 django-countries==7.4.2 - From 455452d646426b68ebc8328ba78ffe73468acb1e Mon Sep 17 00:00:00 2001 From: Fraborcar1 Date: Sun, 11 Dec 2022 18:11:13 +0100 Subject: [PATCH 3/6] fix: Fixed Codacy issues Added a lot of optimization to graphics test and fixed Codacy issues --- decide/visualizer/tests.py | 76 ++++++-------------------------------- 1 file changed, 11 insertions(+), 65 deletions(-) diff --git a/decide/visualizer/tests.py b/decide/visualizer/tests.py index 0f98e13dee..6586db4065 100644 --- a/decide/visualizer/tests.py +++ b/decide/visualizer/tests.py @@ -1,5 +1,4 @@ import random -import itertools from django.utils import timezone from django.conf import settings from django.test import TestCase @@ -15,30 +14,14 @@ from django.contrib.auth.models import User from selenium import webdriver -from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By -from selenium.webdriver.support import expected_conditions as EC -from selenium.webdriver.common.keys import Keys # Create your tests here. class GraphicsTestCases(BaseTestCase): def setUp(self): - - super().setUp() - - def tearDown(self): - super().tearDown() - - def encrypt_msg(self, msg, v, bits=settings.KEYBITS): - pk = v.pub_key - p, g, y = (pk.p, pk.g, pk.y) - k = MixCrypt(bits=bits) - k.k = ElGamal.construct((p, g, y)) - return k.encrypt(msg) - - def create_voting(self): + #Creamos votación q = Question(desc='Tipos de helados') q.save() @@ -57,55 +40,29 @@ def create_voting(self): a.save() v.auths.add(a) - return v - - def create_voters(self, v): + #Creamos usuarios para la votación for i in range(25): u, _ = User.objects.get_or_create(username='testvoter{}'.format(i)) u.is_active = True u.save() c = Census(voter_id=u.id, voting_id=v.id) c.save() - - def get_or_create_user(self, pk): - user, _ = User.objects.get_or_create(pk=pk) - user.username = 'user{}'.format(pk) + + user, _ = User.objects.get_or_create(pk=v.pk) + user.username = 'user{}'.format(v.pk) user.set_password('qwerty') user.save() - return user - - def store_votes(self, v): - voters = list(Census.objects.filter(voting_id=v.id)) - voter = voters.pop() - - clear = {} - for opt in v.question.options.all(): - clear[opt.number] = 0 - for i in range(random.randint(0, 5)): - a, b = self.encrypt_msg(opt.number, v) - data = { - 'voting': v.id, - 'voter': voter.voter_id, - 'vote': { 'a': a, 'b': b }, - } - clear[opt.number] += 1 - user = self.get_or_create_user(voter.voter_id) - self.login(user=user.username) - voter = voters.pop() - mods.post('store', json=data) - return clear + super().setUp() + + def tearDown(self): + super().tearDown() def test_correct_access_graphics(self): - v = self.create_voting() - self.create_voters(v) - - v.create_pubkey() + v = Voting.objects.get(name='Helado') v.start_date = timezone.now() v.save() - clear = self.store_votes(v) - self.login() # set token v.tally_votes(self.token) @@ -114,15 +71,11 @@ def test_correct_access_graphics(self): self.assertEquals(response.status_code,200) def test_graphic_template_correct(self): - v = self.create_voting() - self.create_voters(v) + v = Voting.objects.get(name='Helado') - v.create_pubkey() v.start_date = timezone.now() v.save() - clear = self.store_votes(v) - self.login() # set token v.tally_votes(self.token) @@ -149,13 +102,6 @@ def tearDown(self): self.base.tearDown() - def encrypt_msg(self, msg, v, bits=settings.KEYBITS): - pk = v.pub_key - p, g, y = (pk.p, pk.g, pk.y) - k = MixCrypt(bits=bits) - k.k = ElGamal.construct((p, g, y)) - return k.encrypt(msg) - def create_voting(self): q = Question(desc='Tipos de helados') q.save() From ac066271efffd55d71a9651cebb33998f9aa3440 Mon Sep 17 00:00:00 2001 From: Fraborcar1 Date: Sun, 11 Dec 2022 18:21:39 +0100 Subject: [PATCH 4/6] fix: Fixed more Codacy issues Some imports deleted, and hope Codacy will work --- decide/visualizer/tests.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/decide/visualizer/tests.py b/decide/visualizer/tests.py index 6586db4065..0a6007b09a 100644 --- a/decide/visualizer/tests.py +++ b/decide/visualizer/tests.py @@ -1,13 +1,9 @@ -import random from django.utils import timezone from django.conf import settings from django.test import TestCase from django.contrib.staticfiles.testing import StaticLiveServerTestCase -from base import mods from base.tests import BaseTestCase -from mixnet.mixcrypt import ElGamal -from mixnet.mixcrypt import MixCrypt from mixnet.models import Auth from voting.models import Voting, Question, QuestionOption from census.models import Census @@ -24,7 +20,6 @@ def setUp(self): #Creamos votación q = Question(desc='Tipos de helados') q.save() - opt1 = QuestionOption(question=q, option='Chocolate') opt1.save() opt2 = QuestionOption(question=q, option='Vainilla') @@ -93,19 +88,8 @@ def setUp(self): options = webdriver.ChromeOptions() options.headless = True self.driver = webdriver.Chrome(options=options) - - super().setUp() - - def tearDown(self): - super().tearDown() - self.driver.quit() - - self.base.tearDown() - - def create_voting(self): q = Question(desc='Tipos de helados') q.save() - opt1 = QuestionOption(question=q, option='Chocolate') opt1.save() opt2 = QuestionOption(question=q, option='Vainilla') @@ -121,10 +105,16 @@ def create_voting(self): a.save() v.auths.add(a) - return v + super().setUp() + + def tearDown(self): + super().tearDown() + self.driver.quit() + + self.base.tearDown() def test_exist_graphic_bars(self): - v = self.create_voting() + v = Voting.objects.get(name='Helado') self.driver.get('{}/graphics/{}'.format(self.live_server_url, v.pk)) tituloPag = self.driver.find_element(By.XPATH, '//div[@class="logo"]').text From 741e312e73b3eb0b703881bdf6fffdce45efb898 Mon Sep 17 00:00:00 2001 From: Fraborcar1 Date: Sun, 11 Dec 2022 18:28:23 +0100 Subject: [PATCH 5/6] fix: Trailing whitespace Codacy and his problems. --- decide/visualizer/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/decide/visualizer/tests.py b/decide/visualizer/tests.py index 0a6007b09a..0b04f1628b 100644 --- a/decide/visualizer/tests.py +++ b/decide/visualizer/tests.py @@ -42,7 +42,6 @@ def setUp(self): u.save() c = Census(voter_id=u.id, voting_id=v.id) c.save() - user, _ = User.objects.get_or_create(pk=v.pk) user.username = 'user{}'.format(v.pk) user.set_password('qwerty') From 274a023fa2f3919bb71e926fc9aab90473ed2a5a Mon Sep 17 00:00:00 2001 From: Fraborcar1 Date: Sun, 11 Dec 2022 18:45:32 +0100 Subject: [PATCH 6/6] fix: more trailing whitespace Codacy whitespace hopes fixed this time --- decide/visualizer/tests.py | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/decide/visualizer/tests.py b/decide/visualizer/tests.py index 0b04f1628b..a75346b228 100644 --- a/decide/visualizer/tests.py +++ b/decide/visualizer/tests.py @@ -26,15 +26,12 @@ def setUp(self): opt2.save() opt3 = QuestionOption(question=q, option='Frambuesa') opt3.save() - v = Voting(name='Helado', question=q) v.save() - a, _ = Auth.objects.get_or_create(url=settings.BASEURL, defaults={'me': True, 'name': 'test auth'}) a.save() v.auths.add(a) - #Creamos usuarios para la votación for i in range(25): u, _ = User.objects.get_or_create(username='testvoter{}'.format(i)) @@ -46,9 +43,8 @@ def setUp(self): user.username = 'user{}'.format(v.pk) user.set_password('qwerty') user.save() - super().setUp() - + def tearDown(self): super().tearDown() @@ -56,34 +52,27 @@ def test_correct_access_graphics(self): v = Voting.objects.get(name='Helado') v.start_date = timezone.now() v.save() - self.login() # set token v.tally_votes(self.token) - #Se comprueba que se puede acceder a las gráficas de dicha votación response = self.client.post('/graphics/{}'.format(v.pk), format='json') self.assertEquals(response.status_code,200) def test_graphic_template_correct(self): v = Voting.objects.get(name='Helado') - v.start_date = timezone.now() v.save() - self.login() # set token v.tally_votes(self.token) - #Se comprueba que el template se cargó correctamente response = self.client.post('/graphics/{}'.format(v.pk), format='json') self.assertTemplateUsed(response, "graphics.html") - -class SeleniumGraphics(StaticLiveServerTestCase): +class SeleniumGraphics(StaticLiveServerTestCase): def setUp(self): self.base = BaseTestCase() self.base.setUp() - options = webdriver.ChromeOptions() options.headless = True self.driver = webdriver.Chrome(options=options) @@ -95,34 +84,24 @@ def setUp(self): opt2.save() opt3 = QuestionOption(question=q, option='Frambuesa') opt3.save() - v = Voting(name='Helado', question=q) v.save() - a, _ = Auth.objects.get_or_create(url=settings.BASEURL, defaults={'me': True, 'name': 'test auth'}) a.save() v.auths.add(a) - super().setUp() def tearDown(self): super().tearDown() self.driver.quit() - self.base.tearDown() def test_exist_graphic_bars(self): v = Voting.objects.get(name='Helado') - self.driver.get('{}/graphics/{}'.format(self.live_server_url, v.pk)) tituloPag = self.driver.find_element(By.XPATH, '//div[@class="logo"]').text graficaBarras = self.driver.find_elements(By.ID, 'grafic') self.assertTrue(len(graficaBarras)==1) self.assertEquals(tituloPag, "Votaciones para 'Helado'") - - - - - - + \ No newline at end of file