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
|
public class UserServiceImplTest {
|
71
|
protected static final Logger logger = Logger.getLogger(UserServiceImplTest.class);
|
72
|
@Autowired
|
73
|
private AuthenticationManager authenticationManager;
|
74
|
|
75
|
@Autowired
|
76
|
private IUserService userService;
|
77
|
|
78
|
@Autowired
|
79
|
private IGroupService groupService;
|
80
|
|
81
|
@Autowired
|
82
|
private ITaxonService taxonService;
|
83
|
|
84
|
|
85
|
@Autowired
|
86
|
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
|
87
|
this.dataSource=dataSource;
|
88
|
}
|
89
|
|
90
|
|
91
|
|
92
|
@TestDataSource
|
93
|
protected DataSource dataSource;
|
94
|
|
95
|
private Set<GrantedAuthority> expectedRoles;
|
96
|
private UsernamePasswordAuthenticationToken token;
|
97
|
|
98
|
private Authentication authentication;
|
99
|
|
100
|
private PermissionEvaluator permissionEvaluator;
|
101
|
|
102
|
@Before
|
103
|
public void setUp() {
|
104
|
|
105
|
|
106
|
expectedRoles = new HashSet<GrantedAuthority>();
|
107
|
|
108
|
GrantedAuthorityImpl update = GrantedAuthorityImpl.NewInstance();
|
109
|
update.setAuthority("USER.Update");
|
110
|
update.setUuid(UUID.fromString("14788361-1a7e-4eed-b22f-fd90a0b424ac"));
|
111
|
update.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
112
|
GrantedAuthorityImpl annotate = GrantedAuthorityImpl.NewInstance();
|
113
|
annotate.setAuthority("USER.Create");
|
114
|
annotate.setUuid(UUID.fromString("fa56073c-0ffd-4384-b459-b2f07e35b689"));
|
115
|
annotate.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
116
|
GrantedAuthorityImpl checkAnnotation = GrantedAuthorityImpl.NewInstance();
|
117
|
checkAnnotation.setAuthority("USER.Delete");
|
118
|
checkAnnotation.setUuid(UUID.fromString("e5354c0e-657b-4b4d-bb2f-791612199711"));
|
119
|
checkAnnotation.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
120
|
GrantedAuthorityImpl userAdmin = GrantedAuthorityImpl.NewInstance();
|
121
|
userAdmin.setAuthority("USER.Admin");
|
122
|
|
123
|
expectedRoles.add(update);
|
124
|
expectedRoles.add(annotate);
|
125
|
expectedRoles.add(checkAnnotation);
|
126
|
expectedRoles.add(userAdmin);
|
127
|
String username = "useradmin";
|
128
|
String password = "password";
|
129
|
User user = User.NewInstance(username, password);
|
130
|
user.setAccountNonExpired(true);
|
131
|
user.setGrantedAuthorities(expectedRoles);
|
132
|
userService.save(user);
|
133
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
134
|
authentication = authenticationManager.authenticate(token);
|
135
|
SecurityContext context = SecurityContextHolder.getContext();
|
136
|
context.setAuthentication(authentication);
|
137
|
permissionEvaluator = new CdmPermissionEvaluator();
|
138
|
|
139
|
}
|
140
|
|
141
|
|
142
|
@Test
|
143
|
@DataSet
|
144
|
public void testCreateUser() {
|
145
|
|
146
|
|
147
|
|
148
|
String username = "user2";
|
149
|
String password = "password";
|
150
|
User user = User.NewInstance(username, password);
|
151
|
|
152
|
userService.createUser(user);
|
153
|
|
154
|
List<User> userList = userService.listByUsername("user2", MatchMode.EXACT, null, null, null, null, null);
|
155
|
Assert.assertNotNull(userList);
|
156
|
|
157
|
|
158
|
|
159
|
|
160
|
|
161
|
}
|
162
|
|
163
|
@Test
|
164
|
@DataSet
|
165
|
public void testIfAnyGranted() {
|
166
|
|
167
|
|
168
|
Object p = authentication.getPrincipal();
|
169
|
Assert.assertTrue(p instanceof User);
|
170
|
User principal = (User)p;
|
171
|
|
172
|
Assert.assertEquals(principal.getUsername(),"useradmin");
|
173
|
|
174
|
Assert.assertNotNull(expectedRoles);
|
175
|
Assert.assertEquals(expectedRoles.size(), authentication.getAuthorities().size());
|
176
|
}
|
177
|
|
178
|
|
179
|
@Test
|
180
|
@DataSet
|
181
|
|
182
|
public void testCreateGroup(){
|
183
|
|
184
|
|
185
|
|
186
|
GrantedAuthorityImpl userAdminUpdate = GrantedAuthorityImpl.NewInstance();
|
187
|
userAdminUpdate.setAuthority("USER.update");
|
188
|
GrantedAuthorityImpl userAdminCreate = GrantedAuthorityImpl.NewInstance();
|
189
|
userAdminCreate.setAuthority("USER.create");
|
190
|
GrantedAuthorityImpl userAdminDelete = GrantedAuthorityImpl.NewInstance();
|
191
|
userAdminDelete.setAuthority("USER.delete");
|
192
|
List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
|
193
|
list.add(userAdminUpdate);
|
194
|
list.add(userAdminDelete);
|
195
|
list.add(userAdminCreate);
|
196
|
|
197
|
|
198
|
userService.createGroup("UserAdmins", list);
|
199
|
String username = "user3";
|
200
|
String password = "password";
|
201
|
User user = User.NewInstance(username, password);
|
202
|
userService.createUser(user);
|
203
|
List<String> groups = userService.findAllGroups();
|
204
|
Assert.assertEquals("UserAdmins", groups.get(0));
|
205
|
|
206
|
|
207
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
208
|
authentication = authenticationManager.authenticate(token);
|
209
|
SecurityContext context = SecurityContextHolder.getContext();
|
210
|
context.setAuthentication(authentication);
|
211
|
|
212
|
username = "user4";
|
213
|
password = "password";
|
214
|
user = User.NewInstance(username, password);
|
215
|
try{
|
216
|
userService.createUser(user);
|
217
|
}catch(Exception e){
|
218
|
Assert.assertEquals("Access is denied", e.getMessage());
|
219
|
}
|
220
|
groupService.addUserToGroup("user3", "UserAdmins");
|
221
|
|
222
|
System.err.println(context.getAuthentication().getName());
|
223
|
try{
|
224
|
userService.createUser(user);
|
225
|
}catch(Exception e){
|
226
|
System.err.println(e.getMessage());
|
227
|
Assert.fail();
|
228
|
}
|
229
|
|
230
|
}
|
231
|
|
232
|
|
233
|
@Test
|
234
|
public void testHasRole(){
|
235
|
String username = "useradmin";
|
236
|
String newPassword = "password2";
|
237
|
userService.changePasswordForUser(username, newPassword);
|
238
|
username = "user4";
|
239
|
String password = "password";
|
240
|
User user = User.NewInstance(username, password);
|
241
|
userService.createUser(user);
|
242
|
try{
|
243
|
userService.changePasswordForUser(username, "newPassword");
|
244
|
}catch (Exception e){
|
245
|
System.err.println(e.getMessage());
|
246
|
Assert.fail();
|
247
|
}
|
248
|
|
249
|
}
|
250
|
|
251
|
|
252
|
@Test
|
253
|
|
254
|
public void testHasPermission(){
|
255
|
Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()),null);
|
256
|
boolean hasPermission = taxonService.hasPermission(authentication, taxon, CdmPermission.UPDATE);
|
257
|
assertFalse(hasPermission);
|
258
|
User testUser = User.NewInstance("username123", "1234");
|
259
|
hasPermission = userService.hasPermission(authentication, testUser, CdmPermission.UPDATE);
|
260
|
assertTrue(hasPermission);
|
261
|
}
|
262
|
|
263
|
|
264
|
|
265
|
|
266
|
}
|