Steffen Nitschke
4 years ago
5 changed files with 169 additions and 0 deletions
-
104fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
-
36fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java
-
6fh.fd.ci.shared/src/main/java/de/fd/fh/shared/Utils.java
-
11fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginRequest.java
-
12fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.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; |
||||
|
}); |
||||
|
} |
||||
|
} |
@ -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(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,6 @@ |
|||||
|
package de.fd.fh.shared; |
||||
|
|
||||
|
public class Utils |
||||
|
{ |
||||
|
public static final String AUTHENTICATION_HEADER = "Authorization"; |
||||
|
} |
@ -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; |
||||
|
} |
@ -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; |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue