Skip to content

Commit

Permalink
SHIBUI-2394
Browse files Browse the repository at this point in the history
Added ability to unapprove
  • Loading branch information
chasegawa committed Oct 4, 2022
1 parent b9cfc4b commit 4672fc9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class ApprovalController {
@PatchMapping(path = "/entityDescriptor/{resourceId}/{mode}")
@Transactional
public ResponseEntity<?> approveEntityDescriptor(@PathVariable String resourceId, @PathVariable String mode) throws PersistentEntityNotFound, ForbiddenException {
// boolean status = "approve".equalsIgnoreCase(mode); // can we un-approve?
EntityDescriptorRepresentation edr = entityDescriptorService.approveEntityDescriptor(resourceId);
boolean status = "approve".equalsIgnoreCase(mode);
EntityDescriptorRepresentation edr = entityDescriptorService.changeApproveStatusOfEntityDescriptor(resourceId, status);
return ResponseEntity.ok(edr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,10 @@ public void addApproval(Group group) {
public int approvedCount() {
return approved.size();
}

public void removeLastApproval() {
if (!approved.isEmpty()) {
approved.remove(approved.size() - 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ EntityDescriptorRepresentation updateEntityDescriptorEnabledStatus(String resour

EntityDescriptorRepresentation updateGroupForEntityDescriptor(String resourceId, String groupId);

EntityDescriptorRepresentation approveEntityDescriptor(String resourceId) throws PersistentEntityNotFound, ForbiddenException;
EntityDescriptorRepresentation changeApproveStatusOfEntityDescriptor(String resourceId, boolean status) throws PersistentEntityNotFound, ForbiddenException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,25 @@ public class JPAEntityDescriptorServiceImpl implements EntityDescriptorService {
private UserService userService;

@Override
public EntityDescriptorRepresentation approveEntityDescriptor(String resourceId) throws PersistentEntityNotFound, ForbiddenException {
public EntityDescriptorRepresentation changeApproveStatusOfEntityDescriptor(String resourceId, boolean status) throws PersistentEntityNotFound, ForbiddenException {
EntityDescriptor ed = entityDescriptorRepository.findByResourceId(resourceId);
if (ed == null) {
throw new PersistentEntityNotFound("Entity with resourceid[" + resourceId + "] was not found for approval");
}
int approvedCount = ed.approvedCount();
List<Approvers> approversList = groupService.find(ed.getIdOfOwner()).getApproversList();
if (!approversList.isEmpty() && approversList.size() > approvedCount) {
Approvers approvers = approversList.get(approvedCount); // yea for index zero - use the count to get the next approvers
if (!userService.currentUserCanApprove(approvers.getApproverGroups())) {
throw new ForbiddenException("You do not have the permissions necessary to approve this entity descriptor.");
if (status) { // approve
int approvedCount = ed.approvedCount();
List<Approvers> approversList = groupService.find(ed.getIdOfOwner()).getApproversList();
if (!approversList.isEmpty() && approversList.size() > approvedCount) {
Approvers approvers = approversList.get(
approvedCount); // yea for index zero - use the count to get the next approvers
if (!userService.currentUserCanApprove(approvers.getApproverGroups())) {
throw new ForbiddenException("You do not have the permissions necessary to approve this entity descriptor.");
}
ed.addApproval(userService.getCurrentUserGroup());
ed = entityDescriptorRepository.save(ed);
}
ed.addApproval(userService.getCurrentUserGroup());
} else { // un-approve
ed.removeLastApproval();
ed = entityDescriptorRepository.save(ed);
}
return createRepresentationFromDescriptor(ed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,25 @@ class ApproveControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath("\$.approved").value(true))
}

@WithMockUser(value = "BUser", roles = ["USER"])
def 'Approver can approve and un-approve an entity descriptor'() {
when:
def result = mockMvc.perform(patch("/api/approve/entityDescriptor/" + defaultEntityDescriptorResourceId + "/approve"))

then:
result.andExpect(status().isOk())
.andExpect(jsonPath("\$.id").value(defaultEntityDescriptorResourceId))
.andExpect(jsonPath("\$.serviceEnabled").value(false))
.andExpect(jsonPath("\$.approved").value(true))

when:
def result2 = mockMvc.perform(patch("/api/approve/entityDescriptor/" + defaultEntityDescriptorResourceId + "/unapprove"))

then:
result2.andExpect(status().isOk())
.andExpect(jsonPath("\$.id").value(defaultEntityDescriptorResourceId))
.andExpect(jsonPath("\$.serviceEnabled").value(false))
.andExpect(jsonPath("\$.approved").value(false))

}
}

0 comments on commit 4672fc9

Please sign in to comment.