Skip to content

Commit 0630e67

Browse files
added test for SAMLAssertionWriter
1 parent 16247ed commit 0630e67

File tree

3 files changed

+197
-1
lines changed

3 files changed

+197
-1
lines changed

src/test/java/it/pagopa/pn/national/registries/client/SecureWebClientUtilsTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import it.pagopa.pn.national.registries.config.adecheckcf.CheckCfSecretConfig;
77
import it.pagopa.pn.national.registries.model.SSLData;
88
import it.pagopa.pn.national.registries.utils.X509CertificateUtils;
9+
import org.junit.jupiter.api.Assertions;
910
import org.junit.jupiter.api.Test;
1011
import org.junit.jupiter.api.extension.ExtendWith;
11-
import org.mockito.Mock;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.boot.test.mock.mockito.MockBean;
1414
import org.springframework.test.context.ContextConfiguration;
@@ -35,6 +35,33 @@ class SecureWebClientUtilsTest {
3535
private X509CertificateUtils x509CertificateUtils;
3636

3737

38+
@Test
39+
void getSslContextForAde_shouldReturnSslContextWhenTrustIsProvided() throws SSLException {
40+
SslContextBuilder sslContextBuilder = mock(SslContextBuilder.class);
41+
when(sslContextBuilder.build()).thenReturn(mock(SslContext.class));
42+
when(sslContextBuilder.trustManager(any(InputStream.class))).thenReturn(sslContextBuilder);
43+
when(sslContextBuilder.keyManager(any(PrivateKey.class), any(X509Certificate.class))).thenReturn(sslContextBuilder);
44+
45+
SSLData sslData = new SSLData();
46+
when(x509CertificateUtils.getKeyAndCertificate(any())).thenReturn(sslData);
47+
when(x509CertificateUtils.getPrivateKey(any())).thenReturn(mock(PrivateKey.class));
48+
when(x509CertificateUtils.loadCertificate(any())).thenReturn(mock(X509Certificate.class));
49+
50+
SslContext sslContext = secureWebClientUtils.getSslContextForAde(sslContextBuilder, "dHJ1c3QK");
51+
assertNotNull(sslContext);
52+
verify(sslContextBuilder).trustManager(any(InputStream.class));
53+
verify(sslContextBuilder).keyManager(any(PrivateKey.class), any(X509Certificate.class));
54+
}
55+
56+
57+
@Test
58+
void getSslContextForAde_NoTrust() throws SSLException {
59+
SslContextBuilder sslContextBuilder = mock(SslContextBuilder.class);
60+
when(sslContextBuilder.build()).thenReturn(mock(SslContext.class));
61+
SslContext sslContext = secureWebClientUtils.getSslContextForAde(sslContextBuilder, null);
62+
Assertions.assertNotNull(sslContext);
63+
}
64+
3865
/**
3966
* Method under test:
4067
* {@link SecureWebClientUtils#getSslContext(SslContextBuilder, String)}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package it.pagopa.pn.national.registries.config.adecheckcf;
2+
3+
import io.netty.handler.ssl.SslContext;
4+
import it.pagopa.pn.national.registries.client.SecureWebClientUtils;
5+
import it.pagopa.pn.national.registries.config.CustomRetryConfig;
6+
import it.pagopa.pn.national.registries.generated.openapi.msclient.ade.v1.api.VerificheApi;
7+
import it.pagopa.pn.national.registries.model.TrustData;
8+
import it.pagopa.pn.national.registries.service.PnNationalRegistriesSecretService;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.mockito.Mock;
13+
import org.springframework.boot.test.mock.mockito.MockBean;
14+
import org.springframework.test.context.junit.jupiter.SpringExtension;
15+
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
16+
import org.springframework.web.reactive.function.client.WebClient;
17+
18+
import javax.net.ssl.SSLException;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.ArgumentMatchers.anyString;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
26+
27+
@ExtendWith(SpringExtension.class)
28+
class CheckCfClientConfigtest {
29+
30+
@MockBean
31+
static SecureWebClientUtils secureWebClientUtils;
32+
@MockBean
33+
static CheckCfSecretConfig checkCfSecretConfig;
34+
@MockBean
35+
static PnNationalRegistriesSecretService pnNationalRegistriesSecretService;
36+
@Mock
37+
private WebClient.Builder webClientBuilder;
38+
39+
@Mock
40+
private CustomRetryConfig customRetryConfig;
41+
42+
@Mock
43+
private WebClient webClient;
44+
45+
private CheckCfClientConfig checkCfClientConfig;
46+
47+
@BeforeEach
48+
void setUp() {
49+
when(webClientBuilder.build()).thenReturn(webClient);
50+
when(webClientBuilder.defaultHeader(any(), any())).thenReturn(webClientBuilder);
51+
when(webClientBuilder.baseUrl(any())).thenReturn(webClientBuilder);
52+
when(webClientBuilder.filters(any())).thenReturn(webClientBuilder);
53+
when(webClientBuilder.filter(any())).thenReturn(webClientBuilder);
54+
when(webClientBuilder.clientConnector(any())).thenReturn(webClientBuilder);
55+
checkCfClientConfig = new CheckCfClientConfig(customRetryConfig, pnNationalRegistriesSecretService, checkCfSecretConfig, secureWebClientUtils);
56+
}
57+
58+
@Test
59+
void e002ServiceApi() throws SSLException {
60+
TrustData trustData = new TrustData();
61+
trustData.setTrust("trust");
62+
63+
when(pnNationalRegistriesSecretService.getTrustedCertFromSecret(any())).thenReturn(trustData);
64+
when(secureWebClientUtils.getSslContextForAde(any(), any())).thenReturn(mock(SslContext.class));
65+
when(customRetryConfig.buildRetryExchangeFilterFunction()).thenReturn(mock(ExchangeFilterFunction.class));
66+
VerificheApi verificheapi = checkCfClientConfig.verificheApi("basePath");
67+
68+
assertNotNull(verificheapi);
69+
assertEquals("basePath", verificheapi.getApiClient().getBasePath());
70+
}
71+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package it.pagopa.pn.national.registries.utils;
2+
3+
import it.pagopa.pn.commons.utils.MDCUtils;
4+
import it.pagopa.pn.national.registries.config.adelegal.AdeLegalSecretConfig;
5+
import it.pagopa.pn.national.registries.model.SSLData;
6+
import kotlin.jvm.internal.unsafe.MonitorKt;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.mockito.InjectMocks;
11+
import org.mockito.Mock;
12+
import org.mockito.MockedStatic;
13+
import org.mockito.Mockito;
14+
import org.mockito.junit.jupiter.MockitoExtension;
15+
import org.opensaml.core.xml.NamespaceManager;
16+
import org.opensaml.core.xml.XMLObject;
17+
import org.opensaml.saml.common.SAMLVersion;
18+
import org.opensaml.saml.saml2.core.*;
19+
import org.opensaml.xmlsec.signature.*;
20+
import org.springframework.test.context.ContextConfiguration;
21+
22+
import javax.security.auth.x500.X500Principal;
23+
import java.nio.charset.StandardCharsets;
24+
import java.security.PrivateKey;
25+
import java.security.cert.X509Certificate;
26+
import java.util.Base64;
27+
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
import static org.junit.jupiter.api.Assertions.assertFalse;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.ArgumentMatchers.anyString;
35+
import static org.mockito.Mockito.*;
36+
37+
@ExtendWith(MockitoExtension.class)
38+
class SAMLAssertionWriterTest {
39+
40+
@Mock
41+
private OpenSAMLUtils openSAMLUtils;
42+
43+
@Mock
44+
private X509CertificateUtils x509CertificateUtils;
45+
46+
@Mock
47+
private AdeLegalSecretConfig adeLegalSecretConfig;
48+
49+
@InjectMocks
50+
private SAMLAssertionWriter samlAssertionWriter;
51+
52+
@Test
53+
void buildDefaultAssertion_shouldHandleRootTraceId() {
54+
try (MockedStatic<MDCUtils> mocked = mockStatic(MDCUtils.class)) {
55+
mocked.when(MDCUtils::retrieveMDCContextMap).thenReturn(new HashMap<>());
56+
MDCUtils.retrieveMDCContextMap().put("rootTraceId", "12345");
57+
58+
Assertion assertion = mock(Assertion.class);
59+
when(assertion.getNamespaceManager()).thenReturn(mock(NamespaceManager.class));
60+
Mockito.when(openSAMLUtils.buildSAMLObject(Assertion.DEFAULT_ELEMENT_NAME, null)).thenReturn(assertion);
61+
Mockito.when(openSAMLUtils.buildSAMLObject(Issuer.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(Issuer.class));
62+
Mockito.when(openSAMLUtils.buildSAMLObject(Signature.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(Signature.class));
63+
Mockito.when(openSAMLUtils.buildSAMLObject(KeyInfo.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(KeyInfo.class));
64+
Mockito.when(openSAMLUtils.buildSAMLObject(X509Data.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(X509Data.class));
65+
Mockito.when(openSAMLUtils.buildSAMLObject(org.opensaml.xmlsec.signature.X509Certificate.DEFAULT_ELEMENT_NAME, null))
66+
.thenReturn(mock(org.opensaml.xmlsec.signature.X509Certificate.class));
67+
Mockito.when(openSAMLUtils.buildSAMLObject(X509IssuerName.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(X509IssuerName.class));
68+
Mockito.when(openSAMLUtils.buildSAMLObject(X509IssuerSerial.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(X509IssuerSerial.class));
69+
Mockito.when(openSAMLUtils.buildSAMLObject(X509SerialNumber.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(X509SerialNumber.class));
70+
Mockito.when(openSAMLUtils.buildSAMLObject(NameID.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(NameID.class));
71+
Mockito.when(openSAMLUtils.buildSAMLObject(Subject.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(Subject.class));
72+
Mockito.when(openSAMLUtils.buildSAMLObject(SubjectConfirmation.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(SubjectConfirmation.class));
73+
Mockito.when(openSAMLUtils.buildSAMLObject(SubjectConfirmationData.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(SubjectConfirmationData.class));
74+
Mockito.when(openSAMLUtils.buildSAMLObject(Conditions.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(Conditions.class));
75+
Mockito.when(openSAMLUtils.buildSAMLObject(AuthnContextClassRef.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(AuthnContextClassRef.class));
76+
Mockito.when(openSAMLUtils.buildSAMLObject(AuthnContext.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(AuthnContext.class));
77+
Mockito.when(openSAMLUtils.buildSAMLObject(AuthnStatement.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(AuthnStatement.class));
78+
Mockito.when(openSAMLUtils.buildSAMLObject(AttributeStatement.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(AttributeStatement.class));
79+
Mockito.when(openSAMLUtils.buildSAMLObject(Attribute.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(Attribute.class));
80+
Mockito.when(openSAMLUtils.buildSAMLObject(AttributeValue.DEFAULT_ELEMENT_NAME, null)).thenReturn(mock(AttributeValue.class));
81+
82+
X509Certificate cert = mock(X509Certificate.class);
83+
X500Principal x500Principal = mock(X500Principal.class);
84+
when(x500Principal.getName(X500Principal.RFC1779)).thenReturn("CN=subject");
85+
when(cert.getIssuerX500Principal()).thenReturn(x500Principal);
86+
when(x509CertificateUtils.loadCertificate(any())).thenReturn(cert);
87+
SSLData sslData = new SSLData();
88+
sslData.setCert(Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.UTF_8)));
89+
when(x509CertificateUtils.getKeyAndCertificate(any())).thenReturn(sslData);
90+
when(x509CertificateUtils.getPrivateKey(any())).thenReturn(mock(PrivateKey.class));
91+
92+
Assertion response = samlAssertionWriter.buildDefaultAssertion();
93+
assertNotNull(response);
94+
}catch (Exception e){
95+
assertFalse(true);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)