Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Nov 26, 2024
1 parent 338c1ad commit 8957734
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

import com.azure.spring.data.cosmos.repository.config.EnableReactiveCosmosRepositories;

import reactor.core.publisher.Hooks;

@SpringBootApplication
@EnableReactiveCosmosRepositories
@ConfigurationPropertiesScan()
public class RtpActivatorApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.spring.data.cosmos.config.AbstractCosmosConfiguration;
import com.azure.spring.data.cosmos.repository.config.EnableCosmosRepositories;
import com.azure.spring.data.cosmos.repository.config.EnableReactiveCosmosRepositories;

@Configuration
@EnableCosmosRepositories
@EnableReactiveCosmosRepositories
public class CosmosDBConfig extends AbstractCosmosConfiguration {

@Autowired
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ cosmos.credential.db-name=${DB_NAME:rtp}
cosmos.credential.endpoint=${COSMOS_CONNECTION_STRING:""}


activation.base-url=${BASE_URL:"http://localhost"}
activation.base-url=${BASE_URL:"http://localhost"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package it.gov.pagopa.rtp.activator;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
class RtpActivatorApplicationTests {

@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import it.gov.pagopa.rtp.activator.model.generated.PayerDto;
import it.gov.pagopa.rtp.activator.repository.ActivationDBRepository;
import it.gov.pagopa.rtp.activator.service.ActivationPayerService;
import it.gov.pagopa.rtp.activator.service.ActivationPayerServiceImpl;

import it.gov.pagopa.rtp.activator.utils.Users;
import reactor.core.publisher.Mono;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -25,11 +25,11 @@
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;

import java.time.Instant;
import java.util.UUID;
Expand All @@ -42,8 +42,12 @@
@ExtendWith(SpringExtension.class)
@WebFluxTest(controllers = { ActivationAPIControllerImpl.class })
@Import({ SecurityConfig.class })
@DisabledInAotMode
class ActivationAPIControllerImplTest {

@MockBean
private ActivationDBRepository activationDBRepository;

@MockBean
private ActivationPayerService activationPayerService;

Expand Down Expand Up @@ -75,72 +79,53 @@ public void testActivatePayerSuccessful() {
when(activationPropertiesConfig.getBaseUrl()).thenReturn("http://localhost:8080/");

webTestClient.post()
.uri(uriBuilder -> uriBuilder.path("/activation")
.queryParam("requestId", UUID.randomUUID().toString())
.queryParam("version", "v1").build())
.contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(generateActivationRequest()))
.uri("/activations")
.header("RequestId", UUID.randomUUID().toString())
.header("Version", "v1")
.bodyValue(generateActivationRequest())
.exchange()
.expectStatus().isCreated().expectHeader()
.location("http://localhost:8080/" + payer.payerID().toString());
}
/*
* @Test
*
* @Users.RtpWriter
* public void testActivatePayerAlreadyExists() {
* when(activationPayerService.activatePayer(any(String.class),
* any(String.class)))
* .thenReturn(Mono.error(new PayerAlreadyExists()));
* webTestClient.post()
* .uri(uriBuilder -> uriBuilder.path("/activation")
* .queryParam("requestId", UUID.randomUUID().toString())
* .queryParam("version", "v1").build())
* .contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(
* activationReqDto))
* .exchange()
* .expectStatus().isEqualTo(409);
* }
*
* @Test
*
* @Users.RtpWriter
* void shouldCreateNewActivation() {
* webTestClient.post()
* .uri("/activations")
* .header("RequestId", UUID.randomUUID().toString())
* .header("Version", "v1")
* .bodyValue(generateActivationRequest())
* .exchange()
* .expectStatus().isEqualTo(HttpStatus.CREATED)
* .expectHeader().exists(HttpHeaders.LOCATION);
* }
*
* @Test
*
* @WithMockUser(value = "another", roles = Users.ACTIVATION_WRITE_ROLE)
* void authorizedUserShouldNotActivateForAnotherServiceProvider() {
* webTestClient.post()
* .uri("/activations")
* .header("RequestId", UUID.randomUUID().toString())
* .header("Version", "v1")
* .bodyValue(generateActivationRequest())
* .exchange()
* .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
* }
*
* @Test
*
* @WithMockUser
* void userWithoutEnoughPermissionShouldNotCreateNewActivation() {
* webTestClient.post()
* .uri("/activations")
* .header("RequestId", UUID.randomUUID().toString())
* .header("Version", "v1")
* .bodyValue(generateActivationRequest())
* .exchange()
* .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
* }
*/

@Test
@Users.RtpWriter
public void testActivatePayerAlreadyExists() {
when(activationPayerService.activatePayer(any(String.class),
any(String.class)))
.thenReturn(Mono.error(new PayerAlreadyExists()));
webTestClient.post()
.uri("/activations")
.header("RequestId", UUID.randomUUID().toString())
.header("Version", "v1")
.bodyValue(generateActivationRequest())
.exchange()
.expectStatus().isEqualTo(409);
}

@Test
@WithMockUser(value = "another", roles = Users.ACTIVATION_WRITE_ROLE)
void authorizedUserShouldNotActivateForAnotherServiceProvider() {
webTestClient.post()
.uri("/activations")
.header("RequestId", UUID.randomUUID().toString())
.header("Version", "v1")
.bodyValue(generateActivationRequest())
.exchange()
.expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
}

@Test
@WithMockUser
void userWithoutEnoughPermissionShouldNotCreateNewActivation() {
webTestClient.post()
.uri("/activations")
.header("RequestId", UUID.randomUUID().toString())
.header("Version", "v1")
.bodyValue(generateActivationRequest())
.exchange()
.expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
}

private ActivationReqDto generateActivationRequest() {
return new ActivationReqDto(new PayerDto("RSSMRA85T10A562S", SERVICE_PROVIDER_ID));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/gov/pagopa/rtp/activator/utils/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Users {

public static final String SERVICE_PROVIDER_ID = "1234";
public static final String SERVICE_PROVIDER_ID = "984500A9EB6B07AC2G71";

public static final String ACTIVATION_WRITE_ROLE = "write_rtp_activations";
public static final String ACTIVATION_READ_ROLE = "read_rtp_activations";
Expand Down

0 comments on commit 8957734

Please sign in to comment.