Source code for django_webapps_fullstack.test.test_account_password_change

"""
test program for the django template "password_change"
based on the "unittest" and "selenium" functionality
"""


import os
import sys
import configparser
import unittest
from selenium                        import webdriver
from selenium.webdriver.common.by    import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support      import expected_conditions as EC



# ------- get the parameter to log onto odoo Webservice API

# Get the path to the parent directory
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# Define the directory 'settings' under the parent directory that should be added
sub_dir = os.path.join(parent_dir, 'settings')
# Append this directory to sys.path
sys.path.append(sub_dir)
# call the function "get_settings_odoo" to get the parameters
# for the logging of the model on the "odoo Webservice API"
from get_settings_odoo import get_settings_odoo
result   = get_settings_odoo()
db       = result[0]
uid      = result[1]
pword    = result[3]
models   = result[4]


# ------- get some parameter for the test

# Name and full path of the "ini" file
# Get the path to the parent directory
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# Define the directory 'settings' under the parent directory that should be added
sub_dir = os.path.join(parent_dir, 'settings')
# file is in a subdirectory under the parent directory
pathname = sub_dir

# Read parameter_test.ini
filename_test  = "parameter_test.ini"
fullpath_test  = os.path.join(pathname, filename_test)
config_test = configparser.ConfigParser()
config_test.read(fullpath_test)

# Read parameter_global.ini
filename_global  = "parameter_global.ini"
fullpath_global  = os.path.join(pathname, filename_global)
config_global = configparser.ConfigParser()
config_global.read(fullpath_global)

#  get the url of the webserver to be tested
custom_url = config_global.get('settings_django_server', 'url')
#  get the dedicated email of the customer used as the login
custom_login = config_test.get('get_settings_customer', 'login')
#  get the dedicated password of the customer
custom_password = config_test.get('get_settings_customer', 'password')
#  get the title of the "login" template
custom_title_login = config_test.get('get_settings_title', 'title_login')
#  get the title of the "dashboard" template
custom_title_dashboard = config_test.get('get_settings_title', 'title_dashboard')
#  get the window size for the browser
custom_window_size = config_test.get('get_settings_browser', 'window_size')



[docs] class test_account_password_change(unittest.TestCase): """ Class for the test cases of the django template "password_change" """
[docs] def setUp(self): options = webdriver.ChromeOptions() # runs the browser in headless mode options.add_argument('-headless') # pages render at a realistic size and elements are on-screen/clickable options.add_argument(f'--window-size={custom_window_size}') # set the browser preferred language to english to display web pages in english options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'}) self.driver = webdriver.Chrome(options=options)
[docs] def tearDown(self): # Close the browser window self.driver.quit() # Properly clean up test environment super().tearDown()
[docs] def test_cases(self): """ test cases for the template "password_change" """ # find the user for whom the password should be updated res_users_id = models.execute_kw(db, uid, pword, 'res.users', 'search', [[ ['login', '=', custom_login], ['x_activate_complete', '=', False], ]]) # if a user exist: # update of 'x_activate_complete' so that the password could be uptaded for x in res_users_id: id = x result = models.execute_kw(db, uid, pword, 'res.users', 'write', [[id], {'x_activate_complete': True}]) # login of the user to create the request session # url of template "login" = '/account/login' self.driver.get(custom_url + '/account/login') # assertion to confirm that the title has the keyword in it assert custom_title_login in self.driver.title # search and enter email and submit button login = self.driver.find_element(By.ID, 'id_login') password = self.driver.find_element(By.ID, 'id_password') button = self.driver.find_element(By.CLASS_NAME, 'btn-primary') login.send_keys(custom_login) password.send_keys(custom_password) button.click() # wait for the change to the expected title of the new template "dashboard" wait = WebDriverWait(self.driver, 20) wait.until(EC.title_contains(custom_title_dashboard)) # assertion to test that template "Dashboard" is called after submit assert custom_title_dashboard in self.driver.title # user has been logged into, "password_change" procedure could be started # url of template "password_change" = '/account/password_change' self.driver.get(custom_url + '/account/password_change') # assertion to confirm that the title has the keyword in it assert 'Change Password' in self.driver.title # search and enter email and submit button password_current = self.driver.find_element(By.ID, 'id_password_current') password_new = self.driver.find_element(By.ID, 'id_password_new') password_confirm = self.driver.find_element(By.ID, 'id_password_confirm') button = self.driver.find_element(By.CLASS_NAME, 'btn-primary') password_current.send_keys(custom_password) password_new.send_keys(custom_password) password_confirm.send_keys(custom_password) button.click() # wait for the change to the expected title of the new template wait = WebDriverWait(self.driver, 10) wait.until(EC.title_contains("Password Change Done")) # assertion to test that template "password_recovery_done" is called after submit assert "Password Change Done" in self.driver.title
if __name__ == "__main__": unittest.main()