Repository navigation
feat: import test tuples and context into assertions #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
@@ -494,3 +495,185 @@ func setupWriteAssertionsMock( | |
| mockWriteAssertions.EXPECT().Options(expectedOptions).Return(mockWriteAssertions) | ||
| mockWriteAssertions.EXPECT().Execute().Return(nil, nil) | ||
| } | ||
|
|
||
| func TestImportStoreAssertionsCarryTestTuplesAndContext(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| modelID, storeID := testModelID, testStoreID | ||
| expectedOptions := client.ClientWriteAssertionsOptions{AuthorizationModelId: &modelID, StoreId: &storeID} | ||
|
|
||
| testTuples := []client.ClientContextualTupleKey{ | ||
| {User: "folder:5", Relation: "parent", Object: "folder:product-2021"}, | ||
| } | ||
| context := map[string]any{"some": "value"} | ||
|
|
||
| expectedAssertions := []client.ClientAssertion{ | ||
| { | ||
| User: "user:anne", | ||
| Relation: "viewer", | ||
| Object: "folder:product-2021", | ||
| Expectation: true, | ||
| Context: &context, | ||
| ContextualTuples: testTuples, | ||
| }, | ||
| } | ||
|
|
||
| mockCtrl := gomock.NewController(t) | ||
| mockFgaClient := mockclient.NewMockSdkClient(mockCtrl) | ||
|
|
||
| defer mockCtrl.Finish() | ||
|
|
||
| setupWriteAssertionsMock(mockCtrl, mockFgaClient, expectedAssertions, expectedOptions) | ||
| setupWriteModelMock(mockCtrl, mockFgaClient, modelID) | ||
| setupCreateStoreMock(mockCtrl, mockFgaClient, storeID) | ||
|
|
||
| testStore := storetest.StoreData{ | ||
| Model: `type user | ||
| type folder | ||
| relations | ||
| define parent: [folder] | ||
| define viewer: [user]`, | ||
| Tests: []storetest.ModelTest{ | ||
| { | ||
| Name: "Test", | ||
| Tuples: testTuples, | ||
| Check: []storetest.ModelTestCheck{ | ||
| { | ||
| User: "user:anne", | ||
| Object: "folder:product-2021", | ||
| Context: &context, | ||
| Assertions: map[string]bool{"viewer": true}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| _, err := importStore(t.Context(), &fga.ClientConfig{}, mockFgaClient, &testStore, "", "", 10, 1, "") | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestImportStoreAssertionsDropTestTuplesOverLimit(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| modelID, storeID := testModelID, testStoreID | ||
| expectedOptions := client.ClientWriteAssertionsOptions{AuthorizationModelId: &modelID, StoreId: &storeID} | ||
|
|
||
| // One more tuple than an assertion accepts as contextual tuples | ||
| testTuples := make([]client.ClientContextualTupleKey, maxContextualTuplesPerAssertion+1) | ||
| for i := range testTuples { | ||
| testTuples[i] = client.ClientContextualTupleKey{ | ||
| User: fmt.Sprintf("user:%d", i), Relation: "viewer", Object: "document:doc1", | ||
| } | ||
| } | ||
|
|
||
| expectedAssertions := []client.ClientAssertion{ | ||
| {User: "user:anne", Relation: "viewer", Object: "document:doc1", Expectation: false}, | ||
| } | ||
|
|
||
| mockCtrl := gomock.NewController(t) | ||
| mockFgaClient := mockclient.NewMockSdkClient(mockCtrl) | ||
|
|
||
| defer mockCtrl.Finish() | ||
|
|
||
| setupWriteAssertionsMock(mockCtrl, mockFgaClient, expectedAssertions, expectedOptions) | ||
| setupWriteModelMock(mockCtrl, mockFgaClient, modelID) | ||
| setupCreateStoreMock(mockCtrl, mockFgaClient, storeID) | ||
|
|
||
| testStore := storetest.StoreData{ | ||
| Model: `type user | ||
| type document | ||
| relations | ||
| define viewer: [user]`, | ||
| Tests: []storetest.ModelTest{ | ||
| { | ||
| Name: "Test", | ||
| Tuples: testTuples, | ||
| Check: []storetest.ModelTestCheck{ | ||
| { | ||
| User: "user:anne", | ||
| Object: "document:doc1", | ||
| Assertions: map[string]bool{"viewer": false}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| _, err := importStore(t.Context(), &fga.ClientConfig{}, mockFgaClient, &testStore, "", "", 10, 1, "") | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestDropContextualTuplesOverSizeLimit(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tuples := make([]client.ClientContextualTupleKey, maxContextualTuplesPerAssertion) | ||
| for i := range tuples { | ||
| tuples[i] = client.ClientContextualTupleKey{ | ||
| User: fmt.Sprintf("user:member-%02d", i), Relation: "viewer", Object: "document:roadmap-2026", | ||
| } | ||
| } | ||
|
|
||
| t.Run("small requests are left alone", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| assertions := []client.ClientAssertion{ | ||
| {User: "user:anne", Relation: "viewer", Object: "document:roadmap-2026", ContextualTuples: tuples}, | ||
| } | ||
| dropContextualTuplesOverSizeLimit(assertions) | ||
| require.Len(t, assertions[0].ContextualTuples, maxContextualTuplesPerAssertion) | ||
| }) | ||
|
|
||
| t.Run("the last assertions lose their tuples until the request fits", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| assertions := make([]client.ClientAssertion, maxAssertionsPerWrite) | ||
| for i := range assertions { | ||
| assertions[i] = client.ClientAssertion{ | ||
| User: fmt.Sprintf("user:%d", i), Relation: "viewer", Object: "document:roadmap-2026", | ||
| ContextualTuples: tuples, | ||
| } | ||
| } | ||
|
|
||
| dropContextualTuplesOverSizeLimit(assertions) | ||
|
|
||
| total := 0 | ||
| for _, assertion := range assertions { | ||
| total += assertionSize(assertion) | ||
| } | ||
|
|
||
| require.LessOrEqual(t, total, maxAssertionsRequestBytes) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: these checks measure with
|
||
| require.Len(t, assertions[0].ContextualTuples, maxContextualTuplesPerAssertion) | ||
| require.Empty(t, assertions[len(assertions)-1].ContextualTuples) | ||
| }) | ||
|
|
||
| t.Run("the last assertions lose their context when dropping tuples isn't enough", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| values := make([]any, 60) | ||
| for i := range values { | ||
| values[i] = i | ||
| } | ||
|
|
||
| checkContext := map[string]any{"allowed": values} | ||
|
|
||
| assertions := make([]client.ClientAssertion, maxAssertionsPerWrite) | ||
| for i := range assertions { | ||
| assertions[i] = client.ClientAssertion{ | ||
| User: fmt.Sprintf("user:%d", i), Relation: "viewer", Object: "document:roadmap-2026", | ||
| Context: &checkContext, | ||
| } | ||
| } | ||
|
|
||
| dropContextualTuplesOverSizeLimit(assertions) | ||
|
|
||
| total := 0 | ||
| for _, assertion := range assertions { | ||
| total += assertionSize(assertion) | ||
| } | ||
|
|
||
| require.LessOrEqual(t, total, maxAssertionsRequestBytes) | ||
| require.NotNil(t, assertions[0].Context) | ||
| require.Nil(t, assertions[len(assertions)-1].Context) | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.