Skip to content

Commit

Permalink
Reworked tests to be integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moonraker595 committed Sep 5, 2024
1 parent 70b614c commit b1b9931
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 136 deletions.
56 changes: 56 additions & 0 deletions src/test/java/org/icatproject/authn_db/AuthenticateIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.icatproject.authn_db;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

@QuarkusIntegrationTest
public class AuthenticateIT {

@Test
public void testValidLoginUser() {
// JSON string to be sent as form data
String jsonString = "{\"credentials\":[{\"username\":\"user1\"},{\"password\":\"sunshine\"}]}";

given()
.header("Content-Type", "application/x-www-form-urlencoded")
.formParam("json", jsonString)
.when()
.post("/authn.db/authenticate")
.then()
.statusCode(Response.Status.OK.getStatusCode()) // Expect a 200 OK status
.body("username", equalTo("user1")) // Validate the response body
.body("mechanism", equalTo("db")); // Validate the response body
}
@Test
public void testInvalidUsername() {
String jsonString = "{\"credentials\":[{\"username\":\"invaliduser\"},{\"password\":\"sunshine\"}]}";

// Perform an HTTP POST with invalid username, sending the JSON as a form parameter
given()
.header("Content-Type", "application/x-www-form-urlencoded") // Set Content-Type for form-urlencoded
.formParam("json", jsonString) // Send the JSON string as a form parameter with the key 'json'
.when()
.post("/authn.db/authenticate") // Ensure the path is correct
.then()
.statusCode(Response.Status.FORBIDDEN.getStatusCode()) // Expect 403 Forbidden
.body("message", equalTo("The username and password do not match"));
}
@Test
public void testInvalidPassword() {
String jsonString = "{\"credentials\":[{\"username\":\"user1\"},{\"password\":\"trainspotting\"}]}";

// Perform an HTTP POST with invalid password, sending the JSON as a form parameter
given()
.header("Content-Type", "application/x-www-form-urlencoded") // Set Content-Type for form-urlencoded
.formParam("json", jsonString) // Send the JSON string as a form parameter with the key 'json'
.when()
.post("/authn.db/authenticate") // Ensure the path is correct
.then()
.statusCode(Response.Status.FORBIDDEN.getStatusCode()) // Expect 403 Forbidden
.body("message", equalTo("The username and password do not match"));
}
}
20 changes: 20 additions & 0 deletions src/test/java/org/icatproject/authn_db/DescriptionIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.icatproject.authn_db;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.equalTo;

@QuarkusIntegrationTest
public class DescriptionIT {

@Test
public void getDescription() {
RestAssured.given()
.when().get("/authn.db/description")
.then()
.statusCode(200)
.body(equalTo("{\"keys\":[{\"name\":\"username\"},{\"name\":\"password\",\"hide\":true}]}"));
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/icatproject/authn_db/IPTestProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IPTestProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"ip", "192.168.0.1/24 130.10.0.1/24" // Override config property
"ip", "192.168.0.1/24 130.10.0.1/24" // Override config property and add ips
);
}
}
49 changes: 0 additions & 49 deletions src/test/java/org/icatproject/authn_db/IPTests.java

This file was deleted.

61 changes: 61 additions & 0 deletions src/test/java/org/icatproject/authn_db/IPTestsIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.icatproject.authn_db;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.quarkus.test.junit.TestProfile;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

@QuarkusIntegrationTest
@TestProfile(IPTestProfile.class)
public class IPTestsIT {

@Test
public void testNoIpInRequest() {
String jsonString = "{\"credentials\":[{\"username\":\"user1\"},{\"password\":\"sunshine\"}]}";

// Perform an HTTP POST request without IP in the request body
given()
.header("Content-Type", "application/x-www-form-urlencoded") // Set Content-Type for form-urlencoded
.formParam("json", jsonString) // Send the JSON string as a form parameter with the key 'json'
.when()
.post("/authn.db/authenticate")
.then()
.statusCode(Response.Status.BAD_REQUEST.getStatusCode()) // Expect 400 Bad Request
.body("message", equalTo("An Ip address must be provided"));
}

@Test
public void badIpInRequest() {
String jsonString = "{\"credentials\":[{\"username\":\"user1\"},{\"password\":\"sunshine\"}], \"ip\":\"192.167.0.125\"}";

// Perform an HTTP POST request with a bad IP address
given()
.header("Content-Type", "application/x-www-form-urlencoded") // Set Content-Type for form-urlencoded
.formParam("json", jsonString) // Send the JSON string as a form parameter with the key 'json'
.when()
.post("/authn.db/authenticate")
.then()
.statusCode(Response.Status.FORBIDDEN.getStatusCode()) // Expect 403 Forbidden
.body("message", equalTo("authn_db does not allow log in from your IP address 192.167.0.125"));
}

@Test
public void goodIpInRequest() {
String jsonString = "{\"credentials\":[{\"username\":\"user1\"},{\"password\":\"sunshine\"}], \"ip\":\"192.168.0.125\"}";

// Perform an HTTP POST request with a valid IP address
given()
.header("Content-Type", "application/x-www-form-urlencoded") // Set Content-Type for form-urlencoded
.formParam("json", jsonString) // Send the JSON string as a form parameter with the key 'json'
.when()
.post("/authn.db/authenticate")
.then()
.statusCode(Response.Status.OK.getStatusCode()) // Expect 200 OK
.body("username", equalTo("user1"))
.body("mechanism", equalTo("db")); // Adjust this based on your actual mechanism
}

}
50 changes: 0 additions & 50 deletions src/test/java/org/icatproject/authn_db/TestAuthenticate.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/test/java/org/icatproject/authn_db/TestGetDescription.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.inject.Inject;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.RestAssured;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.StringReader;

@QuarkusTest
public class TestGetVersion {

@Inject
DB_Authenticator authn;
@QuarkusIntegrationTest
public class VersionIT {

@Test
public void testVersion() throws Exception {
// Get the version from the pom file
// Get the version from the pom.xml
String expectedVersion = getVersionFromPom();

// Set the projectVersion field to simulate injection (if needed)
authn.projectVersion = expectedVersion;

// Call the getVersion method
String versionResponse = authn.getVersion();
// Send a request to the version endpoint
String versionResponse = RestAssured.given()
.when().get("/authn.db/version")
.then()
.statusCode(200)
.extract().asString();

// Parse the JSON response
JsonObject versionJson;
try (JsonReader jsonReader = Json.createReader(new java.io.StringReader(versionResponse))) {
try (JsonReader jsonReader = Json.createReader(new StringReader(versionResponse))) {
versionJson = jsonReader.readObject();
}

Expand All @@ -55,4 +54,4 @@ private String getVersionFromPom() throws Exception {
Element versionElement = (Element) doc.getElementsByTagName("version").item(0);
return versionElement.getTextContent();
}
}
}

0 comments on commit b1b9931

Please sign in to comment.