-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.py.old
386 lines (314 loc) · 13.9 KB
/
tests.py.old
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
import json
import unittest
import os
import time
import server
import main
import routes.xuid
class Data():
xuid_valid = {
"gamertag": "x11net",
"xuid": "2535453932267307"
}
xuid_invalid = {
"error": 404,
"gamertag": " ",
"message": "user not found"
}
titleidstats_params = {
# jcxLDN has stats for Forza Motorsport 6
"gamertag": "jcxLDN",
"xuid": "2535469727531765",
"titleID": "1694054782"
}
titleidstats_params_nostats = {
# x11net does not have stats for Forza Motorsport 6
"gamertag": "x11net",
"xuid": "2535453932267307"
}
class BaseTest(unittest.TestCase):
def tearDown(self):
# After every test, sleep 0.5 seconds to hopefully avoid 429 responses. (Too Many Requests)
time.sleep(0.5)
def printCurrentTest(self):
# print(": " + self.id().split('.')[-1])
print("\n" + self.id().split('__.')[1])
def assertIs200(self, req):
return self.assertEqual(req.status_code, 200)
def assertIsJSON(self, req):
return self.assertEqual(req.mimetype, "application/json")
def setUp(self):
server.app.config["TESTING"] = True
self.app = server.app.test_client()
self.printCurrentTest()
class TestXuid(BaseTest):
def test_xuid(self):
with server.app.test_request_context():
req = self.app.get("/xuid/x11net")
# self.assertEqual(data["xuid"], req.json)
self.assertDictEqual(Data.xuid_valid, req.json)
def test_xuid_raw(self):
with server.app.test_request_context():
req = self.app.get(
"/xuid/{}/raw".format(Data.xuid_valid["gamertag"]))
self.assertEqual(int(Data.xuid_valid["xuid"]), int(req.data))
self.assertIs200(req)
def test_xuid_invalid(self):
with server.app.test_request_context():
req = self.app.get("/xuid/%20")
self.assertEqual(req.status_code, 404)
self.assertEqual(req.mimetype, "application/json")
self.assertDictEqual(Data.xuid_invalid, req.json)
class TestProfile(BaseTest):
def assertIsValidProfileUsersResponse(self, req):
self.assertIs200(req)
self.assertIsJSON(req)
self.assertIn("profileUsers", req.json)
self.assertEqual(req.json["profileUsers"]
[0]["id"], Data.xuid_valid["xuid"])
def test_xuid(self):
with server.app.test_request_context():
req = self.app.get(
"/profile/xuid/{}".format(Data.xuid_valid["xuid"]))
self.assertIsValidProfileUsersResponse(req)
def test_gamertag(self):
with server.app.test_request_context():
req = self.app.get(
"/profile/gamertag/{}".format(Data.xuid_valid["gamertag"]))
self.assertIsValidProfileUsersResponse(req)
def test_xuid_invalid_length(self):
with server.app.test_request_context():
req = self.app.get("/profile/xuid/123")
self.assertEqual(req.status_code, 400)
self.assertEqual(req.mimetype, "application/json")
self.assertDictEqual(
{"error": 400, "message": "invalid xuid"}, req.json)
def test_xuid_invalid_user(self):
with server.app.test_request_context():
req = self.app.get("/profile/xuid/2584878536129842")
self.assertEqual(req.status_code, 404)
self.assertEqual(req.mimetype, "application/json")
self.assertDictEqual(
{"error": 404, "message": "user not found"}, req.json)
def test_gamertag_invalid(self):
with server.app.test_request_context():
req = self.app.get("/profile/gamertag/%20")
self.assertEqual(req.status_code, 404)
self.assertEqual(req.mimetype, "application/json")
self.assertDictEqual(Data.xuid_invalid, req.json)
def test_xuid_settings(self):
with server.app.test_request_context():
req = self.app.get(
"/profile/settings/xuid/{}".format(Data.xuid_valid["xuid"]))
self.assertIsValidProfileUsersResponse(req)
def test_gamertag_settings(self):
with server.app.test_request_context():
req = self.app.get(
"/profile/settings/gamertag/{}".format(Data.xuid_valid["gamertag"]))
self.assertIsValidProfileUsersResponse(req)
class TestUserStats(BaseTest):
def assertTitleIdMatchesOriginalRequest(self, req):
return self.assertEqual(req.json["groups"][0]["titleid"],
Data.titleidstats_params["titleID"])
def assertXuidMatchesOriginalRequest(self, req, params):
return self.assertEqual(req.json["groups"][0]["statlistscollection"]
[0]["arrangebyfieldid"], params["xuid"])
def assertIsValidTitleIdResponse(self, req):
self.assertIs200(req)
self.assertIsJSON(req)
# TitleID matches original request
self.assertTitleIdMatchesOriginalRequest(req)
# XUID matches original request
self.assertXuidMatchesOriginalRequest(req, Data.titleidstats_params)
# User has stats in the game
self.assertIn("value", req.json["groups"]
[0]["statlistscollection"][0]["stats"][0])
def assertIsValidTitleIdResponseNoStats(self, req):
self.assertIs200(req)
self.assertIsJSON(req)
# TitleID matches original request
self.assertTitleIdMatchesOriginalRequest(req)
# XUID matches original request
self.assertXuidMatchesOriginalRequest(
req, Data.titleidstats_params_nostats)
# User DOES NOT have stats in the game
self.assertNotIn("value", req.json["groups"]
[0]["statlistscollection"][0]["stats"][0])
def test_titleidstats_gamertag(self):
with server.app.test_request_context():
req = self.app.get(
"/userstats/gamertag/{}/titleid/{}".format(
Data.titleidstats_params["gamertag"],
Data.titleidstats_params["titleID"]
))
self.assertIsValidTitleIdResponse(req)
def test_titleidstats_xuid(self):
with server.app.test_request_context():
req = self.app.get(
"/userstats/xuid/{}/titleid/{}".format(
Data.titleidstats_params["xuid"],
Data.titleidstats_params["titleID"]
))
self.assertIsValidTitleIdResponse(req)
def test_titleidstats_gamertag_no_stats(self):
with server.app.test_request_context():
req = self.app.get(
"/userstats/gamertag/{}/titleid/{}".format(
Data.titleidstats_params_nostats["gamertag"],
Data.titleidstats_params["titleID"]
))
self.assertIsValidTitleIdResponseNoStats(req)
def test_titleidstats_xuid_no_stats(self):
with server.app.test_request_context():
req = self.app.get(
"/userstats/xuid/{}/titleid/{}".format(
Data.titleidstats_params_nostats["xuid"],
Data.titleidstats_params["titleID"]
))
self.assertIsValidTitleIdResponseNoStats(req)
class TestFriends(BaseTest):
def assertIsValidSummaryResponse(self, req):
self.assertIs200(req)
self.assertIsJSON(req)
self.assertIn("followers", req.json)
self.assertIn("following", req.json)
self.assertTrue(isinstance(req.json["followers"], int))
self.assertTrue(isinstance(req.json["following"], int))
def test_summary_xuid(self):
with server.app.test_request_context():
req = self.app.get(
"/friends/summary/xuid/{}".format(Data.xuid_valid["xuid"]))
self.assertIsValidSummaryResponse(req)
def test_summary_gamertag(self):
with server.app.test_request_context():
req = self.app.get(
"/friends/summary/gamertag/{}".format(Data.xuid_valid["gamertag"]))
self.assertIsValidSummaryResponse(req)
# Probably a good idea to add invalid tests, see GH-1
class TestMisc(BaseTest):
def test_info(self):
with server.app.test_request_context():
req = self.app.get("/info")
self.assertIs200(req)
self.assertIsJSON(req)
self.assertEqual(req.json["sha"], server.get_sha())
self.assertEqual(req.json["routes"], server.get_routes())
def test_readme(self):
with server.app.test_request_context():
req = self.app.get("/readme")
self.assertIs200(req)
with open("README.md") as f:
self.assertEqual(req.get_data(
as_text=True).replace("\r", ""), f.read())
def test_index(self):
with server.app.test_request_context():
req = self.app.get("/")
self.assertIs200(req)
with open("static/index.html") as f:
self.assertEqual(req.get_data(
as_text=True).replace("\r", ""), f.read())
def test_titleinfo(self):
with server.app.test_request_context():
# Forza Motorsport 6
req = self.app.get(
"/titleinfo/" + Data.titleidstats_params["titleID"])
self.assertIs200(req)
self.assertIsJSON(req)
data = req.json["titles"][0]
# Ensure the titleId matches
self.assertEqual(
data["titleId"], Data.titleidstats_params["titleID"])
self.assertEqual(data["modernTitleId"],
Data.titleidstats_params["titleID"])
# Ensure some data matches
self.assertEqual(data["name"], "Forza Motorsport 6")
self.assertEqual(data["type"], "Game")
self.assertEqual(data["mediaItemType"], "Application")
self.assertEqual(
data["pfn"], "Microsoft.MonumentBaseGame_8wekyb3d8bbwe")
self.assertEqual(data["serviceConfigId"],
"417d0100-b230-41cf-975d-3eaa64f9397e")
def test_legacysearch(self):
with server.app.test_request_context():
# This will return some data, currently 9 items
req = self.app.get("/legacysearch/Halo")
self.assertIs200(req)
self.assertIsJSON(req)
total_count = req.json["Totals"][0]["Count"]
self.assertEqual(len(req.json["Items"]), total_count)
self.assertEqual(total_count, 9)
self.assertEqual(req.json["Totals"][0]["Name"], "GameType")
def test_legacysearch_nodata(self):
with server.app.test_request_context():
# This will not return any results
req = self.app.get("/legacysearch/123456789")
self.assertIs200(req)
self.assertIsJSON(req)
self.assertEqual(req.json["Totals"][0]["Name"], "GameType")
self.assertEqual(req.json["Totals"][0]["Count"], 0)
def test_gamertag_check(self):
with server.app.test_request_context():
req = self.app.get("/gamertag/check/Prouser123")
self.assertIs200(req)
self.assertIsJSON(req)
self.assertEqual(req.json["available"], "false")
self.assertEqual(req.json["code"], 409)
def test_gamertag_check_free(self):
with server.app.test_request_context():
req = self.app.get("/gamertag/check/sdakjdwaskwad")
self.assertIs200(req)
self.assertIsJSON(req)
self.assertEqual(req.json["available"], "true")
self.assertEqual(req.json["code"], 200)
def test_usercolors_define(self):
with server.app.test_request_context():
req = self.app.get("/usercolors/define/1/2/3")
self.assertIs200(req)
self.assertEqual(req.mimetype, "image/svg+xml")
# TODO: Check the Response content
def test_usercolors_get_xuid(self):
with server.app.test_request_context():
req = self.app.get("/usercolors/get/xuid/" +
Data.xuid_valid["xuid"])
self.assertIs200(req)
self.assertEqual(req.mimetype, "image/svg+xml")
# TODO: Check the Response content
def test_usercolors_get_gamertag(self):
with server.app.test_request_context():
req = self.app.get("/usercolors/get/gamertag/" +
Data.xuid_valid["gamertag"])
self.assertIs200(req)
self.assertEqual(req.mimetype, "image/svg+xml")
# TODO: Check the Response content
class TestDev(BaseTest):
def isTimestamp(self, str):
try:
time.strptime(str, '%a, %d %b %Y %H:%M:%S %Z')
# %a, %d %B %Y %T %Z - from strftime.net
except ValueError:
return False
else:
return True
def test_reauth(self):
with server.app.test_request_context():
req = self.app.get("/dev/reauth")
self.assertIs200(req)
self.assertIsJSON(req)
self.assertEqual(req.json["message"], "success")
def test_isauth_true(self):
with server.app.test_request_context():
req = self.app.get("/dev/isauth")
self.assertIs200(req)
self.assertIsJSON(req)
self.assertEqual(req.json["authenticated"], True)
self.assertEqual(req.json["gt"], main.auth_mgr.userinfo.gamertag)
self.assertTrue(self.isTimestamp(req.json["access"]["expires"]))
self.assertTrue(self.isTimestamp(req.json["access"]["issued"]))
self.assertTrue(self.isTimestamp(req.json["user"]["expires"]))
self.assertTrue(self.isTimestamp(req.json["user"]["issued"]))
if __name__ == '__main__':
print("HAI")
main.authenticate()
print("----------------------------------------------------------------------")
unittest.main(warnings='ignore')
# unittest.main()