Skip to content

Commit

Permalink
P4ADEV-452 logout businessLogic
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio.torre committed Jun 6, 2024
1 parent 673e71e commit 3273092
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
2 changes: 2 additions & 0 deletions openapi/p4pa-auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ paths:
description: OK
'400':
description: Invalid request
'401':
description: Invalid client_id
security:
- BearerAuth: []
components:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Map<String, Claim> validate(String clientId, String grantType, String sub
return claims;
}

private void validateClient(String clientId) {
public void validateClient(String clientId) {
if (!ALLOWED_CLIENT_ID.equals(clientId)){
throw new InvalidExchangeClientException("Invalid clientId " + clientId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package it.gov.pagopa.payhub.auth.service.logout;

import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.service.exchange.ValidateExternalTokenService;
import org.springframework.stereotype.Service;

@Service
public class LogoutServiceImpl implements LogoutService {

private final ValidateExternalTokenService validateExternalTokenService;
private final TokenStoreService tokenStoreService;

public LogoutServiceImpl(ValidateExternalTokenService validateExternalTokenService, TokenStoreService tokenStoreService) {
this.validateExternalTokenService = validateExternalTokenService;
this.tokenStoreService = tokenStoreService;
}

@Override
public void logout(String clientId, String token) {
// TODO
validateExternalTokenService.validateClient(clientId);
tokenStoreService.delete(token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,30 @@ void givenNoClientIdWhenLogoutThenBadRequest() throws Exception {
}

@Test
void givenCompleteRequestWhenLogoutThenOk() throws Exception {
void givenInvalidClientIdWhenLogoutThenBadRequest() throws Exception {
mockMvc.perform(
post("/payhub/auth/revoke")
.param("token", "token")
).andExpect(status().isBadRequest());
}

@Test
void givenCompleteRequestWhenLogoutThenInvalidClientError() throws Exception {
String clientId = "CLIENTID";
String token = "TOKEN";

mockMvc.perform(
Mockito.doThrow(new InvalidExchangeClientException(""))
.when(authServiceMock).logout(clientId, token);

MvcResult result = mockMvc.perform(
post("/payhub/auth/revoke")
.param("client_id", clientId)
.param("token", token)
).andExpect(status().isOk());
).andExpect(status().isUnauthorized()).andReturn();

Mockito.verify(authServiceMock).logout(clientId, token);
AuthErrorDTO actual = objectMapper.readValue(result.getResponse().getContentAsString(),
AuthErrorDTO.class);
assertEquals(AuthErrorDTO.ErrorEnum.INVALID_CLIENT, actual.getError());
assertEquals("", actual.getErrorDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void givenClaimsWhenSaveThenReturnThem(){
}

@Test
void givenAccessTokenWhenSaveThenNull(){
void givenAccessTokenWhenLoadThenNull(){
// Given
String accessToken = "AccessToken";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package it.gov.pagopa.payhub.auth.service.logout;

import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeClientException;
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.service.exchange.ValidateExternalTokenService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class LogoutServiceTest {

@Mock
private ValidateExternalTokenService validateExternalTokenServiceMock;
@Mock
private TokenStoreService tokenStoreServiceMock;

private LogoutService service;

@BeforeEach
void init(){
service = new LogoutServiceImpl(validateExternalTokenServiceMock, tokenStoreServiceMock);
}

@Test
void givenInvalidClientIdWhenLogoutThenInvalidClientException(){
// Given
String clientId = "clientId";
String token = "token";

InvalidExchangeClientException expectedException = new InvalidExchangeClientException("");
Mockito.doThrow(expectedException)
.when(validateExternalTokenServiceMock).validateClient(clientId);

// When, Then
InvalidExchangeClientException exception = Assertions.assertThrows(InvalidExchangeClientException.class, () -> service.logout(clientId, token));
Assertions.assertSame(expectedException, exception);
}

@Test
void givenCompleteRequestWhenLogoutThenOk(){
// Given
String clientId = "clientId";
String token = "token";

// When
service.logout(clientId, token);

// Then
Mockito.verify(validateExternalTokenServiceMock).validateClient(clientId);
Mockito.verify(tokenStoreServiceMock).delete(token);
}
}

0 comments on commit 3273092

Please sign in to comment.