mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 08:25:21 +00:00
decline revoked invitation
This commit is contained in:
parent
d7703ffad2
commit
26f5817050
@ -1,23 +0,0 @@
|
||||
package com.rbkmoney.orgmanager.controller.error;
|
||||
|
||||
import com.rbkmoney.swag.organizations.model.InlineResponse422;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class ExpiredExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(InviteExpiredException.class)
|
||||
public ResponseEntity<?> handle(InviteExpiredException ex) {
|
||||
InlineResponse422 badResponse = new InlineResponse422()
|
||||
.code(InlineResponse422.CodeEnum.INVITATIONEXPIRED)
|
||||
.message("Invite expired at: " + ex.getExpiredAt());
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(badResponse);
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.rbkmoney.orgmanager.controller.error;
|
||||
package com.rbkmoney.orgmanager.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
@ -0,0 +1,15 @@
|
||||
package com.rbkmoney.orgmanager.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class InviteRevokedException extends RuntimeException {
|
||||
|
||||
private final String revokedAt;
|
||||
|
||||
public InviteRevokedException(String revokedAt) {
|
||||
super();
|
||||
this.revokedAt = revokedAt;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.rbkmoney.orgmanager.exception;
|
||||
|
||||
import com.rbkmoney.swag.organizations.model.InlineResponse422;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -34,4 +35,20 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
.build();
|
||||
}
|
||||
|
||||
@ExceptionHandler(InviteExpiredException.class)
|
||||
public ResponseEntity<?> handleInviteExpiredException(InviteExpiredException ex) {
|
||||
InlineResponse422 badResponse = new InlineResponse422()
|
||||
.code(InlineResponse422.CodeEnum.INVITATIONEXPIRED)
|
||||
.message(String.format("Invite expired at: %s", ex.getExpiredAt()));
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(badResponse);
|
||||
}
|
||||
|
||||
@ExceptionHandler(InviteRevokedException.class)
|
||||
public ResponseEntity<?> handleInviteRevokedException(InviteRevokedException ex) {
|
||||
InlineResponse422 badResponse = new InlineResponse422()
|
||||
.code(InlineResponse422.CodeEnum.INVITATIONEXPIRED)
|
||||
.message(String.format("Invite revoked at: %s", ex.getRevokedAt()));
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(badResponse);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.rbkmoney.orgmanager.service;
|
||||
|
||||
import com.rbkmoney.orgmanager.controller.error.InviteExpiredException;
|
||||
import com.rbkmoney.orgmanager.converter.InvitationConverter;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.InviteExpiredException;
|
||||
import com.rbkmoney.orgmanager.exception.InviteRevokedException;
|
||||
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
@ -113,13 +114,20 @@ public class InvitationService {
|
||||
});
|
||||
}
|
||||
|
||||
public InvitationEntity getByToken(String token) {
|
||||
public InvitationEntity findByToken(String token) {
|
||||
InvitationEntity invitationEntity = invitationRepository.findByAcceptToken(token)
|
||||
.orElseThrow(ResourceNotFoundException::new);
|
||||
if (invitationEntity.isExpired()) {
|
||||
throw new InviteExpiredException(invitationEntity.getExpiresAt().toString());
|
||||
}
|
||||
validateInvitation(invitationEntity);
|
||||
return invitationEntity;
|
||||
}
|
||||
|
||||
private void validateInvitation(InvitationEntity invitationEntity) {
|
||||
if (invitationEntity.isExpired()) {
|
||||
throw new InviteExpiredException(invitationEntity.getExpiresAt().toString());
|
||||
}
|
||||
if (invitationEntity.getStatus().equalsIgnoreCase(InvitationStatusName.REVOKED.getValue())) {
|
||||
throw new InviteRevokedException(invitationEntity.getRevokedAt().toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ public class OrganizationService {
|
||||
|
||||
@Transactional
|
||||
public OrganizationMembership joinOrganization(String token, String userId, String userEmail) {
|
||||
InvitationEntity invitationEntity = invitationService.getByToken(token);
|
||||
InvitationEntity invitationEntity = invitationService.findByToken(token);
|
||||
OrganizationEntity organizationEntity = findById(invitationEntity.getOrganizationId());
|
||||
MemberEntity memberEntity = findOrCreateMember(userId, userEmail);
|
||||
memberEntity.getRoles().addAll(invitationEntity.getInviteeRoles());
|
||||
@ -264,7 +264,7 @@ public class OrganizationService {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public String getOrgIdByInvitationToken(String token) {
|
||||
InvitationEntity invitationEntity = invitationService.getByToken(token);
|
||||
InvitationEntity invitationEntity = invitationService.findByToken(token);
|
||||
OrganizationEntity organizationEntity = findById(invitationEntity.getOrganizationId());
|
||||
return organizationEntity.getId();
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package com.rbkmoney.orgmanager.service;
|
||||
|
||||
import com.rbkmoney.orgmanager.TestObjectFactory;
|
||||
import com.rbkmoney.orgmanager.converter.InvitationConverter;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.InviteExpiredException;
|
||||
import com.rbkmoney.orgmanager.exception.InviteRevokedException;
|
||||
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
@ -15,10 +18,12 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ -172,15 +177,65 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundIfInvitationDoesNotExist() {
|
||||
// Given
|
||||
void shouldThrowNotFoundIfInvitationDoesNotExist() {
|
||||
String orgId = "orgId";
|
||||
String invitationId = "invitationId";
|
||||
|
||||
when(invitationRepository.findByIdAndOrganizationId(invitationId, orgId))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
// When Then
|
||||
assertThrows(ResourceNotFoundException.class, () -> service.revoke(orgId, invitationId, new InlineObject1()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowResourceNotFoundExceptionIfInvitationWithTokenDoesNotExist() {
|
||||
String token = TestObjectFactory.randomString();
|
||||
|
||||
when(invitationRepository.findByAcceptToken(token))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ResourceNotFoundException.class, () -> service.findByToken(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowInviteExpiredExceptionIfInvitationExpired() {
|
||||
String token = TestObjectFactory.randomString();
|
||||
String orgId = TestObjectFactory.randomString();
|
||||
InvitationEntity invitationEntity = TestObjectFactory.buildInvitation(orgId);
|
||||
invitationEntity.setExpiresAt(LocalDateTime.now().minusDays(1));
|
||||
|
||||
when(invitationRepository.findByAcceptToken(token))
|
||||
.thenReturn(Optional.of(invitationEntity));
|
||||
|
||||
assertThrows(InviteExpiredException.class, () -> service.findByToken(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowInviteRevokedExceptionIfInvitationRevoked() {
|
||||
String token = TestObjectFactory.randomString();
|
||||
String orgId = TestObjectFactory.randomString();
|
||||
InvitationEntity invitationEntity = TestObjectFactory.buildInvitation(orgId);
|
||||
invitationEntity.setStatus(InvitationStatusName.REVOKED.getValue());
|
||||
invitationEntity.setRevokedAt(LocalDateTime.now());
|
||||
|
||||
when(invitationRepository.findByAcceptToken(token))
|
||||
.thenReturn(Optional.of(invitationEntity));
|
||||
|
||||
assertThrows(InviteRevokedException.class, () -> service.findByToken(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnInvitationByToken() {
|
||||
String token = TestObjectFactory.randomString();
|
||||
String orgId = TestObjectFactory.randomString();
|
||||
InvitationEntity expectedInvitation = TestObjectFactory.buildInvitation(orgId);
|
||||
|
||||
|
||||
when(invitationRepository.findByAcceptToken(token))
|
||||
.thenReturn(Optional.of(expectedInvitation));
|
||||
|
||||
InvitationEntity actualInvitation = service.findByToken(token);
|
||||
|
||||
assertEquals(expectedInvitation, actualInvitation);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user