-
Notifications
You must be signed in to change notification settings - Fork 601
/
test_pwd_file.py
executable file
·160 lines (128 loc) · 5.58 KB
/
test_pwd_file.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
import os
import unittest
from tempfile import NamedTemporaryFile
from tabpy.tabpy_server.app.app import TabPyApp
class TestPasswordFile(unittest.TestCase):
def setUp(self):
self.config_file = NamedTemporaryFile(mode="w", delete=False)
self.config_file.close()
self.pwd_file = NamedTemporaryFile(mode="w", delete=False)
self.pwd_file.close()
def tearDown(self):
os.remove(self.config_file.name)
self.config_file = None
os.remove(self.pwd_file.name)
self.pwd_file = None
def _set_file(self, file_name, value):
with open(file_name, "w") as f:
f.write(value)
def test_given_no_pwd_file_expect_empty_credentials_list(self):
self._set_file(
self.config_file.name, "[TabPy]\n" "TABPY_TRANSFER_PROTOCOL = http"
)
app = TabPyApp(self.config_file.name)
self.assertDictEqual(
app.credentials,
{},
"Expected no credentials with no password file provided",
)
def test_given_empty_pwd_file_expect_app_fails(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
self._set_file(self.pwd_file.name, "# just a comment")
with self.assertRaises(RuntimeError) as cm:
TabPyApp(self.config_file.name)
ex = cm.exception
self.assertEqual(
f"Failed to read password file {self.pwd_file.name}", ex.args[0]
)
def test_given_missing_pwd_file_expect_app_fails(self):
self._set_file(self.config_file.name, "[TabPy]\n" "TABPY_PWD_FILE = foo")
with self.assertRaises(RuntimeError) as cm:
TabPyApp(self.config_file.name)
ex = cm.exception
self.assertEqual(
f"Failed to read password file {self.pwd_file.name}", ex.args[0]
)
def test_given_one_password_in_pwd_file_expect_one_credentials_entry(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
login = "user_name_123"
pwd = "someting@something_else"
self._set_file(self.pwd_file.name, "# passwords\n" "\n" f"{login} {pwd}")
app = TabPyApp(self.config_file.name)
self.assertEqual(len(app.credentials), 1)
self.assertIn(login, app.credentials)
self.assertEqual(app.credentials[login], pwd)
def test_given_username_but_no_password_expect_parsing_fails(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
login = "user_name_123"
pwd = ""
self._set_file(self.pwd_file.name, "# passwords\n" "\n" f"{login} {pwd}")
with self.assertRaises(RuntimeError) as cm:
TabPyApp(self.config_file.name)
ex = cm.exception
self.assertEqual(
f"Failed to read password file {self.pwd_file.name}", ex.args[0]
)
def test_given_duplicate_usernames_expect_parsing_fails(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
login = "user_name_123"
pwd = "hashedpw"
self._set_file(
self.pwd_file.name, "# passwords\n" "\n" f"{login} {pwd}\n{login} {pwd}"
)
with self.assertRaises(RuntimeError) as cm:
TabPyApp(self.config_file.name)
ex = cm.exception
self.assertEqual(
f"Failed to read password file {self.pwd_file.name}", ex.args[0]
)
def test_given_one_line_with_too_many_params_expect_app_fails(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
self._set_file(
self.pwd_file.name,
"# passwords\n" "user1 pwd1\n" "user_2 pwd#2" "user1 pwd@3",
)
with self.assertRaises(RuntimeError) as cm:
TabPyApp(self.config_file.name)
ex = cm.exception
self.assertEqual(
f"Failed to read password file {self.pwd_file.name}", ex.args[0]
)
def test_given_different_cases_in_pwd_file_expect_app_fails(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
self._set_file(
self.pwd_file.name,
"# passwords\n" "user1 pwd1\n" "user_2 pwd#2" "UseR1 pwd@3",
)
with self.assertRaises(RuntimeError) as cm:
TabPyApp(self.config_file.name)
ex = cm.exception
self.assertEqual(
f"Failed to read password file {self.pwd_file.name}", ex.args[0]
)
def test_given_multiple_credentials_expect_all_parsed(self):
self._set_file(
self.config_file.name, "[TabPy]\n" f"TABPY_PWD_FILE = {self.pwd_file.name}"
)
creds = {"user_1": "pwd_1", "user@2": "pwd@2", "user#3": "pwd#3"}
pwd_file_context = ""
for login in creds:
pwd_file_context += f"{login} {creds[login]}\n"
self._set_file(self.pwd_file.name, pwd_file_context)
app = TabPyApp(self.config_file.name)
self.assertCountEqual(creds, app.credentials)
for login in creds:
self.assertIn(login, app.credentials)
self.assertEqual(creds[login], app.credentials[login])