-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1949 from josephschorr/steelthread-tests
Start on a steel thread test framework
- Loading branch information
Showing
9 changed files
with
3,647 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build steelthread | ||
// +build steelthread | ||
|
||
package steelthreadtesting | ||
|
||
import v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" | ||
|
||
type steelThreadTestCase struct { | ||
name string | ||
datafile string | ||
operations []steelThreadOperationCase | ||
} | ||
|
||
type steelThreadOperationCase struct { | ||
name string | ||
operationName string | ||
arguments map[string]any | ||
} | ||
|
||
type stOperation func(parameters map[string]any, client v1.PermissionsServiceClient) (any, error) | ||
|
||
var steelThreadTestCases = []steelThreadTestCase{ | ||
{ | ||
name: "basic lookup subjects", | ||
datafile: "basic-document.yaml", | ||
operations: []steelThreadOperationCase{ | ||
{ | ||
name: "uncursored lookup subjects for somedoc", | ||
operationName: "lookupSubjects", | ||
arguments: map[string]any{ | ||
"resource_type": "document", | ||
"resource_object_id": "somedoc", | ||
"permission": "view", | ||
"subject_type": "user", | ||
}, | ||
}, | ||
{ | ||
name: "uncursored lookup subjects for public doc", | ||
operationName: "lookupSubjects", | ||
arguments: map[string]any{ | ||
"resource_type": "document", | ||
"resource_object_id": "publicdoc", | ||
"permission": "view", | ||
"subject_type": "user", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//go:build steelthread | ||
// +build steelthread | ||
|
||
package steelthreadtesting | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/authzed/spicedb/pkg/genutil/mapz" | ||
) | ||
|
||
func lookupSubjects(parameters map[string]any, client v1.PermissionsServiceClient) (any, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) | ||
defer cancel() | ||
|
||
r, err := client.LookupSubjects(ctx, &v1.LookupSubjectsRequest{ | ||
Resource: &v1.ObjectReference{ | ||
ObjectType: parameters["resource_type"].(string), | ||
ObjectId: parameters["resource_object_id"].(string), | ||
}, | ||
Permission: parameters["permission"].(string), | ||
SubjectObjectType: parameters["subject_type"].(string), | ||
Consistency: &v1.Consistency{ | ||
Requirement: &v1.Consistency_FullyConsistent{ | ||
FullyConsistent: true, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
foundSubjects := mapz.NewSet[string]() | ||
for { | ||
resp, err := r.Recv() | ||
if err != nil { | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
if !foundSubjects.Add(formatResolvedSubject(resp)) { | ||
return nil, errors.New("duplicate subject found") | ||
} | ||
} | ||
|
||
foundSubjectsSlice := foundSubjects.AsSlice() | ||
sort.Strings(foundSubjectsSlice) | ||
|
||
yamlNodes := make([]yaml.Node, 0, len(foundSubjectsSlice)) | ||
for _, subject := range foundSubjectsSlice { | ||
yamlNodes = append(yamlNodes, yaml.Node{ | ||
Kind: yaml.ScalarNode, | ||
Value: subject, | ||
Style: yaml.SingleQuotedStyle, | ||
}) | ||
} | ||
return yamlNodes, nil | ||
} | ||
|
||
var operations = map[string]stOperation{ | ||
"lookupSubjects": lookupSubjects, | ||
} | ||
|
||
func formatResolvedSubject(sub *v1.LookupSubjectsResponse) string { | ||
var sb strings.Builder | ||
sb.WriteString(sub.Subject.SubjectObjectId) | ||
|
||
if len(sub.ExcludedSubjects) > 0 { | ||
excludedSubjectStrings := make([]string, 0, len(sub.ExcludedSubjects)) | ||
for _, excluded := range sub.ExcludedSubjects { | ||
excludedSubjectString := excluded.SubjectObjectId | ||
if excluded.Permissionship == v1.LookupPermissionship_LOOKUP_PERMISSIONSHIP_CONDITIONAL_PERMISSION { | ||
excludedSubjectString += " (conditional)" | ||
} | ||
|
||
excludedSubjectStrings = append(excludedSubjectStrings, excludedSubjectString) | ||
} | ||
sort.Strings(excludedSubjectStrings) | ||
|
||
sb.WriteString(" - [") | ||
sb.WriteString(strings.Join(excludedSubjectStrings, ", ")) | ||
sb.WriteString("]") | ||
} | ||
|
||
if sub.Subject.Permissionship == v1.LookupPermissionship_LOOKUP_PERMISSIONSHIP_CONDITIONAL_PERMISSION { | ||
sb.WriteString(" (conditional)") | ||
} | ||
|
||
return sb.String() | ||
} |
7 changes: 7 additions & 0 deletions
7
...steelresults/basic-lookup-subjects-uncursored-lookup-subjects-for-public-doc-results.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- '* - [user-0, user-1, user-2, user-3, user-4, user-5]' | ||
- 'user-10' | ||
- 'user-6' | ||
- 'user-7' | ||
- 'user-8' | ||
- 'user-9' |
Oops, something went wrong.