Merge pull request #57 from rbkmoney/ft/JD-768

fix context for owner user
This commit is contained in:
Gregory 2021-12-09 16:57:15 +03:00 committed by GitHub
commit 5cab4a9150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 45 deletions

View File

@ -13,7 +13,7 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Org manager</name>
<name>org-manager</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@ -24,8 +25,6 @@ public class MemberContextEntity implements Serializable {
@JoinColumn(name = "organization_id", referencedColumnName = "id")
private OrganizationEntity organizationEntity;
@OneToOne
@JoinColumn(name = "member_id", referencedColumnName = "id")
private MemberEntity memberEntity;
private String memberId;
}

View File

@ -1,4 +1,11 @@
package com.rbkmoney.orgmanager.exception;
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException() {
}
public ResourceNotFoundException(String message) {
super(message);
}
}

View File

@ -9,6 +9,6 @@ import java.util.Optional;
@Repository
public interface MemberContextRepository extends JpaRepository<MemberContextEntity, String> {
Optional<MemberContextEntity> findByMemberEntityId(String memberId);
Optional<MemberContextEntity> findByMemberId(String memberId);
}

View File

@ -32,5 +32,7 @@ public interface MemberRepository extends JpaRepository<MemberEntity, String> {
" AND m.id = mtmr.member_id ", nativeQuery = true)
List<MemberWithRoleDto> getOrgMemberList(String orgId);
boolean existsById(String id);
}

View File

@ -18,6 +18,7 @@ import org.keycloak.representations.AccessToken;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
@ -263,28 +264,32 @@ public class OrganizationService {
OrganizationEntity organizationEntity = organizationRepository.findById(organizationId)
.orElseThrow(ResourceNotFoundException::new);
Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberEntityId(userId);
memberContextRepository.findByMemberId(userId);
if (memberContextEntityOptional.isPresent()) {
MemberContextEntity memberContextEntity = memberContextEntityOptional.get();
memberContextEntity.setOrganizationEntity(organizationEntity);
memberContextRepository.save(memberContextEntity);
} else {
MemberEntity memberEntity = memberRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("Can't find member. Unknown userId=" + userId));
if (isNotExistUser(userId)) {
throw new ResourceNotFoundException("Can't find member. Unknown userId=" + userId);
}
MemberContextEntity memberContextEntity = new MemberContextEntity();
memberContextEntity.setOrganizationEntity(organizationEntity);
memberContextEntity.setMemberEntity(memberEntity);
memberContextEntity.setMemberId(userId);
memberContextRepository.save(memberContextEntity);
}
}
public MemberContext findMemberContext(String userId) {
MemberContextEntity memberContextEntity = memberContextRepository.findByMemberEntityId(userId)
.orElseThrow(ResourceNotFoundException::new);
private boolean isNotExistUser(String userId) {
return !memberRepository.existsById(userId) &
CollectionUtils.isEmpty(organizationRepository.findAllByOwner(userId));
}
public MemberContext findMemberContext(String userId) {
MemberContextEntity memberContextEntity = memberContextRepository.findByMemberId(userId)
.orElseThrow(ResourceNotFoundException::new);
MemberContext memberContext = new MemberContext();
memberContext.setOrganizationId(memberContextEntity.getOrganizationEntity().getId());
return memberContext;
}

View File

@ -0,0 +1 @@
ALTER TABLE org_manager.member_context DROP CONSTRAINT member_context_to_member_fkey;

View File

@ -1,6 +1,5 @@
package com.rbkmoney.orgmanager.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.rbkmoney.orgmanager.entity.*;
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
@ -311,6 +310,23 @@ public class UserControllerTest extends AbstractControllerTest {
assertNull(organizationSearchResultThird.getContinuationToken());
}
@Test
void switchOrganizationWithNotExistMember() throws Exception {
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
OrganizationSwitchRequest organizationSwitchRequest = new OrganizationSwitchRequest();
organizationSwitchRequest.setOrganizationId(organizationEntity.getId());
String jwtToken = generateRbkAdminJwt();
mockMvc.perform(put("/user/context")
.accept(MediaType.APPLICATION_JSON)
.contentType("application/json")
.header("Authorization", "Bearer " + jwtToken)
.header("X-Request-ID", "testRequestId")
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
)
.andExpect(status().isNotFound());
}
@Test
void switchOrganizationWithNewContextCreation() throws Exception {
String userId = getUserFromToken();
@ -329,10 +345,36 @@ public class UserControllerTest extends AbstractControllerTest {
)
.andExpect(status().isNoContent());
Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberEntityId(userId);
memberContextRepository.findByMemberId(userId);
assertTrue(memberContextEntityOptional.isPresent());
assertEquals(userId, memberContextEntityOptional.get().getMemberEntity().getId());
assertEquals(userId, memberContextEntityOptional.get().getMemberId());
assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
}
@Test
void switchOrganizationWithOwnerUser() throws Exception {
String userId = getUserFromToken();
OrganizationEntity organization = buildOrganization();
organization.setOwner(userId);
OrganizationEntity organizationEntity = organizationRepository.save(organization);
String jwtToken = generateRbkAdminJwt();
OrganizationSwitchRequest organizationSwitchRequest = new OrganizationSwitchRequest();
organizationSwitchRequest.setOrganizationId(organizationEntity.getId());
mockMvc.perform(put("/user/context")
.accept(MediaType.APPLICATION_JSON)
.contentType("application/json")
.header("Authorization", "Bearer " + jwtToken)
.header("X-Request-ID", "testRequestId")
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
)
.andExpect(status().isNoContent());
Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberId(userId);
assertTrue(memberContextEntityOptional.isPresent());
assertEquals(userId, memberContextEntityOptional.get().getMemberId());
assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
}
@ -361,7 +403,7 @@ public class UserControllerTest extends AbstractControllerTest {
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
MemberContextEntity memberContextEntity = memberContextRepository.save(
MemberContextEntity.builder()
.memberEntity(memberEntity)
.memberId(userId)
.organizationEntity(organizationEntity)
.build()
);
@ -379,7 +421,7 @@ public class UserControllerTest extends AbstractControllerTest {
)
.andExpect(status().isNoContent());
Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberEntityId(userId);
memberContextRepository.findByMemberId(userId);
assertTrue(memberContextEntityOptional.isPresent());
assertEquals(newOrganizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
@ -392,21 +434,19 @@ public class UserControllerTest extends AbstractControllerTest {
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
MemberContextEntity memberContextEntity = memberContextRepository.save(
MemberContextEntity.builder()
.memberEntity(memberEntity)
.memberId(userId)
.organizationEntity(organizationEntity)
.build()
);
String jwtToken = generateRbkAdminJwt();
MvcResult mvcResult = mockMvc.perform(get("/user/context")
mockMvc.perform(get("/user/context")
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + jwtToken)
.header("X-Request-ID", "testRequestId")
)
.andExpect(status().isOk())
.andReturn();
System.out.println(mvcResult);
.andExpect(jsonPath("$.organizationId", equalTo(organizationEntity.getId())));
}
}