1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
|
13
|
import static org.junit.Assert.assertFalse;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.List;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import javax.sql.DataSource;
|
23
|
|
24
|
import org.apache.log4j.Logger;
|
25
|
import org.joda.time.DateTime;
|
26
|
import org.junit.Assert;
|
27
|
import org.junit.Before;
|
28
|
import org.junit.Ignore;
|
29
|
import org.junit.Test;
|
30
|
import org.junit.runner.RunWith;
|
31
|
|
32
|
|
33
|
import org.springframework.beans.factory.annotation.Autowired;
|
34
|
import org.springframework.beans.factory.annotation.Qualifier;
|
35
|
import org.springframework.security.access.PermissionEvaluator;
|
36
|
import org.springframework.security.authentication.AuthenticationManager;
|
37
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
38
|
import org.springframework.security.core.Authentication;
|
39
|
import org.springframework.security.core.GrantedAuthority;
|
40
|
import org.springframework.security.core.context.SecurityContext;
|
41
|
import org.springframework.security.core.context.SecurityContextHolder;
|
42
|
import org.springframework.test.context.ContextConfiguration;
|
43
|
|
44
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
45
|
import org.springframework.test.context.transaction.TransactionConfiguration;
|
46
|
import org.springframework.transaction.PlatformTransactionManager;
|
47
|
import org.springframework.transaction.annotation.Transactional;
|
48
|
|
49
|
import org.unitils.database.annotations.TestDataSource;
|
50
|
import org.unitils.dbunit.annotation.DataSet;
|
51
|
import org.unitils.spring.annotation.SpringBean;
|
52
|
import org.unitils.spring.annotation.SpringBeanByName;
|
53
|
import org.unitils.spring.annotation.SpringBeanByType;
|
54
|
|
55
|
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
|
56
|
import eu.etaxonomy.cdm.model.common.Group;
|
57
|
import eu.etaxonomy.cdm.model.common.User;
|
58
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
59
|
import eu.etaxonomy.cdm.model.name.Rank;
|
60
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
61
|
import eu.etaxonomy.cdm.permission.CdmPermission;
|
62
|
import eu.etaxonomy.cdm.permission.CdmPermissionEvaluator;
|
63
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
64
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
65
|
|
66
|
|
67
|
@RunWith(SpringJUnit4ClassRunner.class)
|
68
|
@ContextConfiguration({"/eu/etaxonomy/cdm/applicationContextSecurity.xml"})
|
69
|
@Transactional
|
70
|
@Ignore
|
71
|
public class UserServiceImplTest {
|
72
|
protected static final Logger logger = Logger.getLogger(UserServiceImplTest.class);
|
73
|
@Autowired
|
74
|
private AuthenticationManager authenticationManager;
|
75
|
|
76
|
@Autowired
|
77
|
private IUserService userService;
|
78
|
|
79
|
@Autowired
|
80
|
private IGroupService groupService;
|
81
|
|
82
|
@Autowired
|
83
|
private ITaxonService taxonService;
|
84
|
|
85
|
|
86
|
@Autowired
|
87
|
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
|
88
|
this.dataSource=dataSource;
|
89
|
}
|
90
|
|
91
|
|
92
|
|
93
|
@TestDataSource
|
94
|
protected DataSource dataSource;
|
95
|
|
96
|
private Set<GrantedAuthority> expectedRoles;
|
97
|
private UsernamePasswordAuthenticationToken token;
|
98
|
|
99
|
private Authentication authentication;
|
100
|
|
101
|
private PermissionEvaluator permissionEvaluator;
|
102
|
UUID uuid;
|
103
|
|
104
|
@Before
|
105
|
public void setUp() {
|
106
|
|
107
|
|
108
|
expectedRoles = new HashSet<GrantedAuthority>();
|
109
|
|
110
|
GrantedAuthorityImpl update = GrantedAuthorityImpl.NewInstance();
|
111
|
update.setAuthority("USER.Update");
|
112
|
update.setUuid(UUID.fromString("14788361-1a7e-4eed-b22f-fd90a0b424ac"));
|
113
|
update.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
114
|
GrantedAuthorityImpl annotate = GrantedAuthorityImpl.NewInstance();
|
115
|
annotate.setAuthority("USER.Create");
|
116
|
annotate.setUuid(UUID.fromString("fa56073c-0ffd-4384-b459-b2f07e35b689"));
|
117
|
annotate.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
118
|
GrantedAuthorityImpl checkAnnotation = GrantedAuthorityImpl.NewInstance();
|
119
|
checkAnnotation.setAuthority("USER.Delete");
|
120
|
checkAnnotation.setUuid(UUID.fromString("e5354c0e-657b-4b4d-bb2f-791612199711"));
|
121
|
checkAnnotation.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
122
|
GrantedAuthorityImpl userAdmin = GrantedAuthorityImpl.NewInstance();
|
123
|
userAdmin.setAuthority("USER.Admin");
|
124
|
|
125
|
expectedRoles.add(update);
|
126
|
expectedRoles.add(annotate);
|
127
|
expectedRoles.add(checkAnnotation);
|
128
|
expectedRoles.add(userAdmin);
|
129
|
String username = "useradmin";
|
130
|
String password = "password";
|
131
|
User user = User.NewInstance(username, password);
|
132
|
user.setAccountNonExpired(true);
|
133
|
user.setGrantedAuthorities(expectedRoles);
|
134
|
uuid = userService.save(user);
|
135
|
|
136
|
User standardUser = User.NewInstance("standardUser", "pw");
|
137
|
uuid = userService.save(standardUser);
|
138
|
|
139
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
140
|
authentication = authenticationManager.authenticate(token);
|
141
|
SecurityContext context = SecurityContextHolder.getContext();
|
142
|
context.setAuthentication(authentication);
|
143
|
permissionEvaluator = new CdmPermissionEvaluator();
|
144
|
|
145
|
}
|
146
|
|
147
|
|
148
|
@Test
|
149
|
@DataSet
|
150
|
public void testCreateUser() {
|
151
|
|
152
|
|
153
|
|
154
|
String username = "user2";
|
155
|
String password = "password";
|
156
|
User user = User.NewInstance(username, password);
|
157
|
|
158
|
userService.createUser(user);
|
159
|
|
160
|
List<User> userList = userService.listByUsername("user2", MatchMode.EXACT, null, null, null, null, null);
|
161
|
Assert.assertNotNull(userList);
|
162
|
|
163
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("standardUser", "pw");
|
164
|
authentication = authenticationManager.authenticate(token);
|
165
|
SecurityContext context = SecurityContextHolder.getContext();
|
166
|
context.setAuthentication(authentication);
|
167
|
try{
|
168
|
userService.createUser(user);
|
169
|
Assert.fail();
|
170
|
}catch(Exception e){
|
171
|
Assert.assertEquals("Access is denied", e.getMessage());
|
172
|
}
|
173
|
|
174
|
|
175
|
}
|
176
|
|
177
|
|
178
|
|
179
|
@Test
|
180
|
public void testUpdateUser(){
|
181
|
User user= userService.find(uuid);
|
182
|
user.setEmailAddress("test@bgbm.org");
|
183
|
try{
|
184
|
userService.updateUser(user);
|
185
|
}catch (Exception e){
|
186
|
Assert.fail();
|
187
|
}
|
188
|
|
189
|
try{
|
190
|
userService.update(user);
|
191
|
}catch (Exception e){
|
192
|
Assert.fail();
|
193
|
}
|
194
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("standardUser", "pw");
|
195
|
authentication = authenticationManager.authenticate(token);
|
196
|
SecurityContext context = SecurityContextHolder.getContext();
|
197
|
context.setAuthentication(authentication);
|
198
|
user.setEmailAddress("user@bgbm.org");
|
199
|
try{
|
200
|
userService.updateUser(user);
|
201
|
Assert.fail();
|
202
|
}catch (Exception e){
|
203
|
Assert.assertEquals("Access is denied", e.getMessage());
|
204
|
}
|
205
|
|
206
|
try{
|
207
|
userService.saveOrUpdate(user);
|
208
|
Assert.fail();
|
209
|
}catch (Exception e){
|
210
|
Assert.assertEquals("Access is denied", e.getMessage());
|
211
|
}
|
212
|
try{
|
213
|
userService.update(user);
|
214
|
Assert.fail();
|
215
|
}catch (Exception e){
|
216
|
Assert.assertEquals("Access is denied", e.getMessage());
|
217
|
}
|
218
|
|
219
|
}
|
220
|
|
221
|
@Test
|
222
|
@DataSet
|
223
|
public void testIfAnyGranted() {
|
224
|
|
225
|
|
226
|
Object p = authentication.getPrincipal();
|
227
|
Assert.assertTrue(p instanceof User);
|
228
|
User principal = (User)p;
|
229
|
|
230
|
Assert.assertEquals(principal.getUsername(),"useradmin");
|
231
|
|
232
|
Assert.assertNotNull(expectedRoles);
|
233
|
Assert.assertEquals(expectedRoles.size(), authentication.getAuthorities().size());
|
234
|
}
|
235
|
|
236
|
|
237
|
@Test
|
238
|
@DataSet
|
239
|
|
240
|
public void testCreateGroup(){
|
241
|
|
242
|
|
243
|
|
244
|
GrantedAuthorityImpl userAdminUpdate = GrantedAuthorityImpl.NewInstance();
|
245
|
userAdminUpdate.setAuthority("USER.update");
|
246
|
GrantedAuthorityImpl userAdminCreate = GrantedAuthorityImpl.NewInstance();
|
247
|
userAdminCreate.setAuthority("USER.create");
|
248
|
GrantedAuthorityImpl userAdminDelete = GrantedAuthorityImpl.NewInstance();
|
249
|
userAdminDelete.setAuthority("USER.delete");
|
250
|
List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
|
251
|
list.add(userAdminUpdate);
|
252
|
list.add(userAdminDelete);
|
253
|
list.add(userAdminCreate);
|
254
|
|
255
|
|
256
|
userService.createGroup("UserAdmins", list);
|
257
|
String username = "user3";
|
258
|
String password = "password";
|
259
|
User user = User.NewInstance(username, password);
|
260
|
userService.createUser(user);
|
261
|
List<String> groups = userService.findAllGroups();
|
262
|
Assert.assertEquals("UserAdmins", groups.get(0));
|
263
|
|
264
|
|
265
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
266
|
authentication = authenticationManager.authenticate(token);
|
267
|
SecurityContext context = SecurityContextHolder.getContext();
|
268
|
context.setAuthentication(authentication);
|
269
|
|
270
|
username = "user4";
|
271
|
password = "password";
|
272
|
user = User.NewInstance(username, password);
|
273
|
try{
|
274
|
userService.createUser(user);
|
275
|
Assert.fail();
|
276
|
}catch(Exception e){
|
277
|
Assert.assertEquals("Access is denied", e.getMessage());
|
278
|
}
|
279
|
groupService.addUserToGroup("user3", "UserAdmins");
|
280
|
|
281
|
System.err.println(context.getAuthentication().getName());
|
282
|
try{
|
283
|
userService.createUser(user);
|
284
|
}catch(Exception e){
|
285
|
System.err.println(e.getMessage());
|
286
|
Assert.fail();
|
287
|
}
|
288
|
|
289
|
}
|
290
|
|
291
|
|
292
|
@Test
|
293
|
public void testHasRole(){
|
294
|
String username = "useradmin";
|
295
|
String newPassword = "password2";
|
296
|
userService.changePasswordForUser(username, newPassword);
|
297
|
username = "user4";
|
298
|
String password = "password";
|
299
|
User user = User.NewInstance(username, password);
|
300
|
userService.createUser(user);
|
301
|
try{
|
302
|
userService.changePasswordForUser(username, "newPassword");
|
303
|
}catch (Exception e){
|
304
|
System.err.println(e.getMessage());
|
305
|
Assert.fail();
|
306
|
}
|
307
|
|
308
|
|
309
|
|
310
|
}
|
311
|
|
312
|
|
313
|
@Test
|
314
|
|
315
|
public void testHasPermission(){
|
316
|
Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()),null);
|
317
|
boolean hasPermission = taxonService.hasPermission(authentication, taxon, CdmPermission.UPDATE);
|
318
|
assertFalse(hasPermission);
|
319
|
User testUser = User.NewInstance("username123", "1234");
|
320
|
hasPermission = userService.hasPermission(authentication, testUser, CdmPermission.UPDATE);
|
321
|
assertTrue(hasPermission);
|
322
|
}
|
323
|
|
324
|
|
325
|
|
326
|
|
327
|
}
|