Skip to content

Commit efaab62

Browse files
committed
Extracted paths to top of file for easier config
1 parent da92b14 commit efaab62

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

tests/login_test.py

+41-42
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@
1010
import json
1111
import requests
1212

13+
ldap_test_server = "vm28.nubes.stfc.ac.uk"
14+
shib_test_server = "vm181.nubes.stfc.ac.uk"
15+
credentials_file = "/home/mnf98541/Dynafed/credentials.json"
16+
firefox_path = "/home/mnf98541/Downloads/firefox-58.0.2/firefox"
17+
firefox_profile_path = "/home/mnf98541/.mozilla/firefox/u2v7wxvi.default"
1318

14-
class LDAPAuthnTest(unittest.TestCase):
15-
server = "vm28.nubes.stfc.ac.uk"
1619

20+
class LDAPAuthnTest(unittest.TestCase):
1721
def setUp(self):
18-
binary = FirefoxBinary("/home/mnf98541/Downloads/firefox-58.0.2/firefox")
22+
binary = FirefoxBinary(firefox_path)
1923
self.driver = webdriver.Firefox(firefox_binary=binary)
2024

21-
with open("../credentials.json", "r") as f:
25+
with open(credentials_file, "r") as f:
2226
f_json = json.load(f)
2327
self.username = f_json["louise"]["username"]
2428
self.password = f_json["louise"]["password"]
2529

2630
def test_login(self):
2731
driver = self.driver
28-
driver.get("https://" + self.server + "/myfed/ldap/")
32+
driver.get("https://" + ldap_test_server + "/myfed/ldap/")
2933

3034
# if we get a pop up, then authentication is on
3135
popup = True
@@ -46,7 +50,7 @@ def test_login(self):
4650

4751
def test_login_fail(self):
4852
driver = self.driver
49-
driver.get("https://" + "wrong_username" + ":" + "wrong_password" + "@" + self.server + "/myfed/ldap")
53+
driver.get("https://" + "wrong_username" + ":" + "wrong_password" + "@" + ldap_test_server + "/myfed/ldap")
5054

5155
# if we get a pop up, then our username and password were wrong
5256
# if we don't get a pop up then it was accepted for some reason
@@ -67,20 +71,18 @@ def tearDown(self):
6771

6872

6973
class LDAPAuthzTest(unittest.TestCase):
70-
server = "vm28.nubes.stfc.ac.uk"
71-
7274
def setUp(self):
73-
binary = FirefoxBinary("/home/mnf98541/Downloads/firefox-58.0.2/firefox")
75+
binary = FirefoxBinary(firefox_path)
7476
self.driver = webdriver.Firefox(firefox_binary=binary)
7577

76-
with open("../credentials.json", "r") as f:
78+
with open(credentials_file, "r") as f:
7779
f_json = json.load(f)
7880
self.username = f_json["louise"]["username"]
7981
self.password = f_json["louise"]["password"]
8082

8183
def test_access_allowed(self):
8284
driver = self.driver
83-
driver.get("https://" + self.server + "/myfed/ldap/test/authorised")
85+
driver.get("https://" + ldap_test_server + "/myfed/ldap/test/authorised")
8486

8587
WebDriverWait(driver, 5).until(EC.alert_is_present())
8688

@@ -94,7 +96,7 @@ def test_access_allowed(self):
9496

9597
def test_access_denied(self):
9698
driver = self.driver
97-
driver.get("https://" + self.server + "/myfed/ldap/test/unauthorised")
99+
driver.get("https://" + ldap_test_server + "/myfed/ldap/test/unauthorised")
98100

99101
WebDriverWait(driver, 5).until(EC.alert_is_present())
100102

@@ -110,37 +112,36 @@ def test_access_denied(self):
110112
def test_download_access_success(self):
111113
# use requests here to test we get a 200 response when trying to directly download a file
112114

113-
r = requests.get("https://" + self.server + "/myfed/ldap/test/authorised/Smudge.jpg", auth=(self.username, self.password), verify=False)
115+
r = requests.get("https://" + ldap_test_server + "/myfed/ldap/test/authorised/Smudge.jpg", auth=(self.username, self.password), verify=False)
114116
self.assertEqual(r.status_code, 200)
115117

116118
def test_download_access_fail(self):
117119
# use requests here to test we get a 403 response when trying to directly download a file
118120

119-
r = requests.get("https://" + self.server + "/myfed/ldap/test/unauthorised/Smudge.jpg", auth=(self.username, self.password), verify=False)
121+
r = requests.get("https://" + ldap_test_server + "/myfed/ldap/test/unauthorised/Smudge.jpg", auth=(self.username, self.password), verify=False)
120122
self.assertEqual(r.status_code, 403)
121123

122124
def tearDown(self):
123125
self.driver.close()
124126

125127

126128
class CertificateAuthSuccessTest(unittest.TestCase):
127-
server = "vm28.nubes.stfc.ac.uk"
128-
129129
def setUp(self):
130-
binary = FirefoxBinary("/home/mnf98541/Downloads/firefox-58.0.2/firefox")
130+
binary = FirefoxBinary(firefox_path)
131131
# need to specify our profile so it can use our certificate
132-
profile = webdriver.FirefoxProfile("/home/mnf98541/.mozilla/firefox/u2v7wxvi.default")
132+
# see https://stackoverflow.com/questions/17437407/how-to-import-ssl-certificates-for-firefox-with-selenium-in-python
133+
profile = webdriver.FirefoxProfile(firefox_profile_path)
133134
self.driver = webdriver.Firefox(profile, firefox_binary=binary)
134135

135136
def test_login(self):
136137
driver = self.driver
137-
driver.get("https://" + self.server + "/myfed/x509/test/unprotected")
138+
driver.get("https://" + ldap_test_server + "/myfed/x509/test/unprotected")
138139

139140
self.assertIn("/C=UK/O=eScience/OU=CLRC/L=RAL/CN=louise davies", driver.page_source)
140141

141142
def test_see_all_buckets(self):
142143
driver = self.driver
143-
driver.get("https://" + self.server + "/myfed/x509")
144+
driver.get("https://" + ldap_test_server + "/myfed/x509")
144145

145146
WebDriverWait(driver, 5).until(EC.title_is("/myfed/x509/"))
146147
self.assertIn("atlas", driver.page_source)
@@ -154,29 +155,29 @@ def test_see_all_buckets(self):
154155

155156
def test_access_allowed_simple(self):
156157
driver = self.driver
157-
driver.get("https://" + self.server + "/myfed/x509/test/authorised")
158+
driver.get("https://" + ldap_test_server + "/myfed/x509/test/authorised")
158159

159160
WebDriverWait(driver, 5).until(EC.title_is("/myfed/x509/test/authorised/"))
160161
self.assertIn("Smudge.jpg", driver.page_source)
161162

162163
def test_access_allowed(self):
163164
driver = self.driver
164-
driver.get("https://" + self.server + "/myfed/x509/enmr/ccp4-data")
165+
driver.get("https://" + ldap_test_server + "/myfed/x509/enmr/ccp4-data")
165166

166167
WebDriverWait(driver, 5).until(EC.title_is("/myfed/x509/enmr/ccp4-data/"))
167168
self.assertIn("Powered by LCGDM-DAV", driver.page_source)
168169

169170
def test_access_denied_simple(self):
170171
driver = self.driver
171-
driver.get("https://" + self.server + "/myfed/x509/test/unauthorised")
172+
driver.get("https://" + ldap_test_server + "/myfed/x509/test/unauthorised")
172173

173174
WebDriverWait(driver, 5).until(EC.title_is("403 Forbidden"))
174175

175176
self.assertNotIn("Smudge.jpg", driver.page_source)
176177

177178
def test_access_denied(self):
178179
driver = self.driver
179-
driver.get("https://" + self.server + "/myfed/x509/enmr/ccp4-jobs")
180+
driver.get("https://" + ldap_test_server + "/myfed/x509/enmr/ccp4-jobs")
180181

181182
WebDriverWait(driver, 5).until(EC.title_is("403 Forbidden"))
182183

@@ -187,16 +188,14 @@ def tearDown(self):
187188

188189

189190
class CertificateAuthFailureTest(unittest.TestCase):
190-
server = "vm28.nubes.stfc.ac.uk"
191-
192191
def setUp(self):
193-
binary = FirefoxBinary("/home/mnf98541/Downloads/firefox-58.0.2/firefox")
192+
binary = FirefoxBinary(firefox_path)
194193
# don't specify profile, so we don't have certificate
195194
self.driver = webdriver.Firefox(firefox_binary=binary)
196195

197196
def test_login_fail(self):
198197
driver = self.driver
199-
driver.get("https://" + self.server + "/myfed/x509/test/authorised")
198+
driver.get("https://" + ldap_test_server + "/myfed/x509/test/authorised")
200199

201200
WebDriverWait(driver, 5).until(EC.title_is("403 Forbidden"))
202201

@@ -207,17 +206,16 @@ def tearDown(self):
207206

208207

209208
class ShibAuthnTest(unittest.TestCase):
210-
server = "vm181.nubes.stfc.ac.uk"
211-
212209
def setUp(self):
213-
binary = FirefoxBinary("/home/mnf98541/Downloads/firefox-58.0.2/firefox")
210+
binary = FirefoxBinary(firefox_path)
214211
self.driver = webdriver.Firefox(firefox_binary=binary)
212+
# these are the username and password for TestShib
215213
self.username = "myself"
216214
self.password = "myself"
217215

218216
def test_login(self):
219217
driver = self.driver
220-
driver.get("https://" + self.server + "/myfed")
218+
driver.get("https://" + shib_test_server + "/myfed")
221219

222220
# if we get a pop up, then authentication is on
223221
try:
@@ -240,7 +238,7 @@ def test_login(self):
240238

241239
def test_login_fail(self):
242240
driver = self.driver
243-
driver.get("https://" + self.server + "/myfed")
241+
driver.get("https://" + shib_test_server + "/myfed")
244242

245243
# if we get a pop up, then authentication is on
246244
login_page = True
@@ -266,17 +264,16 @@ def tearDown(self):
266264

267265

268266
class ShibAuthzTest(unittest.TestCase):
269-
server = "vm181.nubes.stfc.ac.uk"
270-
271267
def setUp(self):
272-
binary = FirefoxBinary("/home/mnf98541/Downloads/firefox-58.0.2/firefox")
268+
binary = FirefoxBinary(firefox_path)
273269
self.driver = webdriver.Firefox(firefox_binary=binary)
270+
# these are the username and password for TestShib
274271
self.username = "myself"
275272
self.password = "myself"
276273

277274
def test_access_allowed(self):
278275
driver = self.driver
279-
driver.get("https://" + self.server + "/myfed/shib/authorised")
276+
driver.get("https://" + shib_test_server + "/myfed/shib/authorised")
280277

281278
WebDriverWait(driver, 5).until(EC.title_is("TestShib Identity Provider Login"))
282279

@@ -292,7 +289,7 @@ def test_access_allowed(self):
292289

293290
def test_access_denied(self):
294291
driver = self.driver
295-
driver.get("https://" + self.server + "/myfed/shib/unauthorised")
292+
driver.get("https://" + shib_test_server + "/myfed/shib/unauthorised")
296293

297294
WebDriverWait(driver, 5).until(EC.title_is("TestShib Identity Provider Login"))
298295

@@ -313,7 +310,7 @@ def test_download_access_success(self):
313310
# need to login first...
314311

315312
driver = self.driver
316-
driver.get("https://" + self.server + "/myfed/")
313+
driver.get("https://" + shib_test_server + "/myfed/")
317314

318315
WebDriverWait(driver, 5).until(EC.title_is("TestShib Identity Provider Login"))
319316

@@ -326,9 +323,10 @@ def test_download_access_success(self):
326323

327324
WebDriverWait(driver, 5).until(EC.title_is("/myfed/"))
328325

326+
# use the cookies to see if this allows us to download a file with our credentials
329327
cookies = {i['name']: i['value'] for i in driver.get_cookies()}
330328

331-
r = requests.get("https://" + self.server + "/myfed/shib/authorised/Smudge.jpg", cookies=cookies, verify=False)
329+
r = requests.get("https://" + shib_test_server + "/myfed/shib/authorised/Smudge.jpg", cookies=cookies, verify=False)
332330
self.assertEqual(r.status_code, 200)
333331

334332
def test_download_access_fail(self):
@@ -337,7 +335,7 @@ def test_download_access_fail(self):
337335
# need to login first...
338336

339337
driver = self.driver
340-
driver.get("https://" + self.server + "/myfed/")
338+
driver.get("https://" + shib_test_server + "/myfed/")
341339

342340
WebDriverWait(driver, 5).until(EC.title_is("TestShib Identity Provider Login"))
343341

@@ -350,9 +348,10 @@ def test_download_access_fail(self):
350348

351349
WebDriverWait(driver, 5).until(EC.title_is("/myfed/"))
352350

351+
# use the cookies to see if this allows us to download a file with our credentials
353352
cookies = {i['name']: i['value'] for i in driver.get_cookies()}
354353

355-
r = requests.get("https://" + self.server + "/myfed/shib/unauthorised/Smudge.jpg", cookies=cookies, verify=False)
354+
r = requests.get("https://" + shib_test_server + "/myfed/shib/unauthorised/Smudge.jpg", cookies=cookies, verify=False)
356355
self.assertEqual(r.status_code, 403)
357356

358357
def tearDown(self):

0 commit comments

Comments
 (0)