Browse Source

extract before method

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

24
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);

25
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<AccessToken> 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();
}
}
Loading…
Cancel
Save