-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremotetest.py
executable file
·241 lines (182 loc) · 9.29 KB
/
remotetest.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
import httplib2
import argparse
import sys
from colorama import init
from termcolor import colored
from StringIO import StringIO
import re as regex
import json
from json import load
"""
remotetest
Script for remote testing for the TopHat server.
"""
__author__ = "Ian Connolly ([email protected], [email protected]) and Kevin Bluett ([email protected], [email protected])"
__license__ = "MIT"
__version__ = "0.0.1"
# Login, API token, Create/Delete user, Create/Delete game, Time requests for user data, game data etc.
# Try to break server... Bad HTTP, bad JSON
SANITY_URL = "http://www.google.com/ "
init()
parser = argparse.ArgumentParser(description="Test suite for TopHat server")
parser.add_argument("-s", "--server", help="MANDATORY: Server on which TopHat platform is running on", dest="server")
parser.add_argument("-u", "--user", help="Provide username to login with", dest="user", default="testuser")
parser.add_argument("-p", "--password", help="Provide password to login with", dest="password", default="test")
parser.add_argument("--noverify", help="Turn off SSL validation", action="store_true")
parser.add_argument("-t", "--testpath", help="Provide path from root of test request, defaults to jsontest/", default="jsontest/", dest="testpath")
parser.add_argument("-j", "--json", help="Provide sample json data for Hello, World test. Defaults to platform default", default='\r{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}\n', dest="test_json")
args = parser.parse_args()
def main ():
testcount = 0
successful = 0
failed = 0
if args.server is None:
print colored("\nYou must supply a server to test!!", "red")
print "\nExpected server URL to be supplied"
print "Closing....\n"
sys.exit(-1)
print colored("\nPARAMS:", "blue")
print "Server: " + colored(args.server, "blue")
print "Username: " + colored(args.user, "blue")
print "Password: " + colored(args.password, "blue")
print "\n\n\n"
h = httplib2.Http()
try:
(resp_headers, content) = h.request(SANITY_URL, "GET") # sanity check
except:
print colored("FAILED: Sanity check", "red") + ": " + "Could not connect to " + SANITY_URL + ", check your internet connection"
print "Closing...."
sys.exit(-1)
if args.noverify:
h = httplib2.Http(disable_ssl_certificate_validation=True)
else:
h = httplib2.Http()
# Hello world test
print "TEST " + colored(testcount, "blue") + ": " + "Attempting 'Hello, world' test..."
try:
(resp_headers, content) = h.request(args.server + args.testpath, "GET")
if content.rstrip() in args.test_json.rstrip() or content.rstrip() in '{"message": "Test Call completed successfully", "error_code": 200}':
print "TEST " + colored(testcount, "blue") + ": " + colored("Successful", "green")
successful += 1
else:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Returned JSON does not equal sample JSON", "red")
print "\tReturned JSON: " + colored(content, "yellow")
failed = failed + 1
except:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored(sys.exc_info()[:2], "red")
failed = failed + 1
testcount = testcount +1
############# TEST #############
print "\nTEST " + colored(testcount, "blue") + ": " + "Attempting to get API version from server..."
try:
(resp_headers, content) = h.request(args.server + "version" + "/", "GET", '{ "os":"android", "version":"2.3.5","appversion":"0.1"}')
if resp_headers.status == 200:
# TO DO: How does server return API versions?
mapped = load(StringIO(content))
print "TEST " + colored(testcount, "blue") + ": " + colored("Successful", "green")
successful += 1
if 'version' in mapped:
print "\tServer Version: " + colored(mapped['version'], "yellow")
elif resp_headers.status >= 400 and resp_headers.status < 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server did not understand API version request", "red")
failed = failed + 1
elif resp_headers.status >= 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server had encountered internal server", "red")
failed = failed + 1
except:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored(sys.exc_info()[:2], "red")
failed = failed + 1
testcount = testcount +1
############# TEST ############
print "\nTEST " + colored(testcount, "blue") + ": " + "Attempting to get an anonymous API key from server..."
try:
(resp_headers, content) = h.request(args.server + "apitokens" + "/", "POST", 'data={}', headers={'content-type':'application/x-www-form-urlencoded'})
if resp_headers.status == 201:
# TO DO: How does server return API versions?
mapped = load(StringIO(content))
print "TEST " + colored(testcount, "blue") + ": " + colored("Successful", "green")
if 'apitoken' in mapped:
print "\tAPI Token: " + colored(mapped['apitoken'], "yellow")
successful += 1
elif resp_headers.status >= 400 and resp_headers.status < 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server did not understand API version request", "red")
failed = failed + 1
elif resp_headers.status >= 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server had encountered internal server", "red")
failed = failed + 1
except:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored(sys.exc_info()[:2], "red")
failed = failed + 1
testcount = testcount +1
############# TEST ############
print "\nTEST " + colored(testcount, "blue") + ": " + "Attempting to get user API key from server..."
try:
(resp_headers, content) = h.request(args.server + "apitokens" + "/", "POST", 'data={"username":"'+args.user+'", "password":"'+args.password+'" }', headers={'content-type':'application/x-www-form-urlencoded'})
if resp_headers.status == 201:
# TO DO: How does server return API versions?
mapped = load(StringIO(content))
print "TEST " + colored(testcount, "blue") + ": " + colored("Successful", "green")
if 'apitoken' in mapped:
print "\tAPI Token: " + colored(mapped['apitoken'], "yellow")
successful += 1
elif resp_headers.status >= 400 and resp_headers.status < 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server did not understand API key request", "red")
mapped = load(StringIO(content))
print "\tError Message: " + colored(mapped['error_message'], "yellow")
failed = failed + 1
elif resp_headers.status >= 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server had encountered internal server", "red")
mapped = load(StringIO(content))
print "\tError Message: " + colored(mapped['error_message'], "yellow")
failed = failed + 1
except:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored(sys.exc_info()[:2], "red")
failed = failed + 1
testcount = testcount +1
############# TEST ############
print "\nTEST " + colored(testcount, "blue") + ": " + "Attempting to get a game from server..."
try:
(resp_headers, content) = h.request(args.server + "games" + "/1", "GET", '')
if resp_headers.status == 200:
# TO DO: How does server return API versions?
if content == '{"game_type_id": 1, "name": "Testing Assasin", "creator": {"id": 1, "name": "Kevin Baker"}, "game_type": "Assassin", "time": "2012-06-05 12:12:30", "id": 1}':
mapped = load(StringIO(content))
print "TEST " + colored(testcount, "blue") + ": " + colored("Successful", "green")
if 'name' in mapped:
print "\tGame Loaded: " + colored(mapped['name'], "yellow")
successful += 1
else:
print "TEST " + colored(testcount, "blue") + ": " + colored("Successful", "red")
print "\tGame Error: " + colored(content, "yellow")
failed = failed + 1
elif resp_headers.status >= 400 and resp_headers.status < 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server did not understand API game versions request", "red")
print "\tERROR: " + colored(content, "yellow")
failed = failed + 1
elif resp_headers.status >= 500:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored("Server had encountered internal server", "red")
failed = failed + 1
except:
print"TEST " + colored(testcount, "blue") + ": " + colored("Failed", "red")
print "\tREASON: " + colored(sys.exc_info()[:2], "red")
failed = failed + 1
testcount = testcount +1
print "\n\n === TESTS COMPLETE ===\n"
print "Tests ran: " + colored(testcount, "blue")
print "Successful: " + colored(successful, "green")
print "Failed: " + colored(failed, "red")
if __name__ == '__main__':
main()