Repository navigation
Expand file tree
/
Copy pathOpenAllModulesIT.java
More file actions
169 lines (135 loc) · 5.72 KB
/
Copy pathOpenAllModulesIT.java
File metadata and controls
169 lines (135 loc) · 5.72 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
package servlets.admin.moduleManagement;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.InstallationException;
public class OpenAllModulesIT {
private static final Logger log = LogManager.getLogger(OpenAllModulesIT.class);
private static final String LANGUAGE_CODE = "en_GB";
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private static String[] testUsers = {
"configAdminTester", "configUserTester", "unauthenticatedUser"
};
private static final String MODULE_CLASS_NAME = "OpenAllModules";
/**
* Sets up DB with levels and users to test with
*
* @throws InstallationException
* @throws SQLException
* @throws IOException
*/
@BeforeAll
public static void readyDb() throws SQLException, IOException {
testUtils.TestProperties.setTestPropertiesFileDirectory(log);
TestProperties.ensureSchemaReady(log);
TestProperties.reseedTestData();
log.debug("Creating - user: " + testUsers[0] + " password: " + testUsers[0]);
TestProperties.verifyTestAdmin(log, null, testUsers[0], testUsers[0]);
log.debug("Creating - user: " + testUsers[1] + " password: " + testUsers[1]);
TestProperties.verifyTestUser(log, null, testUsers[1], testUsers[0]);
}
@BeforeEach
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
private String doMockPost(String csrfToken, String unsafe) {
int expectedResponseCode = 302;
log.debug("Creating " + MODULE_CLASS_NAME + " Servlet Instance");
OpenAllModules servlet = new OpenAllModules();
try {
servlet.init(new MockServletConfig(MODULE_CLASS_NAME));
} catch (ServletException e) {
TestProperties.failAndPrint(
"Could not initialize Servlet Instance for module "
+ MODULE_CLASS_NAME
+ ": "
+ e.toString());
}
// Adding Correct CSRF Token (Token Submitted)
request.addParameter("csrfToken", csrfToken);
request.addParameter("unsafeLevels", unsafe);
log.debug("Running doPost");
try {
servlet.doPost(request, response);
} catch (ServletException | IOException e) {
TestProperties.failAndPrint("Could not post Servlet: " + e.toString());
}
assertFalse(response.getStatus() != expectedResponseCode);
String result = "";
try {
result = response.getContentAsString();
} catch (UnsupportedEncodingException e) {
TestProperties.failAndPrint("Could not get Servlet response: " + e.toString());
}
return (result);
}
@Test
public void testWithUserAuth() {
log.debug("Signing in as Normal User Through LoginServlet");
testUtils.TestProperties.loginDoPost(
log, request, response, testUsers[1], testUsers[1], null, LANGUAGE_CODE);
log.debug("Login Servlet Complete, Getting CSRF Token");
String csrfToken = response.getCookie("token").getValue();
assertFalse(csrfToken.isEmpty());
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = doMockPost(csrfToken, "enable");
assertTrue(responseBody.contains("loggedOutSheep"));
}
@Test
public void testWithAdminAuthUnsafeLevelsClosed() {
String unsafe = "disable";
log.debug("Signing in as Admin User Through LoginServlet");
TestProperties.loginDoPost(
log, request, response, testUsers[0], testUsers[0], null, LANGUAGE_CODE);
log.debug("Login Servlet Complete, Getting CSRF Token");
String csrfToken = response.getCookie("token").getValue();
assertFalse(csrfToken.isEmpty());
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = doMockPost(csrfToken, unsafe);
log.debug("Response Body: " + responseBody);
assertTrue(responseBody.contains("All Modules are Now Open"));
}
@Test
public void testWithAdminAuthUnsafeLevelsOpen() {
String unsafe = "enable";
log.debug("Signing in as Admin User Through LoginServlet");
TestProperties.loginDoPost(
log, request, response, testUsers[0], testUsers[0], null, LANGUAGE_CODE);
log.debug("Login Servlet Complete, Getting CSRF Token");
String csrfToken = response.getCookie("token").getValue();
assertFalse(csrfToken.isEmpty());
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = doMockPost(csrfToken, unsafe);
assertTrue(responseBody.contains("[WARNING] Server is vulnerable. Unsafe levels open!"));
assertTrue(responseBody.contains("All Modules are Now Open"));
}
@Test
public void testWithUnauthenticatedUser() {
String unsafe = "enable";
log.debug("Signing in as Admin User Through LoginServlet");
TestProperties.loginDoPost(
log, request, response, testUsers[2], testUsers[2], null, LANGUAGE_CODE);
log.debug("Login Servlet Complete, Getting CSRF Token");
// Add Cookies from Response to outgoing request
request.setCookies(response.getCookies());
String responseBody = doMockPost("test", unsafe);
assertTrue(responseBody.contains("loggedOutSheep"));
}
}