refactored connection classes to be more generic and accept credentials of different apps.
This commit is contained in:
+27
-14
@@ -16,25 +16,27 @@ import com.jayway.jsonpath.JsonPath;
|
||||
import com.vaessl.app.dto.ConnectionRequest;
|
||||
|
||||
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.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureTestRestTemplate
|
||||
@WireMockTest
|
||||
public class ConnectionIntegrationTest {
|
||||
public class HomeboxIntegrationTest {
|
||||
|
||||
@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) {
|
||||
void shouldReturnTokenAndStatusOkWhenHomeboxCredentialsAreValid(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(API_LOGIN)
|
||||
stubFor(post(HOMEBOX_LOGIN.getEndpoint())
|
||||
.willReturn(okJson("""
|
||||
{
|
||||
"token": "fake-jwt-token",
|
||||
@@ -64,7 +66,7 @@ public class ConnectionIntegrationTest {
|
||||
@Test
|
||||
void shouldFailToConnectWhenHomeboxReturnsUnauthorized(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(API_LOGIN).willReturn(unauthorized()));
|
||||
stubFor(post(HOMEBOX_LOGIN.getEndpoint()).willReturn(unauthorized()));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
|
||||
@@ -78,7 +80,7 @@ public class ConnectionIntegrationTest {
|
||||
@Test
|
||||
void shouldFailToConnectWhenHomeboxReturnsServiceUnavailable(WireMockRuntimeInfo wm) {
|
||||
|
||||
stubFor(post(API_LOGIN).willReturn(serviceUnavailable()));
|
||||
stubFor(post(HOMEBOX_LOGIN.getEndpoint()).willReturn(serviceUnavailable()));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", connectionRequest(wm), String.class);
|
||||
|
||||
@@ -90,15 +92,18 @@ public class ConnectionIntegrationTest {
|
||||
* Checks when the service is unavailable or the app URL is wrong.
|
||||
*/
|
||||
@Test
|
||||
void shouldReturnServiceUnavailableWhenUrlIsCompletelyWrong() {
|
||||
void shouldReturnServiceUnavailableWhenHomeboxUrlIsWrong() {
|
||||
|
||||
ConnectionRequest badRequest = new ConnectionRequest(
|
||||
"http://localhost:1234",
|
||||
"user",
|
||||
"pass");
|
||||
"HOMEBOX",
|
||||
Map.of("username", "myUser", "password", "myPass"),
|
||||
false);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", badRequest, String.class);
|
||||
|
||||
System.out.println("RESPONSE: " + response.getBody());
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
assertThat(response.getBody()).contains("The target URL is unreachable.");
|
||||
}
|
||||
@@ -107,9 +112,9 @@ public class ConnectionIntegrationTest {
|
||||
* Checks if any login fields are empty since all of them are mandatory.
|
||||
*/
|
||||
@Test
|
||||
void shouldReturnBadRequestWhenFieldsAreEmpty() {
|
||||
void shouldReturnBadRequestWhenHomeboxFieldsAreEmpty() {
|
||||
|
||||
ConnectionRequest emtpyRequest = new ConnectionRequest("", "", "");
|
||||
ConnectionRequest emtpyRequest = new ConnectionRequest("", "", Map.of(), false);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity("/login", emtpyRequest, String.class);
|
||||
|
||||
@@ -117,7 +122,15 @@ public class ConnectionIntegrationTest {
|
||||
assertThat(response.getBody()).contains("Fields must not be empty.");
|
||||
}
|
||||
|
||||
private ConnectionRequest connectionRequest(WireMockRuntimeInfo wireMockRuntimeInfo) {
|
||||
return new ConnectionRequest(wireMockRuntimeInfo.getHttpBaseUrl(), "username", "password");
|
||||
/**
|
||||
* Creates a valid connection request with a mock Api throuh
|
||||
* WireMockRuntimeInfo.
|
||||
*
|
||||
* @param wm
|
||||
* @return a mock api connection request.
|
||||
*/
|
||||
private ConnectionRequest connectionRequest(WireMockRuntimeInfo wm) {
|
||||
return new ConnectionRequest(wm.getHttpBaseUrl(), "HOMEBOX", Map.of("username", "admin", "password", "pw"),
|
||||
false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user