View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.wa.starter.pac4j.saml;
20  
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  import static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.ArgumentMatchers.anyString;
24  import static org.mockito.Mockito.doNothing;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.nio.charset.StandardCharsets;
29  import org.apache.commons.io.IOUtils;
30  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
31  import org.apache.syncope.common.rest.api.service.SAML2SPEntityService;
32  import org.apache.syncope.wa.bootstrap.WARestClient;
33  import org.junit.jupiter.api.Test;
34  import org.pac4j.saml.client.SAML2Client;
35  import org.springframework.core.io.ClassPathResource;
36  
37  public class WASAML2ClientCustomizerTest extends BaseWASAML2ClientTest {
38  
39      @Test
40      public void customize() throws Exception {
41          SAML2SPEntityTO entityTO = new SAML2SPEntityTO.Builder()
42                  .key("CAS")
43                  .keystore(getKeystoreAsString())
44                  .metadata(IOUtils.toString(new ClassPathResource("sp-metadata.xml").getInputStream(),
45                          StandardCharsets.UTF_8))
46                  .build();
47          SAML2SPEntityService service = mock(SAML2SPEntityService.class);
48          when(service.get(anyString())).thenReturn(entityTO);
49          doNothing().when(service).set(any(SAML2SPEntityTO.class));
50  
51          WARestClient waRestClient = mock(WARestClient.class);
52          when(waRestClient.getService(SAML2SPEntityService.class)).thenReturn(service);
53  
54          WASAML2ClientCustomizer customizer = new WASAML2ClientCustomizer(waRestClient);
55          SAML2Client client = getSAML2Client();
56          customizer.customize(client);
57          client.init();
58          assertTrue(client.getConfiguration().getKeystoreGenerator() instanceof WASAML2ClientKeystoreGenerator);
59          assertTrue(client.getConfiguration().toMetadataGenerator() instanceof WASAML2ClientMetadataGenerator);
60      }
61  }