Skip to content

Commit

Permalink
Fix tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-madi committed Feb 29, 2024
1 parent 080f36b commit f5a5ef0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
54 changes: 41 additions & 13 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,12 @@ func TestRebuildUserChannels(t *testing.T) {

user2, err := auth.GetUser("testUser")
assert.NoError(t, err)
assert.Equal(t, ch.AtSequence(ch.BaseSetOf(t, "explicit1", "derived1", "derived2", "!"), 1), user2.Channels())
expected := ch.TimedSet{
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.DynamicGrant},
"explicit1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.AdminGrant},
"derived1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.DynamicGrant},
"derived2": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.DynamicGrant}}
assert.Equal(t, expected, user2.Channels())
}

func TestRebuildUserChannelsMultiCollection(t *testing.T) {
Expand Down Expand Up @@ -430,8 +435,15 @@ func TestRebuildUserChannelsMultiCollection(t *testing.T) {

user2, err := auth.GetUser("testUser")
assert.NoError(t, err)
assert.Equal(t, ch.AtSequence(ch.BaseSetOf(t, "explicit1", "derived1", "derived2", "!"), 1), user2.Channels())
assert.Equal(t, ch.AtSequence(ch.BaseSetOf(t, "explicit2", "derived3", "derived4", "!"), 1), user2.CollectionChannels("scope1", "collection1"))

assert.Equal(t, ch.TimedSet{"explicit1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant},
"derived1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"derived2": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant}}, user2.Channels())
assert.Equal(t, ch.TimedSet{"explicit2": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant},
"derived3": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"derived4": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant}}, user2.CollectionChannels("scope1", "collection1"))
}

func TestRebuildUserChannelsNamedCollection(t *testing.T) {
Expand All @@ -454,11 +466,15 @@ func TestRebuildUserChannelsNamedCollection(t *testing.T) {

err = auth.InvalidateChannels("testUser", true, "scope1", "collection1", 2)
assert.NoError(t, err)

expected := ch.TimedSet{
"derived4": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"derived3": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"explicit2": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant}}
user2, err := auth.GetUser("testUser")
assert.NoError(t, err)
assert.Equal(t, ch.TimedSet(nil), user2.Channels())
assert.Equal(t, ch.AtSequence(ch.BaseSetOf(t, "explicit2", "derived3", "derived4", "!"), 1), user2.CollectionChannels("scope1", "collection1"))
assert.Equal(t, expected, user2.CollectionChannels("scope1", "collection1"))
}

// Test cases
Expand All @@ -485,7 +501,12 @@ func TestRebuildRoleChannels(t *testing.T) {

role2, err := auth.GetRole("testRole")
assert.Equal(t, nil, err)
assert.Equal(t, ch.AtSequence(ch.BaseSetOf(t, "explicit1", "derived1", "derived2", "!"), 1), role2.Channels())
expected := ch.TimedSet{
"derived1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"derived2": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.DynamicGrant},
"explicit1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant}}
assert.Equal(t, expected, role2.Channels())
}

func TestRebuildChannelsError(t *testing.T) {
Expand Down Expand Up @@ -526,8 +547,10 @@ func TestRebuildUserRoles(t *testing.T) {
// Retrieve the user, triggers initial build of roles
user1, err := auth.GetUser("testUser")
assert.Equal(t, nil, err)
expected := ch.AtSequence(base.SetOf("role1", "role3"), 1)
expected.AddChannel("role2", 3, ch.AdminGrant)
expected := ch.TimedSet{
"role3": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant},
"role1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant}}
expected.AddChannel("role2", 3, "")
assert.Equal(t, expected, user1.RoleNames())

// Invalidate the roles, triggers rebuild
Expand All @@ -536,8 +559,11 @@ func TestRebuildUserRoles(t *testing.T) {

user2, err := auth.GetUser("testUser")
assert.Equal(t, nil, err)
expected = ch.AtSequence(base.SetOf("role1", "role3"), 1)
expected.AddChannel("role2", 3, ch.AdminGrant)
expected = ch.TimedSet{
"role3": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant},
"role1": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(1), Source: ch.AdminGrant}}

expected.AddChannel("role2", 3, "")
assert.Equal(t, expected, user2.RoleNames())
}

Expand Down Expand Up @@ -567,10 +593,12 @@ func TestRoleInheritance(t *testing.T) {
user2, err := auth.GetUser("arthur")
assert.Equal(t, nil, err)
log.Printf("Channels = %s", user2.Channels())
assert.Equal(t, ch.AtSequence(ch.BaseSetOf(t, "!", "britain"), 1), user2.Channels())
assert.Equal(t, ch.TimedSet{
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1)},
"britain": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1)},
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.DynamicGrant},
"britain": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.AdminGrant}}, user2.Channels())
assert.Equal(t, ch.TimedSet{
"!": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.DynamicGrant},
"britain": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x1), Source: ch.AdminGrant},
"dull": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x3)},
"duller": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x3)},
"dullest": ch.TimedSetEntry{VbSequence: ch.NewVbSimpleSequence(0x3)},
Expand Down
6 changes: 4 additions & 2 deletions db/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,10 +1546,12 @@ func TestAccessFunctionDb(t *testing.T) {

user, err = authenticator.GetUser("naomi")
assert.NoError(t, err, "GetUser")
expected := channels.AtSequence(channels.BaseSetOf(t, "Hulu", "Netflix", "!"), 1)
expected := channels.TimedSet{"Hulu": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.DynamicGrant},
"Netflix": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.AdminGrant},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.DynamicGrant}}
assert.Equal(t, expected, user.CollectionChannels(collection.ScopeName, collection.Name))

expected.AddChannel("CrunchyRoll", 2, "")
expected.AddChannel("CrunchyRoll", 2, channels.AdminGrant)
assert.Equal(t, expected, user.InheritedCollectionChannels(collection.ScopeName, collection.Name))
}

Expand Down
25 changes: 15 additions & 10 deletions docs/api/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2525,10 +2525,9 @@ AllUserChannels:
UpdatedAt:
type: integer
description: Unix timestamp of last update
admin_assigned:
type: boolean
description: Whether a channel was assigned through admin_channels or not

source:
type: string
description: Either JWT, Admin or Dynamic.
dynamic_grants:
description: |-
Channels assigned to the user through the sync function.
Expand All @@ -2544,7 +2543,9 @@ AllUserChannels:
UpdatedAt:
type: integer
description: Unix timestamp of last update

source:
type: string
description: Either JWT, Admin or Dynamic.
jwt_grants:
description: |-
Channels assigned to the user through JWT.
Expand All @@ -2560,7 +2561,9 @@ AllUserChannels:
UpdatedAt:
type: integer
description: Unix timestamp of last update

source:
type: string
description: Either JWT, Admin or Dynamic.
admin_role_grants:
description: |-
Channels assigned to the user through the admin API, by assigning a role to the user through admin_roles,
Expand All @@ -2577,8 +2580,9 @@ AllUserChannels:
UpdatedAt:
type: integer
description: Unix timestamp of last update


source:
type: string
description: Either JWT, Admin or Dynamic.
dynamic_role_grants:
description: |-
Channels assigned to the user through roles assigned using the sync function. Does not include historic roles.
Expand All @@ -2594,5 +2598,6 @@ AllUserChannels:
UpdatedAt:
type: integer
description: Unix timestamp of last update


source:
type: string
description: Either JWT, Admin or Dynamic.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ get:
description: The channels the document has been in.
type: array
items:
sequences:
description: The sequence number that document was added to the channel.
type: string
example: "28-48"
Expand Down
2 changes: 1 addition & 1 deletion rest/user_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestUserAPI(t *testing.T) {
assert.Equal(t, "snej", user.Name())
assert.Equal(t, "[email protected]", user.Email())
assert.Equal(t, channels.TimedSet{"bar": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1), Source: channels.AdminGrant},
"foo": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1)}}, user.CollectionExplicitChannels(s, c))
"foo": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1), Source: channels.AdminGrant}}, user.CollectionExplicitChannels(s, c))
assert.True(t, user.Authenticate("letmein"))

// Change the password and verify it:
Expand Down

0 comments on commit f5a5ef0

Please sign in to comment.