Search in sources :

Example 1 with Captcha

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());
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) User(org.haiku.haikudepotserver.dataobjects.User) CreateUserResult(org.haiku.haikudepotserver.api1.model.user.CreateUserResult) CreateUserRequest(org.haiku.haikudepotserver.api1.model.user.CreateUserRequest) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with Captcha

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());
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) ChangePasswordRequest(org.haiku.haikudepotserver.api1.model.user.ChangePasswordRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with Captcha

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;
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha)

Example 4 with 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());
    }
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) User(org.haiku.haikudepotserver.dataobjects.User) UserPasswordResetToken(org.haiku.haikudepotserver.dataobjects.UserPasswordResetToken) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) InitiatePasswordResetRequest(org.haiku.haikudepotserver.api1.model.user.InitiatePasswordResetRequest) List(java.util.List) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with Captcha

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();
    }
}
Also used : PasswordResetException(org.haiku.haikudepotserver.passwordreset.PasswordResetException) Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) Optional(java.util.Optional) CompletePasswordResetRequest(org.haiku.haikudepotserver.api1.model.user.CompletePasswordResetRequest) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

Captcha (org.haiku.haikudepotserver.captcha.model.Captcha)7 ObjectContext (org.apache.cayenne.ObjectContext)4 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)4 Test (org.junit.jupiter.api.Test)4 User (org.haiku.haikudepotserver.dataobjects.User)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 List (java.util.List)1 Optional (java.util.Optional)1 GenerateCaptchaResult (org.haiku.haikudepotserver.api1.model.captcha.GenerateCaptchaResult)1 ChangePasswordRequest (org.haiku.haikudepotserver.api1.model.user.ChangePasswordRequest)1 CompletePasswordResetRequest (org.haiku.haikudepotserver.api1.model.user.CompletePasswordResetRequest)1 CreateUserRequest (org.haiku.haikudepotserver.api1.model.user.CreateUserRequest)1 CreateUserResult (org.haiku.haikudepotserver.api1.model.user.CreateUserResult)1 InitiatePasswordResetRequest (org.haiku.haikudepotserver.api1.model.user.InitiatePasswordResetRequest)1 UserPasswordResetToken (org.haiku.haikudepotserver.dataobjects.UserPasswordResetToken)1 PasswordResetException (org.haiku.haikudepotserver.passwordreset.PasswordResetException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1