Skip to content

Commit 2894391

Browse files
authored
feat(ci): e2e attribute definitions tests (#384)
Resolves #327
1 parent fd76f0e commit 2894391

File tree

5 files changed

+176
-53
lines changed

5 files changed

+176
-53
lines changed

adr/0000-use-adr-in-directory.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

cmd/policy-attributes.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,17 @@ func policy_deactivateAttribute(cmd *cobra.Command, args []string) {
132132
defer h.Close()
133133

134134
id := c.Flags.GetRequiredID("id")
135+
force := c.Flags.GetOptionalBool("force")
135136

136137
attr, err := h.GetAttribute(id)
137138
if err != nil {
138139
errMsg := fmt.Sprintf("Failed to get attribute (%s)", id)
139140
cli.ExitWithError(errMsg, err)
140141
}
141142

142-
cli.ConfirmAction(cli.ActionDeactivate, "attribute", attr.GetName(), false)
143+
if !force {
144+
cli.ConfirmAction(cli.ActionDeactivate, "attribute", attr.GetName(), false)
145+
}
143146

144147
attr, err = h.DeactivateAttribute(id)
145148
if err != nil {
@@ -371,6 +374,11 @@ func init() {
371374
deactivateDoc.GetDocFlag("id").Default,
372375
deactivateDoc.GetDocFlag("id").Description,
373376
)
377+
deactivateDoc.Flags().Bool(
378+
deactivateDoc.GetDocFlag("force").Name,
379+
false,
380+
deactivateDoc.GetDocFlag("force").Description,
381+
)
374382

375383
// unsafe actions on attributes
376384
unsafeCmd := man.Docs.GetCommand("policy/attributes/unsafe")

docs/man/policy/attributes/deactivate.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ command:
77
shorthand: i
88
description: ID of the attribute
99
required: true
10+
- name: force
11+
description: Force deactivation without interactive confirmation (dangerous)
1012
---
1113

1214
# Deactivate an attribute definition

docs/man/policy/attributes/namespaces/deactivate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ command:
88
description: ID of the attribute namespace
99
required: true
1010
- name: force
11-
description: Force deletion without interactive confirmation (dangerous)
11+
description: Force deactivation without interactive confirmation (dangerous)
1212
---
1313

1414
# Deactivate an attribute namespace

e2e/attributes.bats

Lines changed: 164 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,174 @@
22

33
# Tests for attributes
44

5-
# Create attribute
5+
setup_file() {
6+
echo -n '{"clientId":"opentdf","clientSecret":"secret"}' > creds.json
7+
export WITH_CREDS='--with-client-creds-file ./creds.json'
8+
export HOST='--host http://localhost:8080'
69

7-
# Get Attribute
10+
# Create the namespace to be used by other tests
811

9-
# Update attribute
12+
export NS_NAME="testing-attr.co"
13+
export NS_ID=$(./otdfctl $HOST $WITH_CREDS policy attributes namespaces create -n "$NS_NAME" --json | jq -r '.id')
14+
}
1015

11-
# List attributes
16+
# always create a randomly named attribute
17+
setup() {
18+
load "${BATS_LIB_PATH}/bats-support/load.bash"
19+
load "${BATS_LIB_PATH}/bats-assert/load.bash"
1220

13-
# Deactivate Attribute
21+
# invoke binary with credentials
22+
run_otdfctl_attr () {
23+
run sh -c "./otdfctl $HOST $WITH_CREDS policy attributes $*"
24+
}
1425

15-
# Unsafe Reactivate
26+
export ATTR_NAME_RANDOM=$(LC_ALL=C tr -dc 'a-zA-Z' < /dev/urandom | head -c 16)
27+
export ATTR_ID=$(./otdfctl $HOST $WITH_CREDS policy attributes create --namespace "$NS_ID" --name "$ATTR_NAME_RANDOM" --rule ANY_OF -l key=value --json | jq -r '.id')
28+
}
1629

17-
# Unsafe Delete
30+
# always unsafely delete the created attribute
31+
teardown() {
32+
./otdfctl $HOST $WITH_CREDS policy attributes unsafe delete --force --id "$ATTR_ID"
33+
}
1834

19-
# Cleanup -- delete everything created here
35+
teardown_file() {
36+
# remove the namespace
37+
./otdfctl $HOST $WITH_CREDS policy attributes namespaces unsafe delete --id "$NS_ID" --force
38+
39+
# clear out all test env vars
40+
unset HOST WITH_CREDS NS_NAME NS_ID ATTR_NAME_RANDOM
41+
}
42+
43+
@test "Create an attribute - With Values" {
44+
run_otdfctl_attr create --name attrWithValues --namespace "$NS_ID" --rule HIERARCHY -v val1 -v val2 --json
45+
assert_success
46+
[ "$( echo "$output" | jq -r '.values[0].value' )" = "val1" ]
47+
[ "$( echo "$output" | jq -r '.values[1].value' )" = "val2" ]
48+
}
49+
50+
@test "Create an attribute - Bad" {
51+
# bad rule
52+
run_otdfctl_attr create --name attr1 --namespace "$NS_ID" --rule NONEXISTENT
53+
assert_failure
54+
assert_output --partial "invalid attribute rule: NONEXISTENT, must be one of [ALL_OF, ANY_OF, HIERARCHY]"
55+
56+
# missing flags
57+
run_otdfctl_attr create --name attr1 --rule ALL_OF
58+
assert_failure
59+
run_otdfctl_attr create --name attr1 --namespace "$NS_ID"
60+
assert_failure
61+
run_otdfctl_attr create --rule HIERARCHY --namespace "$NS_ID"
62+
assert_failure
63+
}
64+
65+
@test "Get an attribute definition - Good" {
66+
LOWERED=$(echo "$ATTR_NAME_RANDOM" | awk '{print tolower($0)}')
67+
68+
run_otdfctl_attr get --id "$ATTR_ID"
69+
assert_success
70+
assert_output --regexp "Id.*$ATTR_ID"
71+
assert_output --regexp "Name.*$LOWERED"
72+
assert_output --partial "ANY_OF"
73+
assert_output --regexp "Namespace.*$NS_NAME"
74+
75+
run_otdfctl_attr get --id "$ATTR_ID" --json
76+
assert_success
77+
[ "$(echo "$output" | jq -r '.id')" = "$ATTR_ID" ]
78+
[ "$(echo "$output" | jq -r '.name')" = "$LOWERED" ]
79+
[ "$(echo "$output" | jq -r '.rule')" = 2 ]
80+
[ "$(echo "$output" | jq -r '.namespace.id')" = "$NS_ID" ]
81+
[ "$(echo "$output" | jq -r '.namespace.name')" = "$NS_NAME" ]
82+
[ "$(echo "$output" | jq -r '.metadata.labels.key')" = "value" ]
83+
}
84+
85+
@test "Get an attribute definition - Bad" {
86+
# no id flag
87+
run_otdfctl_attr get
88+
assert_failure
89+
}
90+
91+
@test "Update an attribute definition (Safe) - Good" {
92+
# replace labels
93+
run_otdfctl_attr update --force-replace-labels -l key=somethingElse --id "$ATTR_ID" --json
94+
assert_success
95+
[ "$(echo $output | jq -r '.metadata.labels.key')" = "somethingElse" ]
96+
97+
# extend labels
98+
run_otdfctl_attr update -l other=testing --id "$ATTR_ID" --json
99+
assert_success
100+
[ "$(echo $output | jq -r '.metadata.labels.other')" = "testing" ]
101+
[ "$(echo $output | jq -r '.metadata.labels.key')" = "somethingElse" ]
102+
}
103+
104+
@test "Update an attribute definition (Safe) - Bad" {
105+
# no id
106+
run_otdfctl_attr update
107+
assert_failure
108+
}
109+
110+
@test "List attribute definitions" {
111+
run_otdfctl_attr list
112+
assert_success
113+
assert_output --partial "$ATTR_ID"
114+
115+
run_otdfctl_attr list --state active
116+
assert_success
117+
assert_output --partial "$ATTR_ID"
118+
119+
run_otdfctl_attr list --state inactive
120+
assert_success
121+
refute_output --partial "$ATTR_ID"
122+
}
123+
124+
@test "Deactivate then unsafe reactivate an attribute definition" {
125+
run_otdfctl_attr deactivate
126+
assert_failure
127+
128+
run_otdfctl_attr get --id "$ATTR_ID" --json
129+
assert_success
130+
[ "$(echo "$output" | jq -r '.active.value')" = true ]
131+
132+
run_otdfctl_attr deactivate --id "$ATTR_ID" --force
133+
assert_success
134+
135+
run_otdfctl_attr get --id "$ATTR_ID" --json
136+
assert_success
137+
[ "$(echo "$output" | jq -r '.active')" = {} ]
138+
139+
run_otdfctl_attr unsafe reactivate
140+
assert_failure
141+
142+
run_otdfctl_attr unsafe reactivate --id "$ATTR_ID" --force
143+
assert_success
144+
145+
run_otdfctl_attr get --id "$ATTR_ID" --json
146+
assert_success
147+
[ "$(echo "$output" | jq -r '.active.value')" = true ]
148+
}
149+
150+
@test "Unsafe Update an attribute definition" {
151+
# create with two values
152+
run_otdfctl_attr create --name created --namespace "$NS_ID" --rule HIERARCHY -v val1 -v val2 --json
153+
CREATED_ID=$(echo "$output" | jq -r '.id')
154+
VAL1_ID=$(echo "$output" | jq -r '.values[0].id')
155+
VAL2_ID=$(echo "$output" | jq -r '.values[1].id')
156+
157+
run_otdfctl_attr unsafe update --name updated --id "$CREATED_ID" --json --force
158+
assert_success
159+
run_otdfctl_attr get --id "$CREATED_ID" --json
160+
assert_success
161+
[ "$(echo "$output" | jq -r '.name')" = "updated" ]
162+
163+
run_otdfctl_attr unsafe update --rule ALL_OF --id "$CREATED_ID" --json --force
164+
assert_success
165+
run_otdfctl_attr get --id "$CREATED_ID" --json
166+
assert_success
167+
[ "$(echo "$output" | jq -r '.rule')" = 1 ]
168+
169+
run_otdfctl_attr unsafe update --id "$CREATED_ID" --json --values-order "$VAL2_ID" --values-order "$VAL1_ID" --force
170+
assert_success
171+
run_otdfctl_attr get --id "$CREATED_ID" --json
172+
assert_success
173+
[ "$(echo "$output" | jq -r '.values[0].value')" = "val2" ]
174+
[ "$(echo "$output" | jq -r '.values[1].value')" = "val1" ]
175+
}

0 commit comments

Comments
 (0)