-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpVerifierHandlerInterceptorIntegrationTest.java
151 lines (135 loc) · 7.79 KB
/
HttpVerifierHandlerInterceptorIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* (C)2023 */
package it.pagopa.tech.lollipop.consumer.spring;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import it.pagopa.tech.lollipop.consumer.spring.config.HttpVerifierConfiguration;
import it.pagopa.tech.lollipop.consumer.spring.config.SpringLollipopConsumerRequestConfig;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockserver.integration.ClientAndServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(
classes = {
DemoApplication.class,
SpringLollipopConsumerRequestConfig.class,
HttpVerifierConfiguration.class,
})
public class HttpVerifierHandlerInterceptorIntegrationTest {
@LocalServerPort private int port;
@Autowired private TestRestTemplate restTemplate;
@BeforeAll
public static void startServer() {
mockServer = startClientAndServer(3000);
}
private static ClientAndServer mockServer;
@Autowired private HttpVerifierHandlerInterceptor interceptor;
@Test
void testWithValidRequestReturnsSuccess() {
AssertionSimpleClientTestUtils.createExpectationAssertionFound();
String signatureInput =
"sig123=(\"content-digest\" \"x-pagopa-lollipop-original-method\""
+ " \"x-pagopa-lollipop-original-url\");created=1678293988;nonce=\"aNonce\";alg=\"ecdsa-p256-sha256\";keyid=\"sha256-a7qE0Y0DyqeOFFREIQSLKfu5WlbckdxVXKFasfcI-Dg\"";
var signature =
"sig123=:lTuoRytp53GuUMOB4Rz1z97Y96gfSeEOm/xVpO39d3HR6lLAy4KYiGq+1hZ7nmRFBt2bASWEpen7ov5O4wU3kQ==:";
RestTemplate exec = restTemplate.getRestTemplate();
exec.getClientHttpRequestInitializers()
.add(
request -> {
request.getHeaders()
.add(
"Content-Digest",
"sha-256=:cpyRqJ1VhoVC+MSs9fq4/4wXs4c46EyEFriskys43Zw=:");
request.getHeaders()
.add(
"x-pagopa-lollipop-original-url",
"https://api-app.io.pagopa.it/first-lollipop/sign");
request.getHeaders().add("x-pagopa-lollipop-original-method", "POST");
request.getHeaders()
.add(
"x-pagopa-lollipop-public-key",
"eyJrdHkiOiJFQyIsIngiOiJGcUZEd"
+ "XdFZ3U0TVVYRVJQTVZMLTg1cEd2MkQzWW1MNEoxZ2ZNa2RiYzI0IiwieSI6Im"
+ "hkVjBveG1XRlN4TW9KVURwZGlocjc2clM4VlJCRXFNRmViWXlBZks5LWsiLCJjcnYiO"
+ "iJQLTI1NiJ9");
request.getHeaders()
.add(
"x-pagopa-lollipop-assertion-ref",
"sha256-_ZzL8qeuAM5kQ9pbMB4tn7IDSQZCVXAkW9fm4P7ULPI");
request.getHeaders().add("x-pagopa-lollipop-assertion-type", "SAML");
request.getHeaders().add("x-pagopa-lollipop-auth-jwt", "aValidJWT");
request.getHeaders()
.add("x-pagopa-lollipop-user-id", "AAAAAA89S20I111X");
request.getHeaders().add("Signature-Input", signatureInput);
request.getHeaders().add("Signature", signature);
});
ResponseEntity<String> response =
exec.postForEntity(
"http://localhost:" + port,
"{\"message\":\"a valid message payload\"}",
String.class);
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getStatusCodeValue());
}
@Test
void testWithinvalidPayloadRequestReturnsUnauthorized() {
AssertionSimpleClientTestUtils.createExpectationAssertionFound();
String signatureInput =
"sig123=(\"content-digest\" \"x-pagopa-lollipop-original-method\""
+ " \"x-pagopa-lollipop-original-url\");created=1678293988;nonce=\"aNonce\";alg=\"ecdsa-p256-sha256\";keyid=\"sha256-a7qE0Y0DyqeOFFREIQSLKfu5WlbckdxVXKFasfcI-Dg\"";
var signature =
"sig123=:lTuoRytp53GuUMOB4Rz1z97Y96gfSeEOm/xVpO39d3HR6lLAy4KYiGq+1hZ7nmRFBt2bASWEpen7ov5O4wU3kQ==:";
RestTemplate exec = restTemplate.getRestTemplate();
exec.getClientHttpRequestInitializers()
.add(
request -> {
request.getHeaders()
.add(
"Content-Digest",
"sha-256=:cpyRqJ1VhoVC+MSs9fq4/4wXs4c46EyEFriskys43Zw=:");
request.getHeaders()
.add(
"x-pagopa-lollipop-original-url",
"https://api-app.io.pagopa.it/first-lollipop/sign");
request.getHeaders().add("x-pagopa-lollipop-original-method", "POST");
request.getHeaders()
.add(
"x-pagopa-lollipop-public-key",
"eyJrdHkiOiJFQyIsIngiOiJGcUZEd"
+ "XdFZ3U0TVVYRVJQTVZMLTg1cEd2MkQzWW1MNEoxZ2ZNa2RiYzI0IiwieSI6Im"
+ "hkVjBveG1XRlN4TW9KVURwZGlocjc2clM4VlJCRXFNRmViWXlBZks5LWsiLCJjcnYiO"
+ "iJQLTI1NiJ9");
request.getHeaders()
.add(
"x-pagopa-lollipop-assertion-ref",
"sha256-_ZzL8qeuAM5kQ9pbMB4tn7IDSQZCVXAkW9fm4P7ULPI");
request.getHeaders().add("x-pagopa-lollipop-assertion-type", "SAML");
request.getHeaders().add("x-pagopa-lollipop-auth-jwt", "aValidJWT");
request.getHeaders()
.add("x-pagopa-lollipop-user-id", "AAAAAA89S20I111X");
request.getHeaders().add("Signature-Input", signatureInput);
request.getHeaders().add("Signature", signature);
});
ResponseEntity<String> response =
exec.postForEntity(
"http://localhost:" + port,
"{\"message\":\"an invalid message payload\"}",
String.class);
Assertions.assertNotNull(response);
Assertions.assertEquals(401, response.getStatusCodeValue());
}
@AfterAll
public static void stopServer() {
mockServer.stop();
}
}