Search in sources :

Example 1 with ProjectBadgeTokenDto

use of org.sonar.db.project.ProjectBadgeTokenDto in project sonarqube by SonarSource.

the class TokenAction method doHandle.

private TokenWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        String projectKey = request.mandatoryParam(PROJECT_KEY_PARAM);
        ProjectDto projectDto = dbClient.projectDao().selectProjectByKey(dbSession, projectKey).orElseThrow(() -> new IllegalArgumentException("project not found"));
        userSession.checkProjectPermission(UserRole.USER, projectDto);
        ProjectBadgeTokenDto projectBadgeTokenDto = dbClient.projectBadgeTokenDao().selectTokenByProject(dbSession, projectDto);
        if (projectBadgeTokenDto == null) {
            projectBadgeTokenDto = dbClient.projectBadgeTokenDao().insert(dbSession, tokenGenerator.generate(), projectDto, userSession.getUuid(), userSession.getLogin());
            dbSession.commit();
        }
        return buildResponse(projectBadgeTokenDto);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) ProjectBadgeTokenDto(org.sonar.db.project.ProjectBadgeTokenDto)

Example 2 with ProjectBadgeTokenDto

use of org.sonar.db.project.ProjectBadgeTokenDto in project sonarqube by SonarSource.

the class TokenRenewActionTest method should_replace_existing_token_when__token_already_present_and_update_update_at.

@Test
public void should_replace_existing_token_when__token_already_present_and_update_update_at() {
    ProjectDto project = db.components().insertPrivateProjectDto();
    userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
    when(tokenGenerator.generate()).thenReturn("generated_token");
    // inserting first token with updated at 1000
    ws.newRequest().setParam("project", project.getKey()).execute();
    when(system2.now()).thenReturn(2000L);
    // replacing first token with updated at 2000
    ws.newRequest().setParam("project", project.getKey()).execute();
    ProjectBadgeTokenDto projectBadgeTokenDto = db.getDbClient().projectBadgeTokenDao().selectTokenByProject(db.getSession(), project);
    assertThat(projectBadgeTokenDto).isNotNull();
    assertThat(projectBadgeTokenDto.getToken()).isEqualTo("generated_token");
    assertThat(projectBadgeTokenDto.getUpdatedAt()).isEqualTo(2000L);
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) ProjectBadgeTokenDto(org.sonar.db.project.ProjectBadgeTokenDto) Test(org.junit.Test)

Example 3 with ProjectBadgeTokenDto

use of org.sonar.db.project.ProjectBadgeTokenDto in project sonarqube by SonarSource.

the class TokenRenewActionTest method should_add_token_when_no_token_yet_and_return_204.

@Test
public void should_add_token_when_no_token_yet_and_return_204() {
    ProjectDto project = db.components().insertPrivateProjectDto();
    userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
    when(tokenGenerator.generate()).thenReturn("generated_token");
    TestResponse response = ws.newRequest().setParam("project", project.getKey()).execute();
    ProjectBadgeTokenDto projectBadgeTokenDto = db.getDbClient().projectBadgeTokenDao().selectTokenByProject(db.getSession(), project);
    assertThat(projectBadgeTokenDto).isNotNull();
    assertThat(projectBadgeTokenDto.getToken()).isEqualTo("generated_token");
    response.assertNoContent();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) TestResponse(org.sonar.server.ws.TestResponse) ProjectBadgeTokenDto(org.sonar.db.project.ProjectBadgeTokenDto) Test(org.junit.Test)

Aggregations

ProjectBadgeTokenDto (org.sonar.db.project.ProjectBadgeTokenDto)3 ProjectDto (org.sonar.db.project.ProjectDto)3 Test (org.junit.Test)2 DbSession (org.sonar.db.DbSession)1 TestResponse (org.sonar.server.ws.TestResponse)1