-
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.
- Loading branch information
1 parent
658f8e3
commit 3db7bbc
Showing
8 changed files
with
112 additions
and
16 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
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
13 changes: 10 additions & 3 deletions
13
src/test/java/it/gov/pagopa/rtp/activator/configuration/NoSignatureJwtDecoderTest.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 |
---|---|---|
@@ -1,21 +1,28 @@ | ||
package it.gov.pagopa.rtp.activator.configuration; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import it.gov.pagopa.rtp.activator.JwtTestUtils; | ||
import it.gov.pagopa.rtp.activator.utils.JwtUtils; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.oauth2.jwt.JwtValidationException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class NoSignatureJwtDecoderTest { | ||
|
||
|
||
@Test | ||
void givenSignedTokenMustDecodeWithoutVerifySignature() throws JOSEException { | ||
final var decoder = new NoSignatureJwtDecoder(); | ||
final var token = JwtTestUtils.generateToken("me", "none"); | ||
final var token = JwtUtils.generateToken("me", "none"); | ||
assertThat(decoder.decode(token), Matchers.notNullValue()); | ||
} | ||
|
||
@Test | ||
void givenExpiredTokenMustThrowError() throws JOSEException { | ||
final var decoder = new NoSignatureJwtDecoder(); | ||
final var token = JwtUtils.generateExpiredToken("me", "none"); | ||
assertThrows(JwtValidationException.class, () -> decoder.decode(token)); | ||
} | ||
} | ||
|
71 changes: 71 additions & 0 deletions
71
src/test/java/it/gov/pagopa/rtp/activator/controller/ActivationAPIControllerImplTest.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,71 @@ | ||
package it.gov.pagopa.rtp.activator.controller; | ||
|
||
import it.gov.pagopa.rtp.activator.configuration.SecurityConfig; | ||
import it.gov.pagopa.rtp.activator.model.generated.ActivationReqDto; | ||
import it.gov.pagopa.rtp.activator.model.generated.PayerDto; | ||
import it.gov.pagopa.rtp.activator.utils.Users; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; | ||
|
||
|
||
@ExtendWith(SpringExtension.class) | ||
@WebFluxTest(controllers = {ActivationAPIControllerImpl.class}) | ||
@Import(SecurityConfig.class) | ||
class ActivationAPIControllerImplTest { | ||
@Autowired | ||
ApplicationContext context; | ||
|
||
WebTestClient web; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
web = WebTestClient | ||
.bindToApplicationContext(this.context) | ||
.apply(springSecurity()) | ||
.configureClient() | ||
.build(); | ||
} | ||
|
||
@Test | ||
@Users.RtpWriter | ||
void shouldCreateNewActivation() { | ||
web.post() | ||
.uri("/activations") | ||
.header("RequestId", UUID.randomUUID().toString()) | ||
.header("Version", "v1") | ||
.bodyValue(generateActivationRequest()) | ||
.exchange() | ||
.expectStatus().isEqualTo(HttpStatus.CREATED) | ||
.expectHeader().exists(HttpHeaders.LOCATION); | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
void userWithoutEnoughPermissionShouldNotCreateNewActivation() { | ||
web.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", "134")); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/test/java/it/gov/pagopa/rtp/activator/utils/Users.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,17 @@ | ||
package it.gov.pagopa.rtp.activator.utils; | ||
|
||
import org.springframework.security.test.context.support.WithMockUser; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
public class Users { | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithMockUser(value = "writer", roles = "write_rtp_activations") | ||
public @interface RtpWriter { } | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithMockUser(value = "reader", roles = "read_rtp_activations") | ||
public @interface RtpReader { } | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
logging.level.org.springframework.security=DEBUG | ||
logging.level.org.springframework.security=DEBUG |