Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Testing Authentication app (#719)
Browse files Browse the repository at this point in the history
* Testing Authentication app

* Add docstrings to tests
  • Loading branch information
Monal5031 authored and naman1901 committed Jul 15, 2018
1 parent 27cd6ba commit 4fe8aeb
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 71 deletions.
7 changes: 0 additions & 7 deletions vms/authentication/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
# Django
from django import forms


class AuthenticationForm(forms.Form):
username = forms.CharField(required=True)
password = forms.CharField(widget=forms.PasswordInput(), required=True)
203 changes: 143 additions & 60 deletions vms/authentication/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -1,123 +1,206 @@
# 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

# local Django
from pom.pages.authenticationPage import AuthenticationPage

from pom.locators.authenticationPageLocators import AuthenticationPageLocators
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 TestAccessControl(LiveServerTestCase):
'''
"""
TestAccessControl class contains the functional tests to check Admin and
Volunteer can access '/home' view of VMS. Following tests are included:
Administrator:
- Login admin with correct credentials
- Login admin with incorrect credentials
- Login admin with incorrect credentials
Volunteer:
- Login volunteer with correct credentials
- Login volunteer with incorrect credentials
'''
- Login volunteer with incorrect credentials
"""

@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.maximize_window()
cls.authentication_page = AuthenticationPage(cls.driver)
cls.wait = WebDriverWait(cls.driver, 5)
super(TestAccessControl, cls).setUpClass()

def setUp(self):
admin = create_admin()
volunteer = create_volunteer()
"""
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(TestAccessControl, 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 test_correct_admin_credentials(self):
'''
Method to simulate logging in of a valid admin user and check if they
redirected to '/home' and no errors are generated.
'''
"""
Test user redirected to home page after logging in as
admin with correct credentials.
"""
authentication_page = self.authentication_page
self.authentication_page.server_url = self.live_server_url
authentication_page.login({'username': 'admin', 'password': 'admin'})
self.assertEqual(self.driver.current_url,
authentication_page.server_url = self.live_server_url
authentication_page.go_to_authentication_page()
username = password = 'admin'
self.login(username, password)

self.wait.until(
EC.presence_of_element_located(
(By.XPATH,
"//h1[contains(text(), 'Volunteer Management System')]"))
)

self.assertEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.homepage)

with self.assertRaises(NoSuchElementException):
authentication_page.get_incorrect_login_message()
self.assertRaisesRegexp(NoSuchElementException,
'Message: Unable to locate element: .alert-danger',
authentication_page.get_incorrect_login_message)
authentication_page.logout()

def test_incorrect_admin_credentials(self):
'''
Method to simulate logging in of an Invalid admin user and check if
they are displayed an error and redirected to login page again.
'''
"""
Test correct error message displayed while logging as
admin with incorrect credentials.
"""
authentication_page = self.authentication_page
self.authentication_page.server_url = self.live_server_url
authentication_page.login({
'username': 'admin',
'password': 'wrong_password'
})
self.assertNotEqual(
self.driver.current_url,
self.live_server_url + authentication_page.homepage)
authentication_page.server_url = self.live_server_url
authentication_page.go_to_authentication_page()
username = 'admin'
password = 'wrong_password'
self.login(username, password)

self.wait.until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, '.' + AuthenticationPageLocators.INCORRECT_LOGIN_ERROR))
)

self.assertEqual(self.driver.current_url,
self.assertNotEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.homepage)

self.assertEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.url)

self.assertNotEqual(authentication_page.get_incorrect_login_message(),
None)

def test_correct_volunteer_credentials(self):
'''
Method to simulate logging in of a valid volunteer user and check if
they are redirected to '/home'
'''
"""
Test user redirected to home page after logging in as
volunteer with correct credentials.
"""
authentication_page = self.authentication_page
self.authentication_page.server_url = self.live_server_url
authentication_page.login({
'username': 'volunteer',
'password': 'volunteer'
})
self.assertEqual(self.driver.current_url,
authentication_page.server_url = self.live_server_url
authentication_page.go_to_authentication_page()
username = password = 'volunteer'
self.login(username, password)

self.wait.until(
EC.presence_of_element_located(
(By.XPATH,
"//h1[contains(text(), 'Volunteer Management System')]"))
)

self.assertEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.homepage)

with self.assertRaises(NoSuchElementException):
authentication_page.get_incorrect_login_message()
self.assertRaisesRegexp(NoSuchElementException,
'Message: Unable to locate element: .alert-danger',
authentication_page.get_incorrect_login_message)
authentication_page.logout()

def test_incorrect_volunteer_credentials(self):
'''
Method to simulate logging in of a Invalid volunteer user and check if
they are displayed an error and redirected to login page again.
'''
"""
Test correct error message displayed while logging as
volunteer with incorrect credentials.
"""
authentication_page = self.authentication_page
self.authentication_page.server_url = self.live_server_url
authentication_page.login({
'username': 'volunteer',
'password': 'wrong_password'
})
authentication_page.server_url = self.live_server_url
authentication_page.go_to_authentication_page()
username = 'volunteer'
password = 'wrong_password'
self.login(username, password)

self.assertNotEqual(
self.driver.current_url,
self.live_server_url + authentication_page.homepage)
self.wait.until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, '.' + AuthenticationPageLocators.INCORRECT_LOGIN_ERROR))
)

self.assertEqual(self.driver.current_url,
self.live_server_url + authentication_page.url)
self.assertNotEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.homepage)

<<<<<<< ec806c6d047fd408e0fd890e7c6845e2f54622e6
self.assertEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.url)
self.assertNotEqual(authentication_page.get_incorrect_login_message(), None)
"""

def test_login_page_after_authentication(self):
"""
Test user redirected to home page if they try to access login page
after logging in.
"""
authentication_page = self.authentication_page
authentication_page.server_url = self.live_server_url
username = password = 'admin'
self.login(username, password)

self.wait.until(
EC.presence_of_element_located(
(By.XPATH,
"//h1[contains(text(), 'Volunteer Management System')]"))
)

self.assertEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.homepage)

self.assertRaisesRegexp(NoSuchElementException,
'Message: Unable to locate element: .alert-danger',
authentication_page.get_incorrect_login_message)

authentication_page.get_page(authentication_page.server_url, '/authentication/')

self.assertEqual(authentication_page.remove_i18n(self.driver.current_url),
self.live_server_url + authentication_page.homepage)

authentication_page.logout()

3 changes: 0 additions & 3 deletions vms/authentication/tests/test_services.py

This file was deleted.

2 changes: 1 addition & 1 deletion vms/pom/pages/authenticationPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def login(self, credentials):
self.element_by_xpath(self.elements.SUBMIT_PATH).submit()

def go_to_authentication_page(self):
self.click_link(self.home.LOGIN_TEXT)
self.get_page(self.server_url, PageUrls.authentication_page)

def logout(self):
element = self.find_link(self.home.LOGOUT_TEXT)
Expand Down

0 comments on commit 4fe8aeb

Please sign in to comment.