Skip to content

Commit

Permalink
P4ADEV-1102 added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macacia committed Oct 24, 2024
1 parent bee0c3c commit 583acf7
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ResponseEntity<List<ClientNoSecretDTO>> getClients(String organizationIpa
@Override
public ResponseEntity<Void> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 583acf7

Please sign in to comment.