|
| 1 | +from selenium import webdriver |
| 2 | +# from selenium.webdriver.support.ui import Select |
| 3 | +import unittest |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +class UserTest(unittest.TestCase): |
| 10 | + def setUp(self): |
| 11 | + # Initialize the driver |
| 12 | + # When used with Travis, chromdriver is stored in the same |
| 13 | + # directory as the unit tests |
| 14 | + self.driver = webdriver.Chrome('chromedriver') |
| 15 | + # Allow a little time for the driver to initialize |
| 16 | + self.driver.implicitly_wait(30) |
| 17 | + # Set the base address of the dojo |
| 18 | + self.base_url = "http://localhost:8000/" |
| 19 | + self.verificationErrors = [] |
| 20 | + self.accept_next_alert = True |
| 21 | + |
| 22 | + def login_page(self): |
| 23 | + # Make a member reference to the driver |
| 24 | + driver = self.driver |
| 25 | + # Navigate to the login page |
| 26 | + driver.get(self.base_url + "login") |
| 27 | + # Good practice to clear the entry before typing |
| 28 | + driver.find_element_by_id("id_username").clear() |
| 29 | + # These credentials will be used by Travis when testing new PRs |
| 30 | + # They will not work when testing on your own build |
| 31 | + # Be sure to change them before submitting a PR |
| 32 | + driver.find_element_by_id("id_username").send_keys(os.environ['DD_ADMIN_USER']) |
| 33 | + driver.find_element_by_id("id_password").clear() |
| 34 | + driver.find_element_by_id("id_password").send_keys(os.environ['DD_ADMIN_PASSWORD']) |
| 35 | + # "Click" the but the login button |
| 36 | + driver.find_element_by_css_selector("button.btn.btn-success").click() |
| 37 | + return driver |
| 38 | + |
| 39 | + def test_create_user(self): |
| 40 | + # Login to the site. |
| 41 | + driver = self.login_page() |
| 42 | + # Navigate to the User managegement page |
| 43 | + driver.get(self.base_url + "user") |
| 44 | + # "Click" the dropdown button to see options |
| 45 | + driver.find_element_by_id("dropdownMenu1").click() |
| 46 | + # "Click" the add prodcut button |
| 47 | + driver.find_element_by_link_text("New User").click() |
| 48 | + # Fill in the Necessary User Details |
| 49 | + # username, first name, last name, email, and permissions |
| 50 | + # Don't forget to clear before inserting |
| 51 | + # username |
| 52 | + driver.find_element_by_id("id_username").clear() |
| 53 | + driver.find_element_by_id("id_username").send_keys("propersahm") |
| 54 | + # First Name |
| 55 | + driver.find_element_by_id("id_first_name").clear() |
| 56 | + driver.find_element_by_id("id_first_name").send_keys("Proper") |
| 57 | + # Last Name |
| 58 | + driver.find_element_by_id("id_last_name").clear() |
| 59 | + driver.find_element_by_id("id_last_name").send_keys("Samuel") |
| 60 | + # Email Address |
| 61 | + driver.find_element_by_id("id_email").clear() |
| 62 | + driver. find_element_by_id( "id_email"). send_keys( "[email protected]") |
| 63 | + # Give user super user permissions by ticking the checkbox 'is_superuser' |
| 64 | + driver.find_element_by_name("is_superuser").click() # Clicking will mark the checkbox |
| 65 | + # "Click" the submit button to complete the transaction |
| 66 | + driver.find_element_by_css_selector("input.btn.btn-primary").click() |
| 67 | + # Query the site to determine if the user has been created |
| 68 | + productTxt = driver.find_element_by_tag_name("BODY").text |
| 69 | + # Assert ot the query to dtermine status of failure |
| 70 | + self.assertTrue(re.search(r'User added successfully, you may edit if necessary.', productTxt) or |
| 71 | + re.search(r'A user with that username already exists.', productTxt)) |
| 72 | + |
| 73 | + def test_user_edit_permissions(self): |
| 74 | + # Login to the site. Password will have to be modified |
| 75 | + # to match an admin password in your own container |
| 76 | + driver = self.login_page() |
| 77 | + # Navigate to User Management page |
| 78 | + driver.get(self.base_url + "user") |
| 79 | + # Select the previously created user to edit |
| 80 | + # The User name is not clickable |
| 81 | + # so we would have to select specific user by filtering list of users |
| 82 | + driver.find_element_by_id("show-filters").click() # open d filters |
| 83 | + # Insert username to filter by into user name box |
| 84 | + driver.find_element_by_id("id_username").clear() |
| 85 | + driver.find_element_by_id("id_username").send_keys("propersahm") |
| 86 | + # click on 'apply filter' button |
| 87 | + driver.find_element_by_css_selector("button.btn.btn-sm.btn-primary").click() |
| 88 | + # only the needed user is now available proceed with clicking 'Edit' button |
| 89 | + driver.find_element_by_link_text("Edit").click() |
| 90 | + # Unselect Super Admin Permission from previously created user |
| 91 | + # and only select Staff Permission |
| 92 | + driver.find_element_by_name("is_superuser").click() |
| 93 | + driver.find_element_by_name("is_staff").click() |
| 94 | + # "Click" the submit button to complete the transaction |
| 95 | + driver.find_element_by_css_selector("input.btn.btn-primary").click() |
| 96 | + # Query the site to determine if the User permission has been changed |
| 97 | + productTxt = driver.find_element_by_tag_name("BODY").text |
| 98 | + # Assert ot the query to dtermine status of failure |
| 99 | + self.assertTrue(re.search(r'User saved successfully.', productTxt)) |
| 100 | + |
| 101 | + def test_user_delete(self): |
| 102 | + # Login to the site. Password will have to be modified |
| 103 | + # to match an admin password in your own container |
| 104 | + driver = self.login_page() |
| 105 | + # Navigate to the product page |
| 106 | + driver.get(self.base_url + "user") |
| 107 | + # Select A user to edit |
| 108 | + # The User name is not clickable |
| 109 | + # so we would have to select specific user by filtering list of users |
| 110 | + driver.find_element_by_id("show-filters").click() # open d filters |
| 111 | + # Insert username to filter by into user name box |
| 112 | + driver.find_element_by_id("id_username").clear() |
| 113 | + driver.find_element_by_id("id_username").send_keys("propersahm") |
| 114 | + # click on 'apply filter' button |
| 115 | + driver.find_element_by_css_selector("button.btn.btn-sm.btn-primary").click() |
| 116 | + # only the needed user is now available proceed with clicking 'Edit' button |
| 117 | + driver.find_element_by_link_text("Edit").click() |
| 118 | + # "Click" the delete button to complete the transaction |
| 119 | + driver.find_element_by_css_selector("a.btn.btn-danger").click() |
| 120 | + # confirm deletion, by clicking delete a second time |
| 121 | + driver.find_element_by_css_selector("button.btn.btn-danger").click() |
| 122 | + # Query the site to determine if the User has been deleted |
| 123 | + productTxt = driver.find_element_by_tag_name("BODY").text |
| 124 | + # Assert ot the query to dtermine status of failure |
| 125 | + self.assertTrue(re.search(r'User and relationships removed.', productTxt)) |
| 126 | + |
| 127 | + def tearDown(self): |
| 128 | + self.driver.quit() |
| 129 | + self.assertEqual([], self.verificationErrors) |
| 130 | + |
| 131 | + |
| 132 | +def suite(): |
| 133 | + suite = unittest.TestSuite() |
| 134 | + # Add each test the the suite to be run |
| 135 | + # success and failure is output by the test |
| 136 | + suite.addTest(UserTest('test_create_user')) |
| 137 | + suite.addTest(UserTest('test_user_edit_permissions')) |
| 138 | + suite.addTest(UserTest('test_user_delete')) |
| 139 | + return suite |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + runner = unittest.TextTestRunner(descriptions=True, failfast=True) |
| 144 | + ret = not runner.run(suite()).wasSuccessful() |
| 145 | + sys.exit(ret) |
0 commit comments