Skip to content

Commit 1ac0dd8

Browse files
committed
chore: taxonomy permission testcase added
1 parent 9ae00fa commit 1ac0dd8

File tree

6 files changed

+226
-10
lines changed

6 files changed

+226
-10
lines changed

src/test/java/com/contentstack/cms/stack/RoleAPITest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.contentstack.cms.stack;
22

3+
import com.contentstack.cms.Contentstack;
34
import com.contentstack.cms.TestClient;
5+
import com.contentstack.cms.Utils;
46
import com.contentstack.cms.core.Util;
7+
import com.google.gson.JsonObject;
8+
59
import okhttp3.Request;
610
import okhttp3.ResponseBody;
711
import org.json.simple.JSONObject;
812
import org.junit.jupiter.api.*;
13+
14+
import retrofit2.Call;
915
import retrofit2.Response;
1016

1117
import java.io.IOException;
18+
import java.util.HashMap;
1219

1320
@Tag("api")
1421
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -19,15 +26,22 @@ class RoleAPITest {
1926
protected static String _uid = TestClient.AUTHTOKEN;
2027
protected static String MANAGEMENT_TOKEN = TestClient.MANAGEMENT_TOKEN;
2128
protected static Roles roles;
22-
static Stack stack;
29+
protected static String Taxonomy_uid = "sample_two";
30+
protected static Taxonomy taxonomy;
31+
protected static Terms term;
32+
protected static Stack stack;
2333

2434

2535
@BeforeAll
2636
static void setup() {
27-
stack = TestClient.getStack();
28-
stack.addHeader(Util.API_KEY, API_KEY);
29-
stack.addHeader(Util.AUTHORIZATION, MANAGEMENT_TOKEN);
37+
final String AUTHTOKEN = TestClient.AUTHTOKEN;
38+
HashMap<String, Object> headers = new HashMap<>();
39+
headers.put(Util.API_KEY, API_KEY);
40+
headers.put(Util.AUTHORIZATION, MANAGEMENT_TOKEN);
41+
stack = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build().stack(headers);
3042
roles = stack.roles(_uid);
43+
taxonomy = stack.taxonomy();
44+
term = stack.taxonomy(Taxonomy_uid).terms();
3145
}
3246

3347
@Test
@@ -111,5 +125,33 @@ void deleteRole() {
111125
Assertions.assertNull(request.url().encodedQuery());
112126
Assertions.assertEquals("https://api.contentstack.io/v3/roles/" + _uid, request.url().toString());
113127
}
128+
@Test
129+
void createRoleWithRule1() throws IOException{
130+
JSONObject requestTaxonomy = Utils.readJson("mocktaxonomy/create.json");
131+
taxonomy.addHeader(Util.API_KEY, API_KEY);
132+
taxonomy.addHeader(Util.AUTHORIZATION, MANAGEMENT_TOKEN);
133+
Call<ResponseBody> responseTaxonomy = taxonomy.create(requestTaxonomy);
134+
Response<ResponseBody> response1 = responseTaxonomy.execute();
135+
System.out.println("Taxonomy created" + response1.body().string());
136+
137+
}
138+
@Test
139+
void createRoleWithRule2() throws IOException{
140+
JSONObject requestTerm = Utils.readJson("mocktaxonomy/createTerm.json");
141+
142+
Response<ResponseBody> response2 = term.create(requestTerm).execute();
143+
System.out.println("Term created" + response2.toString());
144+
145+
}
146+
@Test
147+
void createRoleWithTaxonomy() throws IOException{
148+
JSONObject requestBody = Utils.readJson("mockrole/createRole.json");
149+
150+
Call<ResponseBody> responseCall = roles.create(requestBody);
151+
Response<ResponseBody> response = responseCall.execute();
152+
System.out.println("Role created" + response.body().string());
153+
154+
155+
}
114156

115157
}

src/test/java/com/contentstack/cms/stack/RoleUnitTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import com.contentstack.cms.Contentstack;
44
import com.contentstack.cms.TestClient;
5+
import com.contentstack.cms.Utils;
56
import com.contentstack.cms.core.Util;
67
import okhttp3.Request;
8+
import okhttp3.ResponseBody;
9+
import retrofit2.Call;
10+
import retrofit2.Response;
11+
712
import org.json.simple.JSONObject;
813
import org.json.simple.parser.JSONParser;
914
import org.json.simple.parser.ParseException;
1015
import org.junit.jupiter.api.*;
1116

17+
import java.io.IOException;
1218
import java.util.HashMap;
1319

1420
@Tag("unit")
@@ -206,8 +212,11 @@ void singleRole() {
206212

207213
@Test
208214
@Order(8)
209-
void createRole() {
215+
void createRole() throws IOException {
210216
Request request = roles.create(body).request();
217+
Call<ResponseBody> responseBody = roles.create(body);
218+
Response<ResponseBody> resp = responseBody.execute();
219+
System.out.println(resp.toString());
211220
Assertions.assertEquals(0, request.headers().names().size());
212221
Assertions.assertEquals("POST", request.method());
213222
Assertions.assertTrue(request.url().isHttps());
@@ -220,9 +229,30 @@ void createRole() {
220229
"https://api.contentstack.io/v3/roles",
221230
request.url().toString());
222231
}
223-
224232
@Test
225233
@Order(9)
234+
void createRoleWithTaxonomy() throws IOException {
235+
JSONObject roleBody = Utils.readJson("mockrole/createRole.json");
236+
Request request = roles.create(roleBody).request();
237+
// Call<ResponseBody> responseBody = roles.create(roleBody);
238+
// Response<ResponseBody> resp = responseBody.execute();
239+
// System.out.println(resp.toString());
240+
System.out.println(request.toString());
241+
Assertions.assertEquals(2, request.headers().names().size());
242+
Assertions.assertEquals("POST", request.method());
243+
Assertions.assertTrue(request.url().isHttps());
244+
Assertions.assertEquals("api.contentstack.io", request.url().host());
245+
Assertions.assertEquals(2, request.url().pathSegments().size());
246+
Assertions.assertEquals("roles", request.url().pathSegments().get(1));
247+
Assertions.assertNotNull(request.body());
248+
Assertions.assertNull(request.url().encodedQuery());
249+
Assertions.assertEquals(
250+
"https://api.contentstack.io/v3/roles",
251+
request.url().toString());
252+
}
253+
254+
@Test
255+
@Order(10)
226256
void updateRole() {
227257
Request request = roles.update(body).request();
228258
Assertions.assertEquals(0, request.headers().names().size());
@@ -236,7 +266,7 @@ void updateRole() {
236266
}
237267

238268
@Test
239-
@Order(10)
269+
@Order(11)
240270
void deleteRole() {
241271
Request request = roles.delete().request();
242272
Assertions.assertEquals(1, request.headers().names().size());
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"role": {
3+
"name": "test",
4+
"description": "Test for taxonomy permissions",
5+
"rules": [
6+
{
7+
"module": "branch",
8+
"branches": [
9+
"main"
10+
],
11+
"acl": {
12+
"read": true
13+
}
14+
},
15+
{
16+
"module": "environment",
17+
"environments": [],
18+
"acl": {
19+
"read": true
20+
}
21+
},
22+
{
23+
"module": "locale",
24+
"locales": [],
25+
"acl": {
26+
"read": true
27+
}
28+
},
29+
{
30+
"module": "taxonomy",
31+
"taxonomies": [
32+
"sample_two"
33+
],
34+
"terms": [
35+
"term_a"
36+
],
37+
"content_types": [
38+
{
39+
"uid": "$all",
40+
"acl": {
41+
"read": true,
42+
"sub_acl": {
43+
"read": true,
44+
"create": true,
45+
"update": true,
46+
"delete": true,
47+
"publish": true
48+
}
49+
}
50+
}
51+
],
52+
"acl": {
53+
"read": true,
54+
"sub_acl": {
55+
"read": true,
56+
"create": true,
57+
"update": true,
58+
"delete": true,
59+
"publish": true
60+
}
61+
}
62+
}
63+
],
64+
"uid": "role_uid"
65+
}
66+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"role":{
3+
"name":"sample111",
4+
"description":"This is a test role.",
5+
"rules":[
6+
{
7+
"module":"branch",
8+
"branches":[
9+
"main"
10+
],
11+
"acl":{
12+
"read":true
13+
}
14+
},
15+
{
16+
"module":"content_type",
17+
"content_types":[
18+
"$all"
19+
],
20+
"acl":{
21+
"read":true,
22+
"sub_acl":{
23+
"read":true
24+
}
25+
}
26+
},
27+
{
28+
"module":"asset",
29+
"assets":[
30+
"$all"
31+
],
32+
"acl":{
33+
"read":true,
34+
"update":true,
35+
"publish":true,
36+
"delete":true
37+
}
38+
},
39+
{
40+
"module":"folder",
41+
"folders":[
42+
"$all"
43+
],
44+
"acl":{
45+
"read":true,
46+
"update":true,
47+
"publish":true,
48+
"delete":true,
49+
"sub_acl":{
50+
"read":true,
51+
"update":true,
52+
"publish":true,
53+
"delete":true
54+
}
55+
}
56+
},
57+
{
58+
"module":"environment",
59+
"environments":[
60+
"$all"
61+
],
62+
"acl":{
63+
"read":true
64+
}
65+
},
66+
{
67+
"module":"locale",
68+
"locales":[
69+
"$all"
70+
],
71+
"acl":{
72+
"read":true
73+
}
74+
}
75+
],
76+
"uid":"blt668fa7872710da7c"
77+
}
78+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
{
33
"taxonomy": {
4-
"uid": "sample_one",
5-
"name": "Sample One",
4+
"uid": "sample_two",
5+
"name": "Sample Two",
66
"description": "Description for the sample one taxonomy."
77
}
88
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"taxonomy": {
3-
"name": "Updated Sample One",
3+
"name": "Updated Sample Two",
44
"description": "Updated description for the sample one taxonomy."
55
}
66
}

0 commit comments

Comments
 (0)