forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications_test.py
193 lines (161 loc) · 8.34 KB
/
notifications_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import sys
import unittest
from base_test_class import BaseTestCase
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class NotificationTest(BaseTestCase):
def __init__(self, method_name, type):
super().__init__(method_name)
self.type = type
def enable_notification(self):
driver = self.driver
# Navigate to the System Settings
driver.get(self.base_url + "system_settings")
mail_control = driver.find_element(By.ID, f"id_enable_{self.type}_notifications")
if not mail_control.is_selected():
mail_control.click()
driver.find_element(By.CSS_SELECTOR, "input.btn.btn-primary").click()
def disable_notification(self):
driver = self.driver
# Navigate to the System Settings
driver.get(self.base_url + "system_settings")
mail_control = driver.find_element(By.ID, f"id_enable_{self.type}_notifications")
if mail_control.is_selected():
mail_control.click()
driver.find_element(By.CSS_SELECTOR, "input.btn.btn-primary").click()
def test_disable_personal_notification(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
self.disable_notification()
driver.get(self.base_url + "notifications")
try:
driver.find_element(By.XPATH, f"//input[@name='product_added' and @value='{self.type}']")
assert False
except NoSuchElementException:
assert True
def test_enable_personal_notification(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
self.enable_notification()
driver.get(self.base_url + "notifications")
try:
driver.find_element(By.XPATH, f"//input[@name='product_added' and @value='{self.type}']")
assert True
except NoSuchElementException:
if self.type == "msteams":
# msteam should be not in personal notifications
assert True
else:
assert False
def test_disable_system_notification(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
self.disable_notification()
driver.get(self.base_url + "notifications/system")
try:
driver.find_element(By.XPATH, f"//input[@name='product_added' and @value='{self.type}']")
assert False
except NoSuchElementException:
assert True
def test_enable_system_notification(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
self.enable_notification()
driver.get(self.base_url + "notifications/system")
try:
driver.find_element(By.XPATH, f"//input[@name='product_added' and @value='{self.type}']")
assert True
except NoSuchElementException:
assert False
def test_disable_template_notification(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
self.disable_notification()
driver.get(self.base_url + "notifications/template")
try:
driver.find_element(By.XPATH, f"//input[@name='product_added' and @value='{self.type}']")
assert False
except NoSuchElementException:
assert True
def test_enable_template_notification(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
self.enable_notification()
driver.get(self.base_url + "notifications/template")
try:
driver.find_element(By.XPATH, f"//input[@name='product_added' and @value='{self.type}']")
assert True
except NoSuchElementException:
if self.type == "msteams":
# msteam should be not in personal notifications
assert True
else:
assert False
def test_user_mail_notifications_change(self):
# Login to the site. Password will have to be modified
# to match an admin password in your own container
driver = self.driver
wait = WebDriverWait(driver, 5)
actions = ActionChains(driver)
configuration_menu = driver.find_element(By.ID, "menu_configuration")
actions.move_to_element(configuration_menu).perform()
wait.until(EC.visibility_of_element_located((By.LINK_TEXT, "Notifications"))).click()
originally_selected = {
"product_added": driver.find_element(By.XPATH,
"//input[@name='product_added' and @value='mail']").is_selected(),
"scan_added": driver.find_element(By.XPATH, "//input[@name='scan_added' and @value='mail']").is_selected(),
}
driver.find_element(By.XPATH, "//input[@name='product_added' and @value='mail']").click()
driver.find_element(By.XPATH, "//input[@name='scan_added' and @value='mail']").click()
driver.find_element(By.CSS_SELECTOR, "input.btn.btn-primary").click()
self.assertTrue(self.is_success_message_present(text="Settings saved"))
self.assertNotEqual(originally_selected["product_added"],
driver.find_element(By.XPATH, "//input[@name='product_added' and @value='mail']").is_selected())
self.assertNotEqual(originally_selected["scan_added"],
driver.find_element(By.XPATH, "//input[@name='scan_added' and @value='mail']").is_selected())
def suite():
suite = unittest.TestSuite()
# Add each test the the suite to be run
# success and failure is output by the test
suite.addTest(BaseTestCase("test_login"))
suite.addTest(NotificationTest("test_disable_personal_notification", "mail"))
suite.addTest(NotificationTest("test_disable_personal_notification", "slack"))
suite.addTest(NotificationTest("test_disable_personal_notification", "msteams"))
suite.addTest(NotificationTest("test_disable_personal_notification", "webhooks"))
# now test when enabled
suite.addTest(NotificationTest("test_enable_personal_notification", "mail"))
suite.addTest(NotificationTest("test_enable_personal_notification", "slack"))
suite.addTest(NotificationTest("test_enable_personal_notification", "msteams"))
suite.addTest(NotificationTest("test_enable_personal_notification", "webhooks"))
# Now switch to system notifications
suite.addTest(NotificationTest("test_disable_system_notification", "mail"))
suite.addTest(NotificationTest("test_disable_system_notification", "slack"))
suite.addTest(NotificationTest("test_disable_system_notification", "msteams"))
suite.addTest(NotificationTest("test_disable_system_notification", "webhooks"))
# now test when enabled
suite.addTest(NotificationTest("test_enable_system_notification", "mail"))
suite.addTest(NotificationTest("test_enable_system_notification", "slack"))
suite.addTest(NotificationTest("test_enable_system_notification", "msteams"))
suite.addTest(NotificationTest("test_enable_system_notification", "webhooks"))
# not really for the user we created, but still related to user settings
suite.addTest(NotificationTest("test_user_mail_notifications_change", "mail"))
# now do short test for the template
suite.addTest(NotificationTest("test_enable_template_notification", "mail"))
suite.addTest(NotificationTest("test_enable_template_notification", "slack"))
suite.addTest(NotificationTest("test_enable_template_notification", "msteams"))
suite.addTest(NotificationTest("test_enable_template_notification", "webhooks"))
return suite
if __name__ == "__main__":
runner = unittest.TextTestRunner(descriptions=True, failfast=True, verbosity=2)
ret = not runner.run(suite()).wasSuccessful()
BaseTestCase.tearDownDriver()
sys.exit(ret)