diff --git a/vms/home/tests/test_functional.py b/vms/home/tests/test_functional.py index 95c0c0e..afcd4f6 100644 --- a/vms/home/tests/test_functional.py +++ b/vms/home/tests/test_functional.py @@ -1,6 +1,9 @@ # third party from selenium import webdriver from selenium.common.exceptions import NoSuchElementException +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.common.by import By # Django from django.contrib.staticfiles.testing import LiveServerTestCase @@ -12,104 +15,145 @@ from shift.utils import (create_admin, create_volunteer) -# Class contains failing test cases which have been documented -# Test class commented out to prevent travis build failure -""" + class CheckURLAccess(LiveServerTestCase): - ''' + """ CheckURLAccess contains methods to browse(via URL) a volunteer page view after logging in from an admin account and vice-versa. Tests included: - Admin cannot access volunteer URL's - Volunteer cannot access admin URL's - ''' - + """ + @classmethod def setUpClass(cls): + """Method to initiate class level objects. + + This method initiates Firefox WebDriver, WebDriverWait and + the corresponding POM objects for this Test Class + """ cls.driver = webdriver.Firefox() + cls.driver.implicitly_wait(5) cls.driver.maximize_window() cls.home_page = HomePage(cls.driver) cls.authentication_page = AuthenticationPage(cls.driver) + cls.wait = WebDriverWait(cls.driver, 10) super(CheckURLAccess, cls).setUpClass() def setUp(self): + """ + Method consists of statements to be executed before + start of each test. + """ create_admin() create_volunteer() def tearDown(self): + """ + Method consists of statements to be executed at + end of each test. + """ pass @classmethod def tearDownClass(cls): + """ + Class method to quit the Firefox WebDriver session after + execution of all tests in class. + """ cls.driver.quit() super(CheckURLAccess, cls).tearDownClass() - def find_volunteer_page_error(self, volunteer_url): - home_page = self.home_page - home_page.get_page(self.live_server_url, volunteer_url) - page_source = self.driver.page_source - error = re.search('403', page_source) - return error - def verify_admin_page_error(self, admin_url): + """ + Utility function to verify errors raised on the + pages when user tries to admin pages provided as + admin_url in param. + :param admin_url: URL of admin page to check errors on. + """ home_page = self.home_page home_page.get_page(self.live_server_url, admin_url) heading = home_page.get_no_admin_right() body = home_page.get_no_admin_right_content() - self.assertNotEqual(heading,None) - self.assertNotEqual(body,None) - self.assertEqual(heading.text,'No Access') - self.assertEqual(body.text,"You don't have administrator rights") + self.assertNotEqual(heading, None) + self.assertNotEqual(body, None) + self.assertEqual(heading.text, 'No Access') + self.assertEqual(body.text, 'You don\'t have administrator rights') + + def verify_volunteer_page_error(self, volunteer_url): + """ + Utility function to verify errors raised on the + pages when user tries to volunteer pages provided as + volunteer_url in param. + :param volunteer_url: URL of volunteer page to check errors on. + """ + home_page = self.home_page + home_page.get_page(self.live_server_url, volunteer_url) + head = home_page.get_no_volunteer_right() + body = home_page.get_no_volunteer_right_content() + self.assertNotEqual(head, None) + self.assertNotEqual(body, None) + self.assertEqual(head.text, 'No Access') + self.assertEqual(body.text, 'You don\'t have the required rights') + + def login(self, username, password): + """ + Utility function to login with credentials received as parameters. + :param username: Username of the user + :param password: Password of the user + """ + self.authentication_page.login({ + 'username': username, + 'password': password + }) + + def wait_for_home_page(self): + """ + Utility function to perform a explicit wait for home page. + """ + self.wait.until( + EC.presence_of_element_located( + (By.XPATH, + "//h1[contains(text(), 'Volunteer Management System')]") + ) + ) def test_admin_cannot_access_volunteer_urls(self): - ''' - Method logins an admin user and tries to surf volunteer pages through - url. The volunteer views should return a 403 error to deny access. - ''' - + """ + Test admin will be shown errors when they try to access + volunteers URLs. + """ + authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ 'username' : 'admin', 'password' : 'admin'}) - - error = self.find_volunteer_page_error(PageUrls.upcoming_shifts_page+'1') - self.assertNotEqual(error, None) - - error = self.find_volunteer_page_error(PageUrls.completed_shifts_page+'1') - self.assertNotEqual(error, None) - - error = self.find_volunteer_page_error(PageUrls.shift_sign_up_page+'1') - self.assertNotEqual(error, None) + self.login(username='admin', password='admin') + self.wait_for_home_page() - error = self.find_volunteer_page_error(PageUrls.volunteer_report_page+'1') - self.assertNotEqual(error, None) - - error = self.find_volunteer_page_error(PageUrls.volunteer_profile_page+'1') - self.assertNotEqual(error, None) + self.verify_volunteer_page_error(PageUrls.upcoming_shifts_page + '1000') + self.verify_volunteer_page_error(PageUrls.completed_shifts_page + '1000') + self.verify_volunteer_page_error(PageUrls.volunteer_report_page + '1000') + self.verify_volunteer_page_error(PageUrls.volunteer_profile_page + '1000') def test_volunteer_cannot_access_admin_urls(self): - ''' - Method logins a volunteer and tries to surf admin page views through url. - The admin views should return a no admin rights page. - ''' + """ + Test volunteer will be shown errors when they try to access + admin URLs. + """ authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ 'username' : 'volunteer', 'password' : 'volunteer'}) + self.login(username='volunteer', password='volunteer') + self.wait_for_home_page() self.verify_admin_page_error(PageUrls.manage_volunteer_shift_page) - self.verify_admin_page_error(PageUrls.admin_settings) + self.verify_admin_page_error(PageUrls.admin_settings_page) self.verify_admin_page_error(PageUrls.volunteer_search_page) self.verify_admin_page_error(PageUrls.administrator_report_page) -""" - -# Class contains failing test cases which have been documented -# Test class commented out to prevent travis build failure -""" class CheckContentAndRedirection(LiveServerTestCase): - ''' - This Class contains methods to check if + """ + This Class contains methods to check if - - an administrator or a volunteer are provided their respective views + - an administrator or a volunteer are provided their respective views links on their dashboard. - all links in the nav-bar for admin and volunteer page redirect to desired views. @@ -133,47 +177,89 @@ class CheckContentAndRedirection(LiveServerTestCase): - Report - Profile - Logout - ''' + """ @classmethod def setUpClass(cls): + """Method to initiate class level objects. + + This method initiates Firefox WebDriver, WebDriverWait and + the corresponding POM objects for this Test Class + """ cls.driver = webdriver.Firefox() + cls.driver.implicitly_wait(5) cls.driver.maximize_window() cls.home_page = HomePage(cls.driver) cls.authentication_page = AuthenticationPage(cls.driver) + cls.wait = WebDriverWait(cls.driver, 10) super(CheckContentAndRedirection, cls).setUpClass() def setUp(self): + """ + Method consists of statements to be executed before + start of each test. + """ self.admin = create_admin() self.volunteer = create_volunteer() self.volunteer_id = str(self.volunteer.id) def tearDown(self): + """ + Method consists of statements to be executed at + end of each test. + """ pass @classmethod def tearDownClass(cls): + """ + Class method to quit the Firefox WebDriver session after + execution of all tests in class. + """ cls.driver.quit() super(CheckContentAndRedirection, cls).tearDownClass() + def login(self, username, password): + """ + Utility function to login with credentials received as parameters. + :param username: Username of the user + :param password: Password of the user + """ + self.authentication_page.login({ + 'username': username, + 'password': password + }) + + def wait_for_home_page(self): + """ + Utility function to perform a explicit wait for home page. + """ + self.wait.until( + EC.presence_of_element_located( + (By.XPATH, + "//h1[contains(text(), 'Volunteer Management System')]") + ) + ) + def test_check_admin_page_content(self): - ''' + """ Check if an admin user has following functionalities on its home page. - Volunteer Search - Manage Volunteer Shift - Report - Settings - Create Admin Account - ''' + """ authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url home_page = self.home_page - authentication_page.login({'username': 'admin', 'password': 'admin'}) - - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() + self.login(username='admin', password='admin') + self.wait_for_home_page() + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) self.assertNotEqual(home_page.get_volunteer_search_link(), None) self.assertNotEqual(home_page.get_manage_shifts_link(), None) self.assertNotEqual(home_page.get_admin_report_link(), None) @@ -182,7 +268,7 @@ def test_check_admin_page_content(self): self.assertNotEqual(home_page.get_logout_link(), None) def test_check_volunteer_page_content(self): - ''' + """ Check if a volunteer user has following functionalities on its home page. - UpComing Shift @@ -190,18 +276,16 @@ def test_check_volunteer_page_content(self): - Shift Sign Up - Report - Profile - ''' + """ home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ - 'username': 'volunteer', - 'password': 'volunteer' - }) - - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() + self.login(username='volunteer', password='volunteer') + self.wait_for_home_page() + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) self.assertNotEqual(home_page.get_upcoming_shifts_link(), None) self.assertNotEqual(home_page.get_completed_shifts_link(), None) self.assertNotEqual(home_page.get_shift_signup_link(), None) @@ -210,88 +294,85 @@ def test_check_volunteer_page_content(self): self.assertNotEqual(home_page.get_logout_link(), None) def test_admin_page_redirection(self): + """ + Test admin is redirected corrected to home page after + successful authorization by checking different elements + on home page. + """ home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({'username': 'admin', 'password': 'admin'}) + self.login(username='admin', password='admin') + self.wait_for_home_page() - self.assertEqual(self.driver.current_url, + self.assertEqual(authentication_page.remove_i18n(self.driver.current_url), self.live_server_url + PageUrls.homepage) + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() - - volunteer_search_link = home_page.get_volunteer_search_link( - ).get_attribute('href') - self.assertEqual(volunteer_search_link, + volunteer_search_link = home_page.get_volunteer_search_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(volunteer_search_link), self.live_server_url + PageUrls.volunteer_search_page) - manage_volunteer_shift_link = home_page.get_manage_shifts_link( - ).get_attribute('href') - self.assertEqual( - manage_volunteer_shift_link, - self.live_server_url + PageUrls.manage_volunteer_shift_page) + manage_volunteer_shift_link = home_page.get_manage_shifts_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(manage_volunteer_shift_link), + self.live_server_url + PageUrls.manage_volunteer_shift_page) report_link = home_page.get_admin_report_link().get_attribute('href') - self.assertEqual( - report_link, - self.live_server_url + PageUrls.administrator_report_page) + self.assertEqual(home_page.remove_i18n(report_link), + self.live_server_url + PageUrls.administrator_report_page) settings_link = home_page.get_events_link().get_attribute('href') - self.assertEqual(settings_link, + self.assertEqual(home_page.remove_i18n(settings_link), self.live_server_url + PageUrls.admin_settings_page) - creat_account_link = home_page.get_create_admin_link().get_attribute( - 'href') - self.assertEqual( - creat_account_link, - self.live_server_url + PageUrls.admin_registration_page) + create_account_link = home_page.get_create_admin_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(create_account_link), + self.live_server_url + PageUrls.admin_registration_page) logout_link = home_page.get_logout_link().get_attribute('href') - self.assertEqual(logout_link, + self.assertEqual(home_page.remove_i18n(logout_link), self.live_server_url + PageUrls.logout_page) def test_volunteer_page_redirection(self): + """ + Test volunteer is redirected corrected to home page after + successful authorization by checking different elements + on home page. + """ home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ - 'username': 'volunteer', - 'password': 'volunteer' - }) + self.login(username='volunteer', password='volunteer') + self.wait_for_home_page() - self.assertEqual(self.driver.current_url, + self.assertEqual(home_page.remove_i18n(self.driver.current_url), self.live_server_url + PageUrls.homepage) + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() - - upcoming_shift_link = home_page.get_upcoming_shifts_link( - ).get_attribute('href') - self.assertEqual(upcoming_shift_link, self.live_server_url + + upcoming_shift_link = home_page.get_upcoming_shifts_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(upcoming_shift_link), self.live_server_url + PageUrls.upcoming_shifts_page + self.volunteer_id) - shift_hours_link = home_page.get_completed_shifts_link().get_attribute( - 'href') - self.assertEqual(shift_hours_link, self.live_server_url + + shift_hours_link = home_page.get_completed_shifts_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(shift_hours_link), self.live_server_url + PageUrls.completed_shifts_page + self.volunteer_id) - shift_signup_link = home_page.get_shift_signup_link().get_attribute( - 'href') - self.assertEqual(shift_signup_link, self.live_server_url + + shift_signup_link = home_page.get_shift_signup_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(shift_signup_link), self.live_server_url + PageUrls.shift_sign_up_page + self.volunteer_id) - report_link = home_page.get_volunteer_report_link().get_attribute( - 'href') - self.assertEqual(report_link, self.live_server_url + - PageUrls.volunteer_report_page + self.volunteer_id) + report_link = home_page.get_volunteer_report_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(report_link), + self.live_server_url + PageUrls.volunteer_report_page + self.volunteer_id) - profile_link = home_page.get_volunteer_profile_link().get_attribute( - 'href') - self.assertEqual(profile_link, self.live_server_url + - PageUrls.volunteer_profile_page + self.volunteer_id) + profile_link = home_page.get_volunteer_profile_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(profile_link), + self.live_server_url + PageUrls.volunteer_profile_page + self.volunteer_id) logout_link = home_page.get_logout_link().get_attribute('href') - - self.assertEqual(logout_link, self.live_server_url + - PageUrls.logout_page)""" + self.assertEqual(home_page.remove_i18n(logout_link), + self.live_server_url + PageUrls.logout_page) diff --git a/vms/home/tests/test_services.py b/vms/home/tests/test_services.py deleted file mode 100644 index f9e883a..0000000 --- a/vms/home/tests/test_services.py +++ /dev/null @@ -1,3 +0,0 @@ -# Django - -# Create your tests here. diff --git a/vms/home/tests/test_unit.py b/vms/home/tests/test_unit.py deleted file mode 100644 index a2629a5..0000000 --- a/vms/home/tests/test_unit.py +++ /dev/null @@ -1,2 +0,0 @@ -# Django - diff --git a/vms/pom/locators/homePageLocators.py b/vms/pom/locators/homePageLocators.py index 10f778c..bb0159a 100644 --- a/vms/pom/locators/homePageLocators.py +++ b/vms/pom/locators/homePageLocators.py @@ -15,3 +15,5 @@ class HomePageLocators(object): NO_ADMIN_RIGHT_HEAD = 'panel-heading' NO_ADMIN_RIGHT_CONTENT = 'panel-body' + NO_VOLUNTEER_RIGHT_HEAD = 'panel-title' + NO_VOLUNTEER_RIGHT_CONTENT = 'panel-body' diff --git a/vms/pom/pages/homePage.py b/vms/pom/pages/homePage.py index 0b7a6cf..676c21c 100644 --- a/vms/pom/pages/homePage.py +++ b/vms/pom/pages/homePage.py @@ -45,7 +45,13 @@ def get_volunteer_profile_link(self): return self.find_link(self.elements.VOLUNTEER_PROFILE_TEXT) def get_no_admin_right(self): - return self.elements_by_class_name(self.elements.NO_ADMIN_RIGHT_HEAD) + return self.element_by_class_name(self.elements.NO_ADMIN_RIGHT_HEAD) def get_no_admin_right_content(self): - return self.elements_by_class_name(self.elements.NO_ADMIN_RIGHT_CONTENT) + return self.element_by_class_name(self.elements.NO_ADMIN_RIGHT_CONTENT) + + def get_no_volunteer_right(self): + return self.element_by_class_name(self.elements.NO_VOLUNTEER_RIGHT_HEAD) + + def get_no_volunteer_right_content(self): + return self.element_by_class_name(self.elements.NO_VOLUNTEER_RIGHT_CONTENT)