Browse Source

add AccessServiceTest and AccessTokenTest

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
4da6e021b7
  1. 45
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java
  2. 18
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java

45
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.Observable;
import java.util.Observer; import java.util.Observer;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -83,6 +84,28 @@ class AccessServiceTest implements Observer
then(repository).shouldHaveNoMoreInteractions(); 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 @Test
void given_newUser_when_createUser_should_storeNewUser() void given_newUser_when_createUser_should_storeNewUser()
{ {
@ -118,6 +141,28 @@ class AccessServiceTest implements Observer
then(repository).shouldHaveNoMoreInteractions(); 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 @Test
void given_loggedInUser_when_logout_should_logoutUser() void given_loggedInUser_when_logout_should_logoutUser()
{ {

18
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.getRole(), Role.USER);
assertEquals(result.getUserId(), UserId.of("12345")); 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());
}
} }
Loading…
Cancel
Save