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

Function TestSyncFunctionException

rest/sync_fn_test.go:320–388  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

318}
319
320func TestSyncFunctionException(t *testing.T) {
321
322 base.SetUpTestLogging(t, base.LevelInfo, base.KeyHTTP, base.KeyJavascript)
323
324 rtConfig := RestTesterConfig{
325 SyncFn: `
326 function(doc) {
327 if (doc.throwException) {
328 channel(undefinedvariable);
329 }
330 if (doc.throwExplicit) {
331 throw("Explicit exception");
332 }
333 if (doc.throwForbidden) {
334 throw({forbidden: "read only!"})
335 }
336 if (doc.require) {
337 requireAdmin();
338 }
339 }`,
340 GuestEnabled: true,
341 }
342
343 rt := NewRestTester(t, &rtConfig)
344 defer rt.Close()
345
346 // Wait for the DB to be ready before attempting to get initial error count
347 rt.WaitForDBOnline()
348
349 numDBSyncExceptionsStart := rt.GetDatabase().DbStats.Database().SyncFunctionExceptionCount.Value()
350
351 // runtime error
352 response := rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc1", `{"throwException":true}`)
353 assert.Equal(t, http.StatusInternalServerError, response.Code)
354 assert.Contains(t, response.Body.String(), "Exception in JS sync function")
355
356 numDBSyncExceptions := rt.GetDatabase().DbStats.Database().SyncFunctionExceptionCount.Value()
357 assert.Equal(t, numDBSyncExceptionsStart+1, numDBSyncExceptions)
358 numDBSyncExceptionsStart = numDBSyncExceptions
359
360 // explicit throws should cause an exception
361 response = rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc2", `{"throwExplicit":true}`)
362 assert.Equal(t, http.StatusInternalServerError, response.Code)
363 assert.Contains(t, response.Body.String(), "Exception in JS sync function")
364
365 numDBSyncExceptions = rt.GetDatabase().DbStats.Database().SyncFunctionExceptionCount.Value()
366 assert.Equal(t, numDBSyncExceptionsStart+1, numDBSyncExceptions)
367 numDBSyncExceptionsStart = numDBSyncExceptions
368 numDBSyncRejected := rt.GetDatabase().DbStats.Security().NumDocsRejected.Value()
369 assert.Equal(t, int64(0), numDBSyncRejected)
370
371 // throw with a forbidden property shouldn't cause a true exception
372 response = rt.SendRequest("PUT", "/{{.keyspace}}/doc3", `{"throwForbidden":true}`)
373 assert.Equal(t, http.StatusForbidden, response.Code)
374 assert.Contains(t, response.Body.String(), "read only!")
375 numDBSyncExceptions = rt.GetDatabase().DbStats.Database().SyncFunctionExceptionCount.Value()
376 assert.Equal(t, numDBSyncExceptionsStart, numDBSyncExceptions)
377 numDBSyncRejected = rt.GetDatabase().DbStats.Security().NumDocsRejected.Value()

Callers

nothing calls this directly

Calls 13

CloseMethod · 0.95
WaitForDBOnlineMethod · 0.95
GetDatabaseMethod · 0.95
SendAdminRequestMethod · 0.95
SendRequestMethod · 0.95
SetUpTestLoggingFunction · 0.92
NewRestTesterFunction · 0.85
SecurityMethod · 0.80
ContainsMethod · 0.65
StringMethod · 0.65
ValueMethod · 0.45
DatabaseMethod · 0.45

Tested by

no test coverage detected