Browse Source

extract before method

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
ec303026ba
  1. 14
      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

14
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 de.fd.fh.shared.network.messages.RegistrateRequest;
import spark.Request; import spark.Request;
import spark.Response; import spark.Response;
import spark.Spark;
import static spark.Spark.*; import static spark.Spark.*;
@ -86,11 +87,7 @@ public class AccessController
return response; return response;
} }
public AccessController(final AccessService service)
{
this.service = service;
before("/*",
(req, res) ->
public void before(final Request req)
{ {
final String path = req.pathInfo(); final String path = req.pathInfo();
final String token = req.headers(Utils.AUTHENTICATION_HEADER); final String token = req.headers(Utils.AUTHENTICATION_HEADER);
@ -99,7 +96,12 @@ public class AccessController
req.session().attribute("userId", req.session().attribute("userId",
accessToken); accessToken);
});
}
public AccessController(final AccessService service)
{
this.service = service;
Spark.before("/*", (req, res) -> before(req));
post("/accounts/registrate", this::registrate); 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 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.server.access.Role;
import de.fd.fh.server.user.UserId;
import de.fd.fh.shared.network.messages.LoginRequest; 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;
@ -14,6 +16,7 @@ import spark.Response;
import spark.Session; import spark.Session;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -197,4 +200,26 @@ class AccessControllerTest
then(service).shouldHaveNoMoreInteractions(); then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(400), statusCaptor.getValue(), "Should return Status code 400."); 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