|
| 1 | +// Copyright 2025-Present Couchbase, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License included |
| 4 | +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 5 | +// in that file, in accordance with the Business Source License, use of this |
| 6 | +// software will be governed by the Apache License, Version 2.0, included in |
| 7 | +// the file licenses/APL2.txt. |
| 8 | + |
| 9 | +package rest |
| 10 | + |
| 11 | +import ( |
| 12 | + "encoding/json" |
| 13 | + "net/http" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/couchbase/sync_gateway/base" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +func TestDisablePublicAllDocs(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + disablePublicAllDocs *bool |
| 25 | + expectedPublicStatus int |
| 26 | + expectedPublicError string |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "default", |
| 30 | + disablePublicAllDocs: nil, |
| 31 | + expectedPublicStatus: http.StatusOK, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "disabled", |
| 35 | + disablePublicAllDocs: base.Ptr(true), |
| 36 | + expectedPublicStatus: http.StatusForbidden, |
| 37 | + expectedPublicError: "public access to _all_docs is disabled for this database", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "enabled", |
| 41 | + disablePublicAllDocs: base.Ptr(false), |
| 42 | + expectedPublicStatus: http.StatusOK, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + for _, test := range tests { |
| 47 | + t.Run(test.name, func(t *testing.T) { |
| 48 | + rtConfig := RestTesterConfig{ |
| 49 | + DatabaseConfig: &DatabaseConfig{ |
| 50 | + DbConfig: DbConfig{ |
| 51 | + DisablePublicAllDocs: test.disablePublicAllDocs, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + rt := NewRestTester(t, &rtConfig) |
| 56 | + defer rt.Close() |
| 57 | + |
| 58 | + rt.CreateUser("user1", nil) |
| 59 | + rt.CreateTestDoc("doc1") |
| 60 | + rt.CreateTestDoc("doc2") |
| 61 | + |
| 62 | + t.Run("public", func(t *testing.T) { |
| 63 | + response := rt.SendUserRequest("GET", "/{{.keyspace}}/_all_docs", "", "user1") |
| 64 | + RequireStatus(t, response, test.expectedPublicStatus) |
| 65 | + if test.expectedPublicError != "" { |
| 66 | + require.Contains(t, response.Body.String(), test.expectedPublicError) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("admin", func(t *testing.T) { |
| 71 | + response := rt.SendAdminRequest("GET", "/{{.keyspace}}/_all_docs", "") |
| 72 | + RequireStatus(t, response, http.StatusOK) |
| 73 | + var result struct { |
| 74 | + Rows []struct { |
| 75 | + ID string `json:"id"` |
| 76 | + Key string `json:"key"` |
| 77 | + } `json:"rows"` |
| 78 | + } |
| 79 | + require.NoError(t, json.Unmarshal(response.Body.Bytes(), &result)) |
| 80 | + assert.Equal(t, 2, len(result.Rows)) |
| 81 | + assert.Equal(t, "doc1", result.Rows[0].ID) |
| 82 | + assert.Equal(t, "doc2", result.Rows[1].ID) |
| 83 | + }) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments