Search in sources :

Example 1 with Signup

use of com.mercedesbenz.sechub.domain.administration.signup.Signup in project sechub by mercedes-benz.

the class UserCreationService method createUserFromSelfRegistration.

@UseCaseAdminAcceptsSignup(@Step(number = 2, name = "Create user and send events", next = { 3, 4 }, description = "The service will create the user a one time token for api token generation and triggers asynchronous events.\n" + "It will also remove the existing user signup because no longer necessary."))
public void createUserFromSelfRegistration(String userId) {
    String sanitizedLogUserId = logSanitizer.sanitize(userId, 30);
    auditLog.log("accepts signup of user {}", sanitizedLogUserId);
    assertion.assertIsValidUserId(userId);
    Optional<Signup> selfRegistration = selfRegistrationRepository.findById(userId);
    if (!selfRegistration.isPresent()) {
        LOG.warn("Did not found a self registration for user with name:{}, so skipped creation", sanitizedLogUserId);
        return;
    }
    Optional<User> found = userRepository.findById(userId);
    if (found.isPresent()) {
        LOG.warn("Self registration coming in for user:{} but user already exists. So just removing self registration entry", sanitizedLogUserId);
        selfRegistrationRepository.deleteById(userId);
        return;
    }
    String emailAdress = selfRegistration.get().getEmailAdress();
    assertion.assertIsValidEmailAddress(emailAdress);
    found = userRepository.findByEmailAdress(emailAdress);
    if (found.isPresent()) {
        LOG.warn("Self registration coming in for user:{} but mailadress {} already exists. So just removing self registration entry", sanitizedLogUserId, emailAdress);
        selfRegistrationRepository.deleteById(userId);
        return;
    }
    String oneTimeToken = oneTimeTokenGenerator.generateNewOneTimeToken();
    User user = new User();
    user.name = userId;
    // leave it empty, so API auth is disabled - will be filled later after user has
    user.hashedApiToken = "";
    // clicked to link
    user.emailAdress = emailAdress;
    user.oneTimeToken = oneTimeToken;
    user.oneTimeTokenDate = new Date();
    userRepository.save(user);
    LOG.debug("Persisted new user:{}", sanitizedLogUserId);
    selfRegistrationRepository.deleteById(userId);
    LOG.debug("Removed self registration data of user:{}", sanitizedLogUserId);
    informUserAboutSignupAccepted(user);
    informUserCreated(user);
}
Also used : Signup(com.mercedesbenz.sechub.domain.administration.signup.Signup) UseCaseAdminAcceptsSignup(com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminAcceptsSignup) Date(java.util.Date) UseCaseAdminAcceptsSignup(com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminAcceptsSignup)

Example 2 with Signup

use of com.mercedesbenz.sechub.domain.administration.signup.Signup in project sechub by mercedes-benz.

the class SignupAdministrationRestControllerRestDocTest method restdoc_list_user_signups.

@Test
@UseCaseRestDoc(useCase = UseCaseAdminListsOpenUserSignups.class)
public void restdoc_list_user_signups() throws Exception {
    /* prepare */
    String apiEndpoint = https(PORT_USED).buildAdminListsUserSignupsUrl();
    Class<? extends Annotation> useCase = UseCaseAdminListsOpenUserSignups.class;
    Signup signup1 = new Signup();
    signup1.setEmailAdress("john.smith@example.com");
    signup1.setUserId("johnsmith");
    Signup signup2 = new Signup();
    signup2.setEmailAdress("jane.smith@example.com");
    signup2.setUserId("janesmith");
    List<Signup> signupList = new ArrayList<>();
    signupList.add(signup1);
    signupList.add(signup2);
    when(signupRepository.findAll()).thenReturn(signupList);
    /* execute + test @formatter:off */
    this.mockMvc.perform(get(apiEndpoint)).andExpect(status().isOk()).andExpect(content().json("[{\"userId\":\"johnsmith\",\"emailAdress\":\"john.smith@example.com\"},{\"userId\":\"janesmith\",\"emailAdress\":\"jane.smith@example.com\"}]")).andDo(defineRestService().with().useCaseData(useCase).tag(RestDocFactory.extractTag(apiEndpoint)).responseSchema(OpenApiSchema.SIGNUP_LIST.getSchema()).and().document(responseFields(fieldWithPath("[]").description("List of user signups").optional(), fieldWithPath("[]." + RestDocPathParameter.USER_ID.paramName()).type(JsonFieldType.STRING).description("The user id"), fieldWithPath("[].emailAdress").type(JsonFieldType.STRING).description("The email address"))));
/* @formatter:on */
}
Also used : Signup(com.mercedesbenz.sechub.domain.administration.signup.Signup) UseCaseAdminDeletesSignup(com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminDeletesSignup) UseCaseAdminListsOpenUserSignups(com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminListsOpenUserSignups) ArrayList(java.util.ArrayList) UseCaseRestDoc(com.mercedesbenz.sechub.sharedkernel.usecases.UseCaseRestDoc) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with Signup

use of com.mercedesbenz.sechub.domain.administration.signup.Signup in project sechub by mercedes-benz.

the class UserAdministrationRestControllerMockTest method calling_with_api_1_0_and_valid_userid_and_email_returns_HTTP_200.

@Test
public void calling_with_api_1_0_and_valid_userid_and_email_returns_HTTP_200() throws Exception {
    Signup selfReg = new Signup();
    selfReg.setUserId("user1");
    Optional<Signup> selfRegistration = Optional.ofNullable(selfReg);
    /* prepare */
    when(mockedSelfRegistrationRepo.findById("user1")).thenReturn(selfRegistration);
    /* execute + test @formatter:off */
    this.mockMvc.perform(post(https(PORT_USED).buildAdminAcceptsUserSignUpUrl("user1"))).andExpect(status().isCreated());
/* @formatter:on */
}
Also used : Signup(com.mercedesbenz.sechub.domain.administration.signup.Signup) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Aggregations

Signup (com.mercedesbenz.sechub.domain.administration.signup.Signup)3 Test (org.junit.Test)2 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)2 UseCaseRestDoc (com.mercedesbenz.sechub.sharedkernel.usecases.UseCaseRestDoc)1 UseCaseAdminAcceptsSignup (com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminAcceptsSignup)1 UseCaseAdminDeletesSignup (com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminDeletesSignup)1 UseCaseAdminListsOpenUserSignups (com.mercedesbenz.sechub.sharedkernel.usecases.admin.signup.UseCaseAdminListsOpenUserSignups)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1