Review comments processed:

- Ports can now be changed
- User is now a default user making it easier to login and look around after a failure
This commit is contained in:
Nanne Baars 2019-09-08 18:52:12 +02:00
parent 2283f945a9
commit 0982bd982c

View File

@ -17,10 +17,15 @@ import java.net.ServerSocket;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static io.restassured.RestAssured.given;
public abstract class IntegrationTest { public abstract class IntegrationTest {
private static String WEBGOAT_URL = "http://localhost:8080/WebGoat/"; protected static int WG_PORT = 8080;
private static String WEBWOLF_URL = "http://localhost:9090/"; protected static int WW_PORT = 9090;
private static String WEBGOAT_URL = "http://127.0.0.1:" + WG_PORT + "/WebGoat/";
private static String WEBWOLF_URL = "http://127.0.0.1:" + WW_PORT + "/";
//This also allows to test the application with HTTPS when outside testing option is used //This also allows to test the application with HTTPS when outside testing option is used
protected static RestAssuredConfig restConfig = RestAssuredConfig.newConfig().sslConfig(new SSLConfig().relaxedHTTPSValidation()); protected static RestAssuredConfig restConfig = RestAssuredConfig.newConfig().sslConfig(new SSLConfig().relaxedHTTPSValidation());
@ -38,23 +43,19 @@ public abstract class IntegrationTest {
public static void beforeAll() { public static void beforeAll() {
if (!started) { if (!started) {
started = true; started = true;
if (!areAlreadyRunning()) { if (!isAlreadyRunning()) {
SpringApplicationBuilder wgs = new SpringApplicationBuilder(StartWebGoat.class) SpringApplicationBuilder wgs = new SpringApplicationBuilder(StartWebGoat.class)
.properties(Map.of("spring.config.name", "application-webgoat")); .properties(Map.of("spring.config.name", "application-webgoat", "WEBGOAT_PORT", WG_PORT));
wgs.run(); wgs.run();
SpringApplicationBuilder wws = new SpringApplicationBuilder(WebWolf.class) SpringApplicationBuilder wws = new SpringApplicationBuilder(WebWolf.class)
.properties(Map.of("spring.config.name", "application-webwolf")); .properties(Map.of("spring.config.name", "application-webwolf", "WEBWOLF_PORT", WW_PORT));
wws.run(); wws.run();
} }
} }
} }
private static boolean areAlreadyRunning() { private static boolean isAlreadyRunning() {
return checkIfServerIsRunnningOn(9090) && checkIfServerIsRunnningOn(8080); try (var ignored = new ServerSocket(WG_PORT)) {
}
private static boolean checkIfServerIsRunnningOn(int port) {
try (var ignored = new ServerSocket(port)) {
return false; return false;
} catch (IOException e) { } catch (IOException e) {
return true; return true;
@ -75,6 +76,16 @@ public abstract class IntegrationTest {
@Before @Before
public void login() { public void login() {
String location = given()
.when()
.config(restConfig)
.formParam("username", webgoatUser)
.formParam("password", "password")
.post(url("login")).then()
.cookie("JSESSIONID")
.statusCode(302)
.extract().header("Location");
if (location.endsWith("?error")) {
webGoatCookie = RestAssured.given() webGoatCookie = RestAssured.given()
.when() .when()
.config(restConfig) .config(restConfig)
@ -88,6 +99,17 @@ public abstract class IntegrationTest {
.statusCode(302) .statusCode(302)
.extract() .extract()
.cookie("JSESSIONID"); .cookie("JSESSIONID");
} else {
webGoatCookie = given()
.when()
.config(restConfig)
.formParam("username", webgoatUser)
.formParam("password", "password")
.post(url("login")).then()
.cookie("JSESSIONID")
.statusCode(302)
.extract().cookie("JSESSIONID");
}
webWolfCookie = RestAssured.given() webWolfCookie = RestAssured.given()
.when() .when()
@ -107,7 +129,7 @@ public abstract class IntegrationTest {
RestAssured.given() RestAssured.given()
.when() .when()
.config(restConfig) .config(restConfig)
.get(WEBGOAT_URL + "logout") .get(url("logout"))
.then() .then()
.statusCode(200); .statusCode(200);
} }