mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 00:15:23 +00:00
don't allow remove last role
This commit is contained in:
parent
6b1585138c
commit
42ed13eb24
@ -0,0 +1,5 @@
|
||||
package com.rbkmoney.orgmanager.exception;
|
||||
|
||||
public class LastRoleException extends RuntimeException {
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.rbkmoney.orgmanager.exception;
|
||||
|
||||
import com.rbkmoney.swag.organizations.model.InlineResponse422;
|
||||
import com.rbkmoney.swag.organizations.model.InlineResponse4222;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -59,4 +60,12 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(badResponse);
|
||||
}
|
||||
|
||||
@ExceptionHandler(LastRoleException.class)
|
||||
public ResponseEntity<?> handleLastRoleException(LastRoleException ex) {
|
||||
InlineResponse4222 badResponse = new InlineResponse4222()
|
||||
.code(InlineResponse4222.CodeEnum.ONLYROLELEFT)
|
||||
.message("Member have only one role");
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(badResponse);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
|
||||
import com.rbkmoney.orgmanager.exception.LastRoleException;
|
||||
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
@ -87,10 +88,14 @@ public class OrganizationService {
|
||||
public Member getOrgMember(String userId, String orgId) {
|
||||
OrganizationEntity organization = findById(orgId);
|
||||
MemberEntity memberEntity = getMember(userId, organization);
|
||||
List<MemberRoleEntity> rolesInOrg = memberEntity.getRoles().stream()
|
||||
List<MemberRoleEntity> rolesInOrg = getMemberRolesInOrg(orgId, memberEntity);
|
||||
return memberConverter.toDomain(memberEntity, rolesInOrg);
|
||||
}
|
||||
|
||||
private List<MemberRoleEntity> getMemberRolesInOrg(String orgId, MemberEntity memberEntity) {
|
||||
return memberEntity.getRoles().stream()
|
||||
.filter(memberRole -> isActiveOrgMemberRole(orgId, memberRole))
|
||||
.collect(toList());
|
||||
return memberConverter.toDomain(memberEntity, rolesInOrg);
|
||||
}
|
||||
|
||||
private boolean isActiveOrgMemberRole(String orgId, MemberRoleEntity memberRole) {
|
||||
@ -135,6 +140,9 @@ public class OrganizationService {
|
||||
public void removeMemberRole(String orgId, String userId, String memberRoleId) {
|
||||
OrganizationEntity organization = findById(orgId);
|
||||
MemberEntity member = getMember(userId, organization);
|
||||
if (getMemberRolesInOrg(orgId, member).size() == 1) {
|
||||
throw new LastRoleException();
|
||||
}
|
||||
MemberRoleEntity roleToRemove = memberRoleService.findEntityById(memberRoleId);
|
||||
roleToRemove.setActive(Boolean.FALSE);
|
||||
member.getRoles().remove(roleToRemove);
|
||||
|
@ -15,6 +15,8 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
@ -128,7 +130,7 @@ public class OrgsControllerTest extends AbstractControllerTest {
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void removeMemberRoleTest() throws Exception {
|
||||
void removeMemberRoleWithOnlyOneRole() throws Exception {
|
||||
MemberEntity memberEntity = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
|
||||
OrganizationEntity organization = TestObjectFactory.buildOrganization(memberEntity);
|
||||
MemberRoleEntity memberRoleEntity = TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId());
|
||||
@ -141,6 +143,29 @@ public class OrgsControllerTest extends AbstractControllerTest {
|
||||
mockMvc.perform(delete(
|
||||
String.format("/orgs/%s/members/%s/roles/%s", savedOrganization.getId(), savedMember.getId(),
|
||||
savedMemberRole.getId())
|
||||
)
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "Bearer " + generateRbkAdminJwt())
|
||||
.header("X-Request-ID", "testRequestId"))
|
||||
.andExpect(status().isUnprocessableEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void removeMemberRole() throws Exception {
|
||||
MemberEntity memberEntity = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
|
||||
OrganizationEntity organization = TestObjectFactory.buildOrganization(memberEntity);
|
||||
MemberRoleEntity memberRoleEntity = TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId());
|
||||
MemberRoleEntity roleToRemove = TestObjectFactory.buildMemberRole(RoleId.MANAGER, organization.getId());
|
||||
List<MemberRoleEntity> roles = memberRoleRepository.saveAll(List.of(
|
||||
memberRoleEntity, roleToRemove));
|
||||
memberEntity.setRoles(new HashSet<>(roles));
|
||||
MemberEntity savedMember = memberRepository.save(memberEntity);
|
||||
OrganizationEntity savedOrganization = organizationRepository.save(organization);
|
||||
|
||||
mockMvc.perform(delete(
|
||||
String.format("/orgs/%s/members/%s/roles/%s", savedOrganization.getId(), savedMember.getId(),
|
||||
roleToRemove.getId())
|
||||
)
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "Bearer " + generateRbkAdminJwt())
|
||||
@ -148,7 +173,7 @@ public class OrgsControllerTest extends AbstractControllerTest {
|
||||
.andExpect(status().isNoContent());
|
||||
|
||||
|
||||
assertThat(memberRepository.findById(savedMember.getId()).get().getRoles(), not(hasItem(savedMemberRole)));
|
||||
assertThat(memberRepository.findById(savedMember.getId()).get().getRoles(), not(hasItem(roleToRemove)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -8,6 +8,7 @@ import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
|
||||
import com.rbkmoney.orgmanager.exception.InviteAlreadyAcceptedException;
|
||||
import com.rbkmoney.orgmanager.exception.LastRoleException;
|
||||
import com.rbkmoney.orgmanager.repository.AbstractRepositoryTest;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -260,20 +261,37 @@ public class OrganizationServiceIntegrationTest extends AbstractRepositoryTest {
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void removeMemberRoleTest() {
|
||||
void shouldThrowLasRoleException() {
|
||||
MemberEntity member = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
|
||||
OrganizationEntity organization = TestObjectFactory.buildOrganization(member);
|
||||
MemberRoleEntity role =
|
||||
memberRoleRepository.save(TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId()));
|
||||
member.setRoles(Set.of(role));
|
||||
MemberEntity savedMember = memberRepository.save(member);
|
||||
OrganizationEntity savedOrganization = organizationRepository.save(organization);
|
||||
|
||||
assertThrows(LastRoleException.class,
|
||||
() -> organizationService.removeMemberRole(organization.getId(), member.getId(), role.getId()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void shouldRemoveMemberRole() {
|
||||
MemberEntity memberEntity = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
|
||||
OrganizationEntity organization = TestObjectFactory.buildOrganization(memberEntity);
|
||||
MemberRoleEntity savedMemberRole =
|
||||
MemberRoleEntity role =
|
||||
memberRoleRepository.save(TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId()));
|
||||
memberEntity.setRoles(Set.of(savedMemberRole));
|
||||
MemberRoleEntity roleToRemove =
|
||||
memberRoleRepository.save(TestObjectFactory.buildMemberRole(RoleId.MANAGER, organization.getId()));
|
||||
memberEntity.setRoles(Set.of(role, roleToRemove));
|
||||
MemberEntity savedMember = memberRepository.save(memberEntity);
|
||||
OrganizationEntity savedOrganization = organizationRepository.save(organization);
|
||||
|
||||
organizationService.removeMemberRole(savedOrganization.getId(), savedMember.getId(), savedMemberRole.getId());
|
||||
organizationService.removeMemberRole(savedOrganization.getId(), savedMember.getId(), roleToRemove.getId());
|
||||
|
||||
|
||||
assertThat(memberRepository.findById(savedMember.getId()).get().getRoles(), not(hasItem(savedMemberRole)));
|
||||
assertFalse(memberRoleRepository.findById(savedMemberRole.getId()).get().isActive());
|
||||
assertThat(memberRepository.findById(savedMember.getId()).get().getRoles(), not(hasItem(roleToRemove)));
|
||||
assertFalse(memberRoleRepository.findById(roleToRemove.getId()).get().isActive());
|
||||
}
|
||||
|
||||
|
||||
|
@ -198,7 +198,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfUserMotMemberOfOrganization() {
|
||||
void shouldThrowExceptionIfUserNotMemberOfOrganization() {
|
||||
// Given
|
||||
String orgId = TestObjectFactory.randomString();
|
||||
OrganizationEntity organizationEntity = new OrganizationEntity();
|
||||
|
Loading…
Reference in New Issue
Block a user