(t *testing.T)
| 234 | } |
| 235 | |
| 236 | func TestUserViewQuery(t *testing.T) { |
| 237 | if !base.TestsDisableGSI() { |
| 238 | t.Skip("views tests are not applicable under GSI") |
| 239 | } |
| 240 | |
| 241 | rtConfig := RestTesterConfig{ |
| 242 | SyncFn: `function(doc) {channel(doc.channel)}`, |
| 243 | GuestEnabled: true, |
| 244 | } |
| 245 | |
| 246 | rt := NewRestTesterDefaultCollection(t, &rtConfig) // views only use default collection |
| 247 | defer rt.Close() |
| 248 | |
| 249 | ctx := rt.Context() |
| 250 | a := rt.ServerContext().Database(ctx, "db").Authenticator(ctx) |
| 251 | rt.ServerContext().Database(ctx, "db").SetUserViewsEnabled(true) |
| 252 | |
| 253 | // Create a view: |
| 254 | response := rt.SendAdminRequest(http.MethodPut, "/db/_design/foo", `{"views":{"bar": {"map": "function(doc) {emit(doc.key, doc.value);}"}}}`) |
| 255 | RequireStatus(t, response, http.StatusCreated) |
| 256 | |
| 257 | // Create docs: |
| 258 | response = rt.SendRequest(http.MethodPut, "/db/doc1", `{"key":10, "value":"ten", "channel":"W"}`) |
| 259 | RequireStatus(t, response, http.StatusCreated) |
| 260 | response = rt.SendRequest(http.MethodPut, "/db/doc2", `{"key":7, "value":"seven", "channel":"Q"}`) |
| 261 | RequireStatus(t, response, http.StatusCreated) |
| 262 | |
| 263 | // Create a user: |
| 264 | password := "123456" |
| 265 | quinn, _ := a.NewUser("quinn", password, channels.BaseSetOf(t, "Q", "q")) |
| 266 | assert.NoError(t, a.Save(quinn)) |
| 267 | |
| 268 | // Have the user query the view: |
| 269 | result := rt.WaitForNUserViewResults(1, "/db/_design/foo/_view/bar", quinn, password) |
| 270 | require.Len(t, result.Rows, 1) |
| 271 | assert.Equal(t, 1, result.TotalRows) |
| 272 | row := result.Rows[0] |
| 273 | assert.Equal(t, float64(7), row.Key) |
| 274 | assert.Equal(t, "seven", row.Value) |
| 275 | |
| 276 | // Admin should see both rows: |
| 277 | result = rt.WaitForNAdminViewResults(2, "/db/_design/foo/_view/bar") |
| 278 | require.Len(t, result.Rows, 2) |
| 279 | row = result.Rows[0] |
| 280 | assert.Equal(t, float64(7), row.Key) |
| 281 | assert.Equal(t, "seven", row.Value) |
| 282 | |
| 283 | row = result.Rows[1] |
| 284 | assert.Equal(t, float64(10), row.Key) |
| 285 | assert.Equal(t, "ten", row.Value) |
| 286 | |
| 287 | // Make sure users are not allowed to query internal views: |
| 288 | request := Request(http.MethodGet, "/db/_design/sync_gateway/_view/access", "") |
| 289 | request.SetBasicAuth(quinn.Name(), password) |
| 290 | response = rt.Send(request) |
| 291 | RequireStatus(t, response, http.StatusForbidden) |
| 292 | } |
| 293 |
nothing calls this directly
no test coverage detected