diff --git a/decide/census/tests.py b/decide/census/tests.py index e86ff7467..ef6a7169f 100644 --- a/decide/census/tests.py +++ b/decide/census/tests.py @@ -2,10 +2,18 @@ from django.contrib.auth.models import User from django.test import TestCase from rest_framework.test import APIClient +from django.contrib.staticfiles.testing import StaticLiveServerTestCase + +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 from .models import Census from base import mods from base.tests import BaseTestCase +from datetime import datetime class CensusTestCase(BaseTestCase): @@ -73,3 +81,86 @@ def test_destroy_voter(self): response = self.client.delete('/census/{}/'.format(1), data, format='json') self.assertEqual(response.status_code, 204) self.assertEqual(0, Census.objects.count()) + + +class CensusTest(StaticLiveServerTestCase): + def setUp(self): + #Load base test functionality for decide + 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 createCensusSuccess(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.cleaner.get(self.live_server_url+"/admin/census/census/add") + now = datetime.now() + self.cleaner.find_element(By.ID, "id_voting_id").click() + self.cleaner.find_element(By.ID, "id_voting_id").send_keys(now.strftime("%m%d%M%S")) + self.cleaner.find_element(By.ID, "id_voter_id").click() + self.cleaner.find_element(By.ID, "id_voter_id").send_keys(now.strftime("%m%d%M%S")) + self.cleaner.find_element(By.NAME, "_save").click() + + self.assertTrue(self.cleaner.current_url == self.live_server_url+"/admin/census/census") + + def createCensusEmptyError(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.cleaner.get(self.live_server_url+"/admin/census/census/add") + + self.cleaner.find_element(By.NAME, "_save").click() + + self.assertTrue(self.cleaner.find_element_by_xpath('/html/body/div/div[3]/div/div[1]/div/form/div/p').text == 'Please correct the errors below.') + self.assertTrue(self.cleaner.current_url == self.live_server_url+"/admin/census/census/add") + + def createCensusValueError(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.cleaner.get(self.live_server_url+"/admin/census/census/add") + now = datetime.now() + self.cleaner.find_element(By.ID, "id_voting_id").click() + self.cleaner.find_element(By.ID, "id_voting_id").send_keys('64654654654654') + self.cleaner.find_element(By.ID, "id_voter_id").click() + self.cleaner.find_element(By.ID, "id_voter_id").send_keys('64654654654654') + self.cleaner.find_element(By.NAME, "_save").click() + + self.assertTrue(self.cleaner.find_element_by_xpath('/html/body/div/div[3]/div/div[1]/div/form/div/p').text == 'Please correct the errors below.') + self.assertTrue(self.cleaner.current_url == self.live_server_url+"/admin/census/census/add") \ No newline at end of file diff --git a/decide/voting/tests.py b/decide/voting/tests.py index 063c52e1c..b8dc1c226 100644 --- a/decide/voting/tests.py +++ b/decide/voting/tests.py @@ -3,10 +3,17 @@ from django.utils import timezone from django.conf import settings from django.contrib.auth.models import User +from django.contrib.staticfiles.testing import StaticLiveServerTestCase from django.test import TestCase from rest_framework.test import APIClient from rest_framework.test import APITestCase +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 + from base import mods from base.tests import BaseTestCase from census.models import Census @@ -14,6 +21,7 @@ from mixnet.mixcrypt import MixCrypt from mixnet.models import Auth from voting.models import Voting, Question, QuestionOption +from datetime import datetime class VotingTestCase(BaseTestCase): @@ -208,3 +216,148 @@ def test_update_voting(self): response = self.client.put('/voting/{}/'.format(voting.pk), data, format='json') self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), 'Voting already tallied') + +class LogInSuccessTests(StaticLiveServerTestCase): + + def setUp(self): + #Load base test functionality for decide + 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 successLogIn(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + self.assertTrue(self.cleaner.current_url == self.live_server_url+"/admin/") + +class LogInErrorTests(StaticLiveServerTestCase): + + def setUp(self): + #Load base test functionality for decide + 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 usernameWrongLogIn(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("usuarioNoExistente") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("usuarioNoExistente") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.assertTrue(self.cleaner.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/p').text == 'Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.') + + def passwordWrongLogIn(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("wrongPassword") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.assertTrue(self.cleaner.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/p').text == 'Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.') + +class QuestionsTests(StaticLiveServerTestCase): + + def setUp(self): + #Load base test functionality for decide + 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 createQuestionSuccess(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.cleaner.get(self.live_server_url+"/admin/voting/question/add/") + + self.cleaner.find_element(By.ID, "id_desc").click() + self.cleaner.find_element(By.ID, "id_desc").send_keys('Test') + self.cleaner.find_element(By.ID, "id_options-0-number").click() + self.cleaner.find_element(By.ID, "id_options-0-number").send_keys('1') + self.cleaner.find_element(By.ID, "id_options-0-option").click() + self.cleaner.find_element(By.ID, "id_options-0-option").send_keys('test1') + self.cleaner.find_element(By.ID, "id_options-1-number").click() + self.cleaner.find_element(By.ID, "id_options-1-number").send_keys('2') + self.cleaner.find_element(By.ID, "id_options-1-option").click() + self.cleaner.find_element(By.ID, "id_options-1-option").send_keys('test2') + self.cleaner.find_element(By.NAME, "_save").click() + + self.assertTrue(self.cleaner.current_url == self.live_server_url+"/admin/voting/question/") + + def createCensusEmptyError(self): + self.cleaner.get(self.live_server_url+"/admin/login/?next=/admin/") + self.cleaner.set_window_size(1280, 720) + + self.cleaner.find_element(By.ID, "id_username").click() + self.cleaner.find_element(By.ID, "id_username").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").click() + self.cleaner.find_element(By.ID, "id_password").send_keys("decide") + + self.cleaner.find_element(By.ID, "id_password").send_keys("Keys.ENTER") + + self.cleaner.get(self.live_server_url+"/admin/voting/question/add/") + + self.cleaner.find_element(By.NAME, "_save").click() + + self.assertTrue(self.cleaner.find_element_by_xpath('/html/body/div/div[3]/div/div[1]/div/form/div/p').text == 'Please correct the errors below.') + self.assertTrue(self.cleaner.current_url == self.live_server_url+"/admin/voting/question/add/") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4c33b535a..02390a41f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ coverage==6.5.0 jsonnet==0.18.0 django-nose==1.4.6 django-rest-swagger==2.2.0 +selenium==4.7.2