-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/pagopa/p4pa-auth into P4…
…ADEV-1277-postman-test-activity
- Loading branch information
Showing
8 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/it/gov/pagopa/payhub/auth/exception/custom/ClientUnauthorizedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package it.gov.pagopa.payhub.auth.exception.custom; | ||
|
||
public class ClientUnauthorizedException extends RuntimeException { | ||
public ClientUnauthorizedException(String message){ | ||
super(message); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; | ||
import it.gov.pagopa.payhub.auth.mapper.ClientMapper; | ||
import it.gov.pagopa.payhub.model.generated.ClientDTO; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class AuthorizeClientCredentialsRequestService { | ||
private final ClientService clientService; | ||
private final ClientMapper clientMapper; | ||
|
||
public AuthorizeClientCredentialsRequestService(ClientService clientService, ClientMapper clientMapper) { | ||
this.clientService = clientService; | ||
this.clientMapper = clientMapper; | ||
} | ||
|
||
public ClientDTO authorizeCredentials(String clientId, String clientSecret) { | ||
return clientService.getClientByClientId(clientId) | ||
.map(clientMapper::mapToDTO) | ||
.filter(dto -> dto.getClientSecret().equals(clientSecret)) | ||
.orElseThrow(() -> new ClientUnauthorizedException("Unauthorized client for client-credentials")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...a/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; | ||
import it.gov.pagopa.payhub.auth.mapper.ClientMapper; | ||
import it.gov.pagopa.payhub.auth.model.Client; | ||
import it.gov.pagopa.payhub.model.generated.ClientDTO; | ||
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; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AuthorizeClientCredentialsRequestServiceTest { | ||
|
||
@Mock | ||
private ClientService clientServiceMock; | ||
@Mock | ||
private ClientMapper clientMapperMock; | ||
private AuthorizeClientCredentialsRequestService service; | ||
|
||
@BeforeEach | ||
void init() { | ||
service = new AuthorizeClientCredentialsRequestService(clientServiceMock, clientMapperMock); | ||
} | ||
|
||
@Test | ||
void givenRightCredentialsWhenAuthorizeCredentialsThenOk() { | ||
// Given | ||
String organizationIpaCode = "IPA_TEST_2"; | ||
String clientName = "SERVICE_001"; | ||
String clientId = organizationIpaCode + clientName; | ||
String clientSecretMock = UUID.randomUUID().toString(); | ||
|
||
Client mockClient = new Client(); | ||
ClientDTO expectedClientDTO = ClientDTO.builder() | ||
.clientId(clientId) | ||
.clientName(clientName) | ||
.organizationIpaCode(organizationIpaCode) | ||
.clientSecret(clientSecretMock) | ||
.build(); | ||
|
||
Mockito.when(clientServiceMock.getClientByClientId(clientId)).thenReturn(Optional.of(mockClient)); | ||
Mockito.when(clientMapperMock.mapToDTO(mockClient)).thenReturn(expectedClientDTO); | ||
// When | ||
ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecretMock); | ||
// Then | ||
Assertions.assertEquals(expectedClientDTO, actualClientDTO); | ||
} | ||
|
||
@Test | ||
void givenUnexpectedClientIdCredentialsWhenAuthorizeCredentialsThenClientUnauthorizedException() { | ||
// Given | ||
String clientId = "UNEXPECTED_CLIENT_ID"; | ||
String clientSecretMock = UUID.randomUUID().toString(); | ||
|
||
Mockito.when(clientServiceMock.getClientByClientId(clientId)).thenThrow(new ClientUnauthorizedException("error")); | ||
// When, Then | ||
Assertions.assertThrows(ClientUnauthorizedException.class, () -> service.authorizeCredentials(clientId, clientSecretMock)); | ||
} | ||
|
||
@Test | ||
void givenUnexpectedClientSecretCredentialsWhenAuthorizeCredentialsThenClientUnauthorizedException() { | ||
// Given | ||
String organizationIpaCode = "IPA_TEST_2"; | ||
String clientName = "SERVICE_001"; | ||
String clientId = organizationIpaCode + clientName; | ||
String clientSecret = UUID.randomUUID().toString(); | ||
|
||
Client mockClient = new Client(); | ||
ClientDTO expectedClientDTO = ClientDTO.builder() | ||
.clientId(clientId) | ||
.clientName(clientName) | ||
.organizationIpaCode(organizationIpaCode) | ||
.clientSecret(UUID.randomUUID().toString()) | ||
.build(); | ||
|
||
Mockito.when(clientServiceMock.getClientByClientId(clientId)).thenReturn(Optional.of(mockClient)); | ||
Mockito.when(clientMapperMock.mapToDTO(mockClient)).thenReturn(expectedClientDTO); | ||
|
||
// When, Then | ||
Assertions.assertThrows(ClientUnauthorizedException.class, () -> service.authorizeCredentials(clientId, clientSecret)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters