From de04669bd132d6019f7df5afc68f95723607cfad Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 03:03:29 +0100 Subject: [PATCH] extract login method --- .../server/access/web/AccessController.java | 40 ++++++++------- .../access/web/AccessControllerTest.java | 50 +++++++++++++++++++ 2 files changed, 71 insertions(+), 19 deletions(-) diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java index ccef76e..6bd9676 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java +++ b/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; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import de.fd.fh.server.access.AccessService; import de.fd.fh.server.access.AccessToken; @@ -50,6 +51,25 @@ public class AccessController 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) { 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); diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java index a99039f..ecd494d 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java +++ b/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; +import com.fasterxml.jackson.core.JsonProcessingException; import de.fd.fh.server.access.AccessService; 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.Test; import org.mockito.ArgumentCaptor; @@ -109,4 +111,52 @@ class AccessControllerTest 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 statusCaptor = ArgumentCaptor.forClass(Integer.class); + final ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(String.class); + final ArgumentCaptor 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 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."); + } } \ No newline at end of file