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.console;
20  
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  
23  import com.giffing.wicket.spring.boot.context.extensions.WicketApplicationInitConfiguration;
24  import com.giffing.wicket.spring.boot.context.extensions.boot.actuator.WicketEndpointRepository;
25  import com.giffing.wicket.spring.boot.starter.app.classscanner.candidates.WicketClassCandidatesHolder;
26  import com.giffing.wicket.spring.boot.starter.configuration.extensions.core.settings.general.GeneralSettingsProperties;
27  import com.giffing.wicket.spring.boot.starter.configuration.extensions.external.spring.boot.actuator.WicketEndpointRepositoryDefault;
28  import java.io.InputStream;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Properties;
32  import java.util.Set;
33  import org.apache.syncope.client.console.AMConsoleContext;
34  import org.apache.syncope.client.console.ConsoleProperties;
35  import org.apache.syncope.client.console.FlowableConsoleContext;
36  import org.apache.syncope.client.console.IdMConsoleContext;
37  import org.apache.syncope.client.console.IdRepoConsoleContext;
38  import org.apache.syncope.client.console.OIDCC4UIConsoleContext;
39  import org.apache.syncope.client.console.SAML2SP4UIConsoleContext;
40  import org.apache.syncope.client.console.SyncopeWebApplication;
41  import org.apache.syncope.client.console.commons.IdRepoPolicyTabProvider;
42  import org.apache.syncope.client.console.commons.PolicyTabProvider;
43  import org.apache.syncope.client.console.init.ClassPathScanImplementationLookup;
44  import org.apache.syncope.client.console.pages.Login;
45  import org.apache.syncope.client.console.rest.PolicyRestClient;
46  import org.apache.syncope.client.console.topology.Topology;
47  import org.apache.syncope.client.lib.SyncopeClientFactoryBean;
48  import org.apache.syncope.common.keymaster.client.self.SelfKeymasterClientContext;
49  import org.apache.syncope.common.keymaster.client.zookeeper.ZookeeperKeymasterClientContext;
50  import org.apache.syncope.common.rest.api.service.SyncopeService;
51  import org.apache.syncope.fit.AbstractUIITCase;
52  import org.apache.wicket.util.tester.FormTester;
53  import org.apache.wicket.util.tester.WicketTester;
54  import org.junit.jupiter.api.BeforeAll;
55  import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
56  import org.springframework.boot.logging.LoggingSystem;
57  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
58  import org.springframework.context.annotation.Bean;
59  import org.springframework.context.annotation.Configuration;
60  import org.springframework.test.context.support.TestPropertySourceUtils;
61  
62  public abstract class AbstractConsoleITCase extends AbstractUIITCase {
63  
64      @ImportAutoConfiguration(classes = {
65          SelfKeymasterClientContext.class,
66          ZookeeperKeymasterClientContext.class,
67          IdRepoConsoleContext.class,
68          FlowableConsoleContext.class,
69          SAML2SP4UIConsoleContext.class,
70          OIDCC4UIConsoleContext.class })
71      @Configuration(proxyBeanMethods = false)
72      public static class SyncopeConsoleWebApplicationTestConfig {
73  
74          @Bean
75          public ConsoleProperties consoleProperties() {
76              ConsoleProperties consoleProperties = new ConsoleProperties();
77  
78              consoleProperties.setAnonymousUser(ANONYMOUS_UNAME);
79              consoleProperties.setAnonymousKey(ANONYMOUS_KEY);
80  
81              consoleProperties.setCsrf(false);
82              consoleProperties.getPage().put("topology", Topology.class);
83  
84              return consoleProperties;
85          }
86  
87          @Bean
88          public GeneralSettingsProperties generalSettingsProperties() {
89              return new GeneralSettingsProperties();
90          }
91  
92          @Bean
93          public List<WicketApplicationInitConfiguration> configurations() {
94              return List.of();
95          }
96  
97          @Bean
98          public WicketClassCandidatesHolder wicketClassCandidatesHolder() {
99              return new WicketClassCandidatesHolder();
100         }
101 
102         @Bean
103         public WicketEndpointRepository wicketEndpointRepository() {
104             return new WicketEndpointRepositoryDefault();
105         }
106 
107         @Bean
108         public ClassPathScanImplementationLookup classPathScanImplementationLookup() {
109             ClassPathScanImplementationLookup lookup = new ClassPathScanImplementationLookup(Set.of(),
110                     consoleProperties());
111             lookup.load();
112             return lookup;
113         }
114 
115         @Bean
116         public PolicyTabProvider idRepoPolicyTabProvider(final PolicyRestClient policyRestClient) {
117             return new IdRepoPolicyTabProvider(policyRestClient);
118         }
119 
120         @Bean
121         public LoggingSystem loggingSystem() {
122             System.setProperty(LoggingSystem.SYSTEM_PROPERTY, LoggingSystem.NONE);
123             return LoggingSystem.get(getClass().getClassLoader());
124         }
125     }
126 
127     @BeforeAll
128     public static void setUp() {
129         Locale.setDefault(Locale.ENGLISH);
130 
131         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
132 
133         ctx.register(SyncopeConsoleWebApplicationTestConfig.class);
134         ctx.register(SyncopeWebApplication.class);
135         ctx.register(AMConsoleContext.class);
136         ctx.register(IdMConsoleContext.class);
137 
138         String springActiveProfiles = null;
139         try (InputStream propStream = AbstractConsoleITCase.class.getResourceAsStream("/test.properties")) {
140             Properties props = new Properties();
141             props.load(propStream);
142 
143             springActiveProfiles = props.getProperty("springActiveProfiles");
144         } catch (Exception e) {
145             LOG.error("Could not load /test.properties", e);
146         }
147         assertNotNull(springActiveProfiles);
148 
149         if (springActiveProfiles.contains("zookeeper")) {
150             TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
151                     ctx, "keymaster.address=127.0.0.1:2181");
152         } else {
153             TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
154                     ctx, "keymaster.address=http://localhost:9080/syncope/rest/keymaster");
155         }
156         TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
157                 ctx, "keymaster.username=" + ANONYMOUS_UNAME);
158         TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
159                 ctx, "keymaster.password=" + ANONYMOUS_KEY);
160 
161         ctx.refresh();
162 
163         TESTER = new WicketTester(ctx.getBean(SyncopeWebApplication.class));
164 
165         SYNCOPE_SERVICE = new SyncopeClientFactoryBean().
166                 setAddress(ADDRESS).create(ADMIN_UNAME, ADMIN_PWD).
167                 getService(SyncopeService.class);
168     }
169 
170     protected void doLogin(final String user, final String passwd) {
171         TESTER.startPage(Login.class);
172         TESTER.assertRenderedPage(Login.class);
173 
174         FormTester formTester = TESTER.newFormTester("login");
175         formTester.setValue("username", user);
176         formTester.setValue("password", passwd);
177         formTester.submit("submit");
178     }
179 }