diff --git a/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerImpl.java b/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerImpl.java index d550a532..00292f45 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerImpl.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerImpl.java @@ -126,7 +126,7 @@ public ResponseEntity> getClients(String organizationIpa @Override public ResponseEntity revokeClient(String organizationIpaCode, String clientId) { if(!SecurityUtils.isPrincipalAdmin(organizationIpaCode)){ - throw new UserUnauthorizedException("User not allowed to delete operator with clientId " + clientId); + throw new UserUnauthorizedException("User not allowed to delete client with clientId " + clientId); } authzService.revokeClient(organizationIpaCode, clientId); return ResponseEntity.ok(null); diff --git a/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerTest.java b/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerTest.java index 6b7a2c8e..83d2c62f 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthzControllerTest.java @@ -33,9 +33,11 @@ import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.BDDMockito.willDoNothing; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -467,5 +469,55 @@ void givenAlreadyExistentClientWhenRegisterClientThenConflict() throws Exception .content(objectMapper.writeValueAsString(createClientRequest)) ).andExpect(status().isConflict()); } +//end region + +//region revokeClient tests + @Test + void givenAuthorizedUserWhenRevokeClientThenOk() throws Exception { + String organizationIpaCode = "IPA_TEST_2"; + String clientId = "CLIENTID"; + + UserInfo expectedUser = UserInfo.builder() + .userId("USERID") + .organizationAccess(organizationIpaCode) + .organizations(List.of(UserOrganizationRoles.builder() + .organizationIpaCode(organizationIpaCode) + .roles(List.of(Constants.ROLE_ADMIN)) + .build())) + .build(); + + Mockito.when(authnServiceMock.getUserInfo("accessToken")) + .thenReturn(expectedUser); + willDoNothing().given(authzServiceMock).revokeClient(organizationIpaCode, clientId); + + mockMvc.perform( + delete("/payhub/auth/clients/{organizationIpaCode}/{clientId}", organizationIpaCode, clientId) + .header(HttpHeaders.AUTHORIZATION, "Bearer accessToken") + ).andExpect(status().isOk()) + .andDo(print()); + } + + @Test + void givenUnauthorizedUserWhenRevokeClientThenException() throws Exception { + //Given + String organizationIpaCode = "IPA_TEST_2"; + String clientId = "CLIENTID"; + + //When + Mockito.when(authnServiceMock.getUserInfo("accessToken")) + .thenReturn(UserInfo.builder() + .organizations(List.of(UserOrganizationRoles.builder() + .organizationIpaCode("ORG") + .roles(List.of(Constants.ROLE_OPER)) + .build())) + .build()); + + //Then + mockMvc.perform( + delete("/payhub/auth/clients/{organizationIpaCode}/{clientId}", organizationIpaCode, clientId) + .header(HttpHeaders.AUTHORIZATION, "Bearer accessToken") + ).andExpect(status().isUnauthorized()); + } + //end region } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/repository/ClientRepositoryExtImplTest.java b/src/test/java/it/gov/pagopa/payhub/auth/repository/ClientRepositoryExtImplTest.java new file mode 100644 index 00000000..caa06ab1 --- /dev/null +++ b/src/test/java/it/gov/pagopa/payhub/auth/repository/ClientRepositoryExtImplTest.java @@ -0,0 +1,58 @@ +package it.gov.pagopa.payhub.auth.repository; + +import it.gov.pagopa.payhub.auth.model.Client; +import it.gov.pagopa.payhub.auth.model.Operator; +import it.gov.pagopa.payhub.auth.model.User; +import org.junit.jupiter.api.AfterEach; +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 org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; + +@ExtendWith(MockitoExtension.class) +public class ClientRepositoryExtImplTest { + + @Mock + private MongoTemplate mongoTemplateMock; + + private ClientRepositoryExt repository; + + @BeforeEach + void init() { + repository = new ClientRepositoryExtImpl(mongoTemplateMock); + } + + @AfterEach + void verifyNotMoreInvocation() { + Mockito.verifyNoMoreInteractions(mongoTemplateMock); + } + + @Test + void whenDeleteClientThenOk() { + // Given + String organizationIpaCode = "IPA_CODE"; + String clientId = "IPA_CODEclientId"; + Client client = Client.builder() + .clientId(clientId) + .organizationIpaCode(organizationIpaCode) + .build(); + + Mockito.when(mongoTemplateMock.findOne( + Query.query(Criteria.where(Client.Fields.clientId).is(clientId)), + Client.class)).thenReturn(client); + + // When + repository.deleteClient(organizationIpaCode, clientId); + // Then + Mockito.verify(mongoTemplateMock).remove( + Query.query(Criteria + .where(Client.Fields.organizationIpaCode).is(client.getOrganizationIpaCode()) + .and(Client.Fields.clientId).is(client.getClientId())), + Client.class); + } +} diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/AuthzServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/AuthzServiceTest.java index db777dc9..dd8c3c38 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/AuthzServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/AuthzServiceTest.java @@ -299,4 +299,14 @@ void givenOrganizationIpaCodeWhenGetClientsThenInvokeClientService() { Assertions.assertEquals(expectedDTOList, result); } + @Test + void givenClientIdWhenRevokeClientThenVerifyRevoke() { + //Given + String organizationIpaCode = "organizationIpaCode"; + String clientId = "clientId"; + //When + service.revokeClient(organizationIpaCode, clientId); + //Then + Mockito.verify(clientServiceMock).revokeClient(organizationIpaCode, clientId); + } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java index 286f78a8..fe2946bc 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java @@ -122,4 +122,14 @@ void givenClientIdWhenGetClientByClientIdThenInvokeClientService() { Assertions.assertEquals(Optional.of(expectedClient), result); } + @Test + void givenClientIdWhenRevokeClientThenVerifyRevoke() { + // Given + String organizationIpaCode = "organizationIpaCode"; + String clientId = "clientId"; + //When + service.revokeClient(organizationIpaCode, clientId); + //Then + Mockito.verify(clientRemovalServiceMock).revokeClient(organizationIpaCode, clientId); + } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/revoke/ClientRemovalServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/revoke/ClientRemovalServiceTest.java new file mode 100644 index 00000000..79e36b7e --- /dev/null +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/revoke/ClientRemovalServiceTest.java @@ -0,0 +1,29 @@ +package it.gov.pagopa.payhub.auth.service.a2a.revoke; + +import it.gov.pagopa.payhub.auth.repository.ClientRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ClientRemovalServiceTest { + + @Mock + private ClientRepository clientRepository; + @InjectMocks + private ClientRemovalService service; + + @Test + void givenClientIdWhenRevokeClientThenVerifyRevoke() { + // Given + String organizationIpaCode = "organizationIpaCode"; + String clientId = "clientId"; + //When + service.revokeClient(organizationIpaCode, clientId); + //Then + Mockito.verify(clientRepository).deleteClient(organizationIpaCode, clientId); + } +}