You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.6 KiB

package de.fd.fh.server.access.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import de.fd.fh.server.access.AccessService;
import de.fd.fh.server.access.AccessToken;
import de.fd.fh.server.access.Role;
import de.fd.fh.server.user.UserId;
import de.fd.fh.shared.network.messages.LoginRequest;
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 java.io.IOException;
import java.time.LocalDateTime;
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.");
}
@Test
void testLogout()
{
when(request.headers(any())).thenReturn("testHeader");
when(service.logout(any())).thenReturn(true);
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).logout(request, response);
verify(response).status(captor.capture());
then(service).should().logout(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(captor.getValue(), Integer.valueOf(200), "Should return Status code 200.");
}
@Test
void failedLogoutTest()
{
when(request.headers(any())).thenReturn("testHeader");
when(service.logout(any())).thenReturn(false);
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).logout(request, response);
verify(response).status(captor.capture());
then(service).should().logout(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(captor.getValue(), Integer.valueOf(400), "Should return Status code 400.");
}
@Test
void loginTest() throws JsonProcessingException
{
final LoginRequest dummy = new LoginRequest();
dummy.setName("TestName");
dummy.setToken("TestToken");
dummy.setUserId("12345");
when(request.headers(any())).thenReturn("testHeader");
when(service.authorization(any())).thenReturn(dummy);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
final ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
new AccessController(service).login(request, response);
verify(response).status(statusCaptor.capture());
verify(response).type(typeCaptor.capture());
verify(response).body(bodyCaptor.capture());
then(service).should().authorization(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(200), statusCaptor.getValue(), "Should return Status code 200.");
assertEquals("application/json", typeCaptor.getValue(), "Should have return type json.");
assertEquals("{\"name\":\"TestName\",\"userId\":\"12345\",\"token\":\"TestToken\"}",
bodyCaptor.getValue(), "Should return correct Body.");
}
@Test
void failedLoginTest() throws JsonProcessingException
{
when(request.headers(any())).thenReturn("testHeader");
when(service.authorization(any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).login(request, response);
verify(response).status(statusCaptor.capture());
then(service).should().authorization(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(401), statusCaptor.getValue(), "Should return Status code 401.");
}
@Test
void registrateTest() throws IOException
{
when(request.body()).thenReturn("{\"userName\":\"TestName\",\"password\":\"TestPassword\"}");
when(service.createPlayer(any())).thenReturn(true);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).registrate(request, response);
verify(response).status(statusCaptor.capture());
then(service).should().createPlayer(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(201), statusCaptor.getValue(), "Should return Status code 201.");
}
@Test
void failedRegistrateTest() throws IOException
{
when(request.body()).thenReturn("{\"userName\":\"TestName\",\"password\":\"TestPassword\"}");
when(service.createPlayer(any())).thenReturn(false);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new AccessController(service).registrate(request, response);
verify(response).status(statusCaptor.capture());
then(service).should().createPlayer(any());
then(service).shouldHaveNoMoreInteractions();
assertEquals(Integer.valueOf(400), statusCaptor.getValue(), "Should return Status code 400.");
}
@Test
void testBefore()
{
final AccessToken dummy = new AccessToken("TestToken", LocalDateTime.now(), Role.USER, UserId.random());
when(request.pathInfo()).thenReturn("/test/path");
when(request.headers(any())).thenReturn("TestAuthHeader");
when(request.session()).thenReturn(session);
when(service.before(any(), any()))
.thenReturn(dummy);
final ArgumentCaptor<AccessToken> captor = ArgumentCaptor.forClass(AccessToken.class);
new AccessController(service).before(request);
verify(session).attribute(any(), captor.capture());
assertEquals(dummy, captor.getValue(), "Should store Token in Session");
then(service).should().before(any(), any());
then(service).shouldHaveNoMoreInteractions();
}
}