Search in sources :

Example 41 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserRatingSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, UserRatingSpreadsheetJobSpecification specification) throws IOException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    final ObjectContext context = serverRuntime.newContext();
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
        Optional<Pkg> paramPkgOptional = Optional.empty();
        Optional<User> paramUserOptional = Optional.empty();
        Optional<Repository> paramRepositoryOptional = Optional.empty();
        if (!Strings.isNullOrEmpty(specification.getRepositoryCode())) {
            paramRepositoryOptional = Repository.tryGetByCode(context, specification.getRepositoryCode());
            if (!paramRepositoryOptional.isPresent()) {
                throw new IllegalStateException("unable to find the repository; " + specification.getRepositoryCode());
            }
        }
        if (!Strings.isNullOrEmpty(specification.getUserNickname())) {
            paramUserOptional = User.tryGetByNickname(context, specification.getUserNickname());
            if (!paramUserOptional.isPresent()) {
                throw new IllegalStateException("unable to find the user; " + specification.getUserNickname());
            }
        }
        if (!Strings.isNullOrEmpty(specification.getPkgName())) {
            paramPkgOptional = Pkg.tryGetByName(context, specification.getPkgName());
            if (!paramPkgOptional.isPresent()) {
                throw new IllegalStateException("unable to find the package; " + specification.getPkgName());
            }
        }
        writer.writeNext(new String[] { "pkg-name", "repository-code", "architecture-code", "version-coordinates", "user-nickname", "create-timestamp", "modify-timestamp", "rating", "stability-code", "natural-language-code", "comment", "code" });
        // stream out the packages.
        long startMs = System.currentTimeMillis();
        LOGGER.info("will user rating spreadsheet report");
        final DateTimeFormatter dateTimeFormatter = DateTimeHelper.createStandardDateTimeFormat();
        UserRatingSearchSpecification spec = new UserRatingSearchSpecification();
        spec.setPkg(paramPkgOptional.orElse(null));
        spec.setUser(paramUserOptional.orElse(null));
        spec.setRepository(paramRepositoryOptional.orElse(null));
        // TODO; provide a prefetch tree into the user, pkgversion.
        int count = userRatingService.each(context, spec, userRating -> {
            writer.writeNext(new String[] { userRating.getPkgVersion().getPkg().getName(), userRating.getPkgVersion().getRepositorySource().getRepository().getCode(), userRating.getPkgVersion().getArchitecture().getCode(), userRating.getPkgVersion().toVersionCoordinates().toString(), userRating.getUser().getNickname(), dateTimeFormatter.format(Instant.ofEpochMilli(userRating.getCreateTimestamp().getTime())), dateTimeFormatter.format(Instant.ofEpochMilli(userRating.getModifyTimestamp().getTime())), null != userRating.getRating() ? userRating.getRating().toString() : "", null != userRating.getUserRatingStability() ? userRating.getUserRatingStability().getCode() : "", userRating.getNaturalLanguage().getCode(), userRating.getComment(), userRating.getCode() });
            return true;
        });
        LOGGER.info("did produce user rating spreadsheet report for {} user ratings in {}ms", count, System.currentTimeMillis() - startMs);
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) OutputStream(java.io.OutputStream) CSVWriter(com.opencsv.CSVWriter) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) Repository(org.haiku.haikudepotserver.dataobjects.Repository) UserRatingSearchSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingSearchSpecification) OutputStreamWriter(java.io.OutputStreamWriter) ObjectContext(org.apache.cayenne.ObjectContext) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 42 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserApiIT method testGetUser_foundWithUserUsageConditionsAgreement.

@Test
public void testGetUser_foundWithUserUsageConditionsAgreement() {
    ObjectContext context = serverRuntime.newContext();
    User user = integrationTestSupportService.createBasicUser(context, "testuser", "yUe4o2Nwe009");
    integrationTestSupportService.agreeToUserUsageConditions(context, user);
    setAuthenticatedUser("testuser");
    // ------------------------------------
    GetUserResult result = userApi.getUser(new GetUserRequest("testuser"));
    // ------------------------------------
    // just check the few things that come with the additional user usage agreement
    Assertions.assertThat(result.userUsageConditionsAgreement.timestampAgreed).isNotNull();
    Assertions.assertThat(result.userUsageConditionsAgreement.userUsageConditionsCode).isEqualTo("UUC2021V01");
}
Also used : GetUserResult(org.haiku.haikudepotserver.api1.model.user.GetUserResult) User(org.haiku.haikudepotserver.dataobjects.User) GetUserRequest(org.haiku.haikudepotserver.api1.model.user.GetUserRequest) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 43 with User

use of org.haiku.haikudepotserver.dataobjects.User 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 44 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserApiIT method createPasswordResetTestUser.

private void createPasswordResetTestUser() {
    ObjectContext context = serverRuntime.newContext();
    // language is english
    User user = integrationTestSupportService.createBasicUser(context, "testuser", "yUe4o2Nwe009");
    user.setEmail("integration-test-recipient@haiku-os.org");
    context.commitChanges();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext)

Example 45 with User

use of org.haiku.haikudepotserver.dataobjects.User in project haikudepotserver by haiku.

the class UserApiIT method testAgreeUserUsageConditions.

public void testAgreeUserUsageConditions() {
    ObjectContext context = serverRuntime.newContext();
    integrationTestSupportService.createBasicUser(context, "testuser", "yUe4o2Nwe009");
    setAuthenticatedUser("testuser");
    AgreeUserUsageConditionsRequest request = new AgreeUserUsageConditionsRequest();
    request.userUsageConditionsCode = "UUC2019V01";
    request.nickname = "testuser";
    // ------------------------------------
    AgreeUserUsageConditionsResult result = userApi.agreeUserUsageConditions(request);
    // ------------------------------------
    Assertions.assertThat(result).isNotNull();
    {
        User userAfter = User.getByNickname(context, "testuser");
        Assertions.assertThat(userAfter.tryGetUserUsageConditionsAgreement().get().getUserUsageConditions().getCode()).isEqualTo("UUC2019V01");
    }
}
Also used : AgreeUserUsageConditionsRequest(org.haiku.haikudepotserver.api1.model.user.AgreeUserUsageConditionsRequest) AgreeUserUsageConditionsResult(org.haiku.haikudepotserver.api1.model.user.AgreeUserUsageConditionsResult) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

User (org.haiku.haikudepotserver.dataobjects.User)51 ObjectContext (org.apache.cayenne.ObjectContext)47 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)16 Test (org.junit.jupiter.api.Test)16 AccessDeniedException (org.springframework.security.access.AccessDeniedException)14 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)7 ObjectId (org.apache.cayenne.ObjectId)5 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)5 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)5 PermissionUserPkg (org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)4 AuthenticateUserRequest (org.haiku.haikudepotserver.api1.model.user.AuthenticateUserRequest)3 AuthenticateUserResult (org.haiku.haikudepotserver.api1.model.user.AuthenticateUserResult)3 Captcha (org.haiku.haikudepotserver.captcha.model.Captcha)3 Preconditions (com.google.common.base.Preconditions)2 SignedJWT (com.nimbusds.jwt.SignedJWT)2 CSVWriter (com.opencsv.CSVWriter)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Instant (java.time.Instant)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2