@Test public void support_only_issues_with_issue_admin_permission() { IssueDto authorizedIssueDto = newIssue().setType(BUG); DefaultIssue authorizedIssue = authorizedIssueDto.toDefaultIssue(); setUserWithBrowseAndAdministerIssuePermission(authorizedIssueDto); DefaultIssue unauthorizedIssue = newIssue().setType(BUG).toDefaultIssue(); assertThat(action.supports(authorizedIssue.setResolution(null))).isTrue(); assertThat(action.supports(unauthorizedIssue.setResolution(null))).isFalse(); }
@Test public void support_only_unresolved_issues() { IssueDto issueDto = newIssue().setType(BUG); DefaultIssue issue = issueDto.toDefaultIssue(); setUserWithBrowseAndAdministerIssuePermission(issueDto); assertThat(action.supports(issue.setResolution(null))).isTrue(); assertThat(action.supports(issue.setResolution(Issue.RESOLUTION_FIXED))).isFalse(); }
@Test public void insert_entry_in_changelog_when_setting_type() { IssueDto issueDto = issueDbTester.insertIssue(newIssue().setType(CODE_SMELL)); setUserWithBrowseAndAdministerIssuePermission(issueDto); call(issueDto.getKey(), BUG.name()); List<FieldDiffs> fieldDiffs = dbClient.issueChangeDao().selectChangelogByIssue(dbTester.getSession(), issueDto.getKey()); assertThat(fieldDiffs).hasSize(1); assertThat(fieldDiffs.get(0).diffs()).hasSize(1); assertThat(fieldDiffs.get(0).diffs().get("type").newValue()).isEqualTo(BUG.name()); assertThat(fieldDiffs.get(0).diffs().get("type").oldValue()).isEqualTo(CODE_SMELL.name()); }
@Test public void set_type() { IssueDto issueDto = newIssue().setType(BUG); DefaultIssue issue = issueDto.toDefaultIssue(); setUserWithBrowseAndAdministerIssuePermission(issueDto); action.execute(ImmutableMap.of("type", VULNERABILITY.name()), new BulkChangeAction.ActionContext(issue, IssueChangeContext.createUser(NOW, userSession.getUuid()), null)); assertThat(issue.type()).isEqualTo(VULNERABILITY); assertThat(issue.isChanged()).isTrue(); assertThat(issue.updateDate()).isEqualTo(NOW); assertThat(issue.mustSendNotifications()).isFalse(); Map<String, FieldDiffs.Diff> change = issue.currentChange().diffs(); assertThat(change.get("type").newValue()).isEqualTo(VULNERABILITY); assertThat(change.get("type").oldValue()).isEqualTo(BUG); }
@Test public void security_hotspot_type_excluded_by_default() { ComponentDto project = db.components().insertPublicProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleDefinitionDto rule = db.rules().insert(); db.issues().insert(rule, project, file, i -> i.setType(RuleType.BUG)); db.issues().insert(rule, project, file, i -> i.setType(RuleType.VULNERABILITY)); db.issues().insert(rule, project, file, i -> i.setType(RuleType.CODE_SMELL)); db.issues().insert(rule, project, file, i -> i.setType(RuleType.SECURITY_HOTSPOT)); indexPermissions(); indexIssues(); SearchWsResponse result = ws.newRequest().executeProtobuf(SearchWsResponse.class); assertThat(result.getIssuesList()) .extracting(Issue::getType) .containsExactlyInAnyOrder(Common.RuleType.BUG, Common.RuleType.VULNERABILITY, Common.RuleType.CODE_SMELL); }
@SafeVarargs private final IssueDto insertNewClosedIssue(ComponentDto component, RuleDefinitionDto rule, long issueCloseTime, Consumer<IssueDto>... consumers) { IssueDto res = new IssueDto() .setKee(UuidFactoryFast.getInstance().create()) .setRuleId(rule.getId()) .setType(rule.getType()) .setComponentUuid(component.uuid()) .setProjectUuid(component.projectUuid()) .setStatus(Issue.STATUS_CLOSED) .setIssueCloseTime(issueCloseTime); Arrays.asList(consumers).forEach(c -> c.accept(res)); underTest.insert(res); dbSession.commit(); return res; }
private IssueDto newIssue(String assignee) { IssueDto issue = db.issues().insertIssue( issueDto -> issueDto .setAssigneeUuid(assignee) .setCreatedAt(PAST).setIssueCreationTime(PAST) .setUpdatedAt(PAST).setIssueUpdateTime(PAST) .setType(RuleType.CODE_SMELL)); return issue; }
private DefaultIssue prepareIssue(long issueCreatedAt, UserDto user, ComponentDto project, ComponentDto file, RuleDefinitionDto ruleDefinitionDto, RuleType type) { DefaultIssue issue = newIssue(ruleDefinitionDto, project, file).setType(type).toDefaultIssue() .setNew(false).setChanged(true).setSendNotifications(true).setCreationDate(new Date(issueCreatedAt)).setAssigneeUuid(user.getUuid()); ruleRepository.add(ruleDefinitionDto.getKey()).setName(ruleDefinitionDto.getName()); issueCache.newAppender().append(issue).close(); when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true); return issue; }
@Test public void fail_if_bad_type_value() { IssueDto issueDto = issueDbTester.insertIssue(newIssue().setType(CODE_SMELL)); setUserWithBrowseAndAdministerIssuePermission(issueDto); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Value of parameter 'type' (unknown) must be one of: [CODE_SMELL, BUG, VULNERABILITY, SECURITY_HOTSPOT]"); call(issueDto.getKey(), "unknown"); }
@Test public void verify_notification_when_issue_is_linked_on_removed_rule() { RuleDto rule = db.rules().insertRule(r -> r.setStatus(RuleStatus.REMOVED)); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, null); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); assertThat(notificationArgumentCaptor.getValue().getFieldValue("ruleName")).isNull(); }
@Test public void return_issues_of_file() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto file = db.components().insertComponent(newFileDto(module, null)); IssueDto issueOnFile = db.issues().insert(rule, project, file, i -> i.setType(randomRuleTypeExceptHotspot())); IssueDto issueOnModule = db.issues().insert(rule, project, module, i -> i.setType(randomRuleTypeExceptHotspot())); IssueDto issueOnProject = db.issues().insert(rule, project, project, i -> i.setType(randomRuleTypeExceptHotspot())); addPermissionTo(project); try (CloseableIterator<ServerIssue> result = callStream(file.getKey(), null)) { assertThat(result) .extracting(ServerIssue::getKey, ServerIssue::getModuleKey) .containsExactlyInAnyOrder( tuple(issueOnFile.getKey(), module.getKey())); } }
@Test public void hotspots_are_ignored_and_no_notification_is_sent() { UserDto user = db.users().insertUser(); userSession.logIn(user); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); addUserProjectPermissions(user, project, USER, ISSUE_ADMIN); RuleDefinitionDto rule = db.rules().insert(); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setType(SECURITY_HOTSPOT) .setStatus(STATUS_OPEN).setResolution(null)); BulkChangeWsResponse response = call(builder() .setIssues(singletonList(issue.getKey())) .setDoTransition("dismiss") .setSendNotifications(true) .build()); checkResponse(response, 1, 0, 1, 0); verify(notificationManager, never()).scheduleForSending(any()); }
@Test public void verify_no_notification_on_hotspot() { UserDto assignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(RuleType.SECURITY_HOTSPOT)) .setSeverity(MAJOR) .setAssigneeUuid(assignee.getUuid()) .toDefaultIssue(); UserDto changeAuthor = db.users().insertUser(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid()); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, "increase severity"); verify(notificationManager, never()).scheduleForSending(any()); }
@Test public void fail_when_trying_to_assign_hotspot() { IssueDto issueDto = db.issues().insertIssue(i -> i.setType(RuleType.SECURITY_HOTSPOT)); setUserWithBrowsePermission(issueDto); UserDto arthur = insertUser("arthur"); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("It is not allowed to assign a security hotspot"); ws.newRequest() .setParam("issue", issueDto.getKey()) .setParam("assignee", "arthur") .execute(); }
@Test public void return_issues_of_module() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto file = db.components().insertComponent(newFileDto(module, null)); IssueDto issueOnFile = db.issues().insert(rule, project, file, i -> i.setKee("ON_FILE").setType(randomRuleTypeExceptHotspot())); IssueDto issueOnModule = db.issues().insert(rule, project, module, i -> i.setKee("ON_MODULE").setType(randomRuleTypeExceptHotspot())); IssueDto issueOnProject = db.issues().insert(rule, project, project, i -> i.setKee("ON_PROJECT").setType(randomRuleTypeExceptHotspot())); addPermissionTo(project); try (CloseableIterator<ServerIssue> result = callStream(module.getKey(), null)) { assertThat(result) .extracting(ServerIssue::getKey, ServerIssue::getModuleKey) .containsExactlyInAnyOrder( tuple(issueOnFile.getKey(), module.getKey()), tuple(issueOnModule.getKey(), module.getKey())); } }
@Test public void return_issues_by_project_and_branch() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); addPermissionTo(project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); ComponentDto file = db.components().insertComponent(newFileDto(branch)); IssueDto issueOnFile = db.issues().insert(rule, branch, file, i -> i.setType(randomRuleTypeExceptHotspot())); IssueDto issueOnBranch = db.issues().insert(rule, branch, branch, i -> i.setType(randomRuleTypeExceptHotspot())); assertResult(project.getKey(), "my_branch", tuple(issueOnFile.getKey(), branch.getKey()), tuple(issueOnBranch.getKey(), branch.getKey())); }
@Test public void return_issues_by_module_and_branch() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); addPermissionTo(project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); ComponentDto module = db.components().insertComponent(newModuleDto(branch)); ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); ComponentDto file = db.components().insertComponent(newFileDto(subModule)); IssueDto issueOnFile = db.issues().insert(rule, branch, file, i -> i.setType(randomRuleTypeExceptHotspot())); IssueDto issueOnSubModule = db.issues().insert(rule, branch, subModule, i -> i.setType(randomRuleTypeExceptHotspot())); IssueDto issueOnModule = db.issues().insert(rule, branch, module, i -> i.setType(randomRuleTypeExceptHotspot())); assertResult(module.getKey(), "my_branch", tuple(issueOnFile.getKey(), subModule.getKey()), tuple(issueOnSubModule.getKey(), subModule.getKey()), tuple(issueOnModule.getKey(), module.getKey()) ); }
@Test public void fail_when_only_comment_action() { UserDto user = db.users().insertUser(); userSession.logIn(user); ComponentDto project = db.components().insertPrivateProject(); addUserProjectPermissions(user, project, USER); RuleDefinitionDto rule = db.rules().insert(); IssueDto issue = db.issues().insert(rule, project, project, i -> i.setType(BUG) .setStatus(STATUS_OPEN).setResolution(null)); expectedException.expectMessage("At least one action must be provided"); expectedException.expect(IllegalArgumentException.class); call(builder() .setIssues(singletonList(issue.getKey())) .setComment("type was badly defined") .build()); }
@Test public void issues_on_disabled_modules_are_returned() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project).setEnabled(false)); ComponentDto file = db.components().insertComponent(newFileDto(module, null).setEnabled(false)); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setType(randomRuleTypeExceptHotspot())); addPermissionTo(project); try (CloseableIterator<ServerIssue> result = callStream(project.getKey(), null)) { // Module key of removed file should be returned assertThat(result) .extracting(ServerIssue::getKey, ServerIssue::getModuleKey) .containsExactly(tuple(issue.getKey(), module.getKey())); } }
@Test public void fail_when_assignee_is_not_member_of_organization_of_project_issue() { OrganizationDto org = db.organizations().insert(organizationDto -> organizationDto.setKey("Organization key")); IssueDto issueDto = db.issues().insertIssue(org, i -> i.setType(RuleType.CODE_SMELL)); setUserWithBrowsePermission(issueDto); OrganizationDto otherOrganization = db.organizations().insert(); UserDto assignee = db.users().insertUser("arthur"); db.organizations().addMember(otherOrganization, assignee); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("User 'arthur' is not member of organization 'Organization key'"); ws.newRequest() .setParam("issue", issueDto.getKey()) .setParam("assignee", "arthur") .execute(); }