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.fit.core;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  import java.util.UUID;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.syncope.common.lib.SyncopeClientException;
30  import org.apache.syncope.common.lib.policy.AccessPolicyTO;
31  import org.apache.syncope.common.lib.policy.AuthPolicyTO;
32  import org.apache.syncope.common.lib.to.CASSPClientAppTO;
33  import org.apache.syncope.common.lib.to.OIDCRPClientAppTO;
34  import org.apache.syncope.common.lib.to.SAML2SPClientAppTO;
35  import org.apache.syncope.common.lib.types.ClientAppType;
36  import org.apache.syncope.common.lib.types.PolicyType;
37  import org.apache.syncope.fit.AbstractITCase;
38  import org.junit.jupiter.api.Test;
39  
40  public class ClientAppITCase extends AbstractITCase {
41  
42      @Test
43      public void createSAML2SP() {
44          createClientApp(ClientAppType.SAML2SP, buildSAML2SP());
45      }
46  
47      @Test
48      public void readSAML2SP() {
49          SAML2SPClientAppTO samlSpTO = buildSAML2SP();
50          samlSpTO = createClientApp(ClientAppType.SAML2SP, samlSpTO);
51  
52          SAML2SPClientAppTO found = CLIENT_APP_SERVICE.read(ClientAppType.SAML2SP, samlSpTO.getKey());
53          assertNotNull(found);
54          assertFalse(StringUtils.isBlank(found.getEntityId()));
55          assertFalse(StringUtils.isBlank(found.getMetadataLocation()));
56          assertTrue(found.isEncryptAssertions());
57          assertTrue(found.isEncryptionOptional());
58          assertNotNull(found.getRequiredNameIdFormat());
59          assertNotNull(found.getAccessPolicy());
60          assertNotNull(found.getAuthPolicy());
61      }
62  
63      @Test
64      public void updateSAML2SP() {
65          SAML2SPClientAppTO samlSpTO = buildSAML2SP();
66          samlSpTO = createClientApp(ClientAppType.SAML2SP, samlSpTO);
67  
68          AccessPolicyTO accessPolicyTO = new AccessPolicyTO();
69          accessPolicyTO.setKey("NewAccessPolicyTest_" + getUUIDString());
70          accessPolicyTO.setName("New Access policy");
71          accessPolicyTO = createPolicy(PolicyType.ACCESS, accessPolicyTO);
72          assertNotNull(accessPolicyTO);
73  
74          samlSpTO.setEntityId("newEntityId");
75          samlSpTO.setAccessPolicy(accessPolicyTO.getKey());
76  
77          CLIENT_APP_SERVICE.update(ClientAppType.SAML2SP, samlSpTO);
78          SAML2SPClientAppTO updated = CLIENT_APP_SERVICE.read(ClientAppType.SAML2SP, samlSpTO.getKey());
79  
80          assertNotNull(updated);
81          assertEquals("newEntityId", updated.getEntityId());
82          assertNotNull(updated.getAccessPolicy());
83      }
84  
85      @Test
86      public void deleteSAML2SP() {
87          SAML2SPClientAppTO samlSpTO = buildSAML2SP();
88          samlSpTO = createClientApp(ClientAppType.SAML2SP, samlSpTO);
89  
90          CLIENT_APP_SERVICE.delete(ClientAppType.SAML2SP, samlSpTO.getKey());
91  
92          try {
93              CLIENT_APP_SERVICE.read(ClientAppType.SAML2SP, samlSpTO.getKey());
94              fail("This should not happen");
95          } catch (SyncopeClientException e) {
96              assertNotNull(e);
97          }
98      }
99  
100     @Test
101     public void createOIDCRP() {
102         createClientApp(ClientAppType.OIDCRP, buildOIDCRP());
103     }
104 
105     @Test
106     public void createCASSP() {
107         createClientApp(ClientAppType.CASSP, buildCASSP());
108     }
109 
110     @Test
111     public void readOIDCRP() {
112         OIDCRPClientAppTO oidcrpTO = buildOIDCRP();
113         oidcrpTO = createClientApp(ClientAppType.OIDCRP, oidcrpTO);
114 
115         OIDCRPClientAppTO found = CLIENT_APP_SERVICE.read(ClientAppType.OIDCRP, oidcrpTO.getKey());
116         assertNotNull(found);
117         assertFalse(StringUtils.isBlank(found.getClientId()));
118         assertFalse(StringUtils.isBlank(found.getClientSecret()));
119         assertNotNull(found.getSubjectType());
120         assertFalse(found.getSupportedGrantTypes().isEmpty());
121         assertFalse(found.getSupportedResponseTypes().isEmpty());
122         assertNotNull(found.getAccessPolicy());
123         assertNotNull(found.getAuthPolicy());
124     }
125 
126     @Test
127     public void readCASSP() {
128         CASSPClientAppTO casspTO = buildCASSP();
129         casspTO = createClientApp(ClientAppType.CASSP, casspTO);
130         CASSPClientAppTO found = CLIENT_APP_SERVICE.read(ClientAppType.CASSP, casspTO.getKey());
131         assertNotNull(found);
132         assertNotNull(found.getServiceId());
133         assertNotNull(found.getAccessPolicy());
134         assertNotNull(found.getAuthPolicy());
135     }
136 
137     @Test
138     public void updateOIDCRP() {
139         OIDCRPClientAppTO oidcrpTO = buildOIDCRP();
140         oidcrpTO = createClientApp(ClientAppType.OIDCRP, oidcrpTO);
141 
142         AccessPolicyTO accessPolicyTO = new AccessPolicyTO();
143         accessPolicyTO.setKey("NewAccessPolicyTest_" + getUUIDString());
144         accessPolicyTO.setName("New Access policy");
145         accessPolicyTO = createPolicy(PolicyType.ACCESS, accessPolicyTO);
146         assertNotNull(accessPolicyTO);
147 
148         oidcrpTO.setClientId("newClientId");
149         oidcrpTO.setAccessPolicy(accessPolicyTO.getKey());
150 
151         CLIENT_APP_SERVICE.update(ClientAppType.OIDCRP, oidcrpTO);
152         OIDCRPClientAppTO updated = CLIENT_APP_SERVICE.read(ClientAppType.OIDCRP, oidcrpTO.getKey());
153 
154         assertNotNull(updated);
155         assertEquals("newClientId", updated.getClientId());
156         assertNotNull(updated.getAccessPolicy());
157     }
158 
159     @Test
160     public void deleteOIDCRP() {
161         OIDCRPClientAppTO oidcrpTO = buildOIDCRP();
162         oidcrpTO = createClientApp(ClientAppType.OIDCRP, oidcrpTO);
163 
164         CLIENT_APP_SERVICE.delete(ClientAppType.OIDCRP, oidcrpTO.getKey());
165 
166         try {
167             CLIENT_APP_SERVICE.read(ClientAppType.OIDCRP, oidcrpTO.getKey());
168             fail("This should not happen");
169         } catch (SyncopeClientException e) {
170             assertNotNull(e);
171         }
172     }
173 
174     @Test
175     public void deleteCASSP() {
176         CASSPClientAppTO casspTO = buildCASSP();
177         casspTO = createClientApp(ClientAppType.CASSP, casspTO);
178 
179         CLIENT_APP_SERVICE.delete(ClientAppType.CASSP, casspTO.getKey());
180 
181         try {
182             CLIENT_APP_SERVICE.read(ClientAppType.CASSP, casspTO.getKey());
183             fail("This should not happen");
184         } catch (SyncopeClientException e) {
185             assertNotNull(e);
186         }
187     }
188 
189     private CASSPClientAppTO buildCASSP() {
190         AuthPolicyTO authPolicyTO = new AuthPolicyTO();
191         authPolicyTO.setKey("AuthPolicyTest_" + getUUIDString());
192         authPolicyTO.setName("Authentication Policy");
193         authPolicyTO = createPolicy(PolicyType.AUTH, authPolicyTO);
194         assertNotNull(authPolicyTO);
195 
196         AccessPolicyTO accessPolicyTO = new AccessPolicyTO();
197         accessPolicyTO.setKey("AccessPolicyTest_" + getUUIDString());
198         accessPolicyTO.setName("Access policy");
199         accessPolicyTO = createPolicy(PolicyType.ACCESS, accessPolicyTO);
200         assertNotNull(accessPolicyTO);
201 
202         CASSPClientAppTO casspTO = new CASSPClientAppTO();
203         casspTO.setName("ExampleRP_" + getUUIDString());
204         casspTO.setClientAppId(UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE);
205         casspTO.setDescription("Example OIDC RP application");
206         casspTO.setServiceId("https://cassp.example.org/" + UUID.randomUUID().getMostSignificantBits());
207 
208         casspTO.setAuthPolicy(authPolicyTO.getKey());
209         casspTO.setAccessPolicy(accessPolicyTO.getKey());
210         return casspTO;
211     }
212 }