use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class UserApiIT method testCreateUser.
@Test
public void testCreateUser() {
Captcha captcha = captchaService.generate();
CreateUserRequest request = new CreateUserRequest();
request.captchaToken = captcha.getToken();
request.captchaResponse = captcha.getResponse();
request.nickname = "testuser";
request.passwordClear = "Ue4nI92Rw";
request.naturalLanguageCode = "en";
request.userUsageConditionsCode = "UUC2021V01";
// ------------------------------------
CreateUserResult result = userApi.createUser(request);
// ------------------------------------
Assertions.assertThat(result).isNotNull();
ObjectContext context = serverRuntime.newContext();
Optional<User> userOptional = User.tryGetByNickname(context, "testuser");
Assertions.assertThat(userOptional.isPresent()).isTrue();
User user = userOptional.get();
Assertions.assertThat(user.getActive()).isTrue();
Assertions.assertThat(user.getIsRoot()).isFalse();
Assertions.assertThat(user.getNickname()).isEqualTo("testuser");
Assertions.assertThat(user.getNaturalLanguage().getCode()).isEqualTo("en");
Assertions.assertThat(user.getLastAuthenticationTimestamp()).isNull();
Assertions.assertThat(user.tryGetUserUsageConditionsAgreement().get().getUserUsageConditions().getCode()).isEqualTo("UUC2021V01");
Assertions.assertThat(userAuthenticationService.authenticateByNicknameAndPassword("testuser", "Ue4nI92Rw").get()).isEqualTo(userOptional.get().getObjectId());
}
use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class UserApiIT method testChangePassword.
@Test
public void testChangePassword() {
Captcha captcha = captchaService.generate();
ObjectContext context = serverRuntime.newContext();
User user = integrationTestSupportService.createBasicUser(context, "testuser", "U7vqpsu6BB");
setAuthenticatedUser("testuser");
// check that the password is correctly configured.
Assertions.assertThat(userAuthenticationService.authenticateByNicknameAndPassword("testuser", "U7vqpsu6BB").get()).isEqualTo(user.getObjectId());
// now change it.
ChangePasswordRequest request = new ChangePasswordRequest();
request.nickname = "testuser";
request.captchaResponse = captcha.getResponse();
request.captchaToken = captcha.getToken();
request.newPasswordClear = "8R3nlp11gX";
request.oldPasswordClear = "U7vqpsu6BB";
// ------------------------------------
userApi.changePassword(request);
// ------------------------------------
// now check that the old authentication no longer works and the new one does work
Assertions.assertThat(userAuthenticationService.authenticateByNicknameAndPassword("testuser", "U7vqpsu6BB").isPresent()).isFalse();
Assertions.assertThat(userAuthenticationService.authenticateByNicknameAndPassword("testuser", "8R3nlp11gX").get()).isEqualTo(user.getObjectId());
}
use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class CaptchaServiceImpl method generate.
@Override
public Captcha generate() {
// maybe better done less frequently?
captchaRepository.purgeExpired();
Captcha captcha = captchaAlgorithm.generate();
captchaRepository.store(captcha.getToken(), captcha.getResponse());
return captcha;
}
use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class UserApiIT method testInitiatePasswordReset.
/**
* <p>This test will check the initiation of the password reset procedure.</p>
*/
@Test
public void testInitiatePasswordReset() {
createPasswordResetTestUser();
Captcha captcha = captchaService.generate();
InitiatePasswordResetRequest request = new InitiatePasswordResetRequest();
request.captchaToken = captcha.getToken();
request.captchaResponse = captcha.getResponse();
request.email = "integration-test-recipient@haiku-os.org";
// ------------------------------------
userApi.initiatePasswordReset(request);
// ------------------------------------
{
ObjectContext context = serverRuntime.newContext();
User user = User.tryGetByNickname(context, "testuser").get();
// check for the presence of a token.
List<UserPasswordResetToken> tokens = UserPasswordResetToken.findByUser(context, user);
Assertions.assertThat(tokens.size()).isEqualTo(1);
UserPasswordResetToken token = tokens.get(0);
// check that an email did actually get sent.
List<SimpleMailMessage> messages = mailSender.getSentMessages();
Assertions.assertThat(messages.size()).isEqualTo(1);
SimpleMailMessage message = messages.get(0);
Assertions.assertThat(message.getTo()).isEqualTo(new String[] { "integration-test-recipient@haiku-os.org" });
Assertions.assertThat(message.getFrom()).isEqualTo("integration-test-sender@haiku-os.org");
Assertions.assertThat(message.getText()).contains(token.getCode());
}
}
use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class UserApiIT method testCompletePasswordReset_ok.
/**
* <p>This checks a password reset token can be picked-up and actioned. The token will have been sent to the
* user earlier in an email.</p>
*/
@Test
public void testCompletePasswordReset_ok() {
createPasswordResetTestUser();
Assertions.assertThat(getOnlyPasswordResetTokenCodeForTestUser()).isNull();
try {
passwordResetService.initiate("integration-test-recipient@haiku-os.org");
} catch (PasswordResetException pre) {
throw new IllegalStateException("unable to initiate the password reset when testing complete", pre);
}
Captcha captcha = captchaService.generate();
CompletePasswordResetRequest request = new CompletePasswordResetRequest();
request.captchaToken = captcha.getToken();
request.captchaResponse = captcha.getResponse();
request.token = getOnlyPasswordResetTokenCodeForTestUser();
request.passwordClear = "kQ83hWi3oWnYY21k";
// ------------------------------------
userApi.completePasswordReset(request);
// ------------------------------------
// the user should now be able to be authenticated with the new password.
Assertions.assertThat(userAuthenticationService.authenticateByNicknameAndPassword("testuser", "kQ83hWi3oWnYY21k").isPresent()).isTrue();
{
ObjectContext context = serverRuntime.newContext();
Optional<UserPasswordResetToken> token = UserPasswordResetToken.getByCode(context, request.token);
Assertions.assertThat(token.isPresent()).isFalse();
}
}
Aggregations