-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
170 lines (121 loc) · 5.85 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
import os
import sys
import unittest
import json
from django.test.client import Client
client = {}
frontendData = {}
# Runs before each test
def globalSetup():
global client
# Set a new "client" every time
client = Client()
# Test NonExisting routes
class NonExistingTest( unittest.TestCase ):
# Runs before each test in this group
def setUp( self ):
globalSetup()
# Should return 404 error when calling a non-existing route
def test_unexisting_test_route( self ):
response = client.get( '/test' )
self.assertEqual( response.status_code, 404 )
# Test Start routes
class StartTest( unittest.TestCase ):
# Runs before each test in this group
def setUp( self ):
globalSetup()
# Should return 404 error when calling /start without the number of requested images
def test_start_no_number_of_images( self ):
response = client.get( '/start' )
self.assertEqual( response.status_code, 404 )
# Should return 200 when calling /start/5, the image and audio field names, image name, and image values
def test_start_correct( self ):
global frontendData
response = client.get( '/start/5' )
self.assertEqual( response.status_code, 200 )
data = json.loads( response.content )
self.assertIsNotNone( data['imageName'] )
self.assertIsNotNone( data['imageFieldName'] )
self.assertIsNotNone( data['audioFieldName'] )
self.assertIsNotNone( data['values'] )
self.assertTrue( len(data['imageName']) > 0 )
self.assertTrue( len(data['imageFieldName']) > 0 )
self.assertTrue( len(data['audioFieldName']) > 0 )
self.assertIsInstance( data['values'], list )
self.assertTrue( len(data['values']) > 0 )
self.assertIsNotNone( data['values'][0] )
# Test Audio routes
class AudioTest( unittest.TestCase ):
# Runs before each test in this group
def setUp( self ):
globalSetup()
# This request generates a valid visualCaptcha session
response = client.get( '/start/5' )
# Should return an mp3 audio file
def test_audio_mp3( self ):
response = client.get( '/audio' )
self.assertEqual( response.status_code, 200 )
self.assertEqual( response['Content-Type'], "{'Expires': 0, 'Content-Type': 'audio/mpeg', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate'}" )
# Should return an ogg audio file
def test_audio_ogg( self ):
response = client.get( '/audio/ogg' )
self.assertEqual( response.status_code, 200 )
self.assertEqual( response['Content-Type'], "{'Expires': 0, 'Content-Type': 'audio/ogg', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate'}" )
# Test Image routes
class ImageTest( unittest.TestCase ):
# Runs before each test in this group
def setUp( self ):
globalSetup()
# This request generates a valid visualCaptcha session
response = client.get( '/start/5' )
# Should return 404 error when calling /image without the index number
def test_image_no_index( self ):
response = client.get( '/image' )
self.assertEqual( response.status_code, 404 )
# Should return an image file
def test_image_zero( self ):
response = client.get( '/image/0' )
self.assertEqual( response.status_code, 200 )
self.assertEqual( response['Content-Type'], "{'Expires': 0, 'Content-Type': 'image/png', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate'}" )
# Should return another image file
def test_image_one( self ):
response = client.get( '/image/1' )
self.assertEqual( response.status_code, 200 )
self.assertEqual( response['Content-Type'], "{'Expires': 0, 'Content-Type': 'image/png', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate'}" )
# Should return a retina image file
def test_image_retina( self ):
response = client.get( '/image/1?retina=1' )
self.assertEqual( response.status_code, 200 )
self.assertEqual( response['Content-Type'], "{'Expires': 0, 'Content-Type': 'image/png', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate'}" )
# Should return 404 error when calling /image with a non-existing index_number
def test_image_invalid_index( self ):
response = client.get( '/image/100' )
self.assertEqual( response.status_code, 404 )
# Test Try routes
class TryTest( unittest.TestCase ):
# Runs before each test in this group
def setUp( self ):
global frontendData
globalSetup()
# This request generates a valid visualCaptcha session
response = client.get( '/start/5' )
# We need to store this to use it later
frontendData = json.loads( response.content )
# Should redirect to /?status=failedPost when no data is posted
def test_no_data( self ):
response = client.post( '/try', {} )
self.assertEqual( response.status_code, 302 )
self.assertEqual( response.url, 'http://testserver/?status=failedPost' )
# Should redirect to /?status=failedImage when captcha image fails
def test_invalid_image( self ):
response = client.post( '/try', { frontendData['imageFieldName']: 'definitely-wrong-image-answer' } )
self.assertEqual( response.status_code, 302 )
self.assertEqual( response.url, 'http://testserver/?status=failedImage' )
# Should redirect to /?status=failedAudio when captcha image fails
def test_invalid_audio( self ):
response = client.post( '/try', { frontendData['audioFieldName']: 'definitely-wrong-audio-answer' } )
self.assertEqual( response.status_code, 302 )
self.assertEqual( response.url, 'http://testserver/?status=failedAudio' )
if __name__ == '__main__':
print "Running unit tests"
unittest.main()