-
Notifications
You must be signed in to change notification settings - Fork 155
/
RoleInheritsFunctionalTest.java
212 lines (171 loc) · 11.9 KB
/
RoleInheritsFunctionalTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Copyright 2012-2013
Claudio Tesoriero - c.tesoriero-at-baasbox.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// @author: Marco Tibuzzi
import static org.junit.Assert.fail;
import static play.test.Helpers.DELETE;
import static play.test.Helpers.GET;
import static play.test.Helpers.POST;
import static play.test.Helpers.PUT;
import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.route;
import static play.test.Helpers.routeAndCall;
import static play.test.Helpers.running;
import java.util.UUID;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import play.mvc.Http.Status;
import play.mvc.Result;
import play.test.FakeRequest;
import core.AbstractTest;
import core.TestConfig;
public class RoleInheritsFunctionalTest extends AbstractTest{
public String roleName="";
public String userName="";
@Override
public String getMethod() {
return GET;
}
@Override
protected void assertContent(String s) {
// TODO Auto-generated method stub
}
@Override
public String getRouteAddress()
{
roleName="fake"+UUID.randomUUID().toString();
return "/admin/role/"+ roleName;
}
public String getFakeUserCreationAddress(){
userName="fake"+UUID.randomUUID().toString();
return "/admin/user";
}
public String getFakeUserAddress(){
return "/admin/user/"+ userName;
}
@Test
public void testRoleCreate() throws Exception
{
running (getFakeApplication(),
new Runnable() {
public void run() {
try {
String sFakeRole = getRouteAddress();
FakeRequest requestCreation = new FakeRequest(POST, sFakeRole);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
Result result = route(requestCreation);
assertRoute(result, "testRoleCreate.create", Status.CREATED, null, true);
//creates one user in this Role
String sFakeCreateUser = getFakeUserCreationAddress();
requestCreation = new FakeRequest(POST, sFakeCreateUser);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree("{\"username\":\""+userName+"\","
+ "\"password\":\"test\","
+ "\"role\":\""+ roleName +"\"}");
requestCreation = requestCreation.withJsonBody(actualObj);
requestCreation = requestCreation.withHeader("Content-Type", "application/json");
result = route(requestCreation);
assertRoute(result, "testRoleCreate.createUser", Status.CREATED, null, true);
//checks the user
String sFakeCheckUser = getFakeUserAddress();
requestCreation = new FakeRequest(GET, sFakeCheckUser);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = route(requestCreation);
assertRoute(result, "testRoleCreate.checkUser", Status.OK, "\"name\":\""+roleName+"\"", true);
//admin creates a document
String sFakeCollection = new AdminCollectionFunctionalTest().routeCreateCollection();
result = routeCreateDocumentAsAdmin("/document/"+sFakeCollection);
assertRoute(result, "adminCreateDocument", Status.OK, null, true);
String sUUID = getUuid(result);
//and gives to the registered user the read grant
requestCreation = new FakeRequest(PUT, "/document/"+sFakeCollection + "/" + sUUID + "/read/role/registered");
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = route(requestCreation);
assertRoute(result, "testRoleCreate.AdminGivesGrant", Status.OK, null, false);
//now the new user should see it
requestCreation = new FakeRequest(GET, "/document/"+sFakeCollection + "/" + sUUID);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(userName, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.UserShouldRead", Status.OK, "id\":\""+sUUID, true);
//registered user should see it as well
String sFakeRegUser = "regUser_"+UUID.randomUUID();
requestCreation = new FakeRequest(POST, "/admin/user");
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
mapper = new ObjectMapper();
actualObj = mapper.readTree("{\"username\":\""+sFakeRegUser+"\","
+ "\"password\":\"test\","
+ "\"role\":\"registered\",\"isrole\":true}");
requestCreation = requestCreation.withJsonBody(actualObj);
requestCreation = requestCreation.withHeader("Content-Type", "application/json");
result = route(requestCreation);
assertRoute(result, "testRoleCreate.createRegUser", Status.CREATED, null, true);
requestCreation = new FakeRequest(GET, "/document/"+sFakeCollection + "/" + sUUID);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(sFakeRegUser, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.RegShouldRead", Status.OK, "id\":\""+sUUID, true);
//user create a document and gives grant to its role
result = routeCreateDocumentAsUser(sFakeCollection,userName,"test");
assertRoute(result, "testRoleCreate.userCreateDoc", Status.OK,null, false);
sUUID = getUuid(result);
requestCreation = new FakeRequest(PUT, "/document/"+sFakeCollection + "/" + sUUID + "/read/role/"+roleName);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(userName, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.UserGivesGrant", Status.OK, null, false);
//registered user should not see it
requestCreation = new FakeRequest(GET, "/document/"+sFakeCollection + "/" + sUUID);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(sFakeRegUser, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.RegShouldNotSeeRead", Status.NOT_FOUND, null, false);
//
//finally... drop the new role
requestCreation = new FakeRequest(DELETE, sFakeRole);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = route(requestCreation);
assertRoute(result, "testRoleCreate.drop_final", Status.OK, null, false);
}catch (Exception e) {
e.printStackTrace();
fail();
}
}});
}
protected Result routeCreateDocumentAsAdmin(String sAddress)
{
FakeRequest request = new FakeRequest(POST, sAddress);
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
request = request.withJsonBody(getPayload("/documentCreatePayload.json"));
return routeAndCall(request);
}
protected Result routeCreateDocumentAsUser(String collection, String username,String password)
{
FakeRequest request = new FakeRequest(POST, "/document/" + collection);
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(username, password));
request = request.withJsonBody(getPayload("/documentCreatePayload.json"));
return routeAndCall(request);
}
}