From a3edf01c742eb9a9e9694894642407e4d4606a4b Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 02:58:16 +0100 Subject: [PATCH] seperate delete method --- .../server/access/web/AccessController.java | 54 +++++-------- .../access/web/AccessControllerTest.java | 76 +++++++++++++++++++ 2 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java 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 3e32964..3d43c07 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 @@ -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; - }); } } 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 new file mode 100644 index 0000000..1046b8a --- /dev/null +++ b/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 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 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."); + } +} \ No newline at end of file