From 4da6e021b7d6b3faa1b76b581d91dc2b5c08c4b7 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 02:22:34 +0100 Subject: [PATCH] add AccessServiceTest and AccessTokenTest --- .../fh/server/access/AccessServiceTest.java | 45 +++++++++++++++++++ .../fd/fh/server/access/AccessTokenTest.java | 18 ++++++++ 2 files changed, 63 insertions(+) diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java index 073a84a..f3fa3df 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java @@ -14,6 +14,7 @@ import java.util.Base64; import java.util.Observable; import java.util.Observer; +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; import static org.junit.jupiter.api.Assertions.*; @@ -83,6 +84,28 @@ class AccessServiceTest implements Observer then(repository).shouldHaveNoMoreInteractions(); } + @Test + void testUnprotectedLoginPath() + { + final String path = "/accounts/login"; + + final AccessToken result = new AccessService(null) + .before(path, null); + + assertNull(result); + } + + @Test + void testUnprotectedRegistratePath() + { + final String path = "/accounts/registrate"; + + final AccessToken result = new AccessService(null) + .before(path, null); + + assertNull(result); + } + @Test void given_newUser_when_createUser_should_storeNewUser() { @@ -118,6 +141,28 @@ class AccessServiceTest implements Observer then(repository).shouldHaveNoMoreInteractions(); } + @Test + void given_newUser_when_userNameAlreadyExist_should_storeNewUser() + { + final RegistrateRequest request = + RegistrateRequest.of("testUser", "testPwd"); + + final AccessRepository repository = mock(AccessRepository.class); + when(repository.findByUserName(any())) + .thenReturn(new Access()); + + final AccessService service = new AccessService(repository); + service.addObserver(this); + + final boolean result = service.createPlayer(request); + + assertFalse(result); + assertThat("No event thrown", this.event, nullValue()); + + then(repository).should().findByUserName(any()); + then(repository).shouldHaveNoMoreInteractions(); + } + @Test void given_loggedInUser_when_logout_should_logoutUser() { diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java index 1872e62..89e0609 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java @@ -28,4 +28,22 @@ class AccessTokenTest assertEquals(result.getRole(), Role.USER); assertEquals(result.getUserId(), UserId.of("12345")); } + + @Test + void testPrePersist() + { + final Access access = new Access( + "testId", + "testName", + "testPwd", + UserId.of("12345"), + null, + Role.USER); + + final AccessToken accessToken = AccessToken.of(access); + + accessToken.prePersist(); + + assertNotNull(accessToken.getCreatedDate()); + } } \ No newline at end of file