forked from apache/usergrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·1077 lines (947 loc) · 25.2 KB
/
test.js
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/**
* Test suite for all the examples in the readme
*
* NOTE: No, this test suite doesn't use the traditional format for
* a test suite. This is because the goal is to require as little
* alteration as possible during the copy / paste operation from this test
* suite to the readme file.
*
* @author rod simpson ([email protected])
*/
var usergrid = require('./lib/usergrid');
var logSuccess = true;
var successCount = 0;
var logError = true;
var errorCount = 0;
var logNotice = true;
var _unique = new Date().getTime();
var _username = 'marty'+_unique;
var _email = 'marty'+_unique+'@timetravel.com';
var _password = 'password2';
var _newpassword = 'password3';
var client = new usergrid.client({
orgName:'yourorgname',
appName:'sandbox',
logging: true, //optional - turn on logging, off by default
buildCurl: true //optional - turn on curl commands, off by default
});
//call the runner function to start the process
client.logout();
runner(0);
function runner(step, arg, arg2){
step++;
switch(step)
{
case 1:
notice('-----running step '+step+': DELETE user from DB to prep test');
clearUser(step);
break;
case 2:
notice('-----running step '+step+': GET test');
testGET(step);
break;
case 3:
notice('-----running step '+step+': POST test');
testPOST(step);
break;
case 4:
notice('-----running step '+step+': PUT test');
testPUT(step);
break;
case 5:
notice('-----running step '+step+': DELETE test');
testDELETE(step);
break;
case 6:
notice('-----running step '+step+': prepare database - remove all dogs (no real dogs harmed here!!)');
cleanupAllDogs(step);
break;
case 7:
notice('-----running step '+step+': make a new dog');
makeNewDog(step);
break;
case 8:
notice('-----running step '+step+': update our dog');
updateDog(step, arg);
break;
case 9:
notice('-----running step '+step+': refresh our dog');
refreshDog(step, arg);
break;
case 10:
notice('-----running step '+step+': remove our dog from database (no real dogs harmed here!!)');
removeDogFromDatabase(step, arg);
break;
case 11:
notice('-----running step '+step+': make lots of dogs!');
makeSampleData(step, arg);
break;
case 12:
notice('-----running step '+step+': make a dogs collection and show each dog');
testDogsCollection(step);
break;
case 13:
notice('-----running step '+step+': get the next page of the dogs collection and show each dog');
getNextDogsPage(step, arg);
break;
case 14:
notice('-----running step '+step+': get the previous page of the dogs collection and show each dog');
getPreviousDogsPage(step, arg);
break;
case 15:
notice('-----running step '+step+': remove all dogs from the database (no real dogs harmed here!!)');
cleanupAllDogs(step);
break;
case 16:
notice('-----running step '+step+': prepare database (remove existing user if present)');
prepareDatabaseForNewUser(step);
break;
case 17:
notice('-----running step '+step+': create a new user');
createUser(step);
break;
case 18:
notice('-----running step '+step+': update the user');
updateUser(step, arg);
break;
case 19:
notice('-----running step '+step+': get the existing user');
getExistingUser(step, arg);
break;
case 20:
notice('-----running step '+step+': refresh the user from the database');
refreshUser(step, arg);
break;
case 21:
notice('-----running step '+step+': log user in');
loginUser(step, arg);
break;
case 22:
notice('-----running step '+step+': change users password');
changeUsersPassword(step, arg);
break;
case 23:
notice('-----running step '+step+': log user out');
logoutUser(step, arg);
break;
case 24:
notice('-----running step '+step+': relogin user');
reloginUser(step, arg);
break;
case 25:
notice('-----running step '+step+': logged in user creates dog');
createDog(step, arg);
break;
case 26:
notice('-----running step '+step+': logged in user likes dog');
userLikesDog(step, arg, arg2);
break;
case 27:
notice('-----running step '+step+': logged in user removes likes connection to dog');
removeUserLikesDog(step, arg, arg2);
break;
case 28:
notice('-----running step '+step+': user removes dog');
removeDog(step, arg, arg2);
break;
case 29:
notice('-----running step '+step+': log the user out');
logoutUser(step, arg);
break;
case 30:
notice('-----running step '+step+': remove the user from the database');
destroyUser(step, arg);
break;
case 31:
notice('-----running step '+step+': try to create existing entity');
createExistingEntity(step, arg);
break;
case 32:
notice('-----running step '+step+': try to create new entity with no name');
createNewEntityNoName(step, arg);
break;
case 33:
notice('-----running step '+step+': clean up users');
cleanUpUsers(step, arg);
break;
case 34:
notice('-----running step '+step+': clean up dogs');
cleanUpDogs(step, arg);
break;
case 35:
notice('-----running step '+step+': create counter');
counterCreate(step, arg);
break;
case 36:
notice('-----running step '+step+': reset counter');
counterReset(step, arg);
break;
case 37:
notice('-----running step '+step+': increment counter');
counterIncrement(step, arg);
break;
case 38:
notice('-----running step '+step+': decrement counter');
counterDecrement(step, arg);
break;
case 34:
notice('-----running step '+step+': fetch counter data');
counterFetch(step, arg);
break;
default:
notice('-----test complete!-----');
notice('Success count= ' + successCount);
notice('Error count= ' + errorCount);
notice('-----thank you for playing!-----');
}
}
//logging functions
function success(message){
successCount++;
if (logSuccess) {
console.log('SUCCESS: ' + message);
}
}
function error(message){
errorCount++
if (logError) {
console.log('ERROR: ' + message);
}
}
function notice(message){
if (logNotice) {
console.log('NOTICE: ' + message);
}
}
//tests
function clearUser(step) {
var options = {
method:'DELETE',
endpoint:'users/fred'
};
client.request(options, function (err, data) {
//data will contain raw results from API call
success('User cleared from DB');
runner(step);
});
}
function testGET(step) {
var options = {
method:'GET',
endpoint:'users'
};
client.request(options, function (err, data) {
if (err) {
error('GET failed');
} else {
//data will contain raw results from API call
success('GET worked');
runner(step);
}
});
}
function testPOST(step) {
var options = {
method:'POST',
endpoint:'users',
body:{ username:'fred', password:'secret' }
};
client.request(options, function (err, data) {
if (err) {
error('POST failed');
} else {
//data will contain raw results from API call
success('POST worked');
runner(step);
}
});
}
function testPUT(step) {
var options = {
method:'PUT',
endpoint:'users/fred',
body:{ newkey:'newvalue' }
};
client.request(options, function (err, data) {
if (err) {
error('PUT failed');
} else {
//data will contain raw results from API call
success('PUT worked');
runner(step);
}
});
}
function testDELETE(step) {
var options = {
method:'DELETE',
endpoint:'users/fred'
};
client.request(options, function (err, data) {
if (err) {
error('DELETE failed');
} else {
//data will contain raw results from API call
success('DELETE worked');
runner(step);
}
});
}
function makeNewDog(step) {
var options = {
type:'dogs',
name:'Ralph'+_unique
}
client.createEntity(options, function (err, dog) {
if (err) {
error('dog not created');
} else {
success('dog is created');
//once the dog is created, you can set single properties:
dog.set('breed','Dinosaur');
//the set function can also take a JSON object:
var data = {
master:'Fred',
state:'hungry'
}
//set is additive, so previously set properties are not overwritten
dog.set(data);
//finally, call save on the object to save it back to the database
dog.save(function(err){
if (err){
error('dog not saved');
} else {
success('new dog is saved');
runner(step, dog);
}
});
}
});
}
function updateDog(step, dog) {
//change a property in the object
dog.set("state", "fed");
//and save back to the database
dog.save(function(err){
if (err){
error('dog not saved');
} else {
success('dog is saved');
runner(step, dog);
}
});
}
function refreshDog(step, dog){
//call fetch to refresh the data from the server
dog.fetch(function(err){
if (err){
error('dog not refreshed from database');
} else {
//dog has been refreshed from the database
//will only work if the UUID for the entity is in the dog object
success('dog entity refreshed from database');
runner(step, dog);
}
});
}
function removeDogFromDatabase(step, dog){
//the destroy method will delete the entity from the database
dog.destroy(function(err){
if (err){
error('dog not removed from database');
} else {
success('dog removed from database'); // no real dogs were harmed!
dog = null; //no real dogs were harmed!
runner(step, 1);
}
});
}
function makeSampleData(step, i) {
notice('making dog '+i);
var options = {
type:'dogs',
name:'dog'+_unique+i,
index:i
}
client.createEntity(options, function (err, dog) {
if (err) {
error('dog ' + i + ' not created');
} else {
if (i >= 30) {
//data made, ready to go
success('all dogs made');
runner(step);
} else {
success('dog ' + i + ' made');
//keep making dogs
makeSampleData(step, ++i);
}
}
});
}
function testDogsCollection(step) {
var options = {
type:'dogs',
qs:{ql:'order by index'}
}
client.createCollection(options, function (err, dogs) {
if (err) {
error('could not make collection');
} else {
success('new Collection created');
//we got the dogs, now display the Entities:
while(dogs.hasNextEntity()) {
//get a reference to the dog
dog = dogs.getNextEntity();
var name = dog.get('name');
notice('dog is called ' + name);
}
success('looped through dogs');
//create a new dog and add it to the collection
var options = {
name:'extra-dog',
fur:'shedding'
}
//just pass the options to the addEntity method
//to the collection and it is saved automatically
dogs.addEntity(options, function(err, dog, data) {
if (err) {
error('extra dog not saved or added to collection');
} else {
success('extra dog saved and added to collection');
runner(step, dogs);
}
});
}
});
}
function getNextDogsPage(step, dogs) {
if (dogs.hasNextPage()) {
//there is a next page, so get it from the server
dogs.getNextPage(function(err){
if (err) {
error('could not get next page of dogs');
} else {
success('got next page of dogs');
//we got the next page of data, so do something with it:
var i = 11;
while(dogs.hasNextEntity()) {
//get a reference to the dog
var dog = dogs.getNextEntity();
var index = dog.get('index');
if(i !== index) {
error('wrong dog loaded: wanted' + i + ', got ' + index);
}
notice('got dog ' + i);
i++
}
success('looped through dogs')
runner(step, dogs);
}
});
} else {
getPreviousDogsPage(dogs);
}
}
function getPreviousDogsPage(step, dogs) {
if (dogs.hasPreviousPage()) {
//there is a previous page, so get it from the server
dogs.getPreviousPage(function(err){
if(err) {
error('could not get previous page of dogs');
} else {
success('got next page of dogs');
//we got the previous page of data, so do something with it:
var i = 1;
while(dogs.hasNextEntity()) {
//get a reference to the dog
var dog = dogs.getNextEntity();
var index = dog.get('index');
if(i !== index) {
error('wrong dog loaded: wanted' + i + ', got ' + index);
}
notice('got dog ' + i);
i++
}
success('looped through dogs');
runner(step);
}
});
} else {
getAllDogs();
}
}
function cleanupAllDogs(step){
var options = {
type:'dogs',
qs:{limit:50} //limit statement set to 50
}
client.createCollection(options, function (err, dogs) {
if (err) {
error('could not get all dogs');
} else {
success('got at most 50 dogs');
//we got 50 dogs, now display the Entities:
while(dogs.hasNextEntity()) {
//get a reference to the dog
var dog = dogs.getNextEntity();
var name = dog.get('name');
notice('dog is called ' + name);
}
dogs.resetEntityPointer();
//do doggy cleanup
while(dogs.hasNextEntity()) {
//get a reference to the dog
var dog = dogs.getNextEntity();
var dogname = dog.get('name');
notice('removing dog ' + dogname + ' from database');
dog.destroy(function(err, data) {
if (err) {
error('dog not removed');
} else {
success('dog removed');
}
});
}
//no need to wait around for dogs to be removed, so go on to next test
runner(step);
}
});
}
function prepareDatabaseForNewUser(step) {
var options = {
method:'DELETE',
endpoint:'users/',
qs:{ql:"select * where username ='marty*'"}
};
client.request(options, function (err, data) {
if (err) {
notice('database ready - no user to delete');
runner(step);
} else {
//data will contain raw results from API call
success('database ready - user deleted worked');
runner(step);
}
});
}
function createUser(step) {
client.signup(_username, _password, _email, 'Marty McFly',
function (err, marty) {
if (err){
error('user not created');
runner(step, marty);
} else {
success('user created');
runner(step, marty);
}
}
);
}
function updateUser(step, marty) {
//add properties cumulatively
marty.set('state', 'California');
marty.set("girlfriend","Jennifer");
marty.save(function(err){
if (err){
error('user not updated');
} else {
success('user updated');
runner(step, marty);
}
});
}
function getExistingUser(step, marty) {
var options = {
type:'users',
username:_username
}
client.getEntity(options, function(err, existingUser){
if (err){
error('existing user not retrieved');
} else {
success('existing user was retrieved');
var username = existingUser.get('username');
if (username === _username){
success('got existing user username');
} else {
error('could not get existing user username');
}
runner(step, marty);
}
});
}
function refreshUser(step, marty) {
marty.fetch(function(err){
if (err){
error('not refreshed');
} else {
success('user refreshed');
runner(step, marty);
}
});
}
function loginUser(step, marty) {
username = _username;
password = _password;
client.login(username, password,
function (err) {
if (err) {
error('could not log user in');
} else {
success('user has been logged in');
//the login call will return an OAuth token, which is saved
//in the client object for later use. Access it this way:
var token = client.token;
//then make a new client just for the app user, then use this
//client to make calls against the API
var appUserClient = new usergrid.client({
orgName:'yourorgname',
appName:'yourappname',
authType:usergrid.AUTH_APP_USER,
token:token
});
//alternitavely, you can change the authtype of the client:
client.authType = usergrid.AUTH_APP_USER;
//Then make calls against the API. For example, you can
//get the user entity this way:
client.getLoggedInUser(function(err, data, user) {
if(err) {
error('could not get logged in user');
} else {
success('got logged in user');
//you can then get info from the user entity object:
var username = user.get('username');
notice('logged in user was: ' + username);
runner(step, user);
}
});
}
}
);
}
function changeUsersPassword(step, marty) {
marty.set('oldpassword', _password);
marty.set('newpassword', _newpassword);
marty.save(function(err){
if (err){
error('user password not updated');
} else {
success('user password updated');
runner(step, marty);
}
});
}
function logoutUser(step, marty) {
//to log the user out, call the logout() method
client.logout();
//verify the logout worked
if (client.isLoggedIn()) {
error('logout failed');
} else {
success('user has been logged out');
}
runner(step, marty);
}
function reloginUser(step, marty) {
username = _username
password = _newpassword;
client.login(username, password,
function (err) {
if (err) {
error('could not relog user in');
} else {
success('user has been re-logged in');
runner(step, marty);
}
}
);
}
//TODO: currently, this code assumes permissions have been set to support user actions. need to add code to show how to add new role and permission programatically
//
//first create a new permission on the default role:
//POST "https://api.usergrid.com/yourorgname/yourappname/roles/default/permissions" -d '{"permission":"get,post,put,delete:/dogs/**"}'
//then after user actions, delete the permission on the default role:
//DELETE "https://api.usergrid.com/yourorgname/yourappname/roles/default/permissions?permission=get%2Cpost%2Cput%2Cdelete%3A%2Fdogs%2F**"
function createDog(step, marty) {
//see if marty can create a new dog now that he is logged in
var options = {
type:'dogs',
name:'einstein',
breed:'mutt'
}
client.createEntity(options, function (err, dog) {
if (err) {
error('POST new dog by logged in user failed');
} else {
success('POST new dog by logged in user succeeded');
runner(step, marty, dog);
}
});
}
function userLikesDog(step, marty, dog) {
marty.connect('likes', dog, function (err, data) {
if (err) {
error('connection not created');
runner(step, marty);
} else {
//call succeeded, so pull the connections back down
marty.getConnections('likes', function (err, data) {
if (err) {
error('could not get connections');
} else {
//verify that connection exists
if (marty.likes.einstein) {
success('connection exists');
} else {
error('connection does not exist');
}
runner(step, marty, dog);
}
});
}
});
}
function removeUserLikesDog(step, marty, dog) {
marty.disconnect('likes', dog, function (err, data) {
if (err) {
error('connection not deleted');
runner(step, marty);
} else {
//call succeeded, so pull the connections back down
marty.getConnections('likes', function (err, data) {
if (err) {
error('error getting connections');
} else {
//verify that connection exists
if (marty.likes.einstein) {
error('connection still exists');
} else {
success('connection deleted');
}
runner(step, marty, dog);
}
});
}
});
}
function removeDog(step, marty, dog) {
//now delete the dog from the database
dog.destroy(function(err, data) {
if (err) {
error('dog not removed');
} else {
success('dog removed');
}
});
runner(step, marty);
}
function destroyUser(step, marty) {
marty.destroy(function(err){
if (err){
error('user not deleted from database');
} else {
success('user deleted from database');
marty = null; //blow away the local object
runner(step);
}
});
}
function createExistingEntity(step, marty) {
var options = {
type:'dogs',
name:'einstein'
}
client.createEntity(options, function (err, dog) {
if (err) {
error('Create new entity to use for existing entity failed');
} else {
success('Create new entity to use for existing entity succeeded');
var uuid = dog.get('uuid');
//now create new entity, but use same entity name of einstein. This means that
//the original einstein entity now exists. Thus, the new einstein entity should
//be the same as the original + any data differences from the options var:
options = {
type:'dogs',
name:'einstein',
breed:'mutt'
}
client.createEntity(options, function (err, newdog) {
if (err) {
error('Create new entity to use for existing entity failed');
} else {
success('Create new entity to use for existing entity succeeded');
var newuuid = newdog.get('uuid');
if (newuuid === uuid) {
success('UUIDs of new and old entities match');
} else {
error('UUIDs of new and old entities do not match');
}
var breed = newdog.get('breed');
if (breed === 'mutt') {
success('attribute sucesfully set on new entity');
} else {
error('attribute not sucesfully set on new entity');
}
newdog.destroy(function(err){
if (err){
error('existing entity not deleted from database');
} else {
success('existing entity deleted from database');
dog = null; //blow away the local object
newdog = null; //blow away the local object
runner(step);
}
});
}
});
}
});
}
function createNewEntityNoName(step, marty) {
var options = {
type:"something",
othervalue:"something else"
}
client.createEntity(options, function (err, entity) {
if (err) {
error('Create new entity with no name failed');
} else {
success('Create new entity with no name succeeded');
entity.destroy();
runner(step);
}
});
}
function cleanUpUsers(step){
var options = {
type:'users',
qs:{limit:50} //limit statement set to 50
}
client.createCollection(options, function (err, users) {
if (err) {
error('could not get all users');
} else {
success('got users');
//do doggy cleanup
while(users.hasNextEntity()) {
//get a reference to the dog
var user = users.getNextEntity();
var username = user.get('name');
notice('removing dog ' + username + ' from database');
user.destroy(function(err, data) {
if (err) {
error('user not removed');
} else {
success('user removed');
}
});
}
runner(step);
}
});
}
function cleanUpDogs(step){
var options = {
type:'dogs',
qs:{limit:50} //limit statement set to 50
}
client.createCollection(options, function (err, dogs) {
if (err) {
error('could not get all dogs');
} else {
success('got at most 50 dogs');
//we got 50 dogs, now display the Entities:
while(dogs.hasNextEntity()) {