diff --git a/pom.xml b/pom.xml index a2ee6a0..2239f21 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ 3.12.0 3.2.8-RELEASE + 5.4.0 2.4.2 3.13.0 @@ -91,6 +92,13 @@ 3.26.0 test + + + io.rest-assured + rest-assured + ${rest-assured.version} + test + diff --git a/src/main/java/ch/postfinance/swiss/hacks/service/LoginService.java b/src/main/java/ch/postfinance/swiss/hacks/service/LoginService.java index 1b171bf..7981728 100644 --- a/src/main/java/ch/postfinance/swiss/hacks/service/LoginService.java +++ b/src/main/java/ch/postfinance/swiss/hacks/service/LoginService.java @@ -3,11 +3,13 @@ import ch.postfinance.swiss.hacks.domain.Login; import jakarta.enterprise.context.ApplicationScoped; import jakarta.transaction.Transactional; +import jakarta.ws.rs.BadRequestException; import java.security.SecureRandom; import java.time.Instant; import static ch.postfinance.swiss.hacks.domain.Login.newLogin; +import static org.apache.commons.lang3.StringUtils.isEmpty; @ApplicationScoped public class LoginService { @@ -17,6 +19,9 @@ public class LoginService { @Transactional public RegistrationInformation register(String firstName, String lastName, Instant dateOfBirth) { // TODO: Some verification/validity checks, probably + if (isEmpty(firstName)){ + throw new BadRequestException("First name is required"); + } var password = String.valueOf(SECURE_RANDOM.nextInt(100_000, 100_000_000)); var login = newLogin(firstName, lastName, dateOfBirth, password); diff --git a/src/main/resources/openapi/openapi.yml b/src/main/resources/openapi/openapi.yml index c93f7bc..5d3264d 100644 --- a/src/main/resources/openapi/openapi.yml +++ b/src/main/resources/openapi/openapi.yml @@ -10,7 +10,7 @@ paths: /customers/register: post: summary: Register a new customer - description: Allows a new customer to register for an online banking account. + description: Allows a new customer to register for an online banking account. U can later login using the returned credentials. requestBody: required: true content: diff --git a/src/test/java/ch/postfinance/swiss/hacks/service/CustomerApiTest.java b/src/test/java/ch/postfinance/swiss/hacks/service/CustomerApiTest.java new file mode 100644 index 0000000..48b5627 --- /dev/null +++ b/src/test/java/ch/postfinance/swiss/hacks/service/CustomerApiTest.java @@ -0,0 +1,77 @@ +package ch.postfinance.swiss.hacks.service; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import io.restassured.http.ContentType; +import io.restassured.path.json.JsonPath; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +public class CustomerApiTest { + + @Test + public void testSuccessfulRegistration() { + // Define customer data + String firstName = "Foo"; + String lastName = "Bar"; + String dateOfBirth = "2000-01-01"; + + // Build request body + String requestBody = String.format("{" + + "\"firstName\": \"%s\"," + + "\"lastName\": \"%s\"," + + "\"dateOfBirth\": \"%s\"" + + "}", firstName, lastName, dateOfBirth); + + // Send POST request and verify response + String response = RestAssured.given() + .contentType(ContentType.JSON) + .body(requestBody) + .post("/customers/register") + .then() + .statusCode(200) // TODO: this should be 201 according to spec! + .body("username", equalTo((firstName + "." + lastName).toLowerCase())) + // Replace "expected_password" with the actual password generation logic + .body("password", notNullValue()) + .extract().asString(); + + // Extract username from response + String username = JsonPath.from(response).getString("username"); + String password = JsonPath.from(response).getString("password"); + + // Send login request with extracted username and password + RestAssured.given() + // .contentType(ContentType.FORM) // Use form data for login + .formParam("j_username", username) + .formParam("j_password", password) + .post("/j_security_check") + .then() + .statusCode(302) + .header("location", contains("/index.html")); + } + + @Test + public void testMissingRequiredField() { + // Define customer data with missing first name + String lastName = "Doe"; + String dateOfBirth = "2000-01-01"; + + // Build request body + String requestBody = String.format("{" + + "\"lastName\": \"%s\"," + + "\"dateOfBirth\": \"%s\"" + + "}", lastName, dateOfBirth); + + // Send POST request and verify response + RestAssured.given() + .contentType(ContentType.JSON) + .body(requestBody) + .post("/customers/register") + .then() + .statusCode(400); + } +}