From 01fcf86ce2f70dde1009d81065c87e24f2bd0703 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 14:06:12 +0100 Subject: [PATCH] add login method to AccessClient and test --- .../java/de/fd/fh/network/AccessClient.java | 8 +++++ .../de/fd/fh/network/AccessClientTest.java | 32 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java index 9bfa233..6ce4d47 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java @@ -1,6 +1,10 @@ package de.fd.fh.network; +import de.fd.fh.shared.Utils; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; +import feign.Headers; +import feign.Param; import feign.RequestLine; import feign.Response; @@ -8,4 +12,8 @@ public interface AccessClient { @RequestLine("POST /accounts/registrate") Response registrate(RegistrateRequest request); + + @RequestLine("POST /accounts/login") + @Headers(Utils.AUTHENTICATION_HEADER + ":Basic {authToken}") + LoginResponse login(@Param("authToken") final String authenticationHeader); } diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java index adb3016..7080233 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java @@ -1,7 +1,10 @@ package de.fd.fh.network; +import de.fd.fh.shared.Utils; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; import feign.Feign; +import feign.Request; import feign.Response; import feign.gson.GsonDecoder; import feign.gson.GsonEncoder; @@ -11,7 +14,7 @@ import feign.mock.MockTarget; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; class AccessClientTest { @@ -43,4 +46,31 @@ class AccessClientTest mockClient.verifyStatus(); } + + @Test + void testLogin() + { + mockClient + .ok(HttpMethod.POST, "/accounts/login", + "{\n" + + " \"name\":\"testName\",\n" + + " \"userId\": \"12345\",\n" + + " \"token\": \"testToken\"\n" + + "}" + ); + final LoginResponse result = client.login("testToken"); + + final Request request = mockClient.verifyOne(HttpMethod.POST, "/accounts/login"); + + assertNotNull(result); + assertEquals("testName", result.getName()); + assertEquals("12345", result.getUserId()); + assertEquals("testToken", result.getToken()); + + assertTrue(request.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().isPresent(), + "Should have auth header"); + final String authHeader = request.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().get(); + + assertEquals("Basic testToken", authHeader, "Should use given token."); + } } \ No newline at end of file