<commit_before>package org.carlspring.strongbox.controllers.users;

import org.carlspring.strongbox.authorization.dto.PrivilegeDto;
import org.carlspring.strongbox.config.IntegrationTest;
import org.carlspring.strongbox.controllers.users.support.AccessModelOutput;
import org.carlspring.strongbox.controllers.users.support.RepositoryAccessModelOutput;
import org.carlspring.strongbox.controllers.users.support.UserOutput;
import org.carlspring.strongbox.controllers.users.support.UserResponseEntity;
import org.carlspring.strongbox.converters.users.AccessModelToAccessModelOutputConverter;
import org.carlspring.strongbox.forms.users.AccessModelForm;
import org.carlspring.strongbox.forms.users.RepositoryAccessModelForm;
import org.carlspring.strongbox.forms.users.UserForm;
import org.carlspring.strongbox.rest.common.RestAssuredBaseTest;
import org.carlspring.strongbox.users.domain.Privileges;
import org.carlspring.strongbox.users.domain.Roles;
import org.carlspring.strongbox.users.domain.User;
import org.carlspring.strongbox.users.dto.UserAccessModelDto;
import org.carlspring.strongbox.users.dto.UserRepositoryDto;
import org.carlspring.strongbox.users.dto.UserStorageDto;
import org.carlspring.strongbox.users.dto.UserPathPrivilegesDto;
import org.carlspring.strongbox.users.dto.UserDto;
import org.carlspring.strongbox.users.service.UserService;
import org.carlspring.strongbox.users.service.impl.StrongboxUserService.StrongboxUserServiceQualifier;

import javax.inject.Inject;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.commons.collections.SetUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.carlspring.strongbox.controllers.users.UserController.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author <NAME>
 */
@IntegrationTest
@Transactional
public class UserControllerTestIT
        extends RestAssuredBaseTest
{

    @Inject
    @StrongboxUserServiceQualifier
    private UserService userService;

    @Inject
    private PasswordEncoder passwordEncoder;

    @Inject
    private PlatformTransactionManager transactionManager;

    private static Stream<Arguments> usersProvider()
    {
        return Stream.of(
                Arguments.of(MediaType.APPLICATION_JSON_VALUE, "test-create-json"),
                Arguments.of(MediaType.TEXT_PLAIN_VALUE, "test-create-text")
        );
    }

    @Override
    @BeforeEach
    public void init()
            throws Exception
    {
        super.init();
        setContextBaseUrl(getContextBaseUrl() + "/api/users");
    }

    @ParameterizedTest
    @MethodSource("usersProvider")
    public void testGetUser(String acceptHeader)
    {
        String username = "test-user-1";

        deleteCreatedUser(username);

        UserDto user = new UserDto();
        user.setEnabled(true);
        user.setUsername(username);
        user.setPassword("<PASSWORD>");
        user.setSecurityTokenKey("before");
        user.getAuthorities().add("A");

        UserAccessModelDto userAccessModelDto = new UserAccessModelDto();
        UserStorageDto userStorageDto = new UserStorageDto();
        userStorageDto.setStorageId("storage0");
        UserRepositoryDto userRepositoryDto = new UserRepositoryDto();
        userRepositoryDto.setRepositoryId("releases");
        PrivilegeDto repositoryPrivilege = new PrivilegeDto("ARTIFACTS_RESOLVE","ARTIFACTS_RESOLVE");
        userRepositoryDto.getRepositoryPrivileges().add(repositoryPrivilege);
        userStorageDto.getRepositories().add(userRepositoryDto);
        userAccessModelDto.getStorages().add(userStorageDto);
        user.setUserAccessModel(userAccessModelDto);

        UserForm userForm = buildFromUser(new User(user), u -> u.setEnabled(true));

        // create new user
        given().contentType(MediaType.APPLICATION_JSON_VALUE)
                .accept(acceptHeader)
                .body(userForm)
                .when()
                .put(getContextBaseUrl())
                .peek() // Use peek() to print the output
                .then()
                .statusCode(HttpStatus.OK.value()) // check http status code
                .body(containsString(SUCCESSFUL_CREATE_USER))
                .extract()
                .asString();

        // retrieve newly created user and store the objectId
        User createdUser = retrieveUserByName(user.getUsername());
        assertEquals(username, createdUser.getUsername());

        // By default assignableRoles should not be present in the response.
        given().accept(MediaType.APPLICATION_JSON_VALUE)
               .when()
               .get(getContextBaseUrl() + "/{name}", username)
               .peek()
               .then()
               .statusCode(HttpStatus.OK.value())
               .body("user.username", equalTo(username))
               .body("user.authorities", notNullValue())
               .body("user.authorities", hasSize(greaterThan(0)))
               .body("user.accessModel.repositoriesAccess", notNullValue())
               .body("user.accessModel.repositoriesAccess", hasSize(greaterThan(0)))
               .body("assignableRoles", nullValue());


        // assignableRoles should be present only if there is ?assignableRoles=true in the request.
        given().accept(MediaType.APPLICATION_JSON_VALUE)
               .when()
               .get(getContextBaseUrl() + "/{name}?formFields=true", username)
               .peek()
               .then()
               .statusCode(HttpStatus.OK.value())
               .body("user.username", equalTo(username))
               .body("user.authorities", notNullValue())
               .body("user.authorities", hasSize(greaterThan(0)))
               .body("user.accessModel.repositoriesAccess", notNullValue())
               .body("user.accessModel.repositoriesAccess", hasSize(greaterThan(0)))
               .body("assignableRoles", notNullValue())
               .body("assignableRoles", hasSize(greaterThan(0)))
               .body("assignablePrivileges", notNullValue())
               .body("assignablePrivileges", hasSize(greaterThan(0)));

        deleteCreatedUser(username);<commit_msg>Issue 1138: Drop the default test users<commit_after>package org.carlspring.strongbox.controllers.users;

import org.carlspring.strongbox.authorization.dto.PrivilegeDto;
import org.carlspring.strongbox.config.IntegrationTest;
import org.carlspring.strongbox.controllers.users.support.AccessModelOutput;
import org.carlspring.strongbox.controllers.users.support.RepositoryAccessModelOutput;
import org.carlspring.strongbox.controllers.users.support.UserOutput;
import org.carlspring.strongbox.controllers.users.support.UserResponseEntity;
import org.carlspring.strongbox.converters.users.AccessModelToAccessModelOutputConverter;
import org.carlspring.strongbox.forms.users.AccessModelForm;
import org.carlspring.strongbox.forms.users.RepositoryAccessModelForm;
import org.carlspring.strongbox.forms.users.UserForm;
import org.carlspring.strongbox.rest.common.RestAssuredBaseTest;
import org.carlspring.strongbox.users.domain.Privileges;
import org.carlspring.strongbox.users.domain.Roles;
import org.carlspring.strongbox.users.domain.User;
import org.carlspring.strongbox.users.dto.UserAccessModelDto;
import org.carlspring.strongbox.users.dto.UserRepositoryDto;
import org.carlspring.strongbox.users.dto.UserStorageDto;
import org.carlspring.strongbox.users.dto.UserPathPrivilegesDto;
import org.carlspring.strongbox.users.dto.UserDto;
import org.carlspring.strongbox.users.service.UserService;
import org.carlspring.strongbox.users.service.impl.StrongboxUserService.StrongboxUserServiceQualifier;

import javax.inject.Inject;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.commons.collections.SetUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.carlspring.strongbox.controllers.users.UserController.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Pablo Tirado
 */
@IntegrationTest
@Transactional
public class UserControllerTestIT
        extends RestAssuredBaseTest
{

    @Inject
    @StrongboxUserServiceQualifier
    private UserService userService;

    @Inject
    private PasswordEncoder passwordEncoder;

    @Inject
    private PlatformTransactionManager transactionManager;

    private static Stream<Arguments> usersProvider()
    {
        return Stream.of(
                Arguments.of(MediaType.APPLICATION_JSON_VALUE, "test-create-json"),
                Arguments.of(MediaType.TEXT_PLAIN_VALUE, "test-create-text")
        );
    }

    @Override
    @BeforeEach
    public void init()
            throws Exception
    {
        super.init();
        setContextBaseUrl(getContextBaseUrl() + "/api/users");
    }

    @ParameterizedTest
    @MethodSource("usersProvider")
    public void testGetUser(String acceptHeader)
    {
        String username = "test-user-1";

        deleteCreatedUser(username);

        UserDto user = new UserDto();
        user.setEnabled(true);
        user.setUsername(username);
        user.setPassword("<PASSWORD>");
        user.setSecurityTokenKey("before");

        UserAccessModelDto userAccessModelDto = new UserAccessModelDto();
        UserStorageDto userStorageDto = new UserStorageDto();
        userStorageDto.setStorageId("storage0");
        UserRepositoryDto userRepositoryDto = new UserRepositoryDto();
        userRepositoryDto.setRepositoryId("releases");
        PrivilegeDto repositoryPrivilege = new PrivilegeDto("ARTIFACTS_RESOLVE","ARTIFACTS_RESOLVE");
        userRepositoryDto.getRepositoryPrivileges().add(repositoryPrivilege);
        userStorageDto.getRepositories().add(userRepositoryDto);
        userAccessModelDto.getStorages().add(userStorageDto);
        user.setUserAccessModel(userAccessModelDto);

        UserForm userForm = buildFromUser(new User(user), u -> u.setEnabled(true));

        // create new user
        given().contentType(MediaType.APPLICATION_JSON_VALUE)
                .accept(acceptHeader)
                .body(userForm)
                .when()
                .put(getContextBaseUrl())
                .peek() // Use peek() to print the output
                .then()
                .statusCode(HttpStatus.OK.value()) // check http status code
                .body(containsString(SUCCESSFUL_CREATE_USER))
                .extract()
                .asString();

        // retrieve newly created user and store the objectId
        User createdUser = retrieveUserByName(user.getUsername());
        assertEquals(username, createdUser.getUsername());

        // By default assignableRoles should not be present in the response.
        given().accept(MediaType.APPLICATION_JSON_VALUE)
               .when()
               .get(getContextBaseUrl() + "/{name}", username)
               .peek()
               .then()
               .statusCode(HttpStatus.OK.value())
               .body("user.username", equalTo(username))
               .body("user.accessModel.repositoriesAccess", notNullValue())
               .body("user.accessModel.repositoriesAccess", hasSize(greaterThan(0)))
               .body("assignableRoles", nullValue());


        // assignableRoles should be present only if there is ?assignableRoles=true in the request.
        given().accept(MediaType.APPLICATION_JSON_VALUE)
               .when()
               .get(getContextBaseUrl() + "/{name}?formFields=true", username)
               .peek()
               .then()
               .statusCode(HttpStatus.OK.value())
               .body("user.username", equalTo(username))
               .body("user.accessModel.repositoriesAccess", notNullValue())
               .body("user.accessModel.repositoriesAccess", hasSize(greaterThan(0)))
               .body("assignableRoles", notNullValue())
               .body("assignableRoles", hasSize(greaterThan(0)))
               .body("assignablePrivileges", notNullValue())
               .body("assignablePrivileges", hasSize(greaterThan(0)));

        deleteCreatedUser(username);
{'fraction_non_alphanumeric': 0.10464310464310464, 'fraction_numerical': 0.0012474012474012475, 'mean_word_length': 3.5480617711944533, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '23274', 'n_tokens_mistral': 3997, 'n_tokens_neox': 3960, 'n_words': 609}