add owner filer on listOrgMembership

This commit is contained in:
ggmaleva 2021-03-26 15:31:19 +03:00
parent 284d8b5f8c
commit 79b74e2471
2 changed files with 52 additions and 27 deletions

View File

@ -10,13 +10,28 @@ import java.util.List;
@Repository
public interface OrganizationRepository extends JpaRepository<OrganizationEntity, String> {
@Query(value = "SELECT * FROM org_manager.organization AS o WHERE o.id IN " +
"(SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 ) ORDER BY o.id DESC",
@Query(value =
" SELECT * FROM org_manager.organization AS o " +
" WHERE o.id IN " +
" ( " +
" SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 " +
" UNION " +
" SELECT id FROM org_manager.organization WHERE owner = ?1 " +
" ) " +
" ORDER BY o.id DESC",
nativeQuery = true)
List<OrganizationEntity> findAllByMember(String userId);
@Query(value = "SELECT * FROM org_manager.organization AS o WHERE o.id IN " +
"(SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 ) AND o.id < ?2 ORDER BY o.id DESC",
@Query(value =
" SELECT * FROM org_manager.organization AS o " +
" WHERE o.id IN " +
" ( " +
" SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 " +
" UNION " +
" SELECT id FROM org_manager.organization WHERE owner = ?1 " +
" ) " +
" AND o.id < ?2 " +
" ORDER BY o.id DESC",
nativeQuery = true)
List<OrganizationEntity> findAllByMember(String userId, String continuationId);

View File

@ -11,13 +11,8 @@ import com.rbkmoney.orgmanager.repository.InvitationRepository;
import com.rbkmoney.orgmanager.repository.InvitationRepositoryTest;
import com.rbkmoney.orgmanager.repository.MemberRepository;
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
import com.rbkmoney.orgmanager.service.OrganizationService;
import com.rbkmoney.orgmanager.service.ResourceAccessService;
import com.rbkmoney.swag.organizations.model.InvitationStatusName;
import com.rbkmoney.swag.organizations.model.OrganizationJoinRequest;
import com.rbkmoney.swag.organizations.model.OrganizationMembership;
import com.rbkmoney.swag.organizations.model.OrganizationSearchResult;
import com.rbkmoney.swag.organizations.model.RoleId;
import com.rbkmoney.swag.organizations.model.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -35,17 +30,13 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;
import static com.rbkmoney.orgmanager.TestObjectFactory.buildInvitation;
import static com.rbkmoney.orgmanager.TestObjectFactory.buildOrganization;
import static com.rbkmoney.orgmanager.TestObjectFactory.randomString;
import static com.rbkmoney.orgmanager.TestObjectFactory.testMemberEntity;
import static com.rbkmoney.orgmanager.TestObjectFactory.*;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.doThrow;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -77,9 +68,6 @@ public class UserControllerTest extends AbstractControllerTest {
@Autowired
private KeycloakOpenIdStub keycloakOpenIdStub;
@SpyBean
private OrganizationService organizationService;
@SpyBean
private ResourceAccessService resourceAccessService;
@ -187,18 +175,40 @@ public class UserControllerTest extends AbstractControllerTest {
.andExpect(jsonPath("$.member.id", equalTo(userId)));
}
@Test
public void listOrgMembershipWithoutLimitTest() throws Exception {
String jwtToken = generateRBKadminJwt();
String userId = getUserFromToken();
MemberEntity targetMember = memberRepository.save(testMemberEntity(userId));
Set<OrganizationEntity> targetEntities = buildOrganization(targetMember, 7);
organizationRepository.saveAll(targetEntities);
MvcResult mvcResultFirst = mockMvc.perform(get("/user/membership")
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + jwtToken)
.header("X-Request-ID", "testRequestId")
).andExpect(status().isOk()).andReturn();
OrganizationSearchResult organizationSearchResult = objectMapper.readValue(
mvcResultFirst.getResponse().getContentAsString(), OrganizationSearchResult.class);
Assert.assertEquals(7, organizationSearchResult.getResult().size());
}
@Test
public void listOrgMembershipTest() throws Exception {
String jwtToken = generateRBKadminJwt();
String userId = getUserFromToken();
MemberEntity targetMember = memberRepository.save(testMemberEntity(userId));
Set<OrganizationEntity> targetEntities = buildOrganization(targetMember, 8);
Set<OrganizationEntity> targetEntities = buildOrganization(targetMember, 9);
OrganizationEntity anotherOrganization = buildOrganization();
targetEntities.add(anotherOrganization);
OrganizationEntity organizationWithOwner = buildOrganization();
organizationWithOwner.setOwner(userId);
targetEntities.addAll(List.of(anotherOrganization, organizationWithOwner));
organizationRepository.saveAll(targetEntities);
String limit = "4";
MvcResult mvcResultFirst = mockMvc.perform(get("/user/membership")
.queryParam("limit", "3")
.queryParam("limit", limit)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + jwtToken)
.header("X-Request-ID", "testRequestId")
@ -206,10 +216,10 @@ public class UserControllerTest extends AbstractControllerTest {
OrganizationSearchResult organizationSearchResultFirst = objectMapper.readValue(
mvcResultFirst.getResponse().getContentAsString(), OrganizationSearchResult.class);
Assert.assertEquals(3, organizationSearchResultFirst.getResult().size());
Assert.assertEquals(4, organizationSearchResultFirst.getResult().size());
MvcResult mvcResultSecond = mockMvc.perform(get("/user/membership")
.queryParam("limit", "3")
.queryParam("limit", limit)
.queryParam("continuationToken", organizationSearchResultFirst.getContinuationToken())
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + jwtToken)
@ -218,10 +228,10 @@ public class UserControllerTest extends AbstractControllerTest {
OrganizationSearchResult organizationSearchResultSecond = objectMapper.readValue(
mvcResultSecond.getResponse().getContentAsString(), OrganizationSearchResult.class);
Assert.assertEquals(3, organizationSearchResultSecond.getResult().size());
Assert.assertEquals(4, organizationSearchResultSecond.getResult().size());
MvcResult mvcResultThird = mockMvc.perform(get("/user/membership")
.queryParam("limit", "3")
.queryParam("limit", limit)
.queryParam("continuationToken", organizationSearchResultSecond.getContinuationToken())
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + jwtToken)