Skip to content

Commit

Permalink
Added tests for new functionality and corrected disallowed method for…
Browse files Browse the repository at this point in the history
… patch
  • Loading branch information
crobby committed Jan 15, 2025
1 parent 1c7d5f9 commit 2ea5199
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/resources/common/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func formatter(summarycache *summarycache.SummaryCache, asl accesscontrol.Access
delete(resource.Links, "remove")
}
if hasPatch {
if attributes.DisallowMethods(resource.Schema)[http.MethodPut] {
if attributes.DisallowMethods(resource.Schema)[http.MethodPatch] {
resource.Links["patch"] = "blocked"
} else {
resource.Links["patch"] = u
Expand Down
80 changes: 80 additions & 0 deletions pkg/resources/common/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ func Test_formatterLinks(t *testing.T) {
hasGet bool
hasUpdate bool
hasRemove bool
hasPatch bool
}
tests := []struct {
name string
Expand Down Expand Up @@ -935,6 +936,79 @@ func Test_formatterLinks(t *testing.T) {
"view": "/api/v1/namespaces/example-ns/pods/example-pod",
},
},
{
name: "patch permissions",
hasUser: true,
permissions: &permissions{
hasPatch: true,
},
schema: &types.APISchema{
Schema: &schemas.Schema{
ID: "example",
Attributes: map[string]interface{}{
"group": "apps",
"version": "v1",
"resource": "deployments",
},
},
},
apiObject: types.APIObject{
ID: "example",
Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "example-deployment",
Namespace: "example-ns",
},
},
},
currentLinks: map[string]string{
"default": "defaultVal",
"view": "/apis/apps/v1/namespaces/example-ns/deployments/example-deployment",
},
wantLinks: map[string]string{
"default": "defaultVal",
"patch": "/apis/apps/v1/namespaces/example-ns/deployments/example-deployment",
"view": "/apis/apps/v1/namespaces/example-ns/deployments/example-deployment",
},
},
{
name: "patch permissions, but blocked",
hasUser: true,
permissions: &permissions{
hasPatch: true,
},
schema: &types.APISchema{
Schema: &schemas.Schema{
ID: "example",
Attributes: map[string]interface{}{
"group": "apps",
"version": "v1",
"resource": "deployments",
"disallowMethods": map[string]bool{
http.MethodPatch: true,
},
},
},
},
apiObject: types.APIObject{
ID: "example",
Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "example-deployment",
Namespace: "example-ns",
},
},
},
currentLinks: map[string]string{
"default": "defaultVal",
"view": "/apis/apps/v1/namespaces/example-ns/deployments/example-deployment",
},
wantLinks: map[string]string{
"default": "defaultVal",
"patch": "blocked",
"view": "/apis/apps/v1/namespaces/example-ns/deployments/example-deployment",
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -964,6 +1038,12 @@ func Test_formatterLinks(t *testing.T) {
ResourceName: meta.GetName(),
})
}
if test.permissions.hasPatch {
accessSet.Add("patch", gvr.GroupResource(), accesscontrol.Access{
Namespace: meta.GetNamespace(),
ResourceName: meta.GetName(),
})
}
asl.EXPECT().AccessFor(&defaultUserInfo).Return(&accessSet)
} else {
asl.EXPECT().AccessFor(&defaultUserInfo).Return(nil).AnyTimes()
Expand Down
11 changes: 10 additions & 1 deletion pkg/schema/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func TestSchemas(t *testing.T) {
errDesired: false,
},
},
{
name: "basic patch schema test",
config: schemaTestConfig{
permissionVerbs: []string{"patch"},
desiredResourceVerbs: []string{"PATCH"},
desiredCollectionVerbs: []string{},
errDesired: false,
},
},
}
for _, test := range tests {
test := test
Expand Down Expand Up @@ -162,7 +171,7 @@ func makeSchema(resourceType string) *types.APISchema {
"group": testGroup,
"version": testVersion,
"resource": resourceType,
"verbs": []string{"get", "list", "watch", "delete", "update", "create"},
"verbs": []string{"get", "list", "watch", "delete", "update", "create", "patch"},
},
},
}
Expand Down

0 comments on commit 2ea5199

Please sign in to comment.