1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.provisioning.java.pushpull;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.lenient;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.HashSet;
30 import java.util.Set;
31 import org.apache.syncope.common.lib.SyncopeConstants;
32 import org.apache.syncope.common.lib.request.PasswordPatch;
33 import org.apache.syncope.common.lib.request.UserCR;
34 import org.apache.syncope.common.lib.request.UserUR;
35 import org.apache.syncope.common.lib.to.ProvisioningReport;
36 import org.apache.syncope.common.lib.to.UserTO;
37 import org.apache.syncope.common.lib.types.CipherAlgorithm;
38 import org.apache.syncope.common.lib.types.ConnConfPropSchema;
39 import org.apache.syncope.common.lib.types.ConnConfProperty;
40 import org.apache.syncope.core.persistence.api.dao.UserDAO;
41 import org.apache.syncope.core.persistence.api.entity.ConnInstance;
42 import org.apache.syncope.core.persistence.api.entity.user.User;
43 import org.apache.syncope.core.provisioning.api.Connector;
44 import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningProfile;
45 import org.apache.syncope.core.provisioning.java.AbstractTest;
46 import org.identityconnectors.framework.common.objects.SyncDelta;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.mockito.InjectMocks;
50 import org.mockito.Mock;
51 import org.quartz.JobExecutionException;
52 import org.springframework.test.util.ReflectionTestUtils;
53
54 public class DBPasswordPullActionsTest extends AbstractTest {
55
56 @Mock
57 private SyncDelta syncDelta;
58
59 @Mock
60 private ProvisioningProfile<?, ?> profile;
61
62 @Mock
63 private UserDAO userDAO;
64
65 @Mock
66 private ProvisioningReport result;
67
68 @Mock
69 private Connector connector;
70
71 @InjectMocks
72 private DBPasswordPullActions dBPasswordPullActions;
73
74 @Mock
75 private ConnInstance connInstance;
76
77 private Set<ConnConfProperty> connConfProperties;
78
79 private UserTO userTO;
80
81 private UserCR userCR;
82
83 private UserUR userUR;
84
85 private String encodedPassword;
86
87 private CipherAlgorithm cipher;
88
89 private ConnConfProperty connConfProperty;
90
91 @BeforeEach
92 public void initTest() {
93 userTO = new UserTO();
94 encodedPassword = "s3cureP4ssw0rd";
95 cipher = CipherAlgorithm.SHA512;
96 ConnConfPropSchema connConfPropSchema = new ConnConfPropSchema();
97 connConfPropSchema.setName("cipherAlgorithm");
98 connConfProperty = new ConnConfProperty();
99 connConfProperty.setSchema(connConfPropSchema);
100 connConfProperties = new HashSet<>();
101 connConfProperties.add(connConfProperty);
102
103 ReflectionTestUtils.setField(dBPasswordPullActions, "encodedPassword", encodedPassword);
104 ReflectionTestUtils.setField(dBPasswordPullActions, "cipher", cipher);
105
106 lenient().when(profile.getConnector()).thenReturn(connector);
107 lenient().when(connector.getConnInstance()).thenReturn(connInstance);
108 lenient().when(connInstance.getConf()).thenReturn(connConfProperties);
109 }
110
111 @Test
112 public void beforeProvision() throws JobExecutionException {
113 String digest = "SHA256";
114 String password = "t3stPassw0rd";
115 userCR = new UserCR.Builder(SyncopeConstants.ROOT_REALM, "user").password(password).build();
116 connConfProperty.getValues().clear();
117 connConfProperty.getValues().add(digest);
118
119 dBPasswordPullActions.beforeProvision(profile, syncDelta, userCR);
120 userTO.setPassword(password);
121 connConfProperty.getValues().clear();
122 connConfProperty.getValues().add(digest);
123
124 dBPasswordPullActions.beforeProvision(profile, syncDelta, userCR);
125
126 assertEquals(CipherAlgorithm.valueOf(digest), ReflectionTestUtils.getField(dBPasswordPullActions, "cipher"));
127 assertEquals(password, ReflectionTestUtils.getField(dBPasswordPullActions, "encodedPassword"));
128 }
129
130 @Test
131 public void beforeUpdate() throws JobExecutionException {
132 userUR = new UserUR.Builder(null).
133 password(new PasswordPatch.Builder().value("an0therTestP4ss").build()).
134 build();
135
136 dBPasswordPullActions.beforeUpdate(profile, syncDelta, userTO, userUR);
137 userUR = new UserUR();
138 userUR.setPassword(new PasswordPatch.Builder().value("an0therTestP4ss").build());
139
140 dBPasswordPullActions.beforeUpdate(profile, syncDelta, userTO, userUR);
141 assertEquals(cipher, ReflectionTestUtils.getField(dBPasswordPullActions, "cipher"));
142 assertEquals(encodedPassword, ReflectionTestUtils.getField(dBPasswordPullActions, "encodedPassword"));
143 }
144
145 @Test
146 public void after(final @Mock User user) throws JobExecutionException {
147 when(userDAO.find(user.getKey())).thenReturn(user);
148
149 dBPasswordPullActions.after(profile, syncDelta, userTO, result);
150
151 verify(user).setEncodedPassword(anyString(), any(CipherAlgorithm.class));
152 assertNull(ReflectionTestUtils.getField(dBPasswordPullActions, "encodedPassword"));
153 assertNull(ReflectionTestUtils.getField(dBPasswordPullActions, "cipher"));
154 }
155 }