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> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Org manager</name> <name>org-manager</name>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

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

View File

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

View File

@ -9,6 +9,6 @@ import java.util.Optional;
@Repository @Repository
public interface MemberContextRepository extends JpaRepository<MemberContextEntity, String> { 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) " AND m.id = mtmr.member_id ", nativeQuery = true)
List<MemberWithRoleDto> getOrgMemberList(String orgId); 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.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -263,28 +264,32 @@ public class OrganizationService {
OrganizationEntity organizationEntity = organizationRepository.findById(organizationId) OrganizationEntity organizationEntity = organizationRepository.findById(organizationId)
.orElseThrow(ResourceNotFoundException::new); .orElseThrow(ResourceNotFoundException::new);
Optional<MemberContextEntity> memberContextEntityOptional = Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberEntityId(userId); memberContextRepository.findByMemberId(userId);
if (memberContextEntityOptional.isPresent()) { if (memberContextEntityOptional.isPresent()) {
MemberContextEntity memberContextEntity = memberContextEntityOptional.get(); MemberContextEntity memberContextEntity = memberContextEntityOptional.get();
memberContextEntity.setOrganizationEntity(organizationEntity); memberContextEntity.setOrganizationEntity(organizationEntity);
memberContextRepository.save(memberContextEntity); memberContextRepository.save(memberContextEntity);
} else { } else {
MemberEntity memberEntity = memberRepository.findById(userId) if (isNotExistUser(userId)) {
.orElseThrow(() -> new IllegalArgumentException("Can't find member. Unknown userId=" + userId)); throw new ResourceNotFoundException("Can't find member. Unknown userId=" + userId);
}
MemberContextEntity memberContextEntity = new MemberContextEntity(); MemberContextEntity memberContextEntity = new MemberContextEntity();
memberContextEntity.setOrganizationEntity(organizationEntity); memberContextEntity.setOrganizationEntity(organizationEntity);
memberContextEntity.setMemberEntity(memberEntity); memberContextEntity.setMemberId(userId);
memberContextRepository.save(memberContextEntity); memberContextRepository.save(memberContextEntity);
} }
} }
public MemberContext findMemberContext(String userId) { private boolean isNotExistUser(String userId) {
MemberContextEntity memberContextEntity = memberContextRepository.findByMemberEntityId(userId) return !memberRepository.existsById(userId) &
.orElseThrow(ResourceNotFoundException::new); CollectionUtils.isEmpty(organizationRepository.findAllByOwner(userId));
}
public MemberContext findMemberContext(String userId) {
MemberContextEntity memberContextEntity = memberContextRepository.findByMemberId(userId)
.orElseThrow(ResourceNotFoundException::new);
MemberContext memberContext = new MemberContext(); MemberContext memberContext = new MemberContext();
memberContext.setOrganizationId(memberContextEntity.getOrganizationEntity().getId()); memberContext.setOrganizationId(memberContextEntity.getOrganizationEntity().getId());
return memberContext; 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; package com.rbkmoney.orgmanager.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.rbkmoney.orgmanager.entity.*; import com.rbkmoney.orgmanager.entity.*;
import com.rbkmoney.orgmanager.exception.AccessDeniedException; import com.rbkmoney.orgmanager.exception.AccessDeniedException;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException; import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
@ -311,6 +310,23 @@ public class UserControllerTest extends AbstractControllerTest {
assertNull(organizationSearchResultThird.getContinuationToken()); 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 @Test
void switchOrganizationWithNewContextCreation() throws Exception { void switchOrganizationWithNewContextCreation() throws Exception {
String userId = getUserFromToken(); String userId = getUserFromToken();
@ -329,10 +345,36 @@ public class UserControllerTest extends AbstractControllerTest {
) )
.andExpect(status().isNoContent()); .andExpect(status().isNoContent());
Optional<MemberContextEntity> memberContextEntityOptional = Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberEntityId(userId); memberContextRepository.findByMemberId(userId);
assertTrue(memberContextEntityOptional.isPresent()); 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()); assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
} }
@ -361,7 +403,7 @@ public class UserControllerTest extends AbstractControllerTest {
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization()); OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
MemberContextEntity memberContextEntity = memberContextRepository.save( MemberContextEntity memberContextEntity = memberContextRepository.save(
MemberContextEntity.builder() MemberContextEntity.builder()
.memberEntity(memberEntity) .memberId(userId)
.organizationEntity(organizationEntity) .organizationEntity(organizationEntity)
.build() .build()
); );
@ -379,7 +421,7 @@ public class UserControllerTest extends AbstractControllerTest {
) )
.andExpect(status().isNoContent()); .andExpect(status().isNoContent());
Optional<MemberContextEntity> memberContextEntityOptional = Optional<MemberContextEntity> memberContextEntityOptional =
memberContextRepository.findByMemberEntityId(userId); memberContextRepository.findByMemberId(userId);
assertTrue(memberContextEntityOptional.isPresent()); assertTrue(memberContextEntityOptional.isPresent());
assertEquals(newOrganizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId()); assertEquals(newOrganizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
@ -392,21 +434,19 @@ public class UserControllerTest extends AbstractControllerTest {
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization()); OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
MemberContextEntity memberContextEntity = memberContextRepository.save( MemberContextEntity memberContextEntity = memberContextRepository.save(
MemberContextEntity.builder() MemberContextEntity.builder()
.memberEntity(memberEntity) .memberId(userId)
.organizationEntity(organizationEntity) .organizationEntity(organizationEntity)
.build() .build()
); );
String jwtToken = generateRbkAdminJwt(); String jwtToken = generateRbkAdminJwt();
MvcResult mvcResult = mockMvc.perform(get("/user/context") mockMvc.perform(get("/user/context")
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + jwtToken) .header("Authorization", "Bearer " + jwtToken)
.header("X-Request-ID", "testRequestId") .header("X-Request-ID", "testRequestId")
) )
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andExpect(jsonPath("$.organizationId", equalTo(organizationEntity.getId())));
System.out.println(mvcResult);
} }
} }