-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_duplicati_client.py
280 lines (256 loc) · 10.5 KB
/
test_duplicati_client.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import unittest
from mock import patch
from auth import login
import common
import requests
class TestLogin(unittest.TestCase):
def mock_write_config(self):
pass
def mock_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, status_code, headers, url, cookies):
self.status_code = status_code
self.headers = headers
self.url = url
self.cookies = cookies
if args[0] == 'http://duplicati_proxy:80':
headers = {'Server': 'nginx/1.13.8',
'Date': 'Wed, 13 Jun 2018 18:28:32 GMT',
'Content-Type': 'text/html',
'Content-Length': '195',
'Connection': 'keep-alive',
'WWW-Authenticate':
'Basic realm="Restricted duplicati.test.net"'
}
cookies = {"xsrf-token":
"sF02%2FyeH5oXieSAB%2FCRf4bsZuR1UHQGx5wmceEacWlg%3D"}
return MockResponse(200, headers, args[0], cookies)
elif args[0] == 'http://localhost:8200':
headers = {'Cache-Control': 'max-age=86400',
'Last-modified': 'Mon, 02 Apr 2018 12:00:08 GMT',
'Date': 'Wed, 13 Jun 2018 20:45:29 GMT',
'Content-Length': '1189',
'Content-Type': 'text/html; charset=utf-8',
'Server': 'Tiny WebServer',
'Keep-Alive': 'timeout=20, max=400',
'Connection': 'Keep-Alive'}
cookies = {"xsrf-token":
"sF02%2FyeH5oXieSAB%2FCRf4bsZuR1UHQGx5wmceEacWlg%3D"}
return MockResponse(200, headers, args[0], cookies)
def mock_requests_get_redirect(*args, **kwargs):
"""simulates the redirect for login"""
class MockResponse:
def __init__(self, status_code, headers, url, cookies):
self.status_code = status_code
self.headers = headers
self.url = url + "/login.html"
self.cookies = cookies
if args[0] == 'http://duplicati_proxy:80':
headers = {'Server': 'nginx/1.13.8',
'Date': 'Wed, 13 Jun 2018 18:28:32 GMT',
'Content-Type': 'text/html',
'Content-Length': '195',
'Connection': 'keep-alive',
'WWW-Authenticate':
'Basic realm="Restricted duplicati.test.net"'
}
return MockResponse(200, headers, args[0])
elif args[0] == 'http://localhost:8200':
headers = {'Cache-Control': 'max-age=86400',
'Last-modified': 'Mon, 02 Apr 2018 12:00:08 GMT',
'Date': 'Wed, 13 Jun 2018 20:45:29 GMT',
'Content-Length': '1189',
'Content-Type': 'text/html; charset=utf-8',
'Server': 'Tiny WebServer',
'Keep-Alive': 'timeout=20, max=400',
'Connection': 'Keep-Alive'}
cookies = {"xsrf-token":
"sF02%2FyeH5oXieSAB%2FCRf4bsZuR1UHQGx5wmceEacWlg%3D"}
return MockResponse(200, headers, args[0], cookies)
def mock_requests_post(*args, **kwargs):
class MockResponse:
def __init__(self, status_code, headers, url, cookies, json_raw):
self.status_code = status_code
self.headers = headers
self.url = url
self.cookies = cookies
self.json_raw = json_raw
def json(self):
return self.json_raw
if args[0] == 'http://duplicati_proxy:8200':
headers = {'Server': 'nginx/1.13.8',
'Date': 'Wed, 13 Jun 2018 18:28:32 GMT',
'Content-Type': 'text/html',
'Content-Length': '195',
'Connection': 'keep-alive',
'WWW-Authenticate':
'Basic realm="Restricted duplicati.test.net"'
}
return MockResponse(200, headers, args[0])
elif args[0] == 'http://localhost:8200/login.cgi':
headers = {'Cache-Control': 'max-age=86400',
'Last-modified': 'Mon, 02 Apr 2018 12:00:08 GMT',
'Date': 'Wed, 13 Jun 2018 20:45:29 GMT',
'Content-Length': '1189',
'Content-Type': 'text/html; charset=utf-8',
'Server': 'Tiny WebServer',
'Keep-Alive': 'timeout=20, max=400',
'Connection': 'Keep-Alive'}
cookies = {"xsrf-token":
"sF02%2FyeH5oXieSAB%2FCRf4bsZuR1UHQGx5wmceEacWlg%3D",
"session-auth":
"63bgjHEmpOrQuZS2ubupqBiB5mJeU9A3yQWbSQql1n0"}
json = {'Status': 'OK',
'Nonce': 'rK44ZOGiWJKk+aDluN/b60MlwXGbQcRc9SnuxSHv784=',
'Salt': 'H9euyRJMYftnoDGro2TC4tEMsQ/BCpZ5dVSBRN1cDC4='}
return MockResponse(200, headers, args[0], cookies, json)
@patch('requests.get', side_effect=mock_requests_get)
@patch('common.write_config', side_effect=mock_write_config)
def test_integrated_auth_logged_in(self, mock_requests_get,
mock_write_config):
"""This test case validates that login works when no
login redirect happens"""
data = {
"last_login": None,
"parameters_file": None,
"server": {
"port": "8200",
"protocol": "http",
"url": "localhost",
"verify": True
},
'token': None,
'token_expires': None,
'verbose': False,
'authorization': ''
}
try:
auth = login(data, input_url=None, password=None, verify=True,
interactive=True, basic_user=None, basic_pass=None)
self.assertEqual(auth, True)
finally:
pass
@patch('requests.get', side_effect=mock_requests_get_redirect)
@patch('common.write_config', side_effect=mock_write_config)
@patch('requests.post', side_effect=mock_requests_post)
def test_integrated_auth_not_logged_in(self, mock_requests_get,
mock_write_config,
mock_requests_post
):
"""This test case validates that login works when
login redirect happens"""
data = {
"last_login": None,
"parameters_file": None,
"server": {
"port": "8200",
"protocol": "http",
"url": "localhost",
"verify": True
},
'token': None,
'token_expires': None,
'verbose': False,
'authorization': ''
}
try:
auth = login(data, input_url=None, password='1234', verify=True,
interactive=False, basic_user=None, basic_pass=None)
self.assertEqual(auth, True)
finally:
pass
@patch('requests.get', side_effect=mock_requests_get)
@patch('common.write_config', side_effect=mock_write_config)
@patch('requests.post', side_effect=mock_requests_post)
def test_basic_auth_not_logged_in(self, mock_requests_get,
mock_write_config,
mock_requests_post
):
"""This test case validates that login works when
login redirect happens"""
data = {
"last_login": None,
"parameters_file": None,
"server": {
"port": "80",
"protocol": "http",
"url": "duplicati_proxy",
"verify": True
},
'token': None,
'token_expires': None,
'verbose': False,
'authorization': ''
}
try:
auth = login(data, input_url=None, password='1234', verify=True,
interactive=False, basic_user='duplicati',
basic_pass='1234')
self.assertEqual(auth, True)
finally:
pass
class TestResponse(unittest.TestCase):
def setUp(self):
self.data = {
"last_login": None,
"parameters_file": None,
"server": {
"port": "8200",
"protocol": "http",
"url": "localhost",
"verify": True
},
'token': None,
'token_expires': None,
'verbose': False,
'authorization': ''
}
def test_response_400(self):
try:
with self.assertRaises(SystemExit) as cm:
common.check_response(self.data, 400)
self.assertEqual(cm.exception.code, 2)
finally:
pass
def test_response_526(self):
try:
with self.assertRaises(SystemExit) as cm:
common.check_response(self.data, 526)
self.assertEqual(cm.exception.code, 2)
finally:
pass
def test_response_495(self):
try:
with self.assertRaises(SystemExit) as cm:
common.check_response(self.data, 495)
self.assertEqual(cm.exception.code, 2)
finally:
pass
def test_response_503(self):
try:
with self.assertRaises(SystemExit) as cm:
common.check_response(self.data, 503)
self.assertEqual(cm.exception.code, 2)
finally:
pass
class TestResponse200(unittest.TestCase):
# mocking the write_config function
def mock_write_config(self):
pass
@patch('common.write_config', side_effect=mock_write_config)
def test_response_200(self, write_config):
data = {
"last_login": None,
"parameters_file": None,
"server": {
"port": "8200",
"protocol": "http",
"url": "localhost",
"verify": True
},
'token': None,
'token_expires': None,
'verbose': False,
'authorization': ''
}
common.check_response(data, 200)