1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.jpa.inner;
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.assertNull;
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.SyncopeConstants;
29 import org.apache.syncope.common.lib.types.CipherAlgorithm;
30 import org.apache.syncope.common.lib.types.IdMEntitlement;
31 import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
32 import org.apache.syncope.core.persistence.api.dao.RealmDAO;
33 import org.apache.syncope.core.persistence.api.dao.UserDAO;
34 import org.apache.syncope.core.persistence.api.entity.user.User;
35 import org.apache.syncope.core.persistence.jpa.AbstractTest;
36 import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
37 import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
38 import org.junit.jupiter.api.AfterAll;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.Tag;
41 import org.junit.jupiter.api.Test;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
44 import org.springframework.security.core.GrantedAuthority;
45 import org.springframework.security.core.context.SecurityContextHolder;
46 import org.springframework.transaction.annotation.Transactional;
47
48 @Transactional("Two")
49 @Tag("multitenancy")
50 public class MultitenancyTest extends AbstractTest {
51
52 @Autowired
53 private PlainSchemaDAO plainSchemaDAO;
54
55 @Autowired
56 private RealmDAO realmDAO;
57
58 @Autowired
59 private UserDAO userDAO;
60
61 @BeforeAll
62 public static void setAuthContext() {
63 List<GrantedAuthority> authorities = IdMEntitlement.values().stream().
64 map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
65 collect(Collectors.toList());
66
67 UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
68 new org.springframework.security.core.userdetails.User(
69 "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
70 auth.setDetails(new SyncopeAuthenticationDetails("Two", null));
71 SecurityContextHolder.getContext().setAuthentication(auth);
72 }
73
74 @AfterAll
75 public static void unsetAuthContext() {
76 SecurityContextHolder.getContext().setAuthentication(null);
77 }
78
79 @Test
80 public void readPlainSchemas() {
81 assertEquals(1, plainSchemaDAO.findAll().size());
82 }
83
84 @Test
85 public void readRealm() {
86 assertEquals(1, realmDAO.findDescendants(realmDAO.getRoot().getFullPath(), null, -1, -1).size());
87 assertEquals(
88 realmDAO.getRoot(),
89 realmDAO.findDescendants(realmDAO.getRoot().getFullPath(), null, -1, -1).get(0));
90 }
91
92 @Test
93 public void createUser() {
94 assertNull(realmDAO.getRoot().getPasswordPolicy());
95 assertTrue(userDAO.findAll(1, 100).isEmpty());
96
97 User user = entityFactory.newEntity(User.class);
98 user.setRealm(realmDAO.getRoot());
99 user.setPassword("password");
100 user.setCipherAlgorithm(CipherAlgorithm.SHA256);
101 user.setUsername("username");
102
103 User actual = userDAO.save(user);
104 assertNotNull(actual);
105 assertEquals(0, actual.getPasswordHistory().size());
106 }
107 }