-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
278 lines (197 loc) · 9.51 KB
/
tests.py
File metadata and controls
278 lines (197 loc) · 9.51 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
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
import unittest
import mysql.connector
from dotenv import load_dotenv
import os
import random as rand
from frontend.input_validation import val_check_balance, val_deposit, val_withdraw, val_create_account, val_delete_account, val_modify_account, val_get_account
from backend.sql_functions import check_balance, deposit, withdraw, create_account, delete_account, modify_account, get_credentials, get_accounts, get_account
from backend.authentication import authenticate
from backend.utils import hash_salt
'''
Contains unit testing for various functions.
Most importantly, tests functions that connect to SQL database.
Run `python .\tests.py` in order to check against all tests!
'''
class ValidationTestCases(unittest.TestCase):
def test_val_check_balance(self):
self.assertFalse(val_check_balance(-1))
self.assertFalse(val_check_balance(0))
self.assertFalse(val_check_balance(rand.randrange(-10000, 0)))
self.assertTrue(val_check_balance(1))
self.assertTrue(val_check_balance(rand.randrange(2, 10000)))
def test_val_deposit(self):
self.assertFalse(val_deposit(-1, 1))
self.assertFalse(val_deposit(0, 1))
self.assertFalse(val_deposit(rand.randrange(-10000, 0), 1))
self.assertTrue(val_deposit(1, 1))
self.assertTrue(val_deposit(rand.randrange(2, 10000), 1))
self.assertFalse(val_deposit(1, -1))
self.assertFalse(val_deposit(1, rand.randrange(-10000, -1)))
self.assertTrue(val_deposit(1, 1))
self.assertTrue(val_deposit(1, 0))
self.assertTrue(val_deposit(rand.randrange(1, 10000), rand.randrange(0, 10000)))
self.assertFalse(val_deposit(1, -0.5))
self.assertTrue(val_deposit(1, 0.5))
def test_val_withdraw(self):
self.assertFalse(val_withdraw(-1, 1))
self.assertFalse(val_withdraw(0, 1))
self.assertFalse(val_withdraw(rand.randrange(-10000, 0), 1))
self.assertTrue(val_withdraw(1, 1))
self.assertTrue(val_withdraw(rand.randrange(2, 10000), 1))
self.assertFalse(val_withdraw(1, -1))
self.assertFalse(val_withdraw(1, rand.randrange(-10000, -1)))
self.assertTrue(val_withdraw(1, 1))
self.assertTrue(val_withdraw(1, 0))
self.assertTrue(val_withdraw(rand.randrange(1, 10000), rand.randrange(0, 10000)))
self.assertFalse(val_withdraw(1, -0.5))
self.assertTrue(val_withdraw(1, 0.5))
def test_val_create_account(self):
self.assertFalse(val_create_account(1, 'password123*', 0))
self.assertFalse(val_create_account('Test Schmo', 1, 0))
self.assertFalse(val_create_account('Test Schmo', 'password123*', 'user'))
self.assertFalse(val_create_account('Test Schmo', 'password123*', 2))
self.assertFalse(val_create_account('Test Schmo', 'password123*', 1.0))
self.assertTrue(val_create_account('Test Schmo', 'password123*', 0))
self.assertTrue(val_create_account('Test Schmo', 'password123*', 1))
self.assertTrue(val_create_account('Test Schmo', 'password123*', True))
self.assertTrue(val_create_account('Test Schmo', 'password123*', False))
def test_val_delete_account(self):
self.assertFalse(val_delete_account(-1))
self.assertFalse(val_delete_account(0))
self.assertFalse(val_delete_account(rand.randrange(-10000, 0)))
self.assertTrue(val_delete_account(1))
self.assertTrue(val_delete_account(rand.randrange(2, 10000)))
def test_val_modify_account(self):
self.assertFalse(val_modify_account(-1, 'Test Schmo', 'password123*'))
self.assertFalse(val_modify_account(0, 'Test Schmo', 'password123*'))
self.assertFalse(val_modify_account(-1, 'Test Schmo'))
self.assertFalse(val_modify_account(0, 'Test Schmo'))
self.assertFalse(val_modify_account(-1, username='Test Schmo'))
self.assertFalse(val_modify_account(0, username='Test Schmo'))
self.assertFalse(val_modify_account(-1, password='password123*'))
self.assertFalse(val_modify_account(0, password='password123*'))
self.assertFalse(val_modify_account(1, 1, 'password123*'))
self.assertFalse(val_modify_account(1, 1))
self.assertFalse(val_modify_account(1, username=1))
self.assertFalse(val_modify_account(1, 'Test Schmo', 1))
self.assertFalse(val_modify_account(1, password=1))
self.assertTrue(val_modify_account(1, 'Test Schmo', 'password123*'))
self.assertTrue(val_modify_account(1, 'Test Schmo'))
self.assertTrue(val_modify_account(1, username='Test Schmo'))
self.assertTrue(val_modify_account(1, password='password123*'))
def test_val_get_account(self):
self.assertFalse(val_check_balance(-1))
self.assertFalse(val_check_balance(0))
self.assertFalse(val_check_balance(rand.randrange(-10000, 0)))
self.assertTrue(val_check_balance(1))
self.assertTrue(val_check_balance(rand.randrange(2, 10000)))
class SQLTestCases(unittest.TestCase):
def setUp(self):
load_dotenv()
self.connection = mysql.connector.connect( \
user=os.environ.get('SQL_USER'), \
database=os.environ.get('SQL_DATABASE'), \
password=os.environ.get('SQL_PASSWORD'), \
)
self.cursor = self.connection.cursor()
def test_create_delete_user_account(self):
acc = create_account('Test Schmo', 'password123*', 0)
self.assertEqual(acc[1:], ('Test Schmo', hash_salt('password123*'), 0.0, 0))
del_acc = delete_account(acc[0])
self.assertEqual(acc, del_acc)
def test_create_delete_admin_account(self):
acc = create_account('Test Schmo', 'password123*', 1)
self.assertEqual(acc[1:], ('Test Schmo', hash_salt('password123*'), 0.0, 1))
del_acc = delete_account(acc[0])
self.assertEqual(acc, del_acc)
def test_modify_account(self):
orig_acc = create_account('Test Schmo', 'password123*', 0)
mod_1_acc = modify_account(orig_acc[0], username='Test Schme')
self.assertEqual(orig_acc[:1] + orig_acc[2:], mod_1_acc[:1] + mod_1_acc[2:])
self.assertNotEqual(orig_acc[1], mod_1_acc[1])
mod_2_acc = modify_account(orig_acc[0], password='password234*')
self.assertEqual(mod_1_acc[:2] + mod_1_acc[3:], mod_2_acc[:2] + mod_2_acc[3:])
self.assertNotEqual(mod_1_acc[2], mod_2_acc[2])
mod_3_acc = modify_account(orig_acc[0], username='Test Schma', password='password345*')
self.assertEqual(mod_2_acc[:1] + mod_2_acc[3:], mod_3_acc[:1] + mod_3_acc[3:])
self.assertNotEqual(mod_2_acc[1], mod_3_acc[1])
self.assertNotEqual(mod_2_acc[2], mod_3_acc[2])
mod_4_acc = modify_account(orig_acc[0])
self.assertIsNone(mod_4_acc)
delete_account(orig_acc[0])
def test_check_balance(self):
acc = create_account('Test Schmo', 'password123*', 0)
ret_bal = check_balance(acc[0])
self.assertEqual(0, ret_bal)
delete_account(acc[0])
def test_deposit(self):
acc = create_account('Test Schmo', 'password123*', 0)
deposit(acc[0], (dep := rand.randrange(0, 100000)))
self.assertEqual(dep, check_balance(acc[0]))
delete_account(acc[0])
def test_withdraw(self):
acc = create_account('Test Schmo', 'password123*', 0)
deposit(acc[0], (dep := rand.randrange(1000, 10000)))
ret = withdraw(acc[0], (wit := rand.randrange(10, 900)))
self.assertEqual(dep - wit, check_balance(acc[0]))
self.assertEqual(dep - wit, ret)
delete_account(acc[0])
def test_withdraw_fail(self):
acc = create_account('Test Schmo', 'password123*', 0)
deposit(acc[0], (dep := rand.randrange(100, 900)))
ret = withdraw(acc[0], (wit := rand.randrange(1000, 100000)))
self.assertEqual(ret, dep - wit)
self.assertEqual(dep, check_balance(acc[0]))
delete_account(acc[0])
def test_get_credentials(self):
acc = create_account('Test Schmo', 'password123*', 0)
ret = get_credentials('Test Schmo')
self.assertEqual([(acc[0], acc[2], 0)], ret)
delete_account(acc[0])
def test_get_accounts(self):
acc = create_account('Test Schmo', 'password123*', 0)
ret = get_accounts()
self.assertIn((acc[0], acc[1], 0, acc[4]), ret)
delete_account(acc[0])
def test_get_account(self):
acc = create_account('Test Schmo', 'password123*', 0)
ret = get_account(acc[0])
self.assertEqual((acc[0], acc[1], 0, acc[4]), ret)
delete_account(acc[0])
def tearDown(self):
self.cursor.close()
self.connection.close()
class AutheticationTestCases(unittest.TestCase):
def setUp(self):
self.acc1 = create_account('Test Schmo', 'password123*', 0)
self.acc2 = create_account('Test Schme', 'password234*', 0)
self.acc3 = create_account('Test Schma', 'password123*', 0)
self.acc4 = create_account('Test Schmo', 'password234*', 0)
self.acc5 = create_account('Test Schmo', 'password456*', 1)
def test_correct_auth(self):
ret = authenticate('Test Schmo', 'password123*')
self.assertEqual(ret, (self.acc1[0], 0))
def test_incorrect_auth(self):
ret = authenticate('Test Schme', 'password123*')
self.assertEqual(ret, -1)
def test_auth_user_collision(self):
ret1 = authenticate('Test Schmo', 'password123*')
ret2 = authenticate('Test Schmo', 'password234*')
self.assertEqual(ret1, (self.acc1[0], 0))
self.assertEqual(ret2, (self.acc4[0], 0))
def test_auth_pass_collision(self):
ret1 = authenticate('Test Schmo', 'password123*')
ret2 = authenticate('Test Schma', 'password123*')
self.assertEqual(ret1, (self.acc1[0], 0))
self.assertEqual(ret2, (self.acc3[0], 0))
def test_auth_admin(self):
ret = authenticate('Test Schmo', 'password456*')
self.assertEqual(ret, (self.acc5[0], 1))
def tearDown(self):
delete_account(self.acc1[0])
delete_account(self.acc2[0])
delete_account(self.acc3[0])
delete_account(self.acc4[0])
delete_account(self.acc5[0])
if __name__ == '__main__':
unittest.main()