From ec303026ba1b1c287b5de98bb37600803b3b6943 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 03:19:46 +0100 Subject: [PATCH] extract before method --- .../server/access/web/AccessController.java | 24 ++++++++++-------- .../access/web/AccessControllerTest.java | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 11 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 6594de0..c74fa87 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 @@ -10,6 +10,7 @@ import de.fd.fh.shared.network.messages.LoginRequest; import de.fd.fh.shared.network.messages.RegistrateRequest; import spark.Request; import spark.Response; +import spark.Spark; import static spark.Spark.*; @@ -86,20 +87,21 @@ public class AccessController return response; } - public AccessController(final AccessService service) + public void before(final Request req) { - this.service = service; - before("/*", - (req, res) -> - { - final String path = req.pathInfo(); - final String token = req.headers(Utils.AUTHENTICATION_HEADER); + final String path = req.pathInfo(); + final String token = req.headers(Utils.AUTHENTICATION_HEADER); - final AccessToken accessToken = service.before(path, token); + final AccessToken accessToken = service.before(path, token); + + req.session().attribute("userId", + accessToken); + } - req.session().attribute("userId", - accessToken); - }); + public AccessController(final AccessService service) + { + this.service = service; + Spark.before("/*", (req, res) -> before(req)); post("/accounts/registrate", this::registrate); 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 22d64e2..abeb4e9 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 @@ -3,6 +3,8 @@ 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.server.access.Role; +import de.fd.fh.server.user.UserId; import de.fd.fh.shared.network.messages.LoginRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,6 +16,7 @@ import spark.Response; import spark.Session; import java.io.IOException; +import java.time.LocalDateTime; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; @@ -197,4 +200,26 @@ class AccessControllerTest then(service).shouldHaveNoMoreInteractions(); assertEquals(Integer.valueOf(400), statusCaptor.getValue(), "Should return Status code 400."); } + + @Test + void testBefore() + { + final AccessToken dummy = new AccessToken("TestToken", LocalDateTime.now(), Role.USER, UserId.random()); + + when(request.pathInfo()).thenReturn("/test/path"); + when(request.headers(any())).thenReturn("TestAuthHeader"); + when(request.session()).thenReturn(session); + when(service.before(any(), any())) + .thenReturn(dummy); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(AccessToken.class); + + new AccessController(service).before(request); + + verify(session).attribute(any(), captor.capture()); + + assertEquals(dummy, captor.getValue(), "Should store Token in Session"); + then(service).should().before(any(), any()); + then(service).shouldHaveNoMoreInteractions(); + } } \ No newline at end of file