Repository navigation
Expand file tree
/
Copy pathGetterIT.java
More file actions
3298 lines (3158 loc) · 136 KB
/
Copy pathGetterIT.java
File metadata and controls
3298 lines (3158 loc) · 136 KB
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
package dbProcs;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import testUtils.TestProperties;
import utils.ScoreboardStatus;
/**
* Class is targeted to test all of the methods found in the src/dbprocs/Getter.java class, but does
* include some coverage of other classes, such as Setter.java and Database.java
*
* @author mark
*/
public class GetterIT {
private static final Logger log = LogManager.getLogger(GetterIT.class);
private static String lang = "en_GB";
private static Locale locale = new Locale(lang);
private static String applicationRoot = new String();
private static final int totalNumberOfModulesInShepherd = 58;
/** Creates DB or Restores DB to Factory Defaults before running tests */
@BeforeAll
public static void resetDatabase() throws IOException, SQLException {
TestProperties.setTestPropertiesFileDirectory(log);
TestProperties.createMysqlResource();
TestProperties.ensureSchemaReady(log);
TestProperties.reseedTestData();
}
/**
* Searches for class based on class name. If nothing is found, the class is created and the new
* class Id is returned
*
* @param className Name of the class you wish to search / create
* @return The Identifier of the class owning the name submitted
* @throws SQLException
*/
public static String findCreateClassId(String className) throws SQLException {
String classId = new String();
ResultSet rs = Getter.getClassInfo(applicationRoot);
while (rs.next()) {
if (rs.getString(2).compareTo(className) == 0) {
classId = rs.getString(1);
break;
}
}
rs.close();
if (classId.isEmpty()) {
log.debug("Could not find class. Creating it");
if (Setter.classCreate(applicationRoot, className, "2015")) {
log.debug("Class Created. Getting ID");
classId = findCreateClassId(className);
} else {
TestProperties.failAndPrint("Could not Create Class " + className);
}
}
return classId;
}
/**
* Searches for class based on class name. If nothing is found, the class is created and the new
* class Id is returned
*
* @param className Name of the class you wish to search / create
* @return The Identifier of the class owning the name submitted
* @throws SQLException
*/
public static String findCreateClassId(String className, String applicationRoot)
throws SQLException {
String classId = new String();
ResultSet rs = Getter.getClassInfo(applicationRoot);
while (rs.next()) {
if (rs.getString(2).compareTo(className) == 0) {
classId = rs.getString(1);
break;
}
}
rs.close();
if (classId.isEmpty()) {
log.debug("Could not find class. Creating it");
if (Setter.classCreate(applicationRoot, className, "2015")) {
log.debug("Class Created. Getting ID");
classId = findCreateClassId(className, applicationRoot);
} else {
TestProperties.failAndPrint("Could not create create class " + className);
}
}
return classId;
}
/**
* This method will sign in as an admin, or create the admin and sign in as them. If this fails it
* will throw an Exception. This function will pass if correct user credentials are passed as well
*
* @param applicationRoot Context of running application
* @param userName The user name of the admin you want to create or sign in as
* @param password The password of the admin you want to create or sign in as
* @return Boolean value depicting if the user exists and can be authenticated
* @throws SQLException
*/
public static boolean verifyTestAdmin(String applicationRoot, String userName, String password)
throws SQLException {
boolean result = false;
String user[] = Getter.authUser(applicationRoot, userName, userName);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, userName, "admin", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, userName);
}
if (user != null && !user[0].isEmpty()) {
log.debug(userName + " could authenticate. returning true");
result = true;
} else {
TestProperties.failAndPrint(
"Could not verify that user " + userName + " could authenticate at all.");
}
return result;
}
/**
* This method will sign in as a User, or create the user and sign in as them. If this fails it
* will throw an Exception
*
* @param applicationRoot Context of running application
* @param userName The user name of the user you want to create or sign in as
* @param password The password of the user you want to create or sign in as
* @return Boolean value depicting if the user exists and can be authenticated
* @throws SQLException
*/
public static boolean verifyTestUser(String applicationRoot, String userName, String password)
throws SQLException {
boolean result = false;
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@example.com", false);
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug(userName + " could authenticate. returning true");
result = true;
} else {
TestProperties.failAndPrint("Fail: Could not verify user " + userName + " at all.");
}
return result;
}
/**
* This method will sign in as a User, or create the user and sign in as them. If this fails it
* will throw an Exception. They will be added to the submitted class
*
* @param applicationRoot Context of running application
* @param userName The user name of the user you want to create or sign in as
* @param password The password of the user you want to create or sign in as
* @param theClass The identifier of the class
* @return Boolean value depicting if the user exists and can be authenticated
* @throws SQLException up
*/
public static boolean verifyTestUser(
String applicationRoot, String userName, String password, String theClass)
throws SQLException {
boolean result = false;
String className = new String();
className = Getter.getClassInfo(applicationRoot, theClass)[0];
if (className.isEmpty()) {
TestProperties.failAndPrint("Could not find class info for class " + theClass);
}
String user[] = Getter.authUser(applicationRoot, userName, userName);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, theClass, userName, userName, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, userName);
}
if (user != null && !user[0].isEmpty()) {
log.debug(userName + " could authenticate. checking class");
if ((user[4] == null || user[4].isEmpty()) && !theClass.isEmpty()) {
log.debug("Need to update user's class");
Setter.updatePlayerClass(applicationRoot, theClass, user[0]);
} else if (!user[4].equalsIgnoreCase(theClass)) {
log.debug("Need to update user's class");
Setter.updatePlayerClass(applicationRoot, theClass, user[0]);
} else {
log.debug("User in class submitted already");
}
result = true;
} else {
TestProperties.failAndPrint(
"Could not verify that " + userName + " could authenticate at all");
}
return result;
}
@BeforeEach
public void setUp() {
TestProperties.setTestPropertiesFileDirectory(log);
}
@Test
public void testAuthUserCorrectUpperCaseCredentials() {
String userName = "authWithGoodUpperCaseCreds";
String password = "goodPassword";
try {
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName.toUpperCase(), password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
fail("Could not Authenticate as " + userName);
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserCorrectLongUsername() {
// Here is a pretty long username
String userName = "göödUsernämeWithÅDecentOKLengthÖ";
String password = "goodPassword";
try {
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
fail("Could not Authenticate as " + userName);
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserBadLongUsername() {
// Here is a pretty long username
String userName = "badUsernameWithAnIncrediblyLongLengthWhicIsTooMuch";
String password = "goodPassword";
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
try {
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
} catch (SQLException e) {
log.debug("PASS: Could not create bad username " + userName);
return;
}
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("FAIL: Could create a bad username " + userName);
fail("FAIL: Could create a bad username " + userName);
} else {
log.debug("PASS: Could not create bad username " + userName);
return;
}
}
@Test
public void testAuthUserCorrectNonLatinUsername() {
// Here is a non-latin username
String userName = "nonLatinåäöÅÄÖ";
String password = "goodPassword";
log.debug("Attempting to authenticate as " + userName);
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
try {
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
} catch (SQLException e) {
String message = "Could not create user " + userName + ": " + e.toString();
log.debug(message);
throw new RuntimeException(e);
}
log.debug("Created user " + userName);
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
log.debug("FAIL: Successfully signed in as " + userName);
fail("Could not Authenticate as " + userName);
}
}
@Test
@Disabled
public void testAuthUserCorrectEmojiUsername() {
// Here is a very non-latin username
String userName = "😃😅😍💩👍𝑓ᶃ我璃မ္ယက္";
String password = "goodEmojiPassword";
log.debug("Attempting to authenticate as " + userName);
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
try {
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
} catch (SQLException e) {
String message = "Could not create user " + userName + ": " + e.toString();
log.debug(message);
throw new RuntimeException(e);
}
log.debug("Created user " + userName);
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
log.debug("FAIL: Successfully signed in as " + userName);
fail("Could not Authenticate as " + userName);
}
}
@Test
public void testAuthUserCorrectNonLatinPassword() {
String userName = "NonLatinPass";
// Here is a very non-latin password
String password = "אذاसтьᚩᚾåäö123ÅÄÖǎ𝓫𝚌Ⴛḗ𝑓ᶃ";
try {
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
fail("Could not Authenticate as " + userName);
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserCorrectCredentialsLongPassword() {
String userName = "authWithGoodCredsLongPass";
// A 160 character password should work!
String password =
new String(
"goodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgoodPasswordgood");
try {
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
fail("Could not Authenticate as " + userName);
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserCorrectLowerCaseCredentials() {
String userName = new String("authWithGoodLowerCaseCreds");
String password = new String("goodPassword");
try {
String user[] = Getter.authUser(applicationRoot, userName, password);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, password, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName.toLowerCase(), password);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
fail("Could not Authenticate as " + userName);
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserCorrectCredentials() {
String userName = new String("authWithGoodCreds");
try {
String user[] = Getter.authUser(applicationRoot, userName, userName);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, userName, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, userName);
}
if (user != null && !user[0].isEmpty()) {
log.debug("PASS: Successfully signed in as " + userName);
return;
} else {
fail("Could not Authenticate as " + userName);
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserIncorrectCredentials() {
String userName = new String("authWithBadCreds");
try {
String user[] = Getter.authUser(applicationRoot, userName, userName);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, userName, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, userName);
}
if (user != null && !user[0].isEmpty()) {
log.debug("User " + userName + " exists. Checking if Auth Works with bad pass");
if (Getter.authUser(applicationRoot, userName, userName + "wrongPassword") == null) {
log.debug("PASS: Could not authenticate with bad pass for user " + userName);
return;
} else {
fail("Could Authenticate With Bad Pass for User " + userName);
}
} else {
fail("Couldnt verify " + userName + " could authenticate at all");
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserSqlInjection() {
String userName = new String("authWithSqliCreds");
try {
String user[] = Getter.authUser(applicationRoot, userName, userName);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, userName, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, userName);
}
if (user != null && !user[0].isEmpty()) {
log.debug("User " + userName + " exists. Checking if Auth Works with bad pass");
if (Getter.authUser(applicationRoot, userName, "'or'='1'='1") == null) {
log.debug("PASS: Could not authenticate with SQL Injection for user " + userName);
return;
} else {
fail("Could Authenticate With SQL Injection for User " + userName);
}
} else {
fail("Couldnt verify " + userName + " could authenticate at all");
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testAuthUserSqlInjectionUserName() {
String userName = new String("authWithSqli+BadPassCreds");
try {
String user[] = Getter.authUser(applicationRoot, userName, userName);
if (user == null || user[0].isEmpty()) {
log.debug("User not found in DB. Adding user to DB and Retesting before giving up");
Setter.userCreate(
applicationRoot, null, userName, userName, "player", userName + "@test.com", false);
user = Getter.authUser(applicationRoot, userName, userName);
}
if (user != null && !user[0].isEmpty()) {
log.debug("User " + userName + " exists. Checking if Auth Works with bad pass");
if (Getter.authUser(applicationRoot, "'or'='1'='1' -- ", "wrongPassword") == null) {
log.debug("PASS: Could not authenticate with SQL Injection for user " + userName);
return;
} else {
fail("Could Authenticate With SQL Injection for User Name");
}
} else {
fail("Couldnt verify " + userName + " could authenticate at all");
}
} catch (Exception e) {
log.fatal("Could not Create user: " + e.toString());
fail("Could not create user " + userName);
}
}
@Test
public void testCheckPlayerResultWhenModuleComplete() {
String userName = new String("userResultWhenComplete");
String dataStorageLessonId = new String("53a53a66cb3bf3e4c665c442425ca90e29536edd");
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
String userId = Getter.getUserIdFromName(applicationRoot, userName);
// Open all Modules First so that the Module Can Be Opened
if (Setter.openAllModules(applicationRoot, false)
&& Setter.openAllModules(applicationRoot, true)) {
// Simulate user Opening Level
if (!Getter.getModuleAddress(applicationRoot, dataStorageLessonId, userId).isEmpty()) {
// Then, Mark the Challenge Complete for user (Insecure Data Storage Lesson)
String markLevelCompleteTest =
Setter.updatePlayerResult(
applicationRoot, dataStorageLessonId, userId, "Feedback is Disabled", 1, 1, 1);
if (markLevelCompleteTest != null) {
String checkPlayerResultTest =
Getter.checkPlayerResult(applicationRoot, dataStorageLessonId, userId);
log.debug("checkPlayerResultTest" + checkPlayerResultTest);
if (checkPlayerResultTest == null) {
return; // Pass
} else {
fail("Function says user has not completed module"); // Even though this test just
// marked it as Completed
}
} else {
fail("Could not mark data storage lesson as complete for user");
}
} else {
fail("Could not Mark Data Storage Lesson as Opened by Default admin");
}
} else {
fail("Could not Open All Modules");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testCheckPlayerResultWhenModuleNotComplete() {
String userName = new String("userHasModulesOpened");
String contentProviderLeakage = new String("5b461ebe2e5e2797740cb3e9c7e3f93449a93e3a");
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
if (Setter.openAllModules(applicationRoot, false)
&& Setter.openAllModules(applicationRoot, true)) {
// Simulate user Opening Level
if (!Getter.getModuleAddress(
applicationRoot,
contentProviderLeakage,
Getter.getUserIdFromName(applicationRoot, userName))
.isEmpty()) {
String checkPlayerResultTest =
Getter.checkPlayerResult(
applicationRoot,
contentProviderLeakage,
Getter.getUserIdFromName(applicationRoot, userName));
if (checkPlayerResultTest != null) {
return; // Pass
} else {
fail("Function says user has not opened challenge or has completed challenge before");
}
} else {
fail("Could not Content Provider Leakage Lesson as Opened by user");
}
} else {
fail("Could not Mark Modules As Opened");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testCheckPlayerResultWhenModuleNotOpened() {
String userName = new String("noModulesOpened");
String unOpenedModuleId =
new String("0dbea4cb5811fff0527184f99bd5034ca9286f11"); // Insecure Direct Object
// References Module Id
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
String test = Getter.checkPlayerResult(applicationRoot, unOpenedModuleId, userName);
if (test != null) {
log.fatal("result should be null but it was: " + test);
fail(
"Function says User has opened module they should not have opened by"
+ " default"); // User Should
// not have
// completed
// this
// module by
// default
// after
// running a
// fresh DB.
// ensure
// you have
// a fresh
// DB if
// this
// fails
} else {
log.debug("PASS: Function says user has not opened module");
return; // Pass
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testCheckPlayerResultWhenModuleWhenOpened() {
String userName = new String("userHasModulesOpened");
String csrfChallengeThree = new String("5ca9115f3279b9b9f3308eb6a59a4fcd374846d6");
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
if (Setter.openAllModules(applicationRoot, false)
&& Setter.openAllModules(applicationRoot, true)) {
// Simulate user Opening Level
if (!Getter.getModuleAddress(
applicationRoot,
csrfChallengeThree,
Getter.getUserIdFromName(applicationRoot, userName))
.isEmpty()) {
String test =
Getter.checkPlayerResult(
applicationRoot,
csrfChallengeThree,
Getter.getUserIdFromName(applicationRoot, userName));
if (test == null) {
fail(
"Function says "
+ userName
+ " has not opened module"); // User Should have opened and
// not completed CSRF Three
} else {
return; // Pass
}
} else {
fail("Could not Mark CSRF 3 as Opened by " + userName);
}
} else {
fail("Could not Mark Modules As Opened");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testFindPlayerById() {
String userName = new String("UserForPlayerIdSearch");
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
String userId = Getter.getUserIdFromName(applicationRoot, userName);
if (Getter.findPlayerById(applicationRoot, userId)) {
log.debug("PASS: Found user");
return;
} else {
fail("Could Not Find Player in Player Search");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testFindPlayerByIdWithAdminId() {
String userName = new String("playerSearchWithAdmin");
try {
if (verifyTestAdmin(applicationRoot, userName, userName)) {
String userId = Getter.getUserIdFromName(applicationRoot, userName);
if (!Getter.findPlayerById(applicationRoot, userId)) {
return;
} else {
fail("Found Admin in Player Search");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testFindPlayerByIdWithBadUserId() {
String userId = new String("DOES NOT EXIST");
if (!Getter.findPlayerById(applicationRoot, userId)) {
return;
} else {
fail("Found Player That Does Not Exist");
}
}
@Test
public void testGetAllModuleInfo() {
ArrayList<String[]> modules = Getter.getAllModuleInfo(applicationRoot);
if (modules.size()
> 75) // Shepherd v3.0 has 76 Modules. If less than All are Returned, then there is a
// problem with the Open Modules Function or the Retrieve data function
{
log.debug("PASS: Found " + modules.size() + " modules");
return;
} else {
log.fatal("Too Few Modules Returned to Pass Test: " + modules.size());
fail("Only " + modules.size() + "/~76 modules returned from function");
}
}
@Test
public void testGetChallenges() {
String userName = new String("testGetChallengesUser");
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
String userId = Getter.getUserIdFromName(applicationRoot, userName);
// Open all Modules First so that the GetAllModuleInfo method will return data
if (Setter.openAllModules(applicationRoot, false)
&& Setter.openAllModules(applicationRoot, true)) {
String modules = Getter.getChallenges(applicationRoot, userId, locale);
if (!modules.isEmpty()) // Some Modules were included in response
{
// Get number of Challenges returned by getChallenges method
int numberofChallengesReturned =
(modules.length() - modules.replace("class='lesson'", "").length())
/ "class='lesson'".length();
if (numberofChallengesReturned > totalNumberOfModulesInShepherd) {
log.debug("PASS: Found " + numberofChallengesReturned + " modules");
return;
} else {
log.debug(
"Too Few Challenges Returned to pass: "
+ numberofChallengesReturned
+ " returned. Expected at least:"
+ totalNumberOfModulesInShepherd);
fail("Too Few Challenges Returned to Pass");
}
} else {
log.fatal("No Modules Found. Returned empty String");
fail("No Modules Found");
}
} else {
fail("Could Not Mark Modules as Open Before Test");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testGetChallengesWhenModulesClosed() {
String userName = new String("getChallengesCLosedUser");
try {
if (verifyTestUser(applicationRoot, userName, userName)) {
String userId = Getter.getUserIdFromName(applicationRoot, userName);
// Open all Modules First so that the GetAllModuleInfo method will return data
if (Setter.closeAllModules(applicationRoot)) {
String modules = Getter.getChallenges(applicationRoot, userId, locale);
if (!modules.isEmpty()) // Some Modules were included in response
{
// Get number of Challenges returned by getChallenges method
int numberofChallengesReturned =
(modules.length() - modules.replace("class='lesson'", "").length())
/ "class='lesson'".length();
if (!(numberofChallengesReturned > 0)) {
log.debug("PASS: Found " + numberofChallengesReturned + " modules");
return;
} else {
log.debug(
"Too Many Challenges Returned to pass: "
+ numberofChallengesReturned
+ " returned");
fail("Challenges Returned when all modules were closed");
}
} else {
log.fatal("No Modules Found. Returned empty String");
fail("No Modules Found");
}
} else {
fail("Could Not Mark Modules as Open Before Test");
}
} else {
fail("Could not verify user (No Exception Failure)");
}
} catch (Exception e) {
log.fatal("Could not Verify User: " + e.toString());
fail("Could not Verify User " + userName);
}
}
@Test
public void testGetClassCount() {
String className = new String("NewClassForGetCount");
try {
findCreateClassId(className);
} catch (Exception e) {
log.fatal("Could not Find or Create Class : " + e.toString());
fail("Could not Create/Find Class");
}
int classCount = Getter.getClassCount(applicationRoot);
if (classCount < 1) {
fail("Class Count Too Low to Pass");
} else {
log.debug("PASS: Atleast One Class Returned");
return;
}
}
@Test
public void testGetClassInfoString() throws SQLException {
findCreateClassId("NewClassForGetInfo");
ResultSet rs = Getter.getClassInfo(applicationRoot);
if (rs.next()) {
if (!rs.getString(1).isEmpty()) {
log.debug("PASS: Class Information was returned");
} else {
fail("Data in Class Info Result Set was Blank");
}
} else {
fail("No Rows In Class Info Result Set");
}
rs.close();
}
@Test
public void testGetClassInfoStringString() {
String classId = new String();
String className = new String("NewClassForGetInfo2");
try {
findCreateClassId(className);
} catch (Exception e) {
log.fatal("Could not Find or Create Class : " + e.toString());
fail("Could not Create/Find Class");
}
try {
ResultSet rs = Getter.getClassInfo(applicationRoot);
while (rs.next()) {
if (rs.getString(2).equalsIgnoreCase("NewClassForGetInfo2")) {
classId = rs.getString(1);
break;
}
}
rs.close();
if (classId.isEmpty()) {
fail("Could not Find Class ID in Get Info Result");
} else {
String[] classInfo = Getter.getClassInfo(applicationRoot, classId);
if (classInfo[0].equalsIgnoreCase("NewClassForGetInfo2")
&& classInfo[1].equalsIgnoreCase("2015")) {
log.debug("PASS: Expected Data Returned from getClassInfo");
} else {
if (!classInfo[0].equalsIgnoreCase("NewClassForGetInfo2")) {
fail("Incorrect Class Name returned from getClassInfo");
} else if (!classInfo[1].equalsIgnoreCase("2015")) {
fail("Incorrect Class Year returned from getClassInfo");
} else {
fail("Unexpected Failure");
}
}
}
} catch (Exception e) {
log.fatal("ClassInfo Failure: " + e.toString());
fail("Could not open ClassInfo Result Set");
}
}
@Test
public void testGetCsrfForumWithIframe() {
String classId = new String();
String moduleId = new String("0a37cb9296ff3763f7f3a45ff313bce47afa9384"); // CSRF Challenge 5
Locale locale = new Locale("en_GB");
ResourceBundle bundle =
ResourceBundle.getBundle("i18n.servlets.challenges.csrf.csrfGenerics", locale);
try {
classId = findCreateClassId("NewClassForCsrfIframeFourm");
String userName = new String("userforiframeclass");
if (verifyTestUser(applicationRoot, userName, userName, classId)) {
// Open all Modules First so that the Module Can Be Opened by the user
if (Setter.openAllModules(applicationRoot, false)
&& Setter.openAllModules(applicationRoot, true)) {
// Simulate user Opening Level
if (!Getter.getModuleAddress(
applicationRoot, moduleId, Getter.getUserIdFromName(applicationRoot, userName))
.isEmpty()) {
String csrfFourm =
Getter.getCsrfForumWithIframe(applicationRoot, classId, moduleId, bundle);
if (csrfFourm.indexOf(userName) > -1) {
log.debug("PASS: User was found in the fourm");
return;
} else {
log.error("Could not find user name '" + userName + "' in this: " + csrfFourm);
fail("User was not contained in the CSRF iFrame Forum");
}
} else {
fail("Could not open CSRF 5 as Created User");
}
} else {
fail("Could not Mark All Modules as Open");
}
} else {
fail("Could not verify user (No Exception Failure)");
}