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.ui;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.io.IOException;
26  import javax.ws.rs.core.Response;
27  import org.apache.syncope.common.lib.SyncopeClientException;
28  import org.apache.syncope.common.lib.policy.AttrReleasePolicyTO;
29  import org.apache.syncope.common.lib.policy.AuthPolicyTO;
30  import org.apache.syncope.common.lib.policy.DefaultAttrReleasePolicyConf;
31  import org.apache.syncope.common.lib.policy.DefaultAuthPolicyConf;
32  import org.apache.syncope.common.lib.types.ClientExceptionType;
33  import org.apache.syncope.common.lib.types.PolicyType;
34  import org.apache.syncope.common.rest.api.RESTHeaders;
35  import org.apache.syncope.fit.AbstractITCase;
36  import org.junit.jupiter.api.Test;
37  
38  public abstract class AbstractUIITCase extends AbstractITCase {
39  
40      protected static AuthPolicyTO getAuthPolicy() {
41          String syncopeAuthModule = "DefaultSyncopeAuthModule";
42          String ldapAuthModule = "DefaultLDAPAuthModule";
43          String description = "UI auth policy";
44  
45          return POLICY_SERVICE.list(PolicyType.AUTH).stream().
46                  map(AuthPolicyTO.class::cast).
47                  filter(policy -> description.equals(policy.getName())
48                  && policy.getConf() instanceof DefaultAuthPolicyConf
49                  && ((DefaultAuthPolicyConf) policy.getConf()).getAuthModules().contains(syncopeAuthModule)
50                  && ((DefaultAuthPolicyConf) policy.getConf()).getAuthModules().contains(ldapAuthModule)).
51                  findFirst().
52                  orElseGet(() -> {
53                      DefaultAuthPolicyConf policyConf = new DefaultAuthPolicyConf();
54                      policyConf.getAuthModules().add(syncopeAuthModule);
55                      policyConf.getAuthModules().add(ldapAuthModule);
56  
57                      AuthPolicyTO policy = new AuthPolicyTO();
58                      policy.setName(description);
59                      policy.setConf(policyConf);
60  
61                      Response response = POLICY_SERVICE.create(PolicyType.AUTH, policy);
62                      if (response.getStatusInfo().getStatusCode() != Response.Status.CREATED.getStatusCode()) {
63                          fail("Could not create Test Auth Policy");
64                      }
65  
66                      return POLICY_SERVICE.read(
67                              PolicyType.AUTH,
68                              response.getHeaderString(RESTHeaders.RESOURCE_KEY));
69                  });
70      }
71  
72      protected static AttrReleasePolicyTO getAttrReleasePolicy() {
73          String stubAttrRepo = "DefaultStubAttrRepo";
74          String description = "UI attr release policy";
75  
76          return POLICY_SERVICE.list(PolicyType.ATTR_RELEASE).stream().
77                  map(AttrReleasePolicyTO.class::cast).
78                  filter(policy -> description.equals(policy.getName())
79                  && policy.getConf() instanceof DefaultAttrReleasePolicyConf
80                  && ((DefaultAttrReleasePolicyConf) policy.getConf()).getPrincipalAttrRepoConf().
81                          getAttrRepos().contains(stubAttrRepo)).
82                  findFirst().
83                  orElseGet(() -> {
84                      DefaultAttrReleasePolicyConf policyConf = new DefaultAttrReleasePolicyConf();
85                      policyConf.getPrincipalAttrRepoConf().getAttrRepos().add(stubAttrRepo);
86                      policyConf.getReleaseAttrs().put("attr1", "identifier");
87                      policyConf.getReleaseAttrs().put("firstname", "given_name");
88                      policyConf.getReleaseAttrs().put("surname", "family_name");
89                      policyConf.getReleaseAttrs().put("fullname", "name");
90                      policyConf.getReleaseAttrs().put("email", "email");
91  
92                      AttrReleasePolicyTO policy = new AttrReleasePolicyTO();
93                      policy.setName(description);
94                      policy.setConf(policyConf);
95  
96                      Response response = POLICY_SERVICE.create(PolicyType.ATTR_RELEASE, policy);
97                      if (response.getStatusInfo().getStatusCode() != Response.Status.CREATED.getStatusCode()) {
98                          fail("Could not create Test Attr Release Policy");
99                      }
100 
101                     return POLICY_SERVICE.read(
102                             PolicyType.ATTR_RELEASE,
103                             response.getHeaderString(RESTHeaders.RESOURCE_KEY));
104                 });
105     }
106 
107     protected abstract void sso(String baseURL, String username, String password) throws IOException;
108 
109     @Test
110     public void sso2Console() throws IOException {
111         sso(CONSOLE_ADDRESS, "bellini", "password");
112     }
113 
114     @Test
115     public void sso2Enduser() throws IOException {
116         sso(ENDUSER_ADDRESS, "bellini", "password");
117     }
118 
119     @Test
120     public void createUnmatching() throws IOException {
121         try {
122             USER_SERVICE.delete("pullFromLDAP");
123         } catch (SyncopeClientException e) {
124             assertEquals(ClientExceptionType.NotFound, e.getType());
125         }
126 
127         sso(CONSOLE_ADDRESS, "pullFromLDAP", "Password123");
128 
129         assertNotNull(USER_SERVICE.read("pullFromLDAP"));
130     }
131 
132     protected abstract void doSelfReg(Runnable runnable);
133 
134     @Test
135     public void selfRegUnmatching() throws IOException {
136         try {
137             USER_SERVICE.delete("pullFromLDAP");
138         } catch (SyncopeClientException e) {
139             assertEquals(ClientExceptionType.NotFound, e.getType());
140         }
141 
142         doSelfReg(() -> {
143             try {
144                 sso(ENDUSER_ADDRESS, "pullFromLDAP", "Password123");
145             } catch (IOException e) {
146                 fail(e);
147             }
148         });
149 
150         try {
151             USER_SERVICE.read("pullFromLDAP");
152             fail();
153         } catch (SyncopeClientException e) {
154             assertEquals(ClientExceptionType.NotFound, e.getType());
155         }
156     }
157 }