-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Juju 7058/fix pagination for entitlement (#1435)
* fix paging mechanism for openfga tuples
- Loading branch information
1 parent
5fc14d9
commit 33003cf
Showing
5 changed files
with
103 additions
and
52 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 |
---|---|---|
|
@@ -192,7 +192,7 @@ func TestListObjectRelations(t *testing.T) { | |
u := openfga.NewUser(&dbmodel.Identity{Name: "[email protected]"}, ofgaClient) | ||
u.JimmAdmin = true | ||
|
||
user, _, controller, model, _, _, _ := createTestControllerEnvironment(ctx, c, j.Database) | ||
user, group, controller, model, _, cloud, _ := createTestControllerEnvironment(ctx, c, j.Database) | ||
c.Assert(err, qt.IsNil) | ||
|
||
err = j.AddRelation(ctx, u, []apiparams.RelationshipTuple{ | ||
|
@@ -211,28 +211,61 @@ func TestListObjectRelations(t *testing.T) { | |
Relation: names.AuditLogViewerRelation.String(), | ||
TargetObject: controller.ResourceTag().String(), | ||
}, | ||
{ | ||
Object: user.Tag().String(), | ||
Relation: names.AdministratorRelation.String(), | ||
TargetObject: controller.ResourceTag().String(), | ||
}, | ||
{ | ||
Object: user.Tag().String(), | ||
Relation: names.AdministratorRelation.String(), | ||
TargetObject: cloud.ResourceTag().String(), | ||
}, | ||
{ | ||
Object: user.Tag().String(), | ||
Relation: names.CanAddModelRelation.String(), | ||
TargetObject: cloud.ResourceTag().String(), | ||
}, | ||
{ | ||
Object: user.Tag().String(), | ||
Relation: names.MemberRelation.String(), | ||
TargetObject: group.ResourceTag().String(), | ||
}, | ||
}) | ||
|
||
c.Assert(err, qt.IsNil) | ||
type ExpectedTuple struct { | ||
expectedRelation string | ||
expectedTargetId string | ||
} | ||
|
||
testCases := []struct { | ||
description string | ||
object string | ||
initialToken pagination.EntitlementToken | ||
expectedError string | ||
expectedLength int | ||
expectedTuples []ExpectedTuple | ||
description string | ||
object string | ||
initialToken pagination.EntitlementToken | ||
pageSize int32 | ||
expectNumPages int | ||
expectedError string | ||
expectedTuplesLength int | ||
expectedTuples []ExpectedTuple | ||
}{ | ||
{ | ||
description: "test listing all relations", | ||
object: user.Tag().String(), | ||
expectedLength: 3, | ||
description: "test listing all relations in single page", | ||
object: user.Tag().String(), | ||
pageSize: 10, | ||
expectNumPages: 1, | ||
expectedTuplesLength: 7, | ||
}, | ||
{ | ||
description: "test listing all relations in multiple pages", | ||
object: user.Tag().String(), | ||
pageSize: 2, | ||
expectNumPages: 4, | ||
expectedTuplesLength: 7, | ||
}, | ||
{ | ||
description: "invalid initial token", | ||
object: user.Tag().String(), | ||
initialToken: pagination.NewEntitlementToken("bar"), | ||
expectedError: "failed to decode pagination token.*", | ||
}, | ||
|
@@ -247,19 +280,22 @@ func TestListObjectRelations(t *testing.T) { | |
c.Run(t.description, func(c *qt.C) { | ||
token := t.initialToken | ||
tuples := []openfga.Tuple{} | ||
numPages := 0 | ||
for { | ||
res, nextToken, err := j.ListObjectRelations(ctx, u, t.object, 10, token) | ||
res, nextToken, err := j.ListObjectRelations(ctx, u, t.object, t.pageSize, token) | ||
if t.expectedError != "" { | ||
c.Assert(err, qt.ErrorMatches, t.expectedError) | ||
break | ||
} | ||
tuples = append(tuples, res...) | ||
numPages += 1 | ||
if nextToken.String() == "" { | ||
break | ||
} | ||
token = nextToken | ||
} | ||
c.Assert(tuples, qt.HasLen, t.expectedLength) | ||
c.Assert(numPages, qt.Equals, t.expectNumPages) | ||
c.Assert(tuples, qt.HasLen, t.expectedTuplesLength) | ||
for i, expectedTuple := range t.expectedTuples { | ||
c.Assert(tuples[i].Relation.String(), qt.Equals, expectedTuple.expectedRelation) | ||
c.Assert(tuples[i].Target.ID, qt.Equals, expectedTuple.expectedTargetId) | ||
|
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