added login logic excl refresh call
This commit is contained in:
@@ -20,39 +20,50 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||
import static com.vaessl.app.connection.Endpoints.*;
|
||||
import static com.vaessl.app.connection.Endpoint.*;
|
||||
import static com.vaessl.app.exception.ErrorMessage.*;
|
||||
import static com.vaessl.app.connection.ServiceType.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureTestRestTemplate
|
||||
@WireMockTest
|
||||
public class HomeboxIntegrationTest {
|
||||
class HomeboxIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
ConnectionRepository cRepository;
|
||||
|
||||
String okJsonHomeboxResponse = """
|
||||
{
|
||||
"token": "fake-jwt-token",
|
||||
"attachmentToken": "fake-attach",
|
||||
"expiresAt": "2026-04-26T02:23:13Z"
|
||||
}
|
||||
""";
|
||||
|
||||
/**
|
||||
* Returns Token and status code OK when login is successful.
|
||||
*
|
||||
* @param wm
|
||||
* the WiremockRuntimeInfo object
|
||||
*/
|
||||
@Test
|
||||
void shouldReturnTokenAndStatusOkWhenHomeboxCredentialsAreValid(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(HOMEBOX_LOGIN.getEndpoint())
|
||||
.willReturn(okJson("""
|
||||
{
|
||||
"token": "fake-jwt-token",
|
||||
"attachmentToken": "fake-attach",
|
||||
"expiresAt": "2026-04-26T02:23:13Z"
|
||||
}
|
||||
""")));
|
||||
stubFor(post(HOMEBOX_LOGIN.getValue())
|
||||
.willReturn(okJson(okJsonHomeboxResponse)));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), 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");
|
||||
String attachmentToken = documentContext.read("$.extraResponseData.attachmentToken");
|
||||
assertThat(attachmentToken).isEqualTo("fake-attach");
|
||||
|
||||
String expiresAt = documentContext.read("$.expiresAt", String.class);
|
||||
@@ -61,31 +72,38 @@ public class HomeboxIntegrationTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if login request fails with 401 unauthorized.
|
||||
* Tests the Unauthorized custom exception.
|
||||
*
|
||||
* @param wm
|
||||
* the WiremockRuntimeInfo object
|
||||
*/
|
||||
@Test
|
||||
void shouldFailToConnectWhenHomeboxReturnsUnauthorized(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(HOMEBOX_LOGIN.getEndpoint()).willReturn(unauthorized()));
|
||||
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(unauthorized()));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm),
|
||||
String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
assertThat(response.getBody()).contains("Invalid username or password.");
|
||||
assertThat(response.getBody()).contains(UNAUTHORIZED_WRONG_LOGIN.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a server error from the external api.
|
||||
*
|
||||
* @param wm
|
||||
*/
|
||||
@Test
|
||||
void shouldFailToConnectWhenHomeboxReturnsServiceUnavailable(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(HOMEBOX_LOGIN.getEndpoint()).willReturn(serviceUnavailable()));
|
||||
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(serviceUnavailable()));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm),
|
||||
String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
assertThat(response.getBody()).contains("The external app returned a server error");
|
||||
assertThat(response.getBody()).contains(SERVER_ERROR_GENERAL.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,16 +114,14 @@ public class HomeboxIntegrationTest {
|
||||
|
||||
ConnectionRequest badRequest = new ConnectionRequest(
|
||||
"http://localhost:1234",
|
||||
"HOMEBOX",
|
||||
HOMEBOX.getValue(),
|
||||
Map.of("username", "myUser", "password", "myPass"),
|
||||
false);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", badRequest, String.class);
|
||||
|
||||
System.out.println("RESPONSE: " + response.getBody());
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), badRequest, String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
assertThat(response.getBody()).contains("The target URL is unreachable.");
|
||||
assertThat(response.getBody()).contains(SERVICE_UNAVAILABLE_UNREACHABLE_URL.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,10 +132,60 @@ public class HomeboxIntegrationTest {
|
||||
|
||||
ConnectionRequest emtpyRequest = new ConnectionRequest("", "", Map.of(), false);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", emtpyRequest, String.class);
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), emtpyRequest, String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(response.getBody()).contains("Fields must not be empty.");
|
||||
assertThat(response.getBody()).contains(BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the custom ProviderNotFound exception.
|
||||
*/
|
||||
@Test
|
||||
void shouldReturnProviderNotFound() {
|
||||
ConnectionRequest wrongServiceTypeReq = new ConnectionRequest(
|
||||
"http://localhost:1234",
|
||||
"wrong-service-type",
|
||||
Map.of("username", "myUser", "password", "myPass"),
|
||||
false);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), wrongServiceTypeReq,
|
||||
String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(response.getBody()).contains(WRONG_SERVICE_TYPE.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the succesfull persistance of Homebox credential response to the
|
||||
* database.
|
||||
*
|
||||
* @param wm
|
||||
* the WiremockRuntimeInfo object
|
||||
*/
|
||||
@Test
|
||||
void shouldSaveHomeboxConnectionResponseToDb(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(urlEqualTo(HOMEBOX_LOGIN.getValue()))
|
||||
.willReturn(okJson(okJsonHomeboxResponse)));
|
||||
|
||||
ConnectionRequest request = connectionRequest(wm);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), request, String.class);
|
||||
|
||||
DocumentContext responseContext = JsonPath.parse(response.getBody());
|
||||
|
||||
ConnectionEntity dbEntry = cRepository.findByAppUrl(request.appUrl());
|
||||
|
||||
assertThat(dbEntry).isNotNull();
|
||||
|
||||
assertThat(dbEntry.getAppUrl()).isEqualTo(request.appUrl());
|
||||
|
||||
if (dbEntry instanceof HomeboxEntity hbE) {
|
||||
assertThat(hbE.getToken()).isEqualTo(responseContext.read("$.token"));
|
||||
assertThat(hbE.getAttachmentToken()).isEqualTo(responseContext.read("$.extraResponseData.attachmentToken"));
|
||||
assertThat(hbE.getExpiresAt()).isEqualTo(responseContext.read("$.expiresAt"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,10 +193,12 @@ public class HomeboxIntegrationTest {
|
||||
* WireMockRuntimeInfo.
|
||||
*
|
||||
* @param wm
|
||||
* the WiremockRuntimeInfo object
|
||||
* @return a mock api connection request.
|
||||
*/
|
||||
private ConnectionRequest connectionRequest(WireMockRuntimeInfo wm) {
|
||||
return new ConnectionRequest(wm.getHttpBaseUrl(), "HOMEBOX", Map.of("username", "admin", "password", "pw"),
|
||||
false);
|
||||
return new ConnectionRequest(wm.getHttpBaseUrl(), HOMEBOX.getValue(),
|
||||
Map.of("username", "admin", "password", "pw"),
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user