Browse Source

extract login method

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
de04669bd1
  1. 40
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
  2. 50
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java

40
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java

@ -1,5 +1,6 @@
package de.fd.fh.server.access.web; package de.fd.fh.server.access.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import de.fd.fh.server.access.AccessService; import de.fd.fh.server.access.AccessService;
import de.fd.fh.server.access.AccessToken; import de.fd.fh.server.access.AccessToken;
@ -50,6 +51,25 @@ public class AccessController
return response; return response;
} }
public Object login(Request request, Response response) throws JsonProcessingException
{
final String header = request.headers(Utils.AUTHENTICATION_HEADER);
final LoginRequest login = service.authorization(header);
if (login == null)
{
response.status(401);
}
else
{
response.status(200);
response.type("application/json");
response.body(objectMapper.writeValueAsString(login));
}
return response;
}
public AccessController(final AccessService service) public AccessController(final AccessService service)
{ {
this.service = service; this.service = service;
@ -83,25 +103,7 @@ public class AccessController
} }
); );
post("/accounts/login",
(request, response) ->
{
final String header = request.headers(Utils.AUTHENTICATION_HEADER);
final LoginRequest login = service.authorization(header);
if (login == null)
{
response.status(401);
}
else
{
response.status(200);
response.type("application/json");
response.body(objectMapper.writeValueAsString(login));
}
return response;
});
post("/accounts/login", this::login);
post("/accounts/logout",this::logout); post("/accounts/logout",this::logout);

50
fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java

@ -1,7 +1,9 @@
package de.fd.fh.server.access.web; package de.fd.fh.server.access.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import de.fd.fh.server.access.AccessService; import de.fd.fh.server.access.AccessService;
import de.fd.fh.server.access.AccessToken; import de.fd.fh.server.access.AccessToken;
import de.fd.fh.shared.network.messages.LoginRequest;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
@ -109,4 +111,52 @@ class AccessControllerTest
assertEquals(captor.getValue(), Integer.valueOf(400), "Should return Status code 400."); assertEquals(captor.getValue(), Integer.valueOf(400), "Should return Status code 400.");
} }
@Test
void loginTest() throws JsonProcessingException
{
final LoginRequest dummy = new LoginRequest();
dummy.setName("TestName");
dummy.setToken("TestToken");
dummy.setUserId("12345");
when(request.headers(any())).thenReturn("testHeader");
when(service.authorization(any())).thenReturn(dummy);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
final ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
new AccessController(service).login(request, response);
verify(response).status(statusCaptor.capture());
verify(response).type(typeCaptor.capture());
verify(response).body(bodyCaptor.capture());
then(service).should().authorization(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(200), statusCaptor.getValue(), "Should return Status code 200.");
assertEquals("application/json", typeCaptor.getValue(), "Should have return type json.");
assertEquals("{\"name\":\"TestName\",\"userId\":\"12345\",\"token\":\"TestToken\"}",
bodyCaptor.getValue(), "Should return correct Body.");
}
@Test
void failedLoginTest() throws JsonProcessingException
{
when(request.headers(any())).thenReturn("testHeader");
when(service.authorization(any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).login(request, response);
verify(response).status(statusCaptor.capture());
then(service).should().authorization(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(401), statusCaptor.getValue(), "Should return Status code 401.");
}
} }
Loading…
Cancel
Save