예제 #1
0
  @Test
  public void return_rules_in_protobuf() throws Exception {
    dbTester
        .getDbClient()
        .ruleDao()
        .insert(
            dbTester.getSession(),
            RuleTesting.newDto(RuleKey.of("java", "S001")).setConfigKey(null).setName(null));
    dbTester
        .getDbClient()
        .ruleDao()
        .insert(
            dbTester.getSession(),
            RuleTesting.newDto(RuleKey.of("java", "S002"))
                .setConfigKey("I002")
                .setName("Rule Two"));
    dbTester.getSession().commit();

    TestResponse response = tester.newRequest().setMediaType(MimeTypes.PROTOBUF).execute();

    assertThat(response.getMediaType()).isEqualTo(MimeTypes.PROTOBUF);
    Rules.ListResponse listResponse = Rules.ListResponse.parseFrom(response.getInputStream());
    assertThat(listResponse.getRulesCount()).isEqualTo(2);

    assertThat(listResponse.getRules(0).getKey()).isEqualTo("S001");
    assertThat(listResponse.getRules(0).getInternalKey()).isEqualTo("");
    assertThat(listResponse.getRules(0).getName()).isEqualTo("");
    assertThat(listResponse.getRules(1).getKey()).isEqualTo("S002");
    assertThat(listResponse.getRules(1).getInternalKey()).isEqualTo("I002");
    assertThat(listResponse.getRules(1).getName()).isEqualTo("Rule Two");
  }
예제 #2
0
  @Test
  public void search_for_groups_with_one_permission() {
    String result = ws.newRequest().setParam(PARAM_PERMISSION, SCAN_EXECUTION).execute().getInput();

    assertJson(result)
        .isSimilarTo(Resources.getResource(getClass(), "GroupsActionTest/groups.json"));
  }
  @Test
  public void fail_if_not_logged_in() {
    expectedException.expect(UnauthorizedException.class);
    userSession.anonymous();

    ws.newRequest().execute();
  }
  @Test
  public void fail_if_not_admin() {
    expectedException.expect(ForbiddenException.class);
    userSession.login();

    ws.newRequest().execute();
  }
예제 #5
0
  @Test
  public void search_groups_with_project_permissions() {
    dbClient.componentDao().insert(dbSession, newProjectDto("project-uuid").setKey("project-key"));
    ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, "project-uuid");
    GroupDto group = insertGroup(new GroupDto().setName("project-group-name"));
    insertGroupRole(
        new GroupRoleDto()
            .setGroupId(group.getId())
            .setRole(ISSUE_ADMIN)
            .setResourceId(project.getId()));
    userSession.login().addProjectUuidPermissions(UserRole.ADMIN, "project-uuid");

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

    assertThat(result)
        .contains("project-group-name")
        .doesNotContain("group-1")
        .doesNotContain("group-2")
        .doesNotContain("group-3");
  }
예제 #6
0
  @Test
  public void fail_if_not_logged_in() {
    expectedException.expect(UnauthorizedException.class);
    userSession.anonymous();

    ws.newRequest().setParam(PARAM_PERMISSION, "scan").execute();
  }
예제 #7
0
  @Test
  public void fail_if_insufficient_privileges() {
    expectedException.expect(ForbiddenException.class);
    userSession.login("login");

    ws.newRequest().setParam(PARAM_PERMISSION, "scan").execute();
  }
예제 #8
0
  @Test
  public void fail_if_query_length_is_less_than_3_characters() {
    expectedException.expect(BadRequestException.class);
    expectedException.expectMessage("The 'q' parameter must have at least 3 characters");

    call_ws(ws.newRequest().setParam(TEXT_QUERY, "ab"));
  }
  @Test
  public void empty_result() {
    String result = ws.newRequest().execute().getInput();

    assertJson(result)
        .isSimilarTo(getClass().getResource("SearchProjectPermissionsActionTest/empty.json"));
  }
  @Test
  public void enable_trace_logs() {
    userSession.setGlobalPermissions(UserRole.ADMIN);

    actionTester.newRequest().setParam("level", "TRACE").setMethod("POST").execute();
    verify(serverLogging).changeLevel(LoggerLevel.TRACE);
    verify(db).enableSqlLogging(true);
  }
  @Test
  public void enable_debug_logs() {
    userSession.setGlobalPermissions(UserRole.ADMIN);

    actionTester.newRequest().setParam("level", "DEBUG").setMethod("POST").execute();
    verify(serverLogging).changeLevel(LoggerLevel.DEBUG);
    verify(db).enableSqlLogging(false);
  }
예제 #12
0
  @Test
  public void assign_to_me_with_deprecated_param() throws Exception {
    userSession.login("perceval");

    tester.newRequest().setParam("issue", "ABC").setParam("me", "true").execute();

    verify(issueService).assign("ABC", "perceval");
    verify(responseWriter).write(eq("ABC"), any(Request.class), any(Response.class));
  }
예제 #13
0
  @Test
  public void unassign() throws Exception {
    userSession.login("perceval");

    tester.newRequest().setParam("issue", "ABC").execute();

    verify(issueService).assign("ABC", null);
    verify(responseWriter).write(eq("ABC"), any(Request.class), any(Response.class));
  }
  @Test
  public void search_by_query_on_key() {
    insertComponent(newProjectDto().setKey("project-key"));
    insertComponent(newProjectDto().setKey("another-key"));
    commit();

    String result = ws.newRequest().setParam(TEXT_QUERY, "project").execute().getInput();

    assertThat(result).contains("project-key").doesNotContain("another-key");
  }
예제 #15
0
  @Test
  public void search_with_selection() {
    String result =
        ws.newRequest()
            .setParam(PARAM_PERMISSION, GlobalPermissions.SCAN_EXECUTION)
            .setParam(SELECTED, SelectionMode.ALL.value())
            .execute()
            .getInput();

    assertThat(result).containsSequence(DefaultGroups.ANYONE, "group-1", "group-2", "group-3");
  }
예제 #16
0
  @Test
  public void fail_if_project_uuid_and_project_key_are_provided() {
    expectedException.expect(BadRequestException.class);
    dbClient.componentDao().insert(dbSession, newProjectDto("project-uuid").setKey("project-key"));

    ws.newRequest()
        .setParam(PARAM_PERMISSION, SCAN_EXECUTION)
        .setParam(PARAM_PROJECT_ID, "project-uuid")
        .setParam(PARAM_PROJECT_KEY, "project-key")
        .execute();
  }
예제 #17
0
  @Test
  public void search_groups_with_query() {
    String result =
        ws.newRequest()
            .setParam(PARAM_PERMISSION, "scan")
            .setParam(Param.TEXT_QUERY, "group-")
            .execute()
            .getInput();

    assertThat(result).contains("group-1", "group-2").doesNotContain(DefaultGroups.ANYONE);
  }
 private ProjectStatusWsResponse newRequest(String taskId) {
   try {
     return ProjectStatusWsResponse.parseFrom(
         ws.newRequest()
             .setParam("analysisId", taskId)
             .setMediaType(MediaTypes.PROTOBUF)
             .execute()
             .getInputStream());
   } catch (IOException e) {
     throw Throwables.propagate(e);
   }
 }
  @Test
  public void return_json_example() {
    String response =
        ws.newRequest()
            .setMediaType(MediaTypes.JSON)
            .setParam(PARAM_LOGIN, GRACE_HOPPER)
            .setParam(PARAM_NAME, TOKEN_NAME)
            .execute()
            .getInput();

    assertJson(response).isSimilarTo(getClass().getResource("generate-example.json"));
  }
예제 #20
0
  @Test
  public void search_groups_with_pagination() {
    String result =
        ws.newRequest()
            .setParam(PARAM_PERMISSION, "scan")
            .setParam(Param.PAGE_SIZE, "1")
            .setParam(Param.PAGE, "2")
            .execute()
            .getInput();

    assertThat(result).contains("group-2").doesNotContain("group-1").doesNotContain("group-3");
  }
  @Test
  public void remove_user_from_template_by_name_case_insensitive() {
    ws.newRequest()
        .setParam(PARAM_USER_LOGIN, USER_LOGIN)
        .setParam(PARAM_PERMISSION, DEFAULT_PERMISSION)
        .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
        .execute();
    commit();

    assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION))
        .isEmpty();
  }
예제 #22
0
  @Test
  public void search_with_admin_does_not_return_anyone() {
    String result =
        ws.newRequest()
            .setParam(PARAM_PERMISSION, GlobalPermissions.SYSTEM_ADMIN)
            .setParam(SELECTED, SelectionMode.ALL.value())
            .execute()
            .getInput();

    assertThat(result)
        .containsSequence("group-1", "group-2", "group-3")
        .doesNotContain(DefaultGroups.ANYONE);
  }
  @Test
  public void has_projects_ordered_by_name() {
    for (int i = 9; i >= 1; i--) {
      insertComponent(newProjectDto().setName("project-name-" + i));
    }
    commit();

    String result =
        ws.newRequest().setParam(PAGE, "1").setParam(PAGE_SIZE, "3").execute().getInput();

    assertThat(result)
        .contains("project-name-1", "project-name-2", "project-name-3")
        .doesNotContain("project-name-4");
  }
  @Test
  public void search_project_permissions_with_project_permission() {
    userSession.login().addProjectUuidPermissions(UserRole.ADMIN, "project-uuid");
    insertComponent(newProjectDto("project-uuid"));
    commit();

    String result =
        ws.newRequest()
            .setParam(PermissionWsCommons.PARAM_PROJECT_UUID, "project-uuid")
            .execute()
            .getInput();

    assertThat(result).contains("project-uuid");
  }
  private GenerateWsResponse newRequest(String login, String name) {
    InputStream responseStream =
        ws.newRequest()
            .setMediaType(MediaTypes.PROTOBUF)
            .setParam(PARAM_LOGIN, login)
            .setParam(PARAM_NAME, name)
            .execute()
            .getInputStream();

    try {
      return GenerateWsResponse.parseFrom(responseStream);
    } catch (IOException e) {
      throw propagate(e);
    }
  }
예제 #26
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");
  }
  @Test
  public void handle_more_than_1000_projects() {
    for (int i = 1; i <= 1001; i++) {
      insertComponent(newProjectDto("project-uuid-" + i));
    }
    commit();

    String result =
        ws.newRequest()
            .setParam(TEXT_QUERY, "project")
            .setParam(PAGE_SIZE, "1001")
            .execute()
            .getInput();

    assertThat(result).contains("project-uuid-1", "project-uuid-999", "project-uuid-1001");
  }
예제 #28
0
  @Test
  public void search_json_example() {
    ComponentDto jdk7 = insertJdk7();
    ComponentDto cLang = insertClang();
    dbClient
        .componentLinkDao()
        .insert(
            dbSession,
            new ComponentLinkDto()
                .setComponentUuid(jdk7.uuid())
                .setHref("http://www.oracle.com")
                .setType(ComponentLinkDto.TYPE_HOME_PAGE)
                .setName("Home"));
    dbClient
        .componentLinkDao()
        .insert(
            dbSession,
            new ComponentLinkDto()
                .setComponentUuid(jdk7.uuid())
                .setHref("http://download.java.net/openjdk/jdk8/")
                .setType(ComponentLinkDto.TYPE_SOURCES)
                .setName("Sources"));
    long oneTime = DateUtils.parseDateTime("2016-06-10T13:17:53+0000").getTime();
    long anotherTime = DateUtils.parseDateTime("2016-06-11T14:25:53+0000").getTime();
    SnapshotDto jdk7Snapshot =
        dbClient.snapshotDao().insert(dbSession, newAnalysis(jdk7).setCreatedAt(oneTime));
    SnapshotDto cLangSnapshot =
        dbClient.snapshotDao().insert(dbSession, newAnalysis(cLang).setCreatedAt(anotherTime));
    dbClient
        .measureDao()
        .insert(
            dbSession,
            newMeasureDto(alertStatusMetric, jdk7, jdk7Snapshot).setData(Level.ERROR.name()));
    dbClient
        .measureDao()
        .insert(
            dbSession,
            newMeasureDto(alertStatusMetric, cLang, cLangSnapshot).setData(Level.OK.name()));
    insertUserPermission(UserRole.ADMIN, user.getId(), jdk7.getId());
    insertUserPermission(UserRole.ADMIN, user.getId(), cLang.getId());
    db.commit();
    System.setProperty("user.timezone", "UTC");

    String result = ws.newRequest().execute().getInput();

    assertJson(result).isSimilarTo(getClass().getResource("search_my_projects-example.json"));
  }
  private void newRequest(
      @Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
    TestRequest request = ws.newRequest();
    if (userLogin != null) {
      request.setParam(PARAM_USER_LOGIN, userLogin);
    }
    if (templateKey != null) {
      request.setParam(
          org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID,
          templateKey);
    }
    if (permission != null) {
      request.setParam(PARAM_PERMISSION, permission);
    }

    request.execute();
  }
예제 #30
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());
  }