Esempio n. 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);
  }
Esempio n. 2
0
 private ComponentDto insertJdk7() {
   return componentDb.insertComponent(
       newProjectDto(Uuids.UUID_EXAMPLE_02)
           .setName("JDK 7")
           .setKey("net.java.openjdk:jdk7")
           .setDescription("JDK"));
 }
Esempio n. 3
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);
  }
Esempio n. 4
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());
  }
Esempio n. 5
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();
  }
Esempio n. 6
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());
  }
Esempio n. 7
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());
  }
Esempio n. 8
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");
  }
Esempio n. 9
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());
  }
Esempio n. 10
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");
  }
Esempio n. 11
0
 private ComponentDto insertDeveloper() {
   return componentDb.insertComponent(
       newDeveloper("Joda", "4e607bf9-7ed0-484a-946d-d58ba7dab2fb").setKey("joda"));
 }
Esempio n. 12
0
 private ComponentDto insertView() {
   return componentDb.insertComponent(
       newView("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc").setName("Java").setKey("Java"));
 }
Esempio n. 13
0
 private ComponentDto insertClang() {
   return componentDb.insertComponent(
       newProjectDto(Uuids.UUID_EXAMPLE_01).setName("Clang").setKey("clang"));
 }