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);
}
}
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);
}
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();
}
Aggregations