1
|
package eu.etaxonomy.cdm.api.service;
|
2
|
|
3
|
|
4
|
import java.util.UUID;
|
5
|
|
6
|
import org.joda.time.DateTime;
|
7
|
import org.junit.Assert;
|
8
|
import org.junit.Before;
|
9
|
import org.junit.Test;
|
10
|
import org.springframework.security.Authentication;
|
11
|
import org.springframework.security.AuthenticationManager;
|
12
|
import org.springframework.security.GrantedAuthority;
|
13
|
import org.springframework.security.context.SecurityContextHolder;
|
14
|
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
15
|
import org.unitils.dbunit.annotation.DataSet;
|
16
|
import org.unitils.spring.annotation.SpringBeanByType;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
|
19
|
import eu.etaxonomy.cdm.model.common.User;
|
20
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
21
|
|
22
|
public class UserServiceImplTest extends CdmIntegrationTest {
|
23
|
|
24
|
@SpringBeanByType
|
25
|
private AuthenticationManager authenticationManager;
|
26
|
|
27
|
@SpringBeanByType
|
28
|
private IUserService userService;
|
29
|
|
30
|
private GrantedAuthority[] expectedRoles;
|
31
|
private UsernamePasswordAuthenticationToken token;
|
32
|
|
33
|
@Before
|
34
|
public void setUp() {
|
35
|
expectedRoles = new GrantedAuthority[4];
|
36
|
GrantedAuthorityImpl publish = new GrantedAuthorityImpl();
|
37
|
publish.setAuthority("Publish");
|
38
|
publish.setUuid(UUID.fromString("441a3c40-0c84-11de-8c30-0800200c9a66"));
|
39
|
publish.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
40
|
GrantedAuthorityImpl edit = new GrantedAuthorityImpl();
|
41
|
edit.setAuthority("Edit");
|
42
|
edit.setUuid(UUID.fromString("14788361-1a7e-4eed-b22f-fd90a0b424ac"));
|
43
|
edit.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
44
|
GrantedAuthorityImpl annotate = new GrantedAuthorityImpl();
|
45
|
annotate.setAuthority("Annotate");
|
46
|
annotate.setUuid(UUID.fromString("fa56073c-0ffd-4384-b459-b2f07e35b689"));
|
47
|
annotate.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
48
|
GrantedAuthorityImpl checkAnnotation = new GrantedAuthorityImpl();
|
49
|
checkAnnotation.setAuthority("CheckAnnotation");
|
50
|
checkAnnotation.setUuid(UUID.fromString("e5354c0e-657b-4b4d-bb2f-791612199711"));
|
51
|
checkAnnotation.setCreated(new DateTime(2009,2,3,17,52,26,0));
|
52
|
expectedRoles[3] = publish;
|
53
|
expectedRoles[2] = edit;
|
54
|
expectedRoles[0] = annotate;
|
55
|
expectedRoles[1] = checkAnnotation;
|
56
|
token = new UsernamePasswordAuthenticationToken("ben","sPePhAz6");
|
57
|
}
|
58
|
|
59
|
@Test
|
60
|
@DataSet
|
61
|
public void testIfAnyGranted() {
|
62
|
|
63
|
Authentication authentication = authenticationManager.authenticate(token);
|
64
|
Object p = authentication.getPrincipal();
|
65
|
Assert.assertTrue(p instanceof User);
|
66
|
User principal = (User)p;
|
67
|
|
68
|
Assert.assertEquals(principal.getUsername(),"ben");
|
69
|
|
70
|
Assert.assertArrayEquals(expectedRoles, authentication.getAuthorities());
|
71
|
}
|
72
|
|
73
|
@Test
|
74
|
public void testAuthentication() {
|
75
|
String username = "username";
|
76
|
String password = "password";
|
77
|
User user = User.NewInstance(username, password);
|
78
|
|
79
|
userService.save(user);
|
80
|
|
81
|
// userService.createUser(user);
|
82
|
|
83
|
|
84
|
|
85
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
86
|
|
87
|
authenticationManager.authenticate(token);
|
88
|
}
|
89
|
}
|