Commit 5bc0887c authored by Enriko Käsper's avatar Enriko Käsper
Browse files

Merge branch 'TEIS-343' into 'develop'

Resolve TEIS-343

Closes TEIS-343

See merge request teis/officials-service!24
parents 13eec53c ce11c083
theGroup=ee.sm.ti.teis
theVersion=0.1.0-SNAPSHOT
commonsVersion=0.16.12-SNAPSHOT
officeApiGatewayVersion=0.3.35-SNAPSHOT
officeApiGatewayVersion=0.3.36-SNAPSHOT
pluginVersion=0.0.18-SNAPSHOT
mapstructVersion=1.3.0.Final
querydslVersion=4.1.3
......@@ -3,11 +3,15 @@ package ee.sm.ti.teis.officials.group;
import ee.sm.ti.teis.OfficialsAppTestBase;
import ee.sm.ti.teis.errors.CommonErrorCode;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupMemberRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupsRequest;
import ee.sm.ti.teis.officegateway.official.request.UpdateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.response.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.response.CreateOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupMemberResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupsResponse;
import ee.sm.ti.teis.officegateway.official.response.UpdateOfficialsGroupResponse;
......@@ -23,6 +27,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.UUID;
import static java.util.List.of;
......@@ -133,6 +138,50 @@ class OfficialsGroupListenerPrivilegesTest extends OfficialsAppTestBase {
verifyDeleteOfficialsGroupsAccessIsDenied(requestMetaDTO);
}
//ADD OFFICIALS GROUP MEMBER
@Test
void addOfficialGroupMembers_shouldBeAllowed_if_correctPrivileges() {
RequestMetaDTO requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.OFFICIAL_USER, null,
of(RolePrivilegeCode.TI_MANAGE_OFFICIALS_GROUPS.name()));
verifyAddOfficialGroupMembersAccessIsAllowed(requestMetaDTO);
}
@Test
void addOfficialGroupMembers_shouldBeDenied_if_wrongPrivileges() {
RequestMetaDTO requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.OFFICIAL_USER, null,
of(RolePrivilegeCode.TI_MANAGE_REPRESENTATIVES.name()));
verifyAddOfficialGroupMembersAccessIsDenied(requestMetaDTO);
}
@Test
void addOfficialGroupMembers_shouldBeDenied_if_not_officialUser() {
RequestMetaDTO requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.PERSON, null,
of(RolePrivilegeCode.TI_MANAGE_OFFICIALS_GROUPS.name()));
verifyAddOfficialGroupMembersAccessIsDenied(requestMetaDTO);
}
//DELETE OFFICIALS GROUP MEMBER
@Test
void deleteOfficialGroupMember_shouldBeAllowed_if_correctPrivileges() {
RequestMetaDTO requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.OFFICIAL_USER, null,
of(RolePrivilegeCode.TI_MANAGE_OFFICIALS_GROUPS.name()));
verifyDeleteOfficialGroupMemberAccessIsAllowed(requestMetaDTO);
}
@Test
void deleteOfficialGroupMember_shouldBeDenied_if_wrongPrivileges() {
RequestMetaDTO requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.OFFICIAL_USER, null,
of(RolePrivilegeCode.TI_MANAGE_REPRESENTATIVES.name()));
verifyDeleteOfficialGroupMemberAccessIsDenied(requestMetaDTO);
}
@Test
void deleteOfficialGroupMember_shouldBeDenied_if_not_officialUser() {
RequestMetaDTO requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.PERSON, null,
of(RolePrivilegeCode.TI_MANAGE_OFFICIALS_GROUPS.name()));
verifyDeleteOfficialGroupMemberAccessIsDenied(requestMetaDTO);
}
private void verifyDeleteOfficialsGroupsIsAllowed(RequestMetaDTO requestMetaDTO) {
DeleteOfficialsGroupRequest request = new DeleteOfficialsGroupRequest();
request.setPayload(new DeleteOfficialsGroupRequest.Parameters(UUID.randomUUID().toString()), requestMetaDTO);
......@@ -206,5 +255,36 @@ class OfficialsGroupListenerPrivilegesTest extends OfficialsAppTestBase {
assertThat(response.getError().getCode()).isEqualTo(CommonErrorCode.FORBIDDEN.getCode());
}
private void verifyAddOfficialGroupMembersAccessIsAllowed(RequestMetaDTO requestMetaDTO) {
AddOfficialsGroupMembersRequest request = new AddOfficialsGroupMembersRequest();
request.setPayload(new AddOfficialsGroupMembersRequest.Parameters(UUID.randomUUID().toString(), Collections.emptyList()), requestMetaDTO);
AddOfficialsGroupMembersResponse response = groupGwListener.addOfficialsGroupMembers(request);
if (response.getError() != null) {
assertThat(response.getError().getCode()).isNotEqualTo(CommonErrorCode.FORBIDDEN.getCode());
}
}
private void verifyAddOfficialGroupMembersAccessIsDenied(RequestMetaDTO requestMetaDTO) {
AddOfficialsGroupMembersRequest request = new AddOfficialsGroupMembersRequest();
request.setPayload(new AddOfficialsGroupMembersRequest.Parameters(UUID.randomUUID().toString(), Collections.emptyList()), requestMetaDTO);
AddOfficialsGroupMembersResponse response = groupGwListener.addOfficialsGroupMembers(request);
assertThat(response.getError().getCode()).isEqualTo(CommonErrorCode.FORBIDDEN.getCode());
}
private void verifyDeleteOfficialGroupMemberAccessIsAllowed(RequestMetaDTO requestMetaDTO) {
AddOfficialsGroupMembersRequest request = new AddOfficialsGroupMembersRequest();
request.setPayload(new AddOfficialsGroupMembersRequest.Parameters(UUID.randomUUID().toString(), Collections.emptyList()), requestMetaDTO);
AddOfficialsGroupMembersResponse response = groupGwListener.addOfficialsGroupMembers(request);
if (response.getError() != null) {
assertThat(response.getError().getCode()).isNotEqualTo(CommonErrorCode.FORBIDDEN.getCode());
}
}
private void verifyDeleteOfficialGroupMemberAccessIsDenied(RequestMetaDTO requestMetaDTO) {
DeleteOfficialsGroupMemberRequest request = new DeleteOfficialsGroupMemberRequest();
request.setPayload(new DeleteOfficialsGroupMemberRequest.Parameters(UUID.randomUUID().toString(), ""), requestMetaDTO);
DeleteOfficialsGroupMemberResponse response = groupGwListener.deleteOfficialsGroupMember(request);
assertThat(response.getError().getCode()).isEqualTo(CommonErrorCode.FORBIDDEN.getCode());
}
}
package ee.sm.ti.teis.officials.group.listener;
import ee.sm.ti.teis.OfficialsAppTestBase;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupMemberRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupsRequest;
import ee.sm.ti.teis.officegateway.official.request.UpdateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.response.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.response.CreateOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupMemberResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupsResponse;
import ee.sm.ti.teis.officegateway.official.response.UpdateOfficialsGroupResponse;
......@@ -282,6 +284,46 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
void deleteOfficialsGroupMember_test() {
UUID currentGroupId = usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId();
groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(currentGroupId.toString(), TEST_USER1_ID.toString()));
assertThat(usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup()).isNull();
}
@Test
void deleteOfficialsGroupMember_validates_that_user_is_in_group_test() {
UUID currentGroupId = usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId();
assertThat(currentGroupId).isNotEqualTo(TEST_GROUP_ID);
groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(TEST_GROUP_ID.toString(), TEST_USER1_ID.toString()));
assertThat(usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId()).isEqualTo(currentGroupId);
}
@Test
void deleteOfficialsGroupMember_validates_input_test() {
UUID currentGroupId = usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId();
DeleteOfficialsGroupMemberResponse response = groupListener
.deleteOfficialsGroupMember(composeDeleteMemberRequest("", TEST_USER1_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(null, null));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(currentGroupId.toString(), ""));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(currentGroupId.toString(), UUID.randomUUID().toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(UUID.randomUUID().toString(), TEST_USER1_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@NotNull
private OfficialsGroupsRequest composeOfficialsGroupsRequest(String status) {
OfficialsGroupsRequest request = new OfficialsGroupsRequest();
......@@ -325,4 +367,11 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
return request;
}
@NotNull
private DeleteOfficialsGroupMemberRequest composeDeleteMemberRequest(String groupId, String memberId) {
DeleteOfficialsGroupMemberRequest request = new DeleteOfficialsGroupMemberRequest();
request.setPayload(new DeleteOfficialsGroupMemberRequest.Parameters(groupId, memberId), REQUEST_META_DTO);
return request;
}
}
......@@ -13,7 +13,6 @@ import ee.sm.ti.teis.domainrequest.DomainCreateDTO;
import ee.sm.ti.teis.domainrequest.DomainDeleteDTO;
import ee.sm.ti.teis.domainrequest.DomainQueryDTO;
import ee.sm.ti.teis.domainrequest.DomainRequestDTO;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.*;
import org.springframework.context.annotation.Configuration;
......@@ -36,6 +35,7 @@ public class QueueConfig implements QueueList {
public static final String GW_DELETE_OFFICIALS_GROUP_EMTAK_QUEUE = "officials-service.gw.deleteGroupEmtakRequest";
public static final String GW_OFFICES_QUEUE = "officials-service.gw.findOfficesRequest";
public static final String GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE = "officials-service.gw.addGroupMembersRequest";
public static final String GW_DELETE_OFFICIALS_GROUP_MEMBER_QUEUE = "officials-service.gw.deleteGroupMemberRequest";
public static final String MS_OFFICIAL_USER_QUEUE = "officials-service.ms.findUserRequest";
public static final String MS_OFFICIAL_USERS_QUEUE = "officials-service.ms.findUsersRequest";
......@@ -67,6 +67,7 @@ public class QueueConfig implements QueueList {
addGwQueue(queues, "gwDeleteGroupEmtakQueue", DeleteOfficialsGroupEmtakRequest.ROUTING_KEY, GW_DELETE_OFFICIALS_GROUP_EMTAK_QUEUE);
addGwQueue(queues, "gwFindOfficesQueue", OfficesRequest.ROUTING_KEY, GW_OFFICES_QUEUE);
addGwQueue(queues, "gwAddGroupMembersQueue", AddOfficialsGroupMembersRequest.ROUTING_KEY, GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE);
addGwQueue(queues, "gwDeleteGroupMemberQueue", DeleteOfficialsGroupMemberRequest.ROUTING_KEY, GW_DELETE_OFFICIALS_GROUP_MEMBER_QUEUE);
addMsQueue(queues, "msFindUserQueue", new OfficialUserDomainRequest().routingKey(), MS_OFFICIAL_USER_QUEUE);
addMsQueue(queues, "msFindUsersQueue", new OfficialUsersDomainRequest().routingKey(), MS_OFFICIAL_USERS_QUEUE);
......
package ee.sm.ti.teis.officials.group.listener;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupMemberRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupsRequest;
import ee.sm.ti.teis.officegateway.official.request.UpdateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.response.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.response.CreateOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupMemberResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupsResponse;
......@@ -95,4 +97,13 @@ public class OfficialsGroupGwListener {
return response;
}
@RabbitListener(queues = {GW_DELETE_OFFICIALS_GROUP_MEMBER_QUEUE})
@PreAuthorize("@accessController.hasOfficialPrivilege({T(ee.sm.ti.teis.types.enums.RolePrivilegeCode).TI_MANAGE_OFFICIALS_GROUPS})")
public DeleteOfficialsGroupMemberResponse deleteOfficialsGroupMember(DeleteOfficialsGroupMemberRequest request) {
service.deleteMemberFromGroup(request.getPayload().getGroupId(), request.getPayload().getMemberId(), request.getRequestMetaDTO());
DeleteOfficialsGroupMemberResponse response = new DeleteOfficialsGroupMemberResponse();
response.setPayload(new OfficialUserDto(), request.getRequestMetaDTO());
return response;
}
}
......@@ -173,7 +173,7 @@ public class OfficialsGroupService {
@Transactional
public List<OfficialUserDto> addMembersToGroup(String groupId, List<String> memberIds, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(groupId));
final OfficialsGroupEntity group = findGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
validateGroupMembers(memberIds);
List<OfficialUserDto> groupMembers = new ArrayList<>();
for (String memberId : memberIds) {
......@@ -185,6 +185,15 @@ public class OfficialsGroupService {
return groupMembers;
}
@Transactional
public void deleteMemberFromGroup(String groupId, String memberId, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
final OfficialUserEntity officialUser = userService.findUserById(UUID.fromString(StringUtils.defaultString(memberId)));
if (group.getId().equals(officialUser.getOfficialsGroup().getId())) {
userService.updateUserGroupData(officialUser.getId(), null, requestMetaDTO);
}
}
// lets do full validation before performing user group updates, as updates are sending notifications to other micro services
private void validateGroupMembers(List<String> memberIds) {
if (memberIds == null) {
......@@ -197,7 +206,7 @@ public class OfficialsGroupService {
.build());
}
memberIds.forEach(memberId -> {
UUID userId = UUID.fromString(memberId);
UUID userId = UUID.fromString(StringUtils.defaultString(memberId));
userService.findUserById(userId);
});
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment