Browse Source

seperate delete method

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

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

@ -7,15 +7,35 @@ import de.fd.fh.server.user.UserId;
import de.fd.fh.shared.Utils;
import de.fd.fh.shared.network.messages.LoginRequest;
import de.fd.fh.shared.network.messages.RegistrateRequest;
import spark.Request;
import spark.Response;
import static spark.Spark.*;
public class AccessController
{
private final ObjectMapper objectMapper = new ObjectMapper();
private final AccessService service;
public Response deletePlayer(Request request, Response response)
{
final UserId userId = UserId.of(request.params(":player_id"));
final AccessToken token = request.session().attribute("userId");
if (service.deleteAccount(userId, token))
{
response.status(200);
}
else
{
response.status(400);
}
return response;
}
public AccessController(final AccessService service)
{
this.service = service;
before("/*",
(req, res) ->
{
@ -66,39 +86,7 @@ public class AccessController
return response;
});
post("/accounts/logout",
(request, response) ->
{
final String token = request.headers(Utils.AUTHENTICATION_HEADER);
if (service.logout(token))
{
response.status(200);
}
else
{
response.status(400);
}
return response;
});
delete("/accounts/:player_id",
(request, response) ->
{
final UserId userId = UserId.of(request.params(":player_id"));
final AccessToken token = request.session().attribute("userId");
if (service.deleteAccount(userId, token))
{
response.status(200);
}
else
{
response.status(400);
}
post("/accounts/logout", this::deletePlayer);
return response;
});
}
}

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

@ -0,0 +1,76 @@
package de.fd.fh.server.access.web;
import de.fd.fh.server.access.AccessService;
import de.fd.fh.server.access.AccessToken;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import spark.Request;
import spark.Response;
import spark.Session;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AccessControllerTest
{
@Mock
Request request;
@Mock
Response response;
@Mock
Session session;
@Mock
AccessService service;
@BeforeEach
void before()
{
MockitoAnnotations.openMocks(this);
}
@Test
void deletePlayerTest()
{
when(request.params(any())).thenReturn("12345");
when(request.session()).thenReturn(session);
when(session.attribute(any())).thenReturn(new AccessToken());
when(service.deleteAccount(any(), any())).thenReturn(true);
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).deletePlayer(request, response);
verify(response).status(captor.capture());
then(service).should().deleteAccount(any(), any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(captor.getValue(), Integer.valueOf(200), "Should return Status code 200.");
}
@Test
void failedDeletePlayerTest()
{
when(request.params(any())).thenReturn("12345");
when(request.session()).thenReturn(session);
when(session.attribute(any())).thenReturn(new AccessToken());
when(service.deleteAccount(any(), any())).thenReturn(false);
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).deletePlayer(request, response);
verify(response).status(captor.capture());
then(service).should().deleteAccount(any(), any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(captor.getValue(), Integer.valueOf(400), "Should return Status code 400.");
}
}
Loading…
Cancel
Save