-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ml
526 lines (437 loc) · 20.4 KB
/
example.ml
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
(* ************************************************************************** *)
(* Project: Life - the game, Official OCaml SDK *)
(* Author: db0 ([email protected], http://db0.fr/) *)
(* Latest Version is on GitHub: https://github.com/Life-the-game/SDK-OCaml *)
(* ************************************************************************** *)
(** Test cases *)
open ApiTypes
(* ************************************************************************** *)
(* Library configuration *)
(* ************************************************************************** *)
let _ =
let open ApiConf in
verbose := true;
base_url := "http://localhost:8080/api/v1";
(Arg.parse
[("-u", Arg.String (fun url -> base_url := url),
"the URL of the web service")]
(fun _ -> ()) "./example [-u url]")
(* ************************************************************************** *)
(* Tests configuration *)
(* ************************************************************************** *)
let print_success = false
let stop_on_error = true
(* ************************************************************************** *)
(* Tools *)
(* ************************************************************************** *)
(* Generate a random string with the given lenght *)
let random_string (length : int) : string =
let _ = Random.self_init () in
let gen () =
match Random.int (26 + 26 + 10) with
| n when n < 26 -> int_of_char 'a' + n
| n when n < 26 + 26 -> int_of_char 'A' + n - 26
| n -> int_of_char '0' + n - 26 - 26 in
let gen _ = String.make 1 (char_of_int (gen ())) in
String.concat "" (Array.to_list (Array.init length gen))
(* Display the message that explains the test *)
let print_title str =
ApiDump.lprint_endline ("\n\n\n## " ^ str)
let impossible str =
ApiDump.lprint_endline (" IMPOSSIBLE to run this test because "
^ str)
(* Function to display a page (give it to the test function) *)
let pageprint res = ApiDump.page res ApiDump.print
(* Calculate the number of success/failure to display the result at the end *)
type result = { mutable success: int; mutable failure: int; }
let total = { success = 0; failure = 0; }
(* Print the total at the end *)
let print_total () =
let t = string_of_int (total.success + total.failure) in
ApiDump.lprint_endline ("
#################################################
T O T A L
Success : " ^ (string_of_int total.success) ^ " / " ^ t ^ "
Failure : " ^ (string_of_int total.failure) ^ " / " ^ t ^ "
#################################################
")
(* ************************************************************************** *)
(* Data used by the tests *)
(* ************************************************************************** *)
let login = random_string 5
and lang = Lang.default
and firstname = "Barbara"
and lastname = "Lepage"
and gender = Gender.Female
and birthday = Date.of_string "1991-05-30"
and password = "helloworld"
and email = random_string 2 ^ "@gmail.com"
and message = random_string 30
and picture = (["example.png"], "image/png")
and picture2 = (["example2.jpg"], "image/jpeg")
and achievement_name = random_string 15
and achievement_description = random_string 30
and comment_description = random_string 30
(* ************************************************************************** *)
(* Test generic function *)
(* ************************************************************************** *)
let test
?(f = ApiDump.print) (* function to display the result of the test *)
?(t = "") (* not empty if the test should fail (so it's a success) *)
(result : 'a Api.t) : 'a Api.t =
let _failure () = total.failure <- total.failure + 1
and _success () = total.success <- total.success + 1 in
let failure e = if t = "" || t != e.ApiError.stype
then _failure () else _success ()
and success r = if t != "" then _failure () else _success () in
let on_error e =
begin
failure e;
ApiDump.error e;
ApiDump.lprint_endline "\n ----> FAILURE\n";
(* if stop_on_error then print_total (); exit 1 *)
()
end
and on_result r =
begin
success r;
if print_success
then ApiDump.lprint_endline "\n ## OCaml object generated:\n";
f r;
ApiDump.lprint_endline "\n ----> SUCCESS\n"
end in
(match result with
| Error e -> on_error e
| Result r -> on_result r);
result
let auth_test
?(f = ApiDump.print) ?(t = "")
(test_launcher : auth -> 'a Api.t) = function
| Error e -> impossible "it requires authentication that previously failed";
Error e
| Result auth ->
test ~f:f ~t:t (test_launcher (ApiAuth.auth_to_api auth))
(* ************************************************************************** *)
(* It's testing time \o/ *)
(* ************************************************************************** *)
let _ =
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Users tests (without auth) #";
ApiDump.lprint_endline "#################################################";
print_title "Create a new user";
let user =
test (ApiUser.create
~login:login
~email:email
~password:password
~lang:lang
~firstname:firstname
~lastname:lastname
~gender:gender
~birthday:(Some birthday)
~avatar:picture
()) in
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Authentication tests #";
ApiDump.lprint_endline "#################################################";
Unix.sleep 1;
print_title "Authenticate using a login and a password";
let auth = test (ApiAuth.login login password) in
print_title "Get one user...";
print_title "with auth";
ignore (auth_test (fun auth ->
(ApiUser.get_one ~auth:(Some auth) login)) auth);
print_title "Logout (remove token)";
ignore (match auth with
| Error e -> impossible "it requires authentication that previously failed";
Error e
| Result auth -> test (ApiAuth.logout auth));
let auth = test (ApiAuth.login login password) in
print_title "Get one user...";
print_title "with auth";
ignore (auth_test (fun auth ->
(ApiUser.get_one ~auth:(Some auth) login)) auth);
exit 1;
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Achievements tests #";
ApiDump.lprint_endline "#################################################";
(* PRIVATE *)
print_title "Create an achievement";
ignore (auth_test (fun auth ->
ApiAchievement.create ~auth:auth ~name:achievement_name
~description:achievement_description ~badge:picture
~keywords:["hello"; "world"] ()) auth);
(* /PRIVATE *)
print_title "Get achievements";
let achievements = test ~f:pageprint (ApiAchievement.get ~req:(Lang lang) ()) in
(* Note: It is also possible to search through achievements using "term" *)
print_title "Get next page of achievements";
(match achievements with (* Check the previous page*)
| Error e -> impossible "the previous page failed"
| Result achievements ->
match Page.next achievements with (* Check if there is a next page *)
| None -> ApiDump.lprint_endline "It was the last page"
| Some nextpage ->
ignore (test ~f:pageprint (ApiAchievement.get ~req:(Lang lang)
~page:nextpage ())));
(* print_title "Get one achievement"; *)
(* (match achievements with (\* Check if the list exists *\) *)
(* | Error e -> impossible "the previous tests failed" *)
(* | Result page -> *)
(* if page.Page.server_size == 0 (\* Check if there are elements to get *\) *)
(* then ApiDump.lprint_endline "No elements available" *)
(* else *)
(* let achievement_id = *)
(* (List.hd page.Page.items).ApiAchievement.info.Info.id in *)
(* ignore (test (ApiAchievement.get_one *)
(* ~req:(Lang lang) achievement_id))); *)
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Users tests (with auth) #";
ApiDump.lprint_endline "#################################################";
print_title "Get users";
let users = auth_test (fun auth ->
ApiUser.get ~auth:auth ~term:["th"] ()) auth in
print_title "Get one user...";
(match users with (* Check if the list exists *)
| Error e -> impossible "the previous tests failed"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "No elements available"
else
let user_id = (List.hd page.Page.items).ApiUser.login in
print_title "with auth";
ignore (auth_test (fun auth ->
(ApiUser.get_one ~auth:(Some auth) user_id)) auth);
print_title "without auth";
ignore (test (ApiUser.get_one user_id)); ());
(* PRIVATE *)
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Roles tests #";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "No test";
(* /PRIVATE *)
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Game Network tests #";
ApiDump.lprint_endline "#################################################";
print_title "Add a user to my Game Network";
(match users with (* Check if the list exists *)
| Error e -> impossible "the previous tests failed"
| Result page ->
if page.Page.server_size < 2 (* Check if there are elements to get *)
then ApiDump.lprint_endline "No user to add"
else
let user_id = (List.nth page.Page.items 2).ApiUser.login in
ignore (auth_test (fun auth ->
ApiGameNetwork.add
~auth:auth
user_id) auth);
print_title "Get following";
print_title "with auth";
ignore (auth_test (fun auth -> ApiGameNetwork.get_mine
~auth:auth ()) auth);
print_title "without auth (and of someone else)";
ignore (test (ApiGameNetwork.get user_id));
print_title "Get followers...";
print_title "with auth";
ignore (auth_test (fun auth -> ApiGameNetwork.get_my_followers
~auth:auth ()) auth);
print_title "without auth (and of someone else)";
ignore (test (ApiGameNetwork.get_followers user_id));
);
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Achievements statuses tests #";
ApiDump.lprint_endline "#################################################";
print_title "Create an achievement status";
(match achievements with (* Check if some achievements exists *)
| Error e -> impossible "previously failed to get achievements"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "there's no achievement available"
else
let achievement_id =
(List.hd page.Page.items).ApiAchievement.info.Info.id in
ignore (auth_test (fun auth ->
ApiAchievementStatus.create ~auth:auth
~achievement:achievement_id
~status:Status.Objective
~message:message ()) auth));
print_title "Get my objectives ordered by name limit 2 with auth";
let achievements_statuses =
auth_test ~f:pageprint (fun auth ->
ApiAchievementStatus.get
~req:(Auth auth)
~page:(None, Some 2, Some Page.Alphabetic, None)
~status:(Some Status.Objective)
login) auth in
print_title "Get one achievement status...";
(match achievements_statuses with (* Check if the list exists *)
| Error e -> impossible "the previous tests failed"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "there's no elements available"
else
(match user with
| Error e -> impossible "the user has not been created"
| Result user ->
let achievement_status_id =
(List.hd page.Page.items).ApiAchievementStatus.info.Info.id
(* and user_id = user.ApiUser.login *) in
print_title "with auth";
ignore (auth_test (fun auth ->
ApiAchievementStatus.get_one ~req:(Auth auth)
achievement_status_id) auth);
(* print_title "with lang";
ignore (test (ApiAchievementStatus.get_one ~req:(Lang lang)
user_id achievement_status_id)); *)
));
(* print_title "Unlock this ojective + add pictures + remove message"; *)
(* ignore (auth_test (fun auth -> *)
(* ApiAchievementStatus.edit ~auth:auth *)
(* ~status:(Some ApiAchievementStatus.Status.Achieved) *)
(* ~message:(Some "") *)
(* (\* ~add_medias:[picture; picture2] *\) *)
(* achievement_status_id) auth); *)
(* )); *)
print_title "Approve an achievement status";
(match achievements_statuses with (* Check if the list exists *)
| Error e -> impossible "the previous tests failed"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "there's no elements available"
else
(match user with
| Error e -> impossible "the user has not been created"
| Result user ->
let achievement_status_id =
(List.hd page.Page.items).ApiAchievementStatus.info.Info.id
and user_id = user.ApiUser.login in
ignore (auth_test (fun auth ->
ApiAchievementStatus.approve ~auth:auth
(* PRIVATE *)
~approver:user_id
(* /PRIVATE *)
achievement_status_id) auth)
));
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Achievement statuses comments tests #";
ApiDump.lprint_endline "#################################################";
print_title "Create an achievement status comment";
(match achievements_statuses with (* Check if some achievements statuses exist *)
| Error e -> impossible "previously failed to get achievements statuses"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "there's no achievement status available"
else
let achievement_status_id =
(List.hd page.Page.items).ApiAchievementStatus.info.Info.id in
ignore (auth_test (fun auth ->
ApiComment.create ~auth:auth
~content:comment_description
~medias:[picture; picture2]
achievement_status_id) auth));
print_title "Get my comments ordered by name limit 2 with auth";
let _ =
(match achievements_statuses with
| Error e -> impossible "previously failed to get achievements statuses"
| Result page ->
if page.Page.server_size == 0
then ApiDump.lprint_endline "there's no achievement status available"
else
let achievement_status_id =
(List.hd page.Page.items).ApiAchievementStatus.info.Info.id in
ignore (test ~f:pageprint (ApiComment.get achievement_status_id))) in
(* print_title "Get one achievement status comment";
(match achievements_statuses_comments with (* Check if the list exists *)
| Error e -> impossible "the previous tests failed"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "there are no elements available"
else
(match user with
| Error e -> impossible "the user has not been created"
| Result user ->
let achievement_status_comment_id =
(List.hd page.Page.items).ApiComment.info.Info.id in
print_title "with auth";
ignore (auth_test (fun auth ->
ApiComment.get_comment ~req:(Auth auth)
user_id achievement_status_id) auth);
print_title "with lang";
ignore (test (ApiComment.get_comment ~req:(Lang lang)
achievement_status_comment_id user_id));
));
print_title "approve an achievement status";
(match achievements_statuses_comments with
| Error e -> impossible "previously failed to get achievements statuses"
| Result page ->
if page.Page.server_size == 0
then ApiDump.lprint_endline "there's no achievement status comment available"
else
let achievement_status_comment_id =
(List.hd page.Page.items).ApiComment.info.Info.id in
ignore (auth_test (fun auth ->
ApiComment.approve
~auth:auth
(* PRIVATE *)
~approver:login
(* /PRIVATE *)
achievement_status_comment_id login)
auth));
*)
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Playground tests #";
ApiDump.lprint_endline "#################################################";
print_title "Get Playground";
print_title "with auth";
auth_test (fun auth ->
ApiPlayground.get ~auth:(Some auth) login) auth;
print_title "without auth";
test (ApiPlayground.get login);
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Feed tests #";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "No test";
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Notifications tests #";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "No test";
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# News tests #";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "No test";
ApiDump.lprint_endline "\n";
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Client Errors tests #";
ApiDump.lprint_endline "#################################################";
print_title "Invalid file format";
(match achievements_statuses with (* Check if the list exists *)
| Error e -> impossible "the previous achievement_status tests failed"
| Result page ->
if page.Page.server_size == 0 (* Check if there are elements to get *)
then ApiDump.lprint_endline "there's no elements available"
else
let achievement_status_id =
(List.hd page.Page.items).ApiAchievementStatus.info.Info.id in
ignore (auth_test ~t:"CLIENT_InvalidFileFormat" (fun auth ->
ApiAchievementStatus.edit ~auth:auth
~add_medias:[(["hack.sh"], "beurp")] achievement_status_id) auth));
ApiDump.lprint_endline "#################################################";
ApiDump.lprint_endline "# Logout #";
ApiDump.lprint_endline "#################################################";
print_title "Logout (remove token)";
ignore (match auth with
| Error e -> impossible "it requires authentication that previously failed";
Error e
| Result auth -> test (ApiAuth.logout auth));
print_total ()