Пример #1
0
  @Test
  public void select_groups_by_query_with_project_permissions() {
    GroupDto group1 = groupDb.insertGroup();
    GroupDto group2 = groupDb.insertGroup();
    GroupDto group3 = groupDb.insertGroup();

    ComponentDto project = componentDb.insertComponent(newProjectDto());
    ComponentDto anotherProject = componentDb.insertComponent(newProjectDto());

    permissionDb.addProjectPermissionToGroup(SCAN_EXECUTION, group1.getId(), project.getId());
    permissionDb.addProjectPermissionToGroup(PROVISIONING, group1.getId(), project.getId());
    permissionDb.addProjectPermissionToGroup(USER, null, project.getId());

    permissionDb.addProjectPermissionToGroup(SYSTEM_ADMIN, group1.getId(), anotherProject.getId());
    permissionDb.addProjectPermissionToGroup(SYSTEM_ADMIN, null, anotherProject.getId());
    permissionDb.addProjectPermissionToGroup(
        SCAN_EXECUTION, group3.getId(), anotherProject.getId());

    permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group2.getId());

    PermissionQuery.Builder builderOnComponent =
        PermissionQuery.builder().setComponentUuid(project.uuid());
    assertThat(
            underTest.selectGroupNamesByPermissionQuery(
                dbSession, builderOnComponent.withPermissionOnly().build()))
        .containsOnlyOnce(group1.getName());
    assertThat(
            underTest.selectGroupNamesByPermissionQuery(
                dbSession, builderOnComponent.setPermission(SCAN_EXECUTION).build()))
        .containsOnlyOnce(group1.getName());
    assertThat(
            underTest.selectGroupNamesByPermissionQuery(
                dbSession, builderOnComponent.setPermission(USER).build()))
        .containsOnlyOnce(ANYONE);
  }
  @Test
  public void index_all_project() {
    componentDbTester.insertProjectAndSnapshot(newProjectDto());
    componentDbTester.insertProjectAndSnapshot(newProjectDto());
    componentDbTester.insertProjectAndSnapshot(newProjectDto());

    underTest.index();

    assertThat(esTester.countDocuments(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).isEqualTo(3);
  }
  @Test
  public void index_one_project() throws Exception {
    ComponentDto project = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project);
    componentDbTester.insertProjectAndSnapshot(newProjectDto());

    underTest.index(project.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project.uuid());
  }
Пример #4
0
  @Test
  public void select_groups_by_query_with_global_permission() {
    GroupDto group1 = groupDb.insertGroup(newGroupDto().setName("Group-1"));
    GroupDto group2 = groupDb.insertGroup(newGroupDto().setName("Group-2"));
    GroupDto group3 = groupDb.insertGroup(newGroupDto().setName("Group-3"));

    ComponentDto project = componentDb.insertComponent(newProjectDto());

    permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null);
    permissionDb.addGlobalPermissionToGroup(PROVISIONING, null);
    permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group1.getId());
    permissionDb.addGlobalPermissionToGroup(SYSTEM_ADMIN, group3.getId());
    permissionDb.addProjectPermissionToGroup(UserRole.ADMIN, group2.getId(), project.getId());

    assertThat(
            underTest.selectGroupNamesByPermissionQuery(
                dbSession, PermissionQuery.builder().setPermission(SCAN_EXECUTION).build()))
        .containsExactly(ANYONE, "Group-1");

    assertThat(
            underTest.selectGroupNamesByPermissionQuery(
                dbSession, PermissionQuery.builder().setPermission(SYSTEM_ADMIN).build()))
        .containsExactly("Group-3");

    assertThat(
            underTest.selectGroupNamesByPermissionQuery(
                dbSession, PermissionQuery.builder().setPermission(PROVISIONING).build()))
        .containsExactly(ANYONE);
  }
Пример #5
0
 private ComponentDto insertJdk7() {
   return componentDb.insertComponent(
       newProjectDto(Uuids.UUID_EXAMPLE_02)
           .setName("JDK 7")
           .setKey("net.java.openjdk:jdk7")
           .setDescription("JDK"));
 }
Пример #6
0
  @Test
  public void sort_projects_by_name() {
    ComponentDto b_project = componentDb.insertComponent(newProjectDto().setName("B_project_name"));
    ComponentDto c_project = componentDb.insertComponent(newProjectDto().setName("c_project_name"));
    ComponentDto a_project = componentDb.insertComponent(newProjectDto().setName("A_project_name"));

    insertUserPermission(UserRole.ADMIN, user.getId(), b_project.getId());
    insertUserPermission(UserRole.ADMIN, user.getId(), a_project.getId());
    insertUserPermission(UserRole.ADMIN, user.getId(), c_project.getId());

    SearchMyProjectsWsResponse result = call_ws();

    assertThat(result.getProjectsCount()).isEqualTo(3);
    assertThat(result.getProjectsList())
        .extracting(Project::getId)
        .containsExactly(a_project.uuid(), b_project.uuid(), c_project.uuid());
  }
  @Test
  public void index_projects_even_when_no_analysis() {
    ComponentDto project = componentDbTester.insertProject();

    underTest.index();

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project.uuid());
  }
Пример #8
0
  @Test
  public void select_group_permissions_by_group_names_on_project_permissions() {
    GroupDto group1 = groupDb.insertGroup(newGroupDto().setName("Group-1"));
    permissionDb.addGlobalPermissionToGroup(PROVISIONING, group1.getId());

    GroupDto group2 = groupDb.insertGroup(newGroupDto().setName("Group-2"));
    ComponentDto project = componentDb.insertComponent(newProjectDto());
    permissionDb.addProjectPermissionToGroup(USER, group2.getId(), project.getId());

    GroupDto group3 = groupDb.insertGroup(newGroupDto().setName("Group-3"));
    permissionDb.addProjectPermissionToGroup(USER, group3.getId(), project.getId());

    // Anyone group
    permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null);
    permissionDb.addProjectPermissionToGroup(PROVISIONING, null, project.getId());

    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Group-1"), project.getId()))
        .isEmpty();

    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Group-2"), project.getId()))
        .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId)
        .containsOnly(tuple(group2.getId(), USER, project.getId()));

    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Group-3"), project.getId()))
        .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId)
        .containsOnly(tuple(group3.getId(), USER, project.getId()));

    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Anyone"), project.getId()))
        .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId)
        .containsOnly(tuple(0L, PROVISIONING, project.getId()));

    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Group-1", "Group-2", "Anyone"), project.getId()))
        .hasSize(2);
    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Unknown"), project.getId()))
        .isEmpty();
    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, asList("Group-1"), 123L))
        .isEmpty();
    assertThat(
            underTest.selectGroupPermissionsByGroupNamesAndProject(
                dbSession, Collections.emptyList(), project.getId()))
        .isEmpty();
  }
  @Test
  public void delete_project() {
    ComponentDto project1 = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project1);
    ComponentDto project2 = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project2);
    ComponentDto project3 = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project3);
    underTest.index();
    authorizationIndexerTester.indexProjectPermission(project1.uuid(), emptyList(), emptyList());
    authorizationIndexerTester.indexProjectPermission(project2.uuid(), emptyList(), emptyList());
    authorizationIndexerTester.indexProjectPermission(project3.uuid(), emptyList(), emptyList());

    underTest.deleteProject(project1.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project2.uuid(), project3.uuid());
    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_AUTHORIZATION))
        .containsOnly(project2.uuid(), project3.uuid());
  }
Пример #10
0
  @Test
  public void search_my_projects_by_exact_match_on_key() {
    ComponentDto sonarqube = componentDb.insertComponent(newProjectDto().setKey("MY_PROJECT_KEY"));
    ComponentDto ruby =
        componentDb.insertComponent(newProjectDto().setKey("MY_PROJECT_KEY_OR_ELSE"));
    dbClient.snapshotDao().insert(dbSession, newAnalysis(sonarqube), newAnalysis(ruby));
    componentDb.indexAllComponents();
    db.commit();

    insertUserPermission(UserRole.ADMIN, user.getId(), sonarqube.getId());
    insertUserPermission(UserRole.ADMIN, user.getId(), ruby.getId());

    SearchMyProjectsWsResponse result =
        call_ws(ws.newRequest().setParam(TEXT_QUERY, "MY_PROJECT_KEY"));

    assertThat(result.getProjectsCount()).isEqualTo(1);
    assertThat(result.getProjectsList())
        .extracting(Project::getId)
        .containsOnlyOnce(sonarqube.uuid())
        .doesNotContain(ruby.uuid());
  }
  @Test
  public void does_nothing_when_deleting_unknown_project() throws Exception {
    ComponentDto project = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project);
    underTest.index();
    authorizationIndexerTester.indexProjectPermission(project.uuid(), emptyList(), emptyList());

    underTest.deleteProject("UNKNOWN");

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project.uuid());
    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_AUTHORIZATION))
        .containsOnly(project.uuid());
  }
Пример #12
0
  @Test
  public void search_my_projects_by_name() {
    ComponentDto sonarqube =
        componentDb.insertComponent(newProjectDto().setName("ONE_PROJECT_NAME"));
    ComponentDto jdk8 = componentDb.insertComponent(newProjectDto().setName("TWO_PROJECT_NAME"));
    ComponentDto ruby = componentDb.insertComponent(newProjectDto().setName("ANOTHER_42"));
    dbClient
        .snapshotDao()
        .insert(dbSession, newAnalysis(sonarqube), newAnalysis(jdk8), newAnalysis(ruby));
    componentDb.indexAllComponents();
    db.commit();

    insertUserPermission(UserRole.ADMIN, user.getId(), sonarqube.getId());
    insertUserPermission(UserRole.ADMIN, user.getId(), jdk8.getId());
    insertUserPermission(UserRole.ADMIN, user.getId(), ruby.getId());

    SearchMyProjectsWsResponse result = call_ws(ws.newRequest().setParam(TEXT_QUERY, "_project_"));

    assertThat(result.getProjectsCount()).isEqualTo(2);
    assertThat(result.getProjectsList())
        .extracting(Project::getId)
        .containsOnlyOnce(sonarqube.uuid(), jdk8.uuid())
        .doesNotContain(ruby.uuid());
  }
Пример #13
0
  @Test
  public void paginate_projects() {
    for (int i = 0; i < 10; i++) {
      ComponentDto project = componentDb.insertComponent(newProjectDto().setName("project-" + i));
      insertUserPermission(UserRole.ADMIN, user.getId(), project.getId());
    }

    SearchMyProjectsWsResponse result =
        call_ws(ws.newRequest().setParam(Param.PAGE, "2").setParam(Param.PAGE_SIZE, "3"));

    assertThat(result.getProjectsCount()).isEqualTo(3);
    assertThat(result.getProjectsList())
        .extracting(Project::getName)
        .containsExactly("project-3", "project-4", "project-5");
  }
Пример #14
0
  @Test
  public void admin_via_groups_and_users() {
    ComponentDto jdk7 = insertJdk7();
    ComponentDto cLang = insertClang();
    ComponentDto sonarqube = componentDb.insertComponent(newProjectDto());

    GroupDto group = groupDb.insertGroup(newGroupDto());
    groupDb.addUserToGroup(user.getId(), group.getId());

    insertUserPermission(UserRole.ADMIN, user.getId(), jdk7.getId());
    insertGroupPermission(UserRole.ADMIN, group.getId(), cLang.getId());
    // admin via group and user
    insertUserPermission(UserRole.ADMIN, user.getId(), sonarqube.getId());
    insertGroupPermission(UserRole.ADMIN, group.getId(), sonarqube.getId());

    SearchMyProjectsWsResponse result = call_ws();

    assertThat(result.getProjectsCount()).isEqualTo(3);
    assertThat(result.getProjectsList())
        .extracting(Project::getId)
        .containsOnly(jdk7.uuid(), cLang.uuid(), sonarqube.uuid());
  }
Пример #15
0
  @Test
  public void search_groups_on_views() {
    ComponentDto view = componentDb.insertComponent(newView("view-uuid").setKey("view-key"));
    GroupDto group = insertGroup(new GroupDto().setName("project-group-name"));
    insertGroupRole(
        new GroupRoleDto()
            .setGroupId(group.getId())
            .setRole(ISSUE_ADMIN)
            .setResourceId(view.getId()));

    String result =
        ws.newRequest()
            .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
            .setParam(PARAM_PROJECT_ID, "view-uuid")
            .execute()
            .getInput();

    assertThat(result)
        .contains("project-group-name")
        .doesNotContain("group-1")
        .doesNotContain("group-2")
        .doesNotContain("group-3");
  }
  @Test
  public void update_existing_document_when_indexing_one_project() throws Exception {
    String uuid = "PROJECT-UUID";
    esTester.putDocuments(
        INDEX_PROJECT_MEASURES,
        TYPE_PROJECT_MEASURES,
        new ProjectMeasuresDoc()
            .setId(uuid)
            .setKey("Old Key")
            .setName("Old Name")
            .setAnalysedAt(new Date(1_000_000L)));
    ComponentDto project = newProjectDto(uuid).setKey("New key").setName("New name");
    SnapshotDto analysis = componentDbTester.insertProjectAndSnapshot(project);

    underTest.index(project.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).containsOnly(uuid);
    SearchRequestBuilder request =
        esTester
            .client()
            .prepareSearch(INDEX_PROJECT_MEASURES)
            .setTypes(TYPE_PROJECT_MEASURES)
            .setQuery(
                boolQuery()
                    .must(matchAllQuery())
                    .filter(
                        boolQuery()
                            .must(termQuery("_id", uuid))
                            .must(termQuery(ProjectMeasuresIndexDefinition.FIELD_KEY, "New key"))
                            .must(termQuery(ProjectMeasuresIndexDefinition.FIELD_NAME, "New name"))
                            .must(
                                termQuery(
                                    ProjectMeasuresIndexDefinition.FIELD_ANALYSED_AT,
                                    new Date(analysis.getCreatedAt())))));
    assertThat(request.get().getHits()).hasSize(1);
  }
Пример #17
0
 private ComponentDto insertDeveloper() {
   return componentDb.insertComponent(
       newDeveloper("Joda", "4e607bf9-7ed0-484a-946d-d58ba7dab2fb").setKey("joda"));
 }
Пример #18
0
 private ComponentDto insertView() {
   return componentDb.insertComponent(
       newView("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc").setName("Java").setKey("Java"));
 }
Пример #19
0
 private ComponentDto insertClang() {
   return componentDb.insertComponent(
       newProjectDto(Uuids.UUID_EXAMPLE_01).setName("Clang").setKey("clang"));
 }