-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_account.py
124 lines (98 loc) · 4.37 KB
/
test_account.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
from main_window import*
from account_windows import*
import sys
import os
import shutil
from PyQt5.QtWidgets import QApplication
import unittest
app = QApplication(sys.argv)
class TestPermission(unittest.TestCase):
# default variables, setup skeleton of app to run tests in
def setUp(self):
if not os.path.exists('users/test'):
os.makedirs('users/test')
first = False
# .note_accounts needs to exist and contain test username and password
try:
f = open('.note_accounts', 'r')
first = True
except IOError:
f = open('.note_accounts', 'w')
f.write(encrypt('test') + ' ' + encrypt('password') +
' ' + encrypt('[email protected]') + encrypt('\n'))
f.close()
self.main_window = MainWindow()
self.main_window.user = 'test'
self.login_window = LoginWindow()
self.password_window = ChangePasswordWindow()
self.password_window.user = 'test'
self.delete_window = DeleteAccountWindow()
self.delete_window.user = 'test'
self.delete_window.main_window = self.main_window
if first:
f.close()
# test that check_permission() correctly matches user with their permissions
# also essentially tests add_permission()
def testCheckPermission(self):
# user starts out without access to text.txt
self.assertFalse(check_permission(
self.main_window.user, 'users/test/test.txt'))
# add the permission
add_permission(self.main_window.user, 'users/test/test.txt')
# user now has access so check_permission returns 'True'
self.assertTrue(check_permission(
self.main_window.user, 'users/test/test.txt'))
# test that change_password() changes the user's password
def testChangePassword(self):
# set all variables normally set by a QEditLine input box
old = 'password'
new = 'newpassword'
match = 'newpassword'
self.login_window.lineEdit_username.setText('test')
self.login_window.lineEdit_password.setText('password')
self.password_window.lineEdit_oldpassword.setText(old)
self.password_window.lineEdit_newpassword.setText(new)
self.password_window.lineEdit_reenterpassword.setText(match)
# username and password combo set above is correct
self.assertEqual(self.login_window.check_credentials(), 'True')
# change password from what was set above
self.password_window.change_password()
# password has changed, so username and password combo from above no longer works
self.assertEqual(self.login_window.check_credentials(), 'Wrong pass')
# set password variable equal to the newly changed password
self.login_window.lineEdit_password.setText('newpassword')
# username and new password combo work
self.assertEqual(self.login_window.check_credentials(), 'True')
# test that deleting an account also deletes the account's note directory
def testDeleteAccountDirectory(self):
# parameter given to delete_account() must have text() == '&Yes' to trigger deleting the directory
choice = QMessageBox()
choice.setText('&Yes')
# directory exists before call
self.assertTrue(os.path.exists('users/test'))
# delete the account and directory
self.delete_window.delete_account(choice)
# directory no longer exists
self.assertFalse(os.path.exists('users/test'))
# test encryption and decryption of file contents (string)
def testEncryptDecryptFile(self):
original = 'this is a test'
# encrypt the string
encrypted = encrypt_file(original)
# encrypted string should not match the original
self.assertNotEqual(original, encrypted)
# decrypt the string
decrypted = decrypt_file(encrypted)
# decrypted string should match the original
self.assertEqual(original, decrypted)
# cleans up leftover files/directories from tests
def tearDown(self):
if os.path.exists('users/test'):
shutil.rmtree('users/test')
if os.path.exists('.permissions'):
os.remove('.permissions')
if os.path.exists('.note_accounts'):
os.remove('.note_accounts')
if os.path.exists('.key'):
os.remove('.key')
return