aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_ui.py
blob: cd7553762ac728e3499bf8f7bcb5fd0a6df86c45 (plain)
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
from pwman_tstlib import *
initTest(__file__)

import importlib
import os
import pathlib
import shutil
import tempfile

class Test_UI(TestCase):
	"""User interface.
	"""

	def __fakeClearScreen(self):
		print("CLEAR SCREEN")

	def __fakeReadPassphrase(self, prompt, verify=False):
		print("READ PASSPHRASE")
		return "test"

	def setUp(self):
		import libpwman.util
		self.__origClearScreen = libpwman.util.clearScreen
		self.__origReadPassphrase = libpwman.util.readPassphrase
		libpwman.util.clearScreen = self.__fakeClearScreen
		libpwman.util.readPassphrase = self.__fakeReadPassphrase

		import libpwman
		importlib.reload(libpwman.ui)
		self.dbFile = tempfile.NamedTemporaryFile(suffix="_" + self.id())
		self.dbFileSecondary = tempfile.NamedTemporaryFile(suffix="_secondary_" + self.id())
		print("DB file:", self.dbFile.name)
		print("Secondary DB file:", self.dbFileSecondary.name)
		shutil.copy(pathlib.Path("tests", "test_database_v1.db"),
			    self.dbFile.name)
		self.ui = libpwman.PWMan(filename=self.dbFile.name,
					 passphrase="test")

	def tearDown(self):
		try:
			self.dbFile.close()
		except FileNotFoundError as e:
			pass
		try:
			self.dbFileSecondary.close()
		except FileNotFoundError as e:
			pass

		self.dbFile = None
		self.dbFileSecondary = None

		import libpwman.util
		libpwman.util.clearScreen = self.__origClearScreen

	def test_base(self):
		import libpwman

		self.ui.do_help("")
		self.assertRaises(libpwman.PWMan.Quit,
				  lambda: self.ui.do_quit(""))
		self.ui.do_cls("")

		self.ui.do_database("") # No params: db listing.
		self.ui.do_database("main") # Re-select of main shall succeed.
		self.ui.do_commit("")
		self.ui.do_masterp("")
		self.ui.do_dbdump("")
		self.assertRaises(libpwman.PWMan.CommandError,
				  lambda: self.ui.do_dbimport("/does/not/exist"))
		self.ui.do_drop("")

		self.ui.do_list("")
		self.ui.do_list("testcat1 testtitle1 totpkey")
		self.ui.do_find("test")
		self.ui.do_totp("testcat1 testtitle1")
		self.ui.do_diff("")

		self.ui.do_new("a b c")
		self.ui.do_edit_user("a b c")
		self.ui.do_edit_pw("a b c")
		self.ui.do_edit_bulk("a b c")
		self.ui.do_edit_totp("a b GEZDGNAK 6 SHA1")
		self.ui.do_edit_attr("a b c d")
		self.ui.do_move("a b c d")
		self.ui.do_remove("c d")

	def test_multidb_closemain(self):
		import libpwman
		self.assertRaises(libpwman.PWMan.Quit,
				  lambda: self.ui.do_close(""))

	def test_multidb(self):
		import libpwman
		try:
			os.unlink(self.dbFileSecondary.name)
		except FileNotFoundError as e:
			pass

		# main db
		self.ui.do_database("main")
		self.ui.do_new("cat0 ent0 user0 pw0")
		self.ui.do_new("cat1 ent1 user1 pw1")
		self.ui.do_new("bigcat ent10 user10 pw10")
		self.ui.do_new("bigcat ent11 user11 pw11")
		self.ui.do_new("bigcat ent12 user12 pw12")
		self.ui.do_new("bigcat ent13 user13 pw13")
		self.ui.do_edit_bulk("cat0 ent0 bulk0")
		self.ui.do_edit_attr("cat0 ent0 attr0 attrdata0")
		self.ui.do_edit_totp("cat0 ent0 GEZDGNAK 6 SHA1")
		self.ui.do_edit_bulk("bigcat ent10 bulk10")
		self.ui.do_edit_attr("bigcat ent10 attr10 attrdata10")
		self.ui.do_edit_totp("bigcat ent10 GEZDGNAK 8 SHA256")
		# secondary db
		self.ui.do_database("-f %s secondary" % self.dbFileSecondary.name)
		self.ui.do_new("cat2 ent2 user2 pw2")

		# move

		self.ui.do_database("main")

		self.ui.do_move("-d secondary cat0 ent0 cat0")
		self.assertRaises(libpwman.PWMan.CommandError,
			lambda: self.ui.do_move("-d secondary cat0 ent0 cat0"))

		self.ui.do_move("-s secondary -d main cat2 ent2 cat2")
		self.assertRaises(libpwman.PWMan.CommandError,
			lambda: self.ui.do_move("-s secondary -d main cat2 ent2 cat2"))

		self.ui.do_move("-d secondary bigcat")
		self.assertRaises(libpwman.PWMan.CommandError,
			lambda: self.ui.do_move("-d secondary bigcat"))

		# copy

		self.ui.do_database("main")

		self.ui.do_copy("-s secondary -d main cat0 ent0 cat0copy ent0copy")
		self.ui.do_copy("-s secondary -d main bigcat bigcatcopy")

		# check data

		db = self.ui._getDb("main")
		# cat1/ent1
		entry = db.getEntry("cat1", "ent1")
		self.assertEqual(entry.user, "user1")
		self.assertEqual(entry.pw, "pw1")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr1")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)
		# cat2/ent2
		entry = db.getEntry("cat2", "ent2")
		self.assertEqual(entry.user, "user2")
		self.assertEqual(entry.pw, "pw2")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr2")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)
		# cat0copy/ent0copy
		entry = db.getEntry("cat0copy", "ent0copy")
		self.assertEqual(entry.user, "user0")
		self.assertEqual(entry.pw, "pw0")
		bulk = db.getEntryBulk(entry)
		self.assertEqual(bulk.data, "bulk0")
		attr = db.getEntryAttr(entry, "attr0")
		self.assertEqual(attr.name, "attr0")
		self.assertEqual(attr.data, "attrdata0")
		totp = db.getEntryTotp(entry)
		self.assertEqual(totp.key, "GEZDGNAK")
		self.assertEqual(totp.digits, 6)
		self.assertEqual(totp.hmacHash, "SHA1")
		# bigcatcopy/ent10
		entry = db.getEntry("bigcatcopy", "ent10")
		self.assertEqual(entry.user, "user10")
		self.assertEqual(entry.pw, "pw10")
		bulk = db.getEntryBulk(entry)
		self.assertEqual(bulk.data, "bulk10")
		attr = db.getEntryAttr(entry, "attr10")
		self.assertEqual(attr.name, "attr10")
		self.assertEqual(attr.data, "attrdata10")
		totp = db.getEntryTotp(entry)
		self.assertEqual(totp.key, "GEZDGNAK")
		self.assertEqual(totp.digits, 8)
		self.assertEqual(totp.hmacHash, "SHA256")
		# bigcatcopy/ent11
		entry = db.getEntry("bigcatcopy", "ent11")
		self.assertEqual(entry.user, "user11")
		self.assertEqual(entry.pw, "pw11")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr11")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)
		# bigcatcopy/ent12
		entry = db.getEntry("bigcatcopy", "ent12")
		self.assertEqual(entry.user, "user12")
		self.assertEqual(entry.pw, "pw12")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr12")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)
		# bigcatcopy/ent13
		entry = db.getEntry("bigcatcopy", "ent13")
		self.assertEqual(entry.user, "user13")
		self.assertEqual(entry.pw, "pw13")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr13")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)


		db = self.ui._getDb("secondary")
		# cat0/ent0
		entry = db.getEntry("cat0", "ent0")
		self.assertEqual(entry.user, "user0")
		self.assertEqual(entry.pw, "pw0")
		bulk = db.getEntryBulk(entry)
		self.assertEqual(bulk.data, "bulk0")
		attr = db.getEntryAttr(entry, "attr0")
		self.assertEqual(attr.name, "attr0")
		self.assertEqual(attr.data, "attrdata0")
		totp = db.getEntryTotp(entry)
		self.assertEqual(totp.key, "GEZDGNAK")
		self.assertEqual(totp.digits, 6)
		self.assertEqual(totp.hmacHash, "SHA1")
		# bigcat/ent10
		entry = db.getEntry("bigcat", "ent10")
		self.assertEqual(entry.user, "user10")
		self.assertEqual(entry.pw, "pw10")
		bulk = db.getEntryBulk(entry)
		self.assertEqual(bulk.data, "bulk10")
		attr = db.getEntryAttr(entry, "attr10")
		self.assertEqual(attr.name, "attr10")
		self.assertEqual(attr.data, "attrdata10")
		totp = db.getEntryTotp(entry)
		self.assertEqual(totp.key, "GEZDGNAK")
		self.assertEqual(totp.digits, 8)
		self.assertEqual(totp.hmacHash, "SHA256")
		# bigcat/ent11
		entry = db.getEntry("bigcat", "ent11")
		self.assertEqual(entry.user, "user11")
		self.assertEqual(entry.pw, "pw11")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr11")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)
		# bigcat/ent12
		entry = db.getEntry("bigcat", "ent12")
		self.assertEqual(entry.user, "user12")
		self.assertEqual(entry.pw, "pw12")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr12")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)
		# bigcat/ent13
		entry = db.getEntry("bigcat", "ent13")
		self.assertEqual(entry.user, "user13")
		self.assertEqual(entry.pw, "pw13")
		bulk = db.getEntryBulk(entry)
		self.assertIsNone(bulk)
		attr = db.getEntryAttr(entry, "attr13")
		self.assertIsNone(attr)
		totp = db.getEntryTotp(entry)
		self.assertIsNone(totp)


		self.ui.do_commit("-a")
bues.ch cgit interface