MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestUserCollectionAccess

Function TestUserCollectionAccess

auth/collection_access_test.go:40–169  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

38}
39
40func TestUserCollectionAccess(t *testing.T) {
41
42 ctx := base.TestCtx(t)
43 // User with no access:
44 bucket := base.GetTestBucket(t)
45 defer bucket.Close(ctx)
46 options := DefaultAuthenticatorOptions(base.TestCtx(t))
47 options.Collections = map[string]map[string]struct{}{
48 "scope1": {
49 "collection1": struct{}{},
50 },
51 }
52 auth := NewTestAuthenticator(t, bucket.GetSingleDataStore(), nil, options)
53 user, _ := auth.NewUser("foo", "password", nil)
54 scope := "scope1"
55 collection := "collection1"
56 otherScope := "scope2"
57 otherCollection := "collection2"
58 nonMatchingCollections := [][2]string{{base.DefaultScope, base.DefaultCollection}, {scope, otherCollection}, {otherScope, collection}, {otherScope, otherCollection}}
59 // Default collection checks - should not have access based on authenticator
60 requireExpandWildCardChannel(t, user, nil, []string{"*"})
61 requireCannotSeeChannels(t, user, "x", "y", "!", "*")
62 require.ErrorIs(t, user.authorizeAllChannels(ch.BaseSetOf(t, "*")), errNotAllowedChannels)
63 require.ErrorIs(t, user.authorizeAnyChannel(ch.BaseSetOf(t, "x", "y")), errUnauthorized)
64 require.ErrorIs(t, user.authorizeAnyChannel(ch.BaseSetOf(t)), errUnauthorized)
65 // Named collection checks
66 requireExpandCollectionWildCardChannels(t, user, scope, collection, []string{"!"}, []string{"*"})
67 requireCannotSeeCollectionChannels(t, scope, collection, user, "x", "y", "*")
68 require.ErrorIs(t, user.authorizeAllCollectionChannels(scope, collection, ch.BaseSetOf(t, "*")), errNotAllowedChannels)
69 require.ErrorIs(t, user.AuthorizeAnyCollectionChannel(scope, collection, ch.BaseSetOf(t, "x", "y")), errUnauthorized)
70 require.ErrorIs(t, user.AuthorizeAnyCollectionChannel(scope, collection, ch.BaseSetOf(t)), errUnauthorized)
71
72 // User with access to one channel in named collection:
73 user.setCollectionChannels(scope, collection, ch.AtSequence(ch.BaseSetOf(t, "x"), 1))
74 // Matching named collection checks
75 requireExpandCollectionWildCardChannels(t, user, scope, collection, []string{"x"}, []string{"*"})
76 requireCanSeeCollectionChannels(t, scope, collection, user, "x")
77 requireCannotSeeCollectionChannels(t, scope, collection, user, "y", "!", "*")
78 require.ErrorIs(t, user.authorizeAllCollectionChannels(scope, collection, ch.BaseSetOf(t, "x", "y")), errNotAllowedChannels)
79 require.ErrorIs(t, user.authorizeAllCollectionChannels(scope, collection, ch.BaseSetOf(t, "*")), errNotAllowedChannels)
80 require.NoError(t, user.AuthorizeAnyCollectionChannel(scope, collection, ch.BaseSetOf(t, "x", "y")))
81 require.ErrorIs(t, user.AuthorizeAnyCollectionChannel(scope, collection, ch.BaseSetOf(t, "y")), errUnauthorized)
82 require.ErrorIs(t, user.AuthorizeAnyCollectionChannel(scope, collection, ch.BaseSetOf(t)), errUnauthorized)
83
84 // Non-matching collection checks
85 for _, pair := range nonMatchingCollections {
86 s := pair[0]
87 c := pair[1]
88 requireExpandCollectionWildCardChannels(t, user, s, c, nil, []string{"*"})
89 requireCannotSeeCollectionChannels(t, s, c, user, "x", "y", "!", "*")
90 if base.IsDefaultCollection(s, c) {
91 require.ErrorIs(t, user.authorizeAllCollectionChannels(s, c, ch.BaseSetOf(t, "x", "y")), errNotAllowedChannels, "for %s.%s", s, c)
92 require.ErrorIs(t, user.authorizeAllCollectionChannels(s, c, ch.BaseSetOf(t, "*")), errNotAllowedChannels, "for %s.%s", s, c)
93 } else {
94 require.ErrorIs(t, user.authorizeAllCollectionChannels(s, c, ch.BaseSetOf(t, "x", "y")), errUnauthorizedChannels, "for %s.%s", s, c)
95 require.ErrorIs(t, user.authorizeAllCollectionChannels(s, c, ch.BaseSetOf(t, "*")), errUnauthorizedChannels, "for %s.%s", s, c)
96 }
97 require.ErrorIs(t, user.AuthorizeAnyCollectionChannel(s, c, ch.BaseSetOf(t, "x", "y")), errUnauthorized)

Callers

nothing calls this directly

Calls 15

TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
IsDefaultCollectionFunction · 0.92
NewTestAuthenticatorFunction · 0.85
requireCannotSeeChannelsFunction · 0.85
NewUserMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected