Skip to content

Commit

Permalink
Fix timed_set tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-madi committed Feb 29, 2024
1 parent cfedc4c commit e8bc436
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
4 changes: 2 additions & 2 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func TestRebuildUserRoles(t *testing.T) {
user1, err := auth.GetUser("testUser")
assert.Equal(t, nil, err)
expected := ch.AtSequence(base.SetOf("role1", "role3"), 1)
expected.AddChannel("role2", 3, "")
expected.AddChannel("role2", 3, ch.AdminGrant)
assert.Equal(t, expected, user1.RoleNames())

// Invalidate the roles, triggers rebuild
Expand All @@ -537,7 +537,7 @@ 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, "")
expected.AddChannel("role2", 3, ch.AdminGrant)
assert.Equal(t, expected, user2.RoleNames())
}

Expand Down
21 changes: 17 additions & 4 deletions channels/timed_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ func (set TimedSet) String() string {
for channel, entry := range set {
if entry.VbSequence.Sequence > 0 {
if entry.VbSequence.VbNo != nil {
items = append(items, fmt.Sprintf("%s:%d.%d.%s", channel, *entry.VbSequence.VbNo, entry.VbSequence.Sequence, entry.Source))
items = append(items, fmt.Sprintf("%s:%d.%d_%s", channel, *entry.VbSequence.VbNo, entry.VbSequence.Sequence, entry.Source))
} else {
items = append(items, fmt.Sprintf("%s:%d.%s", channel, entry.VbSequence.Sequence, entry.Source))
items = append(items, fmt.Sprintf("%s:%d_%s", channel, entry.VbSequence.Sequence, entry.Source))
}
}
}
Expand All @@ -385,6 +385,19 @@ func TimedSetFromString(encoded string) TimedSet {
return nil
}
// VB sequence handling
source := ""
if strings.Contains(components[1], "_") {
seqComponents := strings.Split(components[1], "_")
switch seqComponents[1] {
case AdminGrant:
source = AdminGrant
case DynamicGrant:
source = DynamicGrant
case JWTGrant:
source = JWTGrant
}
components[1] = seqComponents[0]
}
if strings.Contains(components[1], ".") {
seqComponents := strings.Split(components[1], ".")
if len(seqComponents) != 2 {
Expand All @@ -399,14 +412,14 @@ func TimedSetFromString(encoded string) TimedSet {
if err != nil {
return nil
}
set[channel] = TimedSetEntry{VbSequence: NewVbSequence(uint16(vbNo), vbSeq)}
set[channel] = TimedSetEntry{VbSequence: NewVbSequence(uint16(vbNo), vbSeq), Source: source}
} else {
// Simple sequence handling
seqNo, err := strconv.ParseUint(components[1], 10, 64)
if err != nil || seqNo == 0 {
return nil
}
set[channel] = TimedSetEntry{VbSequence: NewVbSimpleSequence(seqNo)}
set[channel] = TimedSetEntry{VbSequence: NewVbSimpleSequence(seqNo), Source: source}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions channels/timed_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ func TestTimedSetUnmarshal(t *testing.T) {
}

func TestEncodeSequenceID(t *testing.T) {
set := TimedSet{"ABC": TimedSetEntry{VbSequence: NewVbSimpleSequence(17)},
"CBS": TimedSetEntry{VbSequence: NewVbSimpleSequence(23)},
set := TimedSet{"ABC": TimedSetEntry{VbSequence: NewVbSimpleSequence(17), Source: AdminGrant},
"CBS": TimedSetEntry{VbSequence: NewVbSimpleSequence(23), Source: DynamicGrant},
"BBC": TimedSetEntry{VbSequence: NewVbSimpleSequence(1)}}
encoded := set.String()
assert.Equal(t, "ABC:17,BBC:1,CBS:23", encoded)
assert.Equal(t, "ABC:17_Admin,BBC:1_,CBS:23_Dynamic", encoded)
decoded := TimedSetFromString(encoded)
assert.Equal(t, set, decoded)

assert.Equal(t, "ABC:17", TimedSet{"ABC": TimedSetEntry{VbSequence: NewVbSimpleSequence(17)},
assert.Equal(t, "ABC:17_", TimedSet{"ABC": TimedSetEntry{VbSequence: NewVbSimpleSequence(17)},
"CBS": TimedSetEntry{VbSequence: NewVbSimpleSequence(0)}}.String())

assert.Equal(t, TimedSet{}, TimedSetFromString(""))
Expand Down
4 changes: 2 additions & 2 deletions db/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,8 +1527,8 @@ func TestAccessFunctionDb(t *testing.T) {

user, err := authenticator.NewUser("naomi", "letmein", channels.BaseSetOf(t, "Netflix"))
require.NoError(t, err)
user.SetExplicitRoles(channels.TimedSet{"animefan": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1)},
"tumblr": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1)}}, 1)
user.SetExplicitRoles(channels.TimedSet{"animefan": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.AdminGrant},
"tumblr": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.AdminGrant}}, 1)
assert.NoError(t, authenticator.Save(user), "Save")

body := Body{"users": []string{"naomi"}, "userChannels": []string{"Hulu"}}
Expand Down
28 changes: 14 additions & 14 deletions rest/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,20 +861,20 @@ func TestChannelAccessChanges(t *testing.T) {
t,

channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"delta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(deltaGrantDocSeq)},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.DynamicGrant},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.AdminGrant},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.DynamicGrant},
"delta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(deltaGrantDocSeq), Source: channels.DynamicGrant},
}, alice.CollectionChannels(s, c))

zegpold, _ = a.GetUser("zegpold")
assert.Equal(
t,

channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(gammaGrantDocSeq)},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.DynamicGrant},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.AdminGrant},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(gammaGrantDocSeq), Source: channels.DynamicGrant},
}, zegpold.CollectionChannels(s, c))

// Update a document to revoke access to alice and grant it to zegpold:
Expand All @@ -891,20 +891,20 @@ func TestChannelAccessChanges(t *testing.T) {
t,

channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"delta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(deltaGrantDocSeq)},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.DynamicGrant},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.AdminGrant},
"delta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(deltaGrantDocSeq), Source: channels.DynamicGrant},
}, alice.CollectionChannels(s, c))

zegpold, _ = a.GetUser("zegpold")
assert.Equal(
t,

channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1))},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(alphaGrantDocSeq)},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(gammaGrantDocSeq)},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.DynamicGrant},
"zero": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(uint64(1)), Source: channels.AdminGrant},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(alphaGrantDocSeq), Source: channels.DynamicGrant},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(gammaGrantDocSeq), Source: channels.DynamicGrant},
}, zegpold.CollectionChannels(s, c))

rt.MustWaitForDoc("alpha", t)
Expand Down
24 changes: 12 additions & 12 deletions rest/role_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,23 @@ func TestRoleAccessChanges(t *testing.T) {
alice, _ := a.GetUser("alice")
assert.Equal(t,
channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1)},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(alice.Sequence())},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(roleGrantSequence)},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.DynamicGrant},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(alice.Sequence()), Source: channels.AdminGrant},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(roleGrantSequence), Source: channels.DynamicGrant},
}, alice.InheritedCollectionChannels(s, c))

assert.Equal(t,
channels.TimedSet{
"bogus": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(roleGrantSequence)},
"hipster": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(roleGrantSequence)},
"bogus": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(roleGrantSequence), Source: channels.DynamicGrant},
"hipster": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(roleGrantSequence), Source: channels.DynamicGrant},
}, alice.RoleNames())

zegpold, _ := a.GetUser("zegpold")
assert.Equal(t,

channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1)},
"beta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(zegpold.Sequence())},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(1), Source: channels.DynamicGrant},
"beta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(zegpold.Sequence()), Source: channels.AdminGrant},
}, zegpold.InheritedCollectionChannels(s, c))

assert.Equal(t, channels.TimedSet{}, zegpold.RoleNames())
Expand Down Expand Up @@ -400,16 +400,16 @@ func TestRoleAccessChanges(t *testing.T) {
alice, _ = a.GetUser("alice")
assert.Equal(t,
channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1)},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(alice.Sequence())},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1), Source: channels.DynamicGrant},
"alpha": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(alice.Sequence()), Source: channels.AdminGrant},
}, alice.InheritedCollectionChannels(s, c))

zegpold, _ = a.GetUser("zegpold")
assert.Equal(t,
channels.TimedSet{
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1)},
"beta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(zegpold.Sequence())},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(updatedRoleGrantSequence)},
"!": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1), Source: channels.DynamicGrant},
"beta": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(zegpold.Sequence()), Source: channels.AdminGrant},
"gamma": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(updatedRoleGrantSequence), Source: channels.DynamicGrant},
}, zegpold.InheritedCollectionChannels(s, c))

// The complete _changes feed for zegpold contains docs g1 and b1:
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 @@ -302,7 +302,7 @@ func TestUserAPI(t *testing.T) {
user, _ = rt.ServerContext().Database(ctx, "db").Authenticator(ctx).GetUser("snej")
assert.Equal(t, "snej", user.Name())
assert.Equal(t, "jens@couchbase.com", user.Email())
assert.Equal(t, channels.TimedSet{"bar": channels.TimedSetEntry{VbSequence: channels.NewVbSimpleSequence(0x1)},
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))
assert.True(t, user.Authenticate("letmein"))

Expand Down

0 comments on commit e8bc436

Please sign in to comment.