Browse Source

Add AccessController

pr-readme
Steffen Nitschke 4 years ago
parent
commit
7bfcb04f06
  1. 104
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
  2. 36
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java
  3. 6
      fh.fd.ci.shared/src/main/java/de/fd/fh/shared/Utils.java
  4. 11
      fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginRequest.java
  5. 12
      fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java

104
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java

@ -0,0 +1,104 @@
package de.fd.fh.server.access.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.fd.fh.server.access.AccessService;
import de.fd.fh.server.access.AccessToken;
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 static spark.Spark.*;
public class AccessController
{
private final ObjectMapper objectMapper = new ObjectMapper();
public AccessController(final AccessService service)
{
before("/*",
(req, res) ->
{
final String path = req.pathInfo();
final String token = req.headers(Utils.AUTHENTICATION_HEADER);
final AccessToken accessToken = service.before(path, token);
req.session().attribute("userId",
accessToken);
});
post("/accounts/registrate",
(request, response) ->
{
final RegistrateRequest message =
objectMapper.readValue(request.body(), RegistrateRequest.class);
if (service.createPlayer(message))
{
response.status(201);
}
else
{
response.status(400);
}
return response;
}
);
post("/accounts/login",
(request, response) ->
{
final String header = request.headers(Utils.AUTHENTICATION_HEADER);
final LoginRequest login = service.authorization(header);
if (login == null)
{
response.status(401);
}
else
{
response.status(200);
response.type("application/json");
response.body(objectMapper.writeValueAsString(login));
}
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);
}
return response;
});
}
}

36
fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java

@ -0,0 +1,36 @@
package de.fd.fh.server.access;
import de.fd.fh.server.user.UserId;
import de.fd.fh.server.user.UserRepository;
import de.fd.fh.server.user.UserService;
import de.fd.fh.server.user.events.ChangePasswordEvent;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.*;
class AccessContextEventListenerTest
{
@Test
void given_changePasswordEvent_when_passwordChanged_should_changePassword()
{
final ChangePasswordEvent event = new ChangePasswordEvent(UserId.of("12345"), "newPwd");
final AccessRepository repository = mock(AccessRepository.class);
when(repository.findByUserId(any(UserId.class)))
.thenReturn(new Access());
final ArgumentCaptor<Access> captor = ArgumentCaptor.forClass(Access.class);
new AccessContextEventListener(repository).update(null, event);
verify(repository).save(captor.capture());
assertEquals("newPwd", captor.getValue().getPassword(), "Have to be the new password");
then(repository).should().findByUserId(any(UserId.class));
then(repository).should().save(any(Access.class));
then(repository).shouldHaveNoMoreInteractions();
}
}

6
fh.fd.ci.shared/src/main/java/de/fd/fh/shared/Utils.java

@ -0,0 +1,6 @@
package de.fd.fh.shared;
public class Utils
{
public static final String AUTHENTICATION_HEADER = "Authorization";
}

11
fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginRequest.java

@ -0,0 +1,11 @@
package de.fd.fh.shared.network.messages;
import lombok.Data;
@Data
public class LoginRequest
{
private String name;
private String userId;
private String token;
}

12
fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java

@ -0,0 +1,12 @@
package de.fd.fh.shared.network.messages;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor(staticName = "of")
public class RegistrateRequest
{
private String userName;
private String password;
}
Loading…
Cancel
Save