added post request to achieve login response with tokens
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package com.vaessl.app.connection;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.resttestclient.TestRestTemplate;
|
||||
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
|
||||
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
|
||||
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.vaessl.app.dto.ConnectionRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureTestRestTemplate
|
||||
@WireMockTest
|
||||
public class ConnectionIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
TestRestTemplate restTemplate;
|
||||
|
||||
private static final String API_LOGIN = "/api/v1/users/login";
|
||||
|
||||
/**
|
||||
* Returns Token and status code OK when login is successful.
|
||||
*/
|
||||
@Test
|
||||
void shouldReturnTokenAndStatusOkWhenCredentialsAreValid(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(API_LOGIN)
|
||||
.willReturn(okJson("""
|
||||
{
|
||||
"token": "fake-jwt-token",
|
||||
"attachmentToken": "fake-attach",
|
||||
"expiresAt": "2026-04-26T02:23:13Z"
|
||||
}
|
||||
""")));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
|
||||
DocumentContext documentContext = JsonPath.parse(response.getBody());
|
||||
|
||||
String token = documentContext.read("$.token");
|
||||
assertThat(token).isEqualTo("fake-jwt-token");
|
||||
|
||||
String attachmentToken = documentContext.read("$.attachmentToken");
|
||||
assertThat(attachmentToken).isEqualTo("fake-attach");
|
||||
|
||||
String expiresAt = documentContext.read("$.expiresAt", String.class);
|
||||
assertThat(expiresAt).isEqualTo("2026-04-26T02:23:13Z");
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if login request fails with 401 unauthorized.
|
||||
*/
|
||||
@Test
|
||||
void shouldFailToConnectWhenHomeboxReturnsUnauthorized(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(API_LOGIN).willReturn(unauthorized()));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
assertThat(response.getBody()).contains("Invalid username or password.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a server error from the external api.
|
||||
*/
|
||||
@Test
|
||||
void shouldFailToConnectWhenHomeboxReturnsServiceUnavailable(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(API_LOGIN).willReturn(serviceUnavailable()));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
assertThat(response.getBody()).contains("The external app returned a server error");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks when the service is unavailable or the app URL is wrong.
|
||||
*/
|
||||
@Test
|
||||
void shouldReturnServiceUnavailableWhenUrlIsCompletelyWrong() {
|
||||
|
||||
ConnectionRequest badRequest = new ConnectionRequest(
|
||||
"http://localhost:1234",
|
||||
"user",
|
||||
"pass");
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", badRequest, String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
assertThat(response.getBody()).contains("The target URL is unreachable.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnBadRequestWhenUrlIsMissing() {
|
||||
|
||||
ConnectionRequest emtpyRequest = new ConnectionRequest("", "", "");
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", emtpyRequest, String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(response.getBody()).contains("Fields must not be empty.");
|
||||
}
|
||||
|
||||
private ConnectionRequest connectionRequest(WireMockRuntimeInfo wireMockRuntimeInfo) {
|
||||
return new ConnectionRequest(wireMockRuntimeInfo.getHttpBaseUrl(), "username", "password");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user