-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSystem.py
More file actions
180 lines (154 loc) · 5.62 KB
/
TestSystem.py
File metadata and controls
180 lines (154 loc) · 5.62 KB
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
from AccessSystem import getAccessControlMatrix, checkAccess, ROLES, getRoleAccess
from Main import init, LINE_DIVIDER
from PasswordSystem import loadUserInfo, verifyPassword, PASSWORD_FILE, REGEX_PATTERN, checkUsernameAvailability, isValidNewPassword, getUserRole
def accessControlTest():
"""
A test function for printing out the access control matrix and each role's access into the system.
"""
print("ACCESS CONTROL TEST")
acm = getAccessControlMatrix()
for row in acm:
print(row)
print()
for role in ROLES:
print("Checking a " + role.replace('_', ' ').lower() + "'s access: " + str(checkAccess(role)))
def passwordFileTest():
"""
A test function for loading into, verifying from, and checking the content of the password file.
PRECONDITION: the username 'johndoe' must be available.
"""
print("PASSWORD FILE TEST")
if not init():
print("Failed to get and create password file.")
return
fName = "John"
lName = "Doe"
uName = "johndoe"
pWord = "JD@Alpha01"
role = ROLES[0]
print("Attempting to load to the password file...")
if not loadUserInfo(fName, lName, uName, role, pWord):
print("Load failed!")
return
print("Load successful.")
print("Attempting to verify from the password file...")
if not verifyPassword(uName, pWord):
print("Verification failed!")
return
print("Verification successful.")
print("Attempting to verify password file content...")
check = False
with open(PASSWORD_FILE, 'r') as file:
for line in file:
content = REGEX_PATTERN.split(line.strip())
if(uName == content[0]):
if (content[1] == fName) and (content[2] == lName) and (content[3] == role):
check = True
if check:
print("Content check successful.")
else:
print("Content check failed!")
def registrationTest():
"""
A test function for registering a user.
PRECONDITION: the username 'janedoe' must NOT be available.
"""
print("REGISTRATION TEST")
if not init():
print("Failed to get and create password file.")
return
fName = "Jane"
lName = "Doe"
uName = "janedoe"
strongPswd = "JD@Beta02"
weakPswds = ["password", "Hello55", "Qw@1", "Qwerty@123456789", "Qaz123wsx"]
role = ROLES[0]
availability = checkUsernameAvailability(uName)
print("Username '" + uName + "' available: " + str(availability))
if not availability:
print("Registration cannot proceed because the username is already taken.")
return
print("Attempting to register user with weak passwords...")
for pswd in weakPswds:
if isValidNewPassword(uName, pswd):
if loadUserInfo(fName, lName, uName, role, pswd):
print("Registration of user with weak password '" + pswd + "' worked!")
return
else:
print("Registration of user with weak password '" + pswd + "' failed!")
print("Attempting to register user with strong password...")
if isValidNewPassword(uName, strongPswd):
if loadUserInfo(fName, lName, uName, role, strongPswd):
print("Registration of user with strong password '" + strongPswd + "' succeeded!")
else:
print("Registration of user with strong password '" + strongPswd + "' failed!")
print("Username '" + uName + "' available: " + str(checkUsernameAvailability(uName)))
def loginTest():
"""
A test function for logging in a user.
PRECONDITION: the username 'janedoe' must exist in the system.
"""
print("LOGIN TEST")
uName = "janedoe"
wrongPswd = "JD@Beta12"
rightPswd = "JD@Beta02"
print("Attempting to verify a wrong passowd...")
if verifyPassword(uName, wrongPswd):
print("Login attempt using wrong password succeeded!")
return
print("Login attempt using wrong password failed!")
print("Attempting to verify the right password...")
if not verifyPassword(uName, rightPswd):
print("Login attempt using correct password failed!")
return
print("Login attempt using correct password succeeded!")
print("Attempting to check user's access...")
role = getUserRole(uName)
if checkAccess(role):
print("Login successful!")
print(getRoleAccess(role))
else:
print("Login unsuccessful.")
def run():
"""
The run time sequence of the test.
"""
print("TESTING THE SYSTEM")
# get a command from the user and implement it
command = getCommand()
while(True):
print(LINE_DIVIDER)
if(command == 1):
accessControlTest()
elif(command == 2):
passwordFileTest()
elif(command == 3):
registrationTest()
elif(command == 4):
loginTest()
elif(command == 5):
print("You have exited the test, good bye.")
return
elif(command == 0):
print("ERROR: Invalid command, try again.")
print(LINE_DIVIDER)
command = getCommand()
def getCommand():
"""
Provide command options for the user.
If the user's input is not an int, an exception is thrown.
@return the selected command option, 0 for invalid input.
"""
print("COMMAND OPTIONS")
print("[1] Access Control Test")
print("[2] Password File Test")
print("[3] Registration Test")
print("[4] Login Test")
print("[5] Exit")
command = input("Choose a command by inputting the number in the bracket: ")
try:
return int(command)
except ValueError:
return 0
if __name__ == "__main__":
run()