Search in sources :

Example 1 with User

use of com.mercedesbenz.sechub.domain.administration.user.User in project sechub by mercedes-benz.

the class ProjectAdministrationRestControllerMockTest method get_project_details_returns_project_details.

@Test
public void get_project_details_returns_project_details() throws Exception {
    Project project = new Project();
    project.id = "project1";
    project.description = "description";
    project.owner = new User();
    ProjectDetailInformation details = new ProjectDetailInformation(project);
    when(detailService.fetchDetails(matches("project1"))).thenReturn(details);
    /* execute + test @formatter:off */
    this.mockMvc.perform(get(https(PORT_USED).buildAdminFetchProjectInfoUrl(PROJECT_ID.pathElement()), "project1").contentType(MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()).andExpect(jsonPath("$.projectId", CoreMatchers.equalTo("project1"))).andExpect(jsonPath("$.description", CoreMatchers.equalTo("description"))).andExpect(jsonPath("$.owner", CoreMatchers.nullValue())).andExpect(jsonPath("$.users", CoreMatchers.notNullValue()));
    verify(detailService).fetchDetails(matches("project1"));
/* @formatter:on */
}
Also used : User(com.mercedesbenz.sechub.domain.administration.user.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 2 with User

use of com.mercedesbenz.sechub.domain.administration.user.User in project sechub by mercedes-benz.

the class ProjectAssignUserServiceTest method assign_already_added_user_to_project__throws_already_exists_exception.

@Test
public void assign_already_added_user_to_project__throws_already_exists_exception() {
    User existingUser = mock(User.class);
    /* prepare */
    Project project1 = new Project();
    project1.id = "project1";
    project1.users.add(existingUser);
    when(projectRepository.findOrFailProject("project1")).thenReturn(project1);
    when(existingUser.getName()).thenReturn("existing");
    when(userRepository.findOrFailUser("existing")).thenReturn(existingUser);
    /* execute */
    /* test */
    assertThrows(AlreadyExistsException.class, () -> {
        serviceToTest.assignUserToProject(existingUser.getName(), project1.getId());
    });
}
Also used : User(com.mercedesbenz.sechub.domain.administration.user.User) Test(org.junit.Test)

Example 3 with User

use of com.mercedesbenz.sechub.domain.administration.user.User in project sechub by mercedes-benz.

the class ProjectAssignUserServiceTest method assign_new_user_to_project.

@Test
public void assign_new_user_to_project() {
    User newUser = mock(User.class);
    /* prepare */
    Project project1 = new Project();
    project1.id = "project1";
    when(projectRepository.findOrFailProject("project1")).thenReturn(project1);
    when(newUser.getName()).thenReturn("new");
    when(userRepository.findOrFailUser("new")).thenReturn(newUser);
    /* execute */
    serviceToTest.assignUserToProject(newUser.getName(), project1.getId());
    /* test */
    verify(transactionService).saveInOwnTransaction(project1, newUser);
}
Also used : User(com.mercedesbenz.sechub.domain.administration.user.User) Test(org.junit.Test)

Example 4 with User

use of com.mercedesbenz.sechub.domain.administration.user.User in project sechub by mercedes-benz.

the class ProjectCreationService method createProject.

@Validated
/* @formatter:off */
@UseCaseAdminCreatesProject(@Step(number = 2, name = "Create project", description = "The service will create the project when not already existing with such name."))
public /* @formatter:on */
void createProject(@NotNull String projectId, @NotNull String description, @NotNull String owner, @NotNull Set<URI> whitelist, @NotNull ProjectMetaData metaData) {
    LOG.info("Administrator {} triggers create of project:{}, having owner:{}", userContext.getUserId(), projectId, owner);
    assertion.assertIsValidProjectId(projectId);
    assertion.assertIsValidUserId(owner);
    assertion.assertIsValidProjectDescription(description);
    /* assert found */
    Optional<Project> foundProject = projectRepository.findById(projectId);
    if (foundProject.isPresent()) {
        throw new AlreadyExistsException("Project '" + projectId + "' already exists");
    }
    Optional<User> foundOwner = userRepository.findById(owner);
    if (!foundOwner.isPresent()) {
        throw new NotFoundException("Owner '" + owner + "' not found");
    }
    /* setup */
    Project project = new Project();
    project.id = projectId;
    project.description = description;
    User ownerUser = foundOwner.get();
    project.owner = ownerUser;
    /**
     * add only accepted/valid URIs - sanitize
     */
    whitelist.stream().filter(uri -> uriValidation.validate(uri).isValid()).forEach(project.getWhiteList()::add);
    List<ProjectMetaDataEntity> metaDataEntities = metaData.getMetaDataMap().entrySet().stream().map(entry -> new ProjectMetaDataEntity(projectId, entry.getKey(), entry.getValue())).collect(Collectors.toList());
    project.metaData.addAll(metaDataEntities);
    /* store */
    persistenceService.saveInOwnTransaction(project);
    sendProjectCreatedEvent(projectId, whitelist);
    sendRefreshUserAuth(ownerUser);
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) User(com.mercedesbenz.sechub.domain.administration.user.User) MessageID(com.mercedesbenz.sechub.sharedkernel.messaging.MessageID) ProjectMessage(com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage) DomainMessage(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage) Service(org.springframework.stereotype.Service) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Step(com.mercedesbenz.sechub.sharedkernel.Step) URI(java.net.URI) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) MessageDataKeys(com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys) UserRepository(com.mercedesbenz.sechub.domain.administration.user.UserRepository) DomainMessageFactory(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) UserInputAssertion(com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion) Validated(org.springframework.validation.annotation.Validated) Logger(org.slf4j.Logger) UserContextService(com.mercedesbenz.sechub.sharedkernel.UserContextService) Set(java.util.Set) URIValidation(com.mercedesbenz.sechub.sharedkernel.validation.URIValidation) RoleConstants(com.mercedesbenz.sechub.sharedkernel.RoleConstants) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) IsSendingAsyncMessage(com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage) List(java.util.List) DomainMessageService(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService) ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) Optional(java.util.Optional) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Validated(org.springframework.validation.annotation.Validated) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)

Example 5 with User

use of com.mercedesbenz.sechub.domain.administration.user.User in project sechub by mercedes-benz.

the class ProjectDeleteService method deleteProject.

@UseCaseAdminDeleteProject(@Step(number = 2, name = "Service deletes projects.", next = { 3, 4, 5, 6, 7 }, description = "The service will delete the project with dependencies and triggers asynchronous events"))
public void deleteProject(String projectId) {
    auditLogService.log("triggers delete of project {}", logSanitizer.sanitize(projectId, 30));
    assertion.assertIsValidProjectId(projectId);
    Project project = projectRepository.findOrFailProject(projectId);
    /* create message containing data before project is deleted */
    ProjectMessage message = new ProjectMessage();
    message.setProjectId(project.getId());
    message.setProjectActionTriggeredBy(userContext.getUserId());
    User owner = project.getOwner();
    if (owner == null) {
        LOG.warn("No owner found for project {} while deleting", project.getId());
    } else {
        message.setProjectOwnerEmailAddress(owner.getEmailAdress());
        // handle ORM mapping. Avoid cache conflicts
        owner.getOwnedProjects().remove(project);
    }
    for (User user : project.getUsers()) {
        message.addUserEmailAddress(user.getEmailAdress());
        // handle ORM mapping. Avoid cache conflicts
        user.getProjects().remove(project);
    }
    transactionService.deleteWithAssociationsInOwnTransaction(projectId);
    informProjectDeleted(message);
    if (owner != null) {
        sendRefreshUserAuth(owner);
    }
}
Also used : UseCaseAdminDeleteProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminDeleteProject) ProjectMessage(com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage) User(com.mercedesbenz.sechub.domain.administration.user.User) UseCaseAdminDeleteProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminDeleteProject)

Aggregations

User (com.mercedesbenz.sechub.domain.administration.user.User)20 Test (org.junit.Test)11 AlreadyExistsException (com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 Project (com.mercedesbenz.sechub.domain.administration.project.Project)3 UseCaseRestDoc (com.mercedesbenz.sechub.sharedkernel.usecases.UseCaseRestDoc)3 UseCaseAdminCreatesProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)3 UseCaseAdminDeleteProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminDeleteProject)3 UseCaseAdminAssignsUserToProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminAssignsUserToProject)3 UseCaseAdminUnassignsUserFromProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminUnassignsUserFromProject)3 URI (java.net.URI)3 LinkedHashSet (java.util.LinkedHashSet)3 ProjectDetailInformation (com.mercedesbenz.sechub.domain.administration.project.ProjectDetailInformation)2 ProjectMetaDataEntity (com.mercedesbenz.sechub.domain.administration.project.ProjectMetaDataEntity)2 DomainMessage (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage)2 JobMessage (com.mercedesbenz.sechub.sharedkernel.messaging.JobMessage)2 ProjectMessage (com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage)2 ProjectMetaData (com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData)1 UserDetailInformation (com.mercedesbenz.sechub.domain.administration.user.UserDetailInformation)1