Skip to content

Commit 1417375

Browse files
authored
Tests for Object delete button on SideBar (#1746)
Signed-off-by: Lenin Alevski <alevsk.8772@gmail.com>
1 parent 676420a commit 1417375

File tree

8 files changed

+300
-46
lines changed

8 files changed

+300
-46
lines changed

portal-ui/src/common/SecureComponent/__tests__/accessControl.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ const setPolicy3 = () => {
116116
});
117117
};
118118

119+
const setPolicy4 = () => {
120+
store.dispatch({
121+
type: SESSION_RESPONSE,
122+
message: {
123+
distributedMode: true,
124+
features: [],
125+
permissions: {
126+
"arn:aws:s3:::test/*": ["s3:ListBucket"],
127+
"arn:aws:s3:::test": ["s3:GetBucketLocation"],
128+
"arn:aws:s3:::test/digitalinsights/xref_cust_guid_actd*": ["s3:*"],
129+
},
130+
status: "ok",
131+
operator: false,
132+
},
133+
});
134+
};
135+
119136
test("Upload button disabled", () => {
120137
setPolicy1();
121138
expect(hasPermission("testcafe", ["s3:PutObject"])).toBe(false);
@@ -157,3 +174,26 @@ test("Can browse a bucket for a policy with a wildcard", () => {
157174
)
158175
).toBe(true);
159176
});
177+
178+
test("Can delete an object inside a bucket prefix", () => {
179+
setPolicy4();
180+
expect(
181+
hasPermission(
182+
[
183+
"xref_cust_guid_actd-v1.jpg",
184+
"test/digitalinsights/xref_cust_guid_actd-v1.jpg",
185+
],
186+
[IAM_SCOPES.S3_DELETE_OBJECT]
187+
)
188+
).toBe(true);
189+
});
190+
191+
test("Can't delete an object inside a bucket prefix", () => {
192+
setPolicy4();
193+
expect(
194+
hasPermission(
195+
["xref_cust_guid_actd-v1.jpg", "test/xref_cust_guid_actd-v1.jpg"],
196+
[IAM_SCOPES.S3_DELETE_OBJECT]
197+
)
198+
).toBe(false);
199+
});

portal-ui/src/common/SecureComponent/accessControl.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ const hasPermission = (
5454
const replaceWildcard = wildcardItemSection
5555
.replace("/", "\\/")
5656
.replace("*", "($|\\/?(.*?))");
57-
58-
const inRegExp = new RegExp(`${replaceWildcard}$`, "gm");
59-
60-
if (inRegExp.exec(path)) {
57+
const inRegExp = new RegExp(`${replaceWildcard}`, "gm");
58+
// Avoid calling inRegExp multiple times and instead use the stored value if need it:
59+
// https://stackoverflow.com/questions/59694142/regex-testvalue-returns-true-when-logged-but-false-within-an-if-statement
60+
const matches = inRegExp.test(path);
61+
if (matches) {
6162
return element;
6263
}
63-
6464
return null;
6565
});
66-
6766
return items.filter((itm) => itm !== null);
6867
};
6968

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// This file is part of MinIO Console Server
2+
// Copyright (c) 2022 MinIO, Inc.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
import * as roles from "../utils/roles";
18+
import { Selector } from "testcafe";
19+
import * as functions from "../utils/functions";
20+
import { testBucketBrowseButtonFor } from "../utils/functions";
21+
22+
fixture("Delete Objects With Prefix Only policy").page(
23+
"http://localhost:9090/"
24+
);
25+
26+
export const sideBar = Selector("div.MuiGrid-root.MuiGrid-item");
27+
export const sideBarDeleteButton = sideBar.find("button").withText("Delete");
28+
const bucket1 = "test-1";
29+
const test1BucketBrowseButton = testBucketBrowseButtonFor(bucket1);
30+
const bucket2 = "test-2";
31+
const test2BucketBrowseButton = testBucketBrowseButtonFor(bucket2);
32+
const bucket3 = "test-3";
33+
const test3BucketBrowseButton = testBucketBrowseButtonFor(bucket3);
34+
test
35+
.before(async (t) => {
36+
await functions.setUpBucket(t, bucket1);
37+
await functions.uploadObjectToBucket(
38+
t,
39+
bucket1,
40+
"test.txt",
41+
"portal-ui/tests/uploads/test.txt"
42+
);
43+
})("Delete button is disabled for object inside bucket", async (t) => {
44+
await t
45+
.useRole(roles.deleteObjectWithPrefixOnly)
46+
.navigateTo(`http://localhost:9090/buckets`)
47+
.click(test1BucketBrowseButton)
48+
.click(
49+
Selector(".ReactVirtualized__Table__rowColumn").withText("test.txt")
50+
)
51+
.expect(sideBarDeleteButton.hasAttribute("disabled"))
52+
.ok();
53+
})
54+
.after(async (t) => {
55+
await functions.cleanUpBucketAndUploads(t, bucket1);
56+
});
57+
58+
test
59+
.before(async (t) => {
60+
await functions.setUpBucket(t, bucket2);
61+
await functions.uploadObjectToBucket(
62+
t,
63+
bucket2,
64+
"digitalinsights/xref_cust_guid_actd-v1.txt",
65+
"portal-ui/tests/uploads/test.txt"
66+
);
67+
})(
68+
"Delete button is enabled for object that matches prefix inside bucket",
69+
async (t) => {
70+
await t
71+
.useRole(roles.deleteObjectWithPrefixOnly)
72+
.navigateTo(`http://localhost:9090/buckets`)
73+
.click(test2BucketBrowseButton)
74+
.click(
75+
Selector(".ReactVirtualized__Table__rowColumn").withText(
76+
"digitalinsights"
77+
)
78+
)
79+
.click(
80+
Selector(".ReactVirtualized__Table__rowColumn").withText(
81+
"xref_cust_guid_actd-v1.txt"
82+
)
83+
)
84+
.expect(sideBarDeleteButton.hasAttribute("disabled"))
85+
.notOk();
86+
}
87+
)
88+
.after(async (t) => {
89+
await functions.cleanUpBucketAndUploads(t, bucket2);
90+
});
91+
92+
test
93+
.before(async (t) => {
94+
await functions.setUpBucket(t, bucket3);
95+
await functions.uploadObjectToBucket(
96+
t,
97+
bucket3,
98+
"digitalinsights/test.txt",
99+
"portal-ui/tests/uploads/test.txt"
100+
);
101+
})(
102+
"Delete button is disabled for object that doesn't matches prefix inside bucket",
103+
async (t) => {
104+
await t
105+
.useRole(roles.deleteObjectWithPrefixOnly)
106+
.navigateTo(`http://localhost:9090/buckets`)
107+
.click(test3BucketBrowseButton)
108+
.click(
109+
Selector(".ReactVirtualized__Table__rowColumn").withText(
110+
"digitalinsights"
111+
)
112+
)
113+
.click(
114+
Selector(".ReactVirtualized__Table__rowColumn").withText("test.txt")
115+
)
116+
.expect(sideBarDeleteButton.hasAttribute("disabled"))
117+
.ok();
118+
}
119+
)
120+
.after(async (t) => {
121+
await functions.cleanUpBucketAndUploads(t, bucket3);
122+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": ["s3:ListBucket"],
7+
"Resource": ["arn:aws:s3:::testbucket-*-test-1/*"]
8+
},
9+
{
10+
"Effect": "Allow",
11+
"Action": ["s3:GetBucketLocation"],
12+
"Resource": ["arn:aws:s3:::testbucket-*-test-1"]
13+
},
14+
{
15+
"Effect": "Allow",
16+
"Action": ["s3:*"],
17+
"Resource": [
18+
"arn:aws:s3:::testbucket-*-test-1/digitalinsights/xref_cust_guid_actd*"
19+
]
20+
},
21+
22+
{
23+
"Effect": "Allow",
24+
"Action": ["s3:ListBucket"],
25+
"Resource": ["arn:aws:s3:::testbucket-*-test-2/*"]
26+
},
27+
{
28+
"Effect": "Allow",
29+
"Action": ["s3:GetBucketLocation"],
30+
"Resource": ["arn:aws:s3:::testbucket-*-test-2"]
31+
},
32+
{
33+
"Effect": "Allow",
34+
"Action": ["s3:*"],
35+
"Resource": [
36+
"arn:aws:s3:::testbucket-*-test-2/digitalinsights/xref_cust_guid_actd*"
37+
]
38+
},
39+
40+
{
41+
"Effect": "Allow",
42+
"Action": ["s3:ListBucket"],
43+
"Resource": ["arn:aws:s3:::testbucket-*-test-3/*"]
44+
},
45+
{
46+
"Effect": "Allow",
47+
"Action": ["s3:GetBucketLocation"],
48+
"Resource": ["arn:aws:s3:::testbucket-*-test-3"]
49+
},
50+
{
51+
"Effect": "Allow",
52+
"Action": ["s3:*"],
53+
"Resource": [
54+
"arn:aws:s3:::testbucket-*-test-3/digitalinsights/xref_cust_guid_actd*"
55+
]
56+
}
57+
]
58+
}

portal-ui/tests/scripts/common.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ create_policies() {
4545
mc admin policy add minio inspect-allowed-$TIMESTAMP portal-ui/tests/policies/inspect-allowed.json
4646
mc admin policy add minio inspect-not-allowed-$TIMESTAMP portal-ui/tests/policies/inspect-not-allowed.json
4747
mc admin policy add minio fix-prefix-policy-ui-crash-$TIMESTAMP portal-ui/tests/policies/fix-prefix-policy-ui-crash.json
48+
mc admin policy add minio delete-object-with-prefix-$TIMESTAMP portal-ui/tests/policies/deleteObjectWithPrefix.json
4849
}
4950

5051
create_users() {
@@ -71,10 +72,12 @@ create_users() {
7172
mc admin user add minio inspect-allowed-$TIMESTAMP insallowed1234
7273
mc admin user add minio inspect-not-allowed-$TIMESTAMP insnotallowed1234
7374
mc admin user add minio prefix-policy-ui-crash-$TIMESTAMP poluicrashfix1234
75+
mc admin user add minio delete-object-with-prefix-$TIMESTAMP deleteobjectwithprefix1234
7476
}
7577

7678
create_buckets() {
7779
mc mb minio/testcafe && mc cp ./portal-ui/tests/uploads/test.txt minio/testcafe/write/test.txt
80+
mc mb minio/test && mc cp ./portal-ui/tests/uploads/test.txt minio/test/test.txt && mc cp ./portal-ui/tests/uploads/test.txt minio/test/digitalinsights/xref_cust_guid_actd-v1.txt && mc cp ./portal-ui/tests/uploads/test.txt minio/test/digitalinsights/test.txt
7881
}
7982

8083
assign_policies() {
@@ -100,4 +103,5 @@ assign_policies() {
100103
mc admin policy set minio bucketwriteprefixonlypolicy-$TIMESTAMP user=bucketwriteprefixonlypolicy-$TIMESTAMP
101104
mc admin policy set minio inspect-allowed-$TIMESTAMP user=inspect-allowed-$TIMESTAMP
102105
mc admin policy set minio inspect-not-allowed-$TIMESTAMP user=inspect-not-allowed-$TIMESTAMP
106+
mc admin policy set minio delete-object-with-prefix-$TIMESTAMP user=delete-object-with-prefix-$TIMESTAMP
103107
}

portal-ui/tests/scripts/permissions.sh

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,54 @@ export SCRIPT_DIR
1616
source "${SCRIPT_DIR}/common.sh"
1717

1818
remove_users() {
19-
mc admin user remove minio bucketassignpolicy-$TIMESTAMP
20-
mc admin user remove minio bucketread-$TIMESTAMP
21-
mc admin user remove minio bucketwrite-$TIMESTAMP
22-
mc admin user remove minio dashboard-$TIMESTAMP
23-
mc admin user remove minio diagnostics-$TIMESTAMP
24-
mc admin user remove minio groups-$TIMESTAMP
25-
mc admin user remove minio heal-$TIMESTAMP
26-
mc admin user remove minio iampolicies-$TIMESTAMP
27-
mc admin user remove minio logs-$TIMESTAMP
28-
mc admin user remove minio notificationendpoints-$TIMESTAMP
29-
mc admin user remove minio settings-$TIMESTAMP
30-
mc admin user remove minio tiers-$TIMESTAMP
31-
mc admin user remove minio trace-$TIMESTAMP
32-
mc admin user remove minio users-$TIMESTAMP
33-
mc admin user remove minio watch-$TIMESTAMP
34-
mc admin user remove minio bucketwriteprefixonlypolicy-$TIMESTAMP
35-
mc admin user remove minio inspect-allowed-$TIMESTAMP
36-
mc admin user remove minio inspect-not-allowed-$TIMESTAMP
37-
mc admin user remove minio prefix-policy-ui-crash-$TIMESTAMP
19+
mc admin user remove minio bucketassignpolicy-"$TIMESTAMP"
20+
mc admin user remove minio bucketread-"$TIMESTAMP"
21+
mc admin user remove minio bucketwrite-"$TIMESTAMP"
22+
mc admin user remove minio dashboard-"$TIMESTAMP"
23+
mc admin user remove minio diagnostics-"$TIMESTAMP"
24+
mc admin user remove minio groups-"$TIMESTAMP"
25+
mc admin user remove minio heal-"$TIMESTAMP"
26+
mc admin user remove minio iampolicies-"$TIMESTAMP"
27+
mc admin user remove minio logs-"$TIMESTAMP"
28+
mc admin user remove minio notificationendpoints-"$TIMESTAMP"
29+
mc admin user remove minio settings-"$TIMESTAMP"
30+
mc admin user remove minio tiers-"$TIMESTAMP"
31+
mc admin user remove minio trace-"$TIMESTAMP"
32+
mc admin user remove minio users-"$TIMESTAMP"
33+
mc admin user remove minio watch-"$TIMESTAMP"
34+
mc admin user remove minio bucketwriteprefixonlypolicy-"$TIMESTAMP"
35+
mc admin user remove minio inspect-allowed-"$TIMESTAMP"
36+
mc admin user remove minio inspect-not-allowed-"$TIMESTAMP"
37+
mc admin user remove minio prefix-policy-ui-crash-"$TIMESTAMP"
38+
mc admin user remove minio delete-object-with-prefix-"$TIMESTAMP"
3839
}
3940

4041
remove_policies() {
41-
mc admin policy remove minio bucketassignpolicy-$TIMESTAMP
42-
mc admin policy remove minio bucketread-$TIMESTAMP
43-
mc admin policy remove minio bucketwrite-$TIMESTAMP
44-
mc admin policy remove minio dashboard-$TIMESTAMP
45-
mc admin policy remove minio diagnostics-$TIMESTAMP
46-
mc admin policy remove minio groups-$TIMESTAMP
47-
mc admin policy remove minio heal-$TIMESTAMP
48-
mc admin policy remove minio iampolicies-$TIMESTAMP
49-
mc admin policy remove minio logs-$TIMESTAMP
50-
mc admin policy remove minio notificationendpoints-$TIMESTAMP
51-
mc admin policy remove minio settings-$TIMESTAMP
52-
mc admin policy remove minio tiers-$TIMESTAMP
53-
mc admin policy remove minio trace-$TIMESTAMP
54-
mc admin policy remove minio users-$TIMESTAMP
55-
mc admin policy remove minio watch-$TIMESTAMP
56-
mc admin policy remove minio bucketwriteprefixonlypolicy-$TIMESTAMP
57-
mc admin policy remove minio inspect-allowed-$TIMESTAMP
58-
mc admin policy remove minio inspect-not-allowed-$TIMESTAMP
59-
mc admin policy remove minio fix-prefix-policy-ui-crash-$TIMESTAMP
42+
mc admin policy remove minio bucketassignpolicy-"$TIMESTAMP"
43+
mc admin policy remove minio bucketread-"$TIMESTAMP"
44+
mc admin policy remove minio bucketwrite-"$TIMESTAMP"
45+
mc admin policy remove minio dashboard-"$TIMESTAMP"
46+
mc admin policy remove minio diagnostics-"$TIMESTAMP"
47+
mc admin policy remove minio groups-"$TIMESTAMP"
48+
mc admin policy remove minio heal-"$TIMESTAMP"
49+
mc admin policy remove minio iampolicies-"$TIMESTAMP"
50+
mc admin policy remove minio logs-"$TIMESTAMP"
51+
mc admin policy remove minio notificationendpoints-"$TIMESTAMP"
52+
mc admin policy remove minio settings-"$TIMESTAMP"
53+
mc admin policy remove minio tiers-"$TIMESTAMP"
54+
mc admin policy remove minio trace-"$TIMESTAMP"
55+
mc admin policy remove minio users-"$TIMESTAMP"
56+
mc admin policy remove minio watch-"$TIMESTAMP"
57+
mc admin policy remove minio bucketwriteprefixonlypolicy-"$TIMESTAMP"
58+
mc admin policy remove minio inspect-allowed-"$TIMESTAMP"
59+
mc admin policy remove minio inspect-not-allowed-"$TIMESTAMP"
60+
mc admin policy remove minio fix-prefix-policy-ui-crash-"$TIMESTAMP"
61+
mc admin policy remove minio delete-object-with-prefix-"$TIMESTAMP"
6062
}
6163

6264
remove_buckets() {
6365
mc rm minio/testcafe/write/test.txt && mc rm minio/testcafe
66+
mc rm minio/test/test.txt && mc rm minio/test/digitalinsights/xref_cust_guid_actd-v1.txt && mc rm minio/test/digitalinsights/test.txt && mc rm minio/test
6467
}
6568

6669
cleanup() {
@@ -70,8 +73,8 @@ cleanup() {
7073
}
7174

7275
__init__() {
73-
export TIMESTAMP=$(date "+%s")
74-
echo $TIMESTAMP > portal-ui/tests/constants/timestamp.txt
76+
TIMESTAMP=$(date "+%s")
77+
echo "$TIMESTAMP" > portal-ui/tests/constants/timestamp.txt
7578
export GOPATH=/tmp/gopath
7679
export PATH=${PATH}:${GOPATH}/bin
7780

0 commit comments

Comments
 (0)