Skip to content

Commit 7ab48ea

Browse files
authored
Merge pull request #25 from evolvedbinary/hotfix/change-group-managers
Group API should correctly change Group Managers
2 parents 6fc08ab + 17a72d6 commit 7ab48ea

File tree

3 files changed

+251
-9
lines changed

3 files changed

+251
-9
lines changed

src/main/xar-resources/modules/security.xqm

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,23 @@ function sec:update-group($groupname, $group-data as map(xs:string, item())) as
215215
then
216216
(
217217
(: change managers? :)
218-
if (not(empty($group-data?managers)))
219-
then
220-
(
221-
(: remove from existing group managers :)
222-
sm:get-group-managers($groupname) ! sm:remove-group-member($groupname, .),
223-
(: add to new manangers :)
224-
array:flatten($group-data?managers) ! sm:add-group-manager($groupname, .)
225-
)
226-
else (),
218+
let $new-managers := array:flatten($group-data?managers)
219+
return
220+
if (not(empty($new-managers)))
221+
then
222+
let $existing-managers := sm:get-group-managers($groupname)
223+
let $managers-to-remove := $existing-managers[not( . = $new-managers)]
224+
let $managers-to-add := $new-managers[not(. = $existing-managers)]
225+
return
226+
(
227+
(: remove from existing group managers :)
228+
$managers-to-remove ! sm:remove-group-manager($groupname, .),
229+
230+
(: add to new managers :)
231+
$managers-to-add ! sm:add-group-manager($groupname, .)
232+
)
233+
else ()
234+
,
227235

228236
(: change metadata? :)
229237
if (not(empty($group-data?metadata)))
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Fusion Studio API - API for Fusion Studio
3+
* Copyright © 2017 Evolved Binary ([email protected])
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.fusiondb.studio.api;
19+
20+
import io.restassured.response.ExtractableResponse;
21+
import io.restassured.response.Response;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
28+
import static com.fusiondb.studio.api.API.*;
29+
import static io.restassured.RestAssured.given;
30+
import static io.restassured.http.ContentType.JSON;
31+
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
32+
import static java.util.Arrays.asList;
33+
import static org.apache.http.HttpStatus.SC_NO_CONTENT;
34+
import static org.apache.http.HttpStatus.SC_OK;
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
37+
public class GroupIT {
38+
39+
@Test
40+
public void createGroup() {
41+
final String groupId = "ABC";
42+
43+
createGroup(groupId);
44+
45+
// get group
46+
final ExtractableResponse<Response> groupResponse = getGroup(groupId);
47+
48+
// check the returned properties
49+
assertEquals("group" + groupId, groupResponse.jsonPath().getString("groupName"));
50+
}
51+
52+
@Test
53+
public void addGroupManager() {
54+
final String groupId = "DEF";
55+
56+
// 1. create a group
57+
createGroup(groupId);
58+
59+
// 2. update the group
60+
final Map<String, Object> requestBody = mapOf(
61+
Tuple("groupName", "group" + groupId),
62+
Tuple("managers", new String[] {"admin", "guest"})
63+
);
64+
given().
65+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
66+
contentType(JSON).
67+
body(requestBody).
68+
when().
69+
put(getApiBaseUri() + "/group/group" + groupId).
70+
then().
71+
statusCode(SC_NO_CONTENT);
72+
73+
// 3. get the group
74+
final ExtractableResponse<Response> groupResponse = getGroup(groupId);
75+
76+
// 4. check the updated group managers
77+
assertEquals("group" + groupId, groupResponse.jsonPath().getString("groupName"));
78+
final List<String> actualGroupManagers = groupResponse.jsonPath().getList("managers");
79+
actualGroupManagers.sort(String::compareTo);
80+
assertEquals(asList("admin", "guest"),actualGroupManagers );
81+
}
82+
83+
@Test
84+
public void changeGroupManager() {
85+
final String groupId = "GHI";
86+
87+
// 1. create a group
88+
createGroup(groupId);
89+
90+
// 2. update the group
91+
final Map<String, Object> requestBody = mapOf(
92+
Tuple("groupName", "group" + groupId),
93+
Tuple("managers", new String[] {"guest"})
94+
);
95+
given().
96+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
97+
contentType(JSON).
98+
body(requestBody).
99+
when().
100+
put(getApiBaseUri() + "/group/group" + groupId).
101+
then().
102+
statusCode(SC_NO_CONTENT);
103+
104+
// 3. get the group
105+
final ExtractableResponse<Response> groupResponse = getGroup(groupId);
106+
107+
// 4. check the updated group managers
108+
assertEquals("group" + groupId, groupResponse.jsonPath().getString("groupName"));
109+
assertEquals(asList("guest"), groupResponse.jsonPath().getList("managers"));
110+
}
111+
112+
@Test
113+
public void removeGroupManager() {
114+
final String groupId = "JKL";
115+
116+
// 1. create a group
117+
createGroup(groupId);
118+
119+
// 2. update the group (add manager)
120+
Map<String, Object> requestBody = mapOf(
121+
Tuple("groupName", "group" + groupId),
122+
Tuple("managers", new String[] {"admin", "guest"})
123+
);
124+
given().
125+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
126+
contentType(JSON).
127+
body(requestBody).
128+
when().
129+
put(getApiBaseUri() + "/group/group" + groupId).
130+
then().
131+
statusCode(SC_NO_CONTENT);
132+
133+
// 3. get the group
134+
ExtractableResponse<Response> groupResponse = getGroup(groupId);
135+
136+
// 4. check the updated group managers
137+
assertEquals("group" + groupId, groupResponse.jsonPath().getString("groupName"));
138+
final List<String> actualGroupManagers = groupResponse.jsonPath().getList("managers");
139+
actualGroupManagers.sort(String::compareTo);
140+
assertEquals(asList("admin", "guest"), actualGroupManagers);
141+
142+
// 5. update the group (remove manager)
143+
requestBody = mapOf(
144+
Tuple("groupName", "group" + groupId),
145+
Tuple("managers", new String[] {"guest"})
146+
);
147+
given().
148+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
149+
contentType(JSON).
150+
body(requestBody).
151+
when().
152+
put(getApiBaseUri() + "/group/group" + groupId).
153+
then().
154+
statusCode(SC_NO_CONTENT);
155+
156+
// 6. get the group
157+
groupResponse = getGroup(groupId);
158+
159+
// 7. check the updated group managers
160+
assertEquals("group" + groupId, groupResponse.jsonPath().getString("groupName"));
161+
assertEquals(asList("guest"), groupResponse.jsonPath().getList("managers"));
162+
163+
}
164+
165+
166+
private ExtractableResponse<Response> getGroup(final String groupId) {
167+
return
168+
given().
169+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
170+
contentType(JSON).
171+
when().
172+
get(getApiBaseUri() + "/group/group" + groupId).
173+
then().
174+
statusCode(SC_OK).
175+
assertThat().
176+
body(matchesJsonSchemaInClasspath("group-schema.json")).
177+
extract();
178+
}
179+
180+
private void createGroup(final String groupId, final String... managers) {
181+
final Map<String, Object> requestBody = mapOf(
182+
Tuple("groupName", "group" + groupId),
183+
Tuple("description", "A group named 'group" + groupId + "'"),
184+
Tuple("metadata", arrayOf(
185+
mapOf(
186+
Tuple("key", "http://axschema.org/pref/language"),
187+
Tuple("value", "en")
188+
)
189+
))
190+
);
191+
192+
if (managers != null && managers.length > 0) {
193+
requestBody.put("managers", managers);
194+
}
195+
196+
given().
197+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
198+
contentType(JSON).
199+
body(requestBody).
200+
when().
201+
put(getApiBaseUri() + "/group/group" + groupId).
202+
then().
203+
statusCode(SC_NO_CONTENT);
204+
}
205+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "User",
4+
"type": "object",
5+
"required": [
6+
"groupName",
7+
"managers"
8+
],
9+
"properties": {
10+
"groupName": {
11+
"type": "string"
12+
},
13+
"description": {
14+
"type": "string"
15+
},
16+
"managers": {
17+
"type": "array",
18+
"items": {
19+
"type": "string"
20+
}
21+
},
22+
"metadata": {
23+
"type": "array",
24+
"items": {
25+
"$ref": "attribute-schema.json"
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)