1
|
|
2
|
package eu.etaxonomy.cdm.api.service;
|
3
|
|
4
|
import static org.junit.Assert.assertEquals;
|
5
|
import static org.junit.Assert.assertFalse;
|
6
|
import static org.junit.Assert.assertTrue;
|
7
|
|
8
|
import java.util.Iterator;
|
9
|
import java.util.Set;
|
10
|
import java.util.UUID;
|
11
|
|
12
|
import javax.sql.DataSource;
|
13
|
|
14
|
import org.junit.Assert;
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.junit.Before;
|
17
|
import org.junit.Ignore;
|
18
|
import org.junit.Test;
|
19
|
import org.junit.runner.RunWith;
|
20
|
import org.springframework.security.authentication.AuthenticationManager;
|
21
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
22
|
import org.springframework.security.core.Authentication;
|
23
|
import org.springframework.security.core.context.SecurityContext;
|
24
|
import org.springframework.security.core.context.SecurityContextHolder;
|
25
|
import org.unitils.UnitilsJUnit4TestClassRunner;
|
26
|
import org.unitils.database.annotations.TestDataSource;
|
27
|
import org.unitils.database.annotations.Transactional;
|
28
|
import org.unitils.database.util.TransactionMode;
|
29
|
import org.unitils.dbunit.annotation.DataSet;
|
30
|
import org.unitils.spring.annotation.SpringApplicationContext;
|
31
|
import org.unitils.spring.annotation.SpringBeanByName;
|
32
|
|
33
|
import eu.etaxonomy.cdm.api.service.DeleteResult.DeleteStatus;
|
34
|
import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
|
35
|
import eu.etaxonomy.cdm.model.description.Feature;
|
36
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
37
|
import eu.etaxonomy.cdm.model.description.TextData;
|
38
|
import eu.etaxonomy.cdm.model.media.Media;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
41
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionEvaluator;
|
42
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTestWithSecurity;
|
43
|
|
44
|
|
45
|
|
46
|
/**
|
47
|
* Test class only for development purposes, must be run in suite.
|
48
|
*
|
49
|
*/
|
50
|
//@RunWith(UnitilsJUnit4TestClassRunner.class)
|
51
|
//@SpringApplicationContext({"/eu/etaxonomy/cdm/applicationContextSecurity.xml"})
|
52
|
//@Transactional
|
53
|
@Ignore // should be ignored
|
54
|
@DataSet("SecurityTest.xml")
|
55
|
public class SecurityWithTransaction extends CdmTransactionalIntegrationTestWithSecurity {
|
56
|
|
57
|
private static final Logger logger = Logger.getLogger(SecurityWithTransaction.class);
|
58
|
|
59
|
@SpringBeanByName
|
60
|
private ITaxonService taxonService;
|
61
|
|
62
|
@SpringBeanByName
|
63
|
private IDescriptionService descriptionService;
|
64
|
|
65
|
@SpringBeanByName
|
66
|
private ITaxonNodeService taxonNodeService;
|
67
|
|
68
|
@SpringBeanByName
|
69
|
private IUserService userService;
|
70
|
|
71
|
|
72
|
@TestDataSource
|
73
|
protected DataSource dataSource;
|
74
|
|
75
|
private Authentication authentication;
|
76
|
|
77
|
@SpringBeanByName
|
78
|
private AuthenticationManager authenticationManager;
|
79
|
|
80
|
@SpringBeanByName
|
81
|
private CdmPermissionEvaluator permissionEvaluator;
|
82
|
|
83
|
private UsernamePasswordAuthenticationToken token;
|
84
|
|
85
|
|
86
|
@Before
|
87
|
public void setUp(){
|
88
|
token = new UsernamePasswordAuthenticationToken("admin", "sPePhAz6");
|
89
|
}
|
90
|
|
91
|
@Test
|
92
|
public void testDeleteTaxon(){
|
93
|
token = new UsernamePasswordAuthenticationToken("taxonomist", "test4");
|
94
|
authentication = authenticationManager.authenticate(token);
|
95
|
SecurityContext context = SecurityContextHolder.getContext();
|
96
|
context.setAuthentication(authentication);
|
97
|
Taxon actualTaxon = (Taxon)taxonService.find(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
|
98
|
|
99
|
//try {
|
100
|
DeleteResult result = taxonService.deleteTaxon(actualTaxon, null, null);
|
101
|
/*} catch (DataChangeNoRollbackException e) {
|
102
|
Assert.fail();
|
103
|
}*/
|
104
|
if (!result.isOk()){
|
105
|
Assert.fail();
|
106
|
}
|
107
|
}
|
108
|
|
109
|
|
110
|
@Test
|
111
|
public void testSaveOrUpdateDescription(){
|
112
|
|
113
|
authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("descriptionEditor", "test"));
|
114
|
SecurityContext context = SecurityContextHolder.getContext();
|
115
|
context.setAuthentication(authentication);
|
116
|
/*Taxon taxon = (Taxon) taxonService.load(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
|
117
|
|
118
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
119
|
|
120
|
Iterator<TaxonDescription> iterator = descriptions.iterator();
|
121
|
|
122
|
TaxonDescription description = iterator.next();*/
|
123
|
TaxonDescription description = (TaxonDescription) descriptionService.find(UUID.fromString("eb17b80a-9be6-4642-a6a8-b19a318925e6"));
|
124
|
|
125
|
TextData textData = new TextData();
|
126
|
textData.setFeature(Feature.ECOLOGY());
|
127
|
Media media = Media.NewInstance();
|
128
|
textData.addMedia(media);
|
129
|
|
130
|
|
131
|
|
132
|
//descriptionService.saveDescriptionElement(textData);
|
133
|
description.addElement(textData);
|
134
|
|
135
|
descriptionService.saveOrUpdate(description);
|
136
|
|
137
|
Taxon taxon = (Taxon) taxonService.find(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
|
138
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
139
|
|
140
|
Iterator<TaxonDescription> iterator = descriptions.iterator();
|
141
|
|
142
|
description = iterator.next();
|
143
|
assertEquals(1, descriptions.size());
|
144
|
assertEquals(2,description.getElements().size());
|
145
|
|
146
|
|
147
|
|
148
|
}
|
149
|
|
150
|
@Test
|
151
|
public void testAllowOnlyAccessToPartOfTree(){
|
152
|
authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("partEditor", "test4"));
|
153
|
SecurityContext context = SecurityContextHolder.getContext();
|
154
|
context.setAuthentication(authentication);
|
155
|
|
156
|
Taxon tribe = (Taxon)taxonService.find(UUID.fromString("928a0167-98cd-4555-bf72-52116d067625"));
|
157
|
Taxon taxon = (Taxon)taxonService.find(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
|
158
|
Iterator<TaxonNode> it = tribe.getTaxonNodes().iterator();
|
159
|
TaxonNode node = it.next();
|
160
|
|
161
|
assertFalse(permissionEvaluator.hasPermission(authentication, node, "UPDATE"));
|
162
|
node = node.getChildNodes().iterator().next();
|
163
|
|
164
|
System.err.println(node.getUuid());
|
165
|
|
166
|
assertTrue(permissionEvaluator.hasPermission(authentication, node, "UPDATE"));
|
167
|
node = node.getChildNodes().iterator().next();
|
168
|
assertTrue(permissionEvaluator.hasPermission(authentication, node, "UPDATE"));
|
169
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
170
|
|
171
|
taxonNodeService.saveOrUpdate(node);
|
172
|
assertFalse(permissionEvaluator.hasPermission(authentication, description, "UPDATE"));
|
173
|
|
174
|
|
175
|
}
|
176
|
|
177
|
|
178
|
}
|