Repository navigation
Expand file tree
/
Copy pathEnableScoreboardIT.java
More file actions
406 lines (383 loc) · 16.1 KB
/
Copy pathEnableScoreboardIT.java
File metadata and controls
406 lines (383 loc) · 16.1 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
package servlets.admin.config;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.sql.SQLException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletConfig;
import testUtils.TestProperties;
import utils.ScoreboardStatus;
public class EnableScoreboardIT {
private static final Logger log = LogManager.getLogger(EnableScoreboardIT.class);
private static String applicationRoot = new String();
private static String lang = "en_GB";
private MockHttpServletRequest request;
private MockHttpServletResponse response;
/** 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();
}
@BeforeEach
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
ScoreboardStatus.disableScoreboard();
// Close Scoreboard
if (ScoreboardStatus.isScoreboardEnabled()) {
log.fatal("Unable to Disable Scoreboard");
fail("Unable to Disable Scoreboard");
}
}
/**
* Method to Simulate the interaction with the enableScoreboard servlet.
*
* @param moduleId The ID of the Module to Search For
* @param csrfToken The CSRF Token of the User
* @return The Content of the Response (Which is supposed to be the location of the module)
* @throws Exception
*/
public String enableScoreboardDoPost(String classId, String csrfToken) throws Exception {
return enableScoreboardDoPost(classId, csrfToken, "");
}
/**
* Method to Simulate the interaction with the enableScoreboard servlet.
*
* @param moduleId The ID of the Module to Search For
* @param csrfToken The CSRF Token of the User
* @return The Content of the Response (Which is supposed to be the location of the module)
* @throws Exception
*/
public String enableScoreboardDoPost(String classId, String csrfToken, String restricted)
throws Exception {
if (classId == null) {
classId = new String();
}
int expectedResponseCode = 302;
log.debug("Creating EnableScoreboard Servlet Instance");
EnableScoreboard servlet = new EnableScoreboard();
servlet.init(new MockServletConfig("EnableScoreboard"));
// Setup Servlet Parameters and Attributes
log.debug("Setting Up Params and Atrributes");
request.addParameter("classId", classId);
request.addParameter("restricted", restricted);
// Adding Correct CSRF Token (Token Submitted)
request.addParameter("csrfToken", csrfToken);
log.debug("Running doPost");
servlet.doPost(request, response);
if (response.getStatus() != expectedResponseCode) {
fail(
"EnableScoreboard Servlet Returned "
+ response.getStatus()
+ " Code. "
+ expectedResponseCode
+ " Expected");
} else {
log.debug(expectedResponseCode + " Detected");
log.debug(
"Servlet Successful, returning response retrieved: " + response.getContentAsString());
return (response.getContentAsString());
}
return null;
}
/** This test checks that non admin users get access errors when enabling the scoreboard */
@Test
public void testNormalUserScoreboardCall() {
String userName = "enableScore1";
String password = userName;
// Verify / Create user in DB
try {
TestProperties.verifyTestUser(log, applicationRoot, userName, password);
// Sign in as Normal User
log.debug("Signing in as User Through LoginServlet");
TestProperties.loginDoPost(log, request, response, userName, userName, null, lang);
log.debug("Login Servlet Complete, Getting CSRF Token");
if (response.getCookie("token") == null) {
fail("No CSRF Token Was Returned from Login Servlet");
}
String csrfToken = response.getCookie("token").getValue();
if (csrfToken.isEmpty()) {
String message = new String("No CSRF token returned from Login Servlet");
log.fatal(message);
fail(message);
} else {
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = enableScoreboardDoPost(null, csrfToken);
if (responseBody.contains("Please try non administrator functions")) {
log.debug("No Admin Access Expected Result Received");
} else {
String message = "Normal user did not get error when performing admin function";
log.fatal(message);
fail(message);
}
}
} catch (Exception e) {
log.fatal("Could not Complete testValidEnableScoreboardCall: " + e.toString());
fail("Could not Complete testValidEnableScoreboardCall");
}
}
/** This test checks that admin users can completely open the scoreboard (Null classID) */
@Test
public void testUserScoreboardCompleteOpen() {
String userName = "enableScore2";
String password = userName;
// Verify / Create user in DB
try {
TestProperties.verifyTestAdmin(log, applicationRoot, userName, password);
// Sign in as Normal User
log.debug("Signing in as User Through LoginServlet");
TestProperties.loginDoPost(log, request, response, userName, userName, null, lang);
log.debug("Login Servlet Complete, Getting CSRF Token");
if (response.getCookie("token") == null) {
fail("No CSRF Token Was Returned from Login Servlet");
}
String csrfToken = response.getCookie("token").getValue();
if (csrfToken.isEmpty()) {
String message = new String("No CSRF token returned from Login Servlet");
log.fatal(message);
fail(message);
} else {
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = enableScoreboardDoPost(null, csrfToken);
String expectedResult =
"Scoreboard is now enabled and lists all users regardless of their class";
if (responseBody.contains(expectedResult)) {
log.debug("Scoreboard was completely opened");
if (ScoreboardStatus.canSeeScoreboard("player")) {
log.debug("Users can see the scoreboard");
} else {
String message =
"Players cannot view scoreboard and they should be able to after this test";
log.fatal(message);
fail(message);
}
} else {
String message = "Did not get expected result message. Expected '" + expectedResult + "'";
log.fatal(message);
fail(message);
}
}
} catch (Exception e) {
log.fatal("Could not Complete testUserScoreboardCompleteOpen: " + e.toString());
fail("Could not Complete testUserScoreboardCompleteOpen");
}
}
/** This test checks that admin users can open the scoreboard for class specific scoreboards */
@Test
public void testUserScoreboardClassSpecificOpen() {
String userName = "enableScore2";
String password = userName;
// Verify / Create user in DB
try {
TestProperties.verifyTestAdmin(log, applicationRoot, userName, password);
// Sign in as Normal User
log.debug("Signing in as User Through LoginServlet");
TestProperties.loginDoPost(log, request, response, userName, userName, null, lang);
log.debug("Login Servlet Complete, Getting CSRF Token");
if (response.getCookie("token") == null) {
fail("No CSRF Token Was Returned from Login Servlet");
}
String csrfToken = response.getCookie("token").getValue();
if (csrfToken.isEmpty()) {
String message = new String("No CSRF token returned from Login Servlet");
log.fatal(message);
fail(message);
} else {
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = enableScoreboardDoPost("classSpecific", csrfToken);
String expectedResult =
"Scoreboard has been enabled and only lists users from the viewer's class. Admin users"
+ " will still see the scoreboard of the default class.";
if (responseBody.contains(expectedResult)) {
log.debug("Scoreboard was completely opened");
} else {
String message = "Did not get expected result message. Expected '" + expectedResult + "'";
log.fatal(message);
fail(message);
}
}
} catch (Exception e) {
log.fatal("Could not Complete testUserScoreboardClassSpecificOpen: " + e.toString());
fail("Could not Complete testUserScoreboardClassSpecificOpen");
}
}
/**
* This test checks that admin users cannot open the scoreboard for an invalid class specific
* scoreboards
*/
@Test
public void testUserScoreboardInvalidClass() {
String userName = "enableScore2";
String password = userName;
// Verify / Create user in DB
try {
TestProperties.verifyTestAdmin(log, applicationRoot, userName, password);
// Sign in as Normal User
log.debug("Signing in as User Through LoginServlet");
TestProperties.loginDoPost(log, request, response, userName, userName, null, lang);
log.debug("Login Servlet Complete, Getting CSRF Token");
if (response.getCookie("token") == null) {
fail("No CSRF Token Was Returned from Login Servlet");
}
String csrfToken = response.getCookie("token").getValue();
if (csrfToken.isEmpty()) {
String message = new String("No CSRF token returned from Login Servlet");
log.fatal(message);
fail(message);
} else {
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = enableScoreboardDoPost("notAClassId", csrfToken);
String expectedResult = "Invalid data was submitted. Please try again.";
if (responseBody.contains(expectedResult)) {
log.debug("Scoreboard was unchanged");
} else {
String message = "Did not get expected result message. Expected '" + expectedResult + "'";
log.fatal(message);
fail(message);
}
}
} catch (Exception e) {
log.fatal("Could not Complete testUserScoreboardClassSpecificOpen: " + e.toString());
fail("Could not Complete testUserScoreboardClassSpecificOpen");
}
}
/**
* This test checks that admin users can configure the scoreboard to be open with a specific class
*/
@Test
public void testUserScoreboardOpenValidClass() {
String userName = "enableScore5";
String password = userName;
// Verify / Create user in DB
try {
String className1 = "scoreEnableClass";
String classId = TestProperties.findCreateClassId(log, className1, applicationRoot);
TestProperties.verifyTestAdmin(log, applicationRoot, userName, password, classId);
// Sign in as Admin User
log.debug("Signing in as User Through LoginServlet");
TestProperties.loginDoPost(log, request, response, userName, userName, null, lang);
log.debug("Login Servlet Complete, Getting CSRF Token");
if (response.getCookie("token") == null) {
fail("No CSRF Token Was Returned from Login Servlet");
}
String csrfToken = response.getCookie("token").getValue();
if (csrfToken.isEmpty()) {
String message = new String("No CSRF token returned from Login Servlet");
log.fatal(message);
fail(message);
} else {
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = enableScoreboardDoPost(classId, csrfToken);
String expectedResult = "Scoreboard has been enabled and only lists users from ";
if (responseBody.contains(expectedResult)) {
log.debug("Scoreboard was changed");
String scoreboardClass = ScoreboardStatus.getScoreboardClass();
if (scoreboardClass.equalsIgnoreCase(classId)) {
log.debug("Correct Class was set");
} else {
String message =
"Did not update scoreboard setting to list specific class. Expected '"
+ classId
+ "' but got '"
+ scoreboardClass
+ "'";
log.fatal(message);
fail(message);
}
} else {
String message = "Did not get expected result message. Expected '" + expectedResult + "'";
log.fatal(message);
fail(message);
}
}
} catch (Exception e) {
log.fatal("Could not Complete testUserScoreboardOpenValidClass: " + e.toString());
fail("Could not Complete testUserScoreboardOpenValidClass");
}
}
/**
* This test checks that admin users can configure the scoreboard to be open with a specific class
* for admins only
*/
@Test
public void testUserScoreboardRestrictedValidClass() {
String userName = "enableScore5";
String password = userName;
// Verify / Create user in DB
try {
String className1 = "scoreEnableClass";
String classId = TestProperties.findCreateClassId(log, className1, applicationRoot);
TestProperties.verifyTestAdmin(log, applicationRoot, userName, password, classId);
// Sign in as Admin User
log.debug("Signing in as User Through LoginServlet");
TestProperties.loginDoPost(log, request, response, userName, userName, null, lang);
log.debug("Login Servlet Complete, Getting CSRF Token");
if (response.getCookie("token") == null) {
fail("No CSRF Token Was Returned from Login Servlet");
}
String csrfToken = response.getCookie("token").getValue();
if (csrfToken.isEmpty()) {
String message = new String("No CSRF token returned from Login Servlet");
log.fatal(message);
fail(message);
} else {
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = enableScoreboardDoPost(classId, csrfToken, "true");
String expectedResult = "Scoreboard has been enabled and only lists users from ";
if (responseBody.contains(expectedResult)) {
log.debug("Scoreboard was changed");
String scoreboardClass = ScoreboardStatus.getScoreboardClass();
if (scoreboardClass.equalsIgnoreCase(classId)) {
log.debug("Correct Class was set");
if (ScoreboardStatus.canSeeScoreboard("admin")) {
log.debug("Admins can see scoreboard but players cannot");
if (!ScoreboardStatus.canSeeScoreboard("player")) {
log.debug("Users cannot see the scoreboard");
} else {
String message =
"Players can view scoreboard and they should not be able to after this test";
log.fatal(message);
fail(message);
}
} else {
String message =
"Admins cannot view scoreboard and they should be able to after this test";
log.fatal(message);
fail(message);
}
} else {
String message =
"Did not update Scoreboard Setting to list specific class. Expected '"
+ classId
+ "' but got '"
+ scoreboardClass
+ "'";
log.fatal(message);
fail(message);
}
} else {
String message = "Did not get expected result message. Expected '" + expectedResult + "'";
log.fatal(message);
fail(message);
}
}
} catch (Exception e) {
log.fatal("Could not Complete testUserScoreboardRestrictedValidClass: " + e.toString());
fail("Could not Complete testUserScoreboardRestrictedValidClass");
}
}
}