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.core.provisioning.java.data;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.List;
27  import java.util.stream.Collectors;
28  import org.apache.syncope.common.lib.Attr;
29  import org.apache.syncope.common.lib.SyncopeConstants;
30  import org.apache.syncope.common.lib.request.AttrPatch;
31  import org.apache.syncope.common.lib.request.MembershipUR;
32  import org.apache.syncope.common.lib.request.UserUR;
33  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
34  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
35  import org.apache.syncope.core.persistence.api.dao.UserDAO;
36  import org.apache.syncope.core.persistence.api.entity.user.UMembership;
37  import org.apache.syncope.core.persistence.api.entity.user.User;
38  import org.apache.syncope.core.provisioning.api.data.UserDataBinder;
39  import org.apache.syncope.core.provisioning.java.AbstractTest;
40  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
41  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
42  import org.junit.jupiter.api.AfterAll;
43  import org.junit.jupiter.api.BeforeAll;
44  import org.junit.jupiter.api.Test;
45  import org.springframework.beans.factory.annotation.Autowired;
46  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
47  import org.springframework.security.core.GrantedAuthority;
48  import org.springframework.security.core.context.SecurityContextHolder;
49  import org.springframework.transaction.annotation.Transactional;
50  
51  @Transactional("Master")
52  public class UserDataBinderTest extends AbstractTest {
53  
54      @BeforeAll
55      public static void setAuthContext() {
56          List<GrantedAuthority> authorities = IdRepoEntitlement.values().stream().
57                  map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
58                  collect(Collectors.toList());
59  
60          UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
61                  new org.springframework.security.core.userdetails.User(
62                          "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
63          auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
64          SecurityContextHolder.getContext().setAuthentication(auth);
65      }
66  
67      @AfterAll
68      public static void unsetAuthContext() {
69          SecurityContextHolder.getContext().setAuthentication(null);
70      }
71  
72      @Autowired
73      private UserDataBinder dataBinder;
74  
75      @Autowired
76      private UserDAO userDAO;
77  
78      @Test
79      public void membershipWithAttrNotAllowed() {
80          UserUR userUR = new UserUR.Builder("1417acbe-cbf6-4277-9372-e75e04f97000").build();
81  
82          // add 'obscure' to user (no membership): works because 'obscure' is from 'other', default class for USER
83          userUR.getPlainAttrs().
84                  add(new AttrPatch.Builder(new Attr.Builder("obscure").value("testvalue").build()).build());
85  
86          // add 'obscure' to user (via 'artDirector' membership): does not work because 'obscure' is from 'other'
87          // but 'artDirector' defines no type extension
88          userUR.getMemberships().add(new MembershipUR.Builder("ece66293-8f31-4a84-8e8d-23da36e70846").
89                  plainAttr(new Attr.Builder("obscure").value("testvalue2").build()).build());
90  
91          assertThrows(InvalidEntityException.class, () -> dataBinder.update(userDAO.find(userUR.getKey()), userUR));
92      }
93  
94      @Test
95      public void membershipWithAttr() {
96          UserUR userUR = new UserUR.Builder("1417acbe-cbf6-4277-9372-e75e04f97000").build();
97  
98          // add 'obscure' (no membership): works because 'obscure' is from 'other', default class for USER
99          userUR.getPlainAttrs().
100                 add(new AttrPatch.Builder(new Attr.Builder("obscure").value("testvalue").build()).build());
101 
102         // add 'obscure' (via 'additional' membership): that group defines type extension with classes 'other' and 'csv'
103         userUR.getMemberships().add(new MembershipUR.Builder("034740a9-fa10-453b-af37-dc7897e98fb1").
104                 plainAttr(new Attr.Builder("obscure").value("testvalue2").build()).build());
105 
106         dataBinder.update(userDAO.find(userUR.getKey()), userUR);
107 
108         User user = userDAO.find(userUR.getKey());
109         UMembership newM = user.getMembership("034740a9-fa10-453b-af37-dc7897e98fb1").get();
110         assertEquals(1, user.getPlainAttrs(newM).size());
111 
112         assertNull(user.getPlainAttr("obscure").get().getMembership());
113         assertEquals(2, user.getPlainAttrs("obscure").size());
114         assertTrue(user.getPlainAttrs("obscure").contains(user.getPlainAttr("obscure").get()));
115         assertTrue(user.getPlainAttrs("obscure").stream().anyMatch(a -> a.getMembership() == null));
116         assertTrue(user.getPlainAttrs("obscure").stream().anyMatch(a -> newM.equals(a.getMembership())));
117     }
118 }