-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
577 lines (392 loc) · 17.4 KB
/
test.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import os
import unittest
from visualcaptcha import *
assetsFullPath = os.path.dirname(os.path.realpath(__file__)) + '/visualcaptcha/assets'
visualCaptcha = None
sessionMock = {}
# Runs before each test
def globalSetup():
global visualCaptcha, sessionMock
# Set a new "session" every time
sessionMock = Session({})
# Start visualCaptcha with that new session
visualCaptcha = Captcha(sessionMock)
# Helper function to check if an object exists in an array
def containsObject(theObject, array):
for element in array:
if theObject == element:
return True
return False
# Test Setup exceptions
class SetupTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return nothing if nothing is set
def test_session_nothing_set(self):
session = Session()
self.assertIsNone(session.get('test'))
# Should return something if something is set
def test_session_set(self):
session = Session()
session.set('test', 'awesome')
self.assertEqual(session.get('test'), 'awesome')
# Should allow an array of dicts to be used instead of the default images for options
def test_image_array(self):
global sessionMock
imageOptions = [{
'name': 'Test',
'path': 'test.png'
}]
visualCaptcha = Captcha(sessionMock, False, imageOptions)
obtainedImages = visualCaptcha.getAllImageOptions()
self.assertIsNotNone(obtainedImages)
self.assertEqual(len(obtainedImages), 1)
# Check the obtained image is the same we sent over
self.assertIsNotNone(obtainedImages[0]['name'])
self.assertIsNotNone(obtainedImages[0]['path'])
self.assertEqual(obtainedImages[0]['name'], 'Test')
self.assertEqual(obtainedImages[0]['path'], 'test.png')
# Should allow an array of dicts to be used instead of the default audios for options
def test_audio_array(self):
global sessionMock
audioOptions = [{
'path': 'test.mp3',
'value': 'test'
}]
visualCaptcha = Captcha(sessionMock, False, False, audioOptions)
obtainedAudios = visualCaptcha.getAllAudioOptions()
self.assertIsNotNone(obtainedAudios)
self.assertEqual(len(obtainedAudios), 1)
# Check the obtained audio is the same we sent over
self.assertIsNotNone(obtainedAudios[0]['path'])
self.assertIsNotNone(obtainedAudios[0]['value'])
self.assertEqual(obtainedAudios[0]['path'], 'test.mp3')
self.assertEqual(obtainedAudios[0]['value'], 'test')
# Test getAllImageOptions
class ImageOptionsTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return the list with all the possible image options
def test_all_image_options(self):
global visualCaptcha
imageOptions = visualCaptcha.getAllImageOptions()
self.assertIsNotNone(imageOptions)
# We should have at least 20 image options, so probability plays a role
self.assertTrue(len(imageOptions) > 19)
# Images need to have a name
self.assertIsNotNone(imageOptions[0]['name'])
# Images need to have a path
self.assertIsNotNone(imageOptions[0]['path'])
# Should find all the files for all the images, and their retina versions, making sure they're not empty
def test_find_all_images_and_retina(self):
global visualCaptcha, assetsFullPath
imageOptions = visualCaptcha.getAllImageOptions()
for imageOption in imageOptions:
currentImagePath = assetsFullPath + '/images/' + imageOption['path']
# Check the image file exists and is a file
self.assertTrue(os.path.isfile(currentImagePath))
# Check the image file is not empty
currentImageStat = os.stat(currentImagePath)
self.assertTrue(currentImageStat.st_size > 0)
# Check the retina image file exists and is a file
currentImagePath = currentImagePath.replace('.png', '@2x.png')
self.assertTrue(os.path.isfile(currentImagePath))
# Check the retina image file is not empty
currentImageStat = os.stat(currentImagePath)
self.assertTrue(currentImageStat.st_size > 0)
# Test getAllAudioOptions
class AudioOptionsTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return the list with all the possible audio options
def test_all_audio_options(self):
global visualCaptcha
audioOptions = visualCaptcha.getAllAudioOptions()
self.assertIsNotNone(audioOptions)
# We should have at least 20 audio options, so probability plays a role
self.assertTrue(len(audioOptions) > 19)
# Audios need to have a path
self.assertIsNotNone(audioOptions[0]['path'])
# Audios need to have a value
self.assertIsNotNone(audioOptions[0]['value'])
# Should find all the files for all the audios, and their .ogg versions, making sure they're not empty
def test_find_all_audios_and_ogg(self):
global visualCaptcha, assetsFullPath
audioOptions = visualCaptcha.getAllAudioOptions()
for audioOption in audioOptions:
currentAudioPath = assetsFullPath + '/audios/' + audioOption['path']
# Check the audio file exists and is a file
self.assertTrue(os.path.isfile(currentAudioPath))
# Check the audio file is not empty
currentAudioStat = os.stat(currentAudioPath)
self.assertTrue(currentAudioStat.st_size > 0)
# Check the ogg file exists and is a file
currentAudioPath = currentAudioPath.replace('.mp3', '.ogg')
self.assertTrue(os.path.isfile(currentAudioPath))
# Check the ogg audio file is not empty
currentAudioStat = os.stat(currentAudioPath)
self.assertTrue(currentAudioStat.st_size > 0)
# Test generate
class GenerateTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should generate a new valid image option each time it's called
def test_generate_new_image(self):
global visualCaptcha
# Generate first values
visualCaptcha.generate()
firstValue = visualCaptcha.getValidImageOption()['value']
# Generate second values
visualCaptcha.generate()
secondValue = visualCaptcha.getValidImageOption()['value']
# Check both values don't match
self.assertNotEqual(firstValue, secondValue)
# Should generate a new valid audio option each time it's called
def test_generate_new_audio(self):
global visualCaptcha
# Generate first values
visualCaptcha.generate()
firstValue = visualCaptcha.getValidAudioOption()['value']
# Generate second values
visualCaptcha.generate()
secondValue = visualCaptcha.getValidAudioOption()['value']
# Check both values don't match
self.assertNotEqual(firstValue, secondValue)
# Should generate new field names each time it's called
def test_generate_new_field_name(self):
global visualCaptcha
# Generate first values
visualCaptcha.generate()
firstImageValue = visualCaptcha.getFrontendData()['imageFieldName']
firstAudioValue = visualCaptcha.getFrontendData()['audioFieldName']
# Generate second values
visualCaptcha.generate()
secondImageValue = visualCaptcha.getFrontendData()['imageFieldName']
secondAudioValue = visualCaptcha.getFrontendData()['audioFieldName']
# Check both values don't match
self.assertNotEqual(firstImageValue, secondImageValue)
self.assertNotEqual(firstAudioValue, secondAudioValue)
# Should generate frontend data
def test_generate_frontend_data(self):
global visualCaptcha
# Generate values
visualCaptcha.generate()
frontendData = visualCaptcha.getFrontendData()
self.assertIsNotNone(frontendData['values'])
self.assertIsNotNone(frontendData['imageName'])
self.assertIsNotNone(frontendData['imageFieldName'])
self.assertIsNotNone(frontendData['audioFieldName'])
self.assertIsInstance(frontendData['values'], list)
self.assertTrue(len(frontendData['imageName']) > 0)
self.assertTrue(len(frontendData['imageFieldName']) > 0)
self.assertTrue(len(frontendData['audioFieldName']) > 0)
# Should generate new frontend data each time it's called
def test_generate_new_frontend_data(self):
global visualCaptcha
# Generate first values
visualCaptcha.generate()
firstValue = visualCaptcha.getFrontendData()
# Generate second values
visualCaptcha.generate()
secondValue = visualCaptcha.getFrontendData()
# Check both values don't match
self.assertNotEqual(firstValue, secondValue)
# Test validateImage
class ValidateImageTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return true if the chosen image option is the valid one
def test_return_true_image(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
optionValue = visualCaptcha.getValidImageOption()['value']
# Validate, sending the right optionValue, expecting a "true" to be returned
self.assertTrue(visualCaptcha.validateImage(optionValue))
# Should return false if the chosen image option is not the valid one
def test_return_false_image(self):
global visualCaptcha
# Will never match the optionValue
optionValue = random.random()
# Generate option values
visualCaptcha.generate()
# Validate, sending the right optionValue, expecting a "false" to be returned
self.assertFalse(visualCaptcha.validateImage(optionValue))
# Test validateAudio
class ValidateAudioTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return true if the chosen audio option is the valid one
def test_return_true_audio(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
optionValue = visualCaptcha.getValidAudioOption()['value']
# Validate, sending the right optionValue, expecting a "true" to be returned
self.assertTrue(visualCaptcha.validateAudio(optionValue))
# Should return false if the chosen audio option is not the valid one
def test_return_false_audio(self):
global visualCaptcha
# Will never match the optionValue
optionValue = random.random()
# Generate option values
visualCaptcha.generate()
# Validate, sending the right optionValue, expecting a "false" to be returned
self.assertFalse(visualCaptcha.validateAudio(optionValue))
# Test getImageOptions
class GetImageOptionsTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return a list with possible image options and the valid image option included
def test_return_list_images(self):
global visualCaptcha
foundValidOptions = 0
numberOfOptions = 4
# Generate option values (4)
visualCaptcha.generate(numberOfOptions)
# Get the options list
options = visualCaptcha.getImageOptions()
optionsLength = len(options)
self.assertEqual(optionsLength, numberOfOptions)
# Loop through all options, and count each time an image was successfully validated
for option in options:
validationResult = visualCaptcha.validateImage(option['value'])
if (validationResult):
foundValidOptions += 1
# We should only find 1 valid option
self.assertEqual(foundValidOptions, 1)
# Should return a list with all possible image options different
def test_return_unique_list(self):
global visualCaptcha
foundSimilarOptions = 0
numberOfOptions = 4
# Generate option values (4)
visualCaptcha.generate(numberOfOptions)
# Get the options list
options = visualCaptcha.getImageOptions()
optionsLength = len(options)
self.assertEqual(optionsLength, numberOfOptions)
# Loop through all options, and count each time a duplicate value exists in the array
for i in range(0, optionsLength, 1):
for j in range(0, optionsLength, 1):
if (i != j and options[i]['value'] == options[j]['value']):
foundSimilarOptions += 1
# We should find no equal options
self.assertEqual(foundSimilarOptions, 0)
# Should return the same current image options list, if we don't generate a new valid value
def test_return_same_list(self):
global visualCaptcha
numberOfOptions = 4
# Generate option values (4)
visualCaptcha.generate(numberOfOptions)
# Get the first options list
firstOptions = visualCaptcha.getImageOptions()
# Get the second options list
secondOptions = visualCaptcha.getImageOptions()
# Get the third options list
thirdOptions = visualCaptcha.getImageOptions()
# Loop through all options, and test each time that the same object exists in both the other arrays
for i in range(0, numberOfOptions, 1):
# Check if all the values match
self.assertEqual(firstOptions[i]['value'], secondOptions[i]['value'])
self.assertEqual(firstOptions[i]['value'], thirdOptions[i]['value'])
# Test getAudioOption
class GetAudioOptionTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should return the current audio option, which should be the valid audio option
def test_return_valid_audio(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Get the option
option = visualCaptcha.getAudioOption()
# Check if the audio was successfully validated
self.assertTrue(visualCaptcha.validateAudio(option['value']))
# Should return the same audio option, if we don't generate a new valid value
def test_return_same_audio(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Get the first option
firstOption = visualCaptcha.getAudioOption()
# Get the second option
secondOption = visualCaptcha.getAudioOption()
# Get the third option
thirdOption = visualCaptcha.getAudioOption()
# Check if all the values match
self.assertEqual(firstOption['value'], secondOption['value'])
self.assertEqual(firstOption['value'], thirdOption['value'])
# Test streamImage
class StreamImageTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should find and stream an image file
def test_stream_image(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Stream the image
fileReturn = visualCaptcha.streamImage({}, 0)
# Check if the image was successfully streamed
self.assertTrue(fileReturn)
# Should find and stream a retina image file
def test_stream_retina(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Stream the retina image
fileReturn = visualCaptcha.streamImage({}, 0, True)
# Check if the image was successfully streamed
self.assertTrue(fileReturn)
# Should fail to find an image file
def test_stream_undefined_index(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Stream the image
fileReturn = visualCaptcha.streamImage({}, 100)
# Check if the image failed streaming
self.assertFalse(fileReturn)
# Test streamAudio
class StreamAudioTest(unittest.TestCase):
# Runs before each test in this group
def setUp(self):
globalSetup()
# Should find and stream an audio file - mp3
def test_stream_mp3(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Stream the mp3 audio file
fileReturn = visualCaptcha.streamAudio({})
# Check if the audio file was successfully streamed
self.assertTrue(fileReturn)
# Should find and stream an audio file - ogg
def test_stream_ogg(self):
global visualCaptcha
# Generate option values
visualCaptcha.generate()
# Stream the ogg audio file
fileReturn = visualCaptcha.streamAudio({}, 'ogg')
# Check if the audio file was successfully streamed
self.assertTrue(fileReturn)
# Should fail to find an audio file
def test_stream_ungenerated_audio(self):
global visualCaptcha
# Stream the audio file (we didn't generate options, so it should fail)
fileReturn = visualCaptcha.streamAudio({})
# Check if the audio failed streaming
self.assertFalse(fileReturn)
if __name__ == '__main__':
print("Running unit tests")
unittest.main()