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.surrogate;
20  
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.Optional;
25  import org.apache.syncope.common.lib.wa.ImpersonationAccount;
26  import org.apache.syncope.common.rest.api.service.wa.ImpersonationService;
27  import org.apache.syncope.wa.bootstrap.WARestClient;
28  import org.apache.syncope.wa.starter.AbstractTest;
29  import org.apereo.cas.authentication.principal.Principal;
30  import org.apereo.cas.authentication.principal.PrincipalFactoryUtils;
31  import org.apereo.cas.authentication.surrogate.SurrogateAuthenticationService;
32  import org.junit.jupiter.api.Test;
33  import org.springframework.beans.factory.annotation.Autowired;
34  
35  public class WASurrogateAuthenticationServiceTest extends AbstractTest {
36  
37      @Autowired
38      private WARestClient waRestClient;
39  
40      @Autowired
41      private SurrogateAuthenticationService surrogateService;
42  
43      @Test
44      public void verifyImpersonation() {
45          String owner = "syncope-principal";
46          ImpersonationAccount account = new ImpersonationAccount.Builder().
47                  impersonated("impersonatee").build();
48  
49          ImpersonationService impersonationService = waRestClient.getService(ImpersonationService.class);
50  
51          impersonationService.create(owner, account);
52  
53          assertFalse(surrogateService.getImpersonationAccounts(owner).isEmpty());
54  
55          Principal principal = PrincipalFactoryUtils.newPrincipalFactory().createPrincipal(owner);
56          assertFalse(surrogateService.canImpersonate("unknown", principal, Optional.empty()));
57          assertTrue(surrogateService.canImpersonate(account.getImpersonated(), principal, Optional.empty()));
58      }
59  }