(t *testing.T)
| 38 | } |
| 39 | |
| 40 | func 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) |
nothing calls this directly
no test coverage detected