Skip to content

Commit

Permalink
Fix skipped user-functions tests (#6838)
Browse files Browse the repository at this point in the history
* Fixed skipped user-functions tests

- The skipped tests in db/functions appear to work fine now.
- The functionsapitest failures were due to inadvertently enabling
  "admin party" when the intent was for the guest user not to have
  channel access; fixed that in the config.
- There was another failure in TestJSFunctionAsGuest due to reversed
  order of args in assert.Contains.

* Make function_test.go use db.SetupTestDBWithOptions

...instead of its own copy of the identical code.
  • Loading branch information
snej authored May 22, 2024
1 parent d106b46 commit 860f00b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
31 changes: 1 addition & 30 deletions db/functions/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ func addUserAlice(t *testing.T, db *db.Database) auth.User {

// Unit test for JS user functions.
func TestUserFunctions(t *testing.T) {
// FIXME : this test doesn't work because the access view does not exist on the collection ???
t.Skip("Skipping test until access view is available with collections")

// base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
db, ctx := setupTestDBWithFunctions(t, &kUserFunctionConfig)
defer db.Close(ctx)
Expand Down Expand Up @@ -333,7 +330,6 @@ func testUserFunctionsAsUser(t *testing.T, ctx context.Context, db *db.Database)

// Test CRUD operations
func TestUserFunctionsCRUD(t *testing.T) {
t.Skip("not collection aware")
// base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
db, ctx := setupTestDBWithFunctions(t, &kUserFunctionConfig)
defer db.Close(ctx)
Expand Down Expand Up @@ -478,9 +474,6 @@ func TestUserFunctionsMaxCodeSize(t *testing.T) {

// Low-level test of channel-name parameter expansion for user query/function auth
func TestUserFunctionAllow(t *testing.T) {
// FIXME : this test doesn't work because the access view does not exist on the collection ???
t.Skip("Skipping test until access view is available with collections")

// base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
db, ctx := setupTestDBWithFunctions(t, &kUserFunctionConfig)
defer db.Close(ctx)
Expand Down Expand Up @@ -653,27 +646,5 @@ func setupTestDBWithFunctions(t *testing.T, fnConfig *FunctionsConfig) (*db.Data
options.UserFunctions, err = CompileFunctions(base.TestCtx(t), *fnConfig)
assert.NoError(t, err)
}
return setupTestDBWithOptions(t, options)
}

func setupTestDBWithOptions(t testing.TB, dbcOptions db.DatabaseContextOptions) (*db.Database, context.Context) {

tBucket := base.GetTestBucket(t)
return setupTestDBForBucketWithOptions(t, tBucket, dbcOptions)
}

func setupTestDBForBucketWithOptions(t testing.TB, tBucket base.Bucket, dbcOptions db.DatabaseContextOptions) (*db.Database, context.Context) {
ctx := base.TestCtx(t)
AddOptionsFromEnvironmentVariables(&dbcOptions)
dbCtx, err := db.NewDatabaseContext(ctx, "db", tBucket, false, dbcOptions)
require.NoError(t, err, "Couldn't create context for database 'db'")

err = dbCtx.StartOnlineProcesses(ctx)
require.NoError(t, err)

db, err := db.CreateDatabase(dbCtx)
require.NoError(t, err, "Couldn't create database 'db'")

ctx = db.AddDatabaseLogContext(ctx)
return db, ctx
return db.SetupTestDBWithOptions(t, options)
}
23 changes: 13 additions & 10 deletions rest/functionsapitest/user_functions_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"
"time"

"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db/functions"
"github.com/couchbase/sync_gateway/rest"
Expand Down Expand Up @@ -79,6 +80,7 @@ func TestJSFunctionAsGuest(t *testing.T) {
DatabaseConfig: &rest.DatabaseConfig{
DbConfig: rest.DbConfig{
UserFunctions: &kUserFunctionAuthTestConfig,
Guest: &auth.PrincipalConfig{Disabled: base.BoolPtr(false)},
},
},
})
Expand All @@ -99,10 +101,9 @@ func TestJSFunctionAsGuest(t *testing.T) {
})

t.Run("user required", func(t *testing.T) {
t.Skip("Does not work with SG_TEST_USE_DEFAULT_COLLECTION=true CBG-2702")
response := sendReqFn("POST", "/db/_function/square", `{"numero": 42}`)
rest.AssertStatus(t, response, http.StatusUnauthorized)
assert.Contains(t, "login required", response.BodyString())
assert.Contains(t, response.BodyString(), "login required")
})

t.Run("admin-only", func(t *testing.T) {
Expand Down Expand Up @@ -209,14 +210,17 @@ func TestUserN1QLQueries(t *testing.T) {
func TestN1QLFunctionAsGuest(t *testing.T) {
TestRequireN1QLSupport(t)

rt := rest.NewRestTester(t, &rest.RestTesterConfig{GuestEnabled: true, EnableUserQueries: true})
defer rt.Close()

rt.DatabaseConfig = &rest.DatabaseConfig{
DbConfig: rest.DbConfig{
UserFunctions: &kUserN1QLFunctionsAuthTestConfig,
rt := rest.NewRestTester(t, &rest.RestTesterConfig{
GuestEnabled: true,
EnableUserQueries: true,
DatabaseConfig: &rest.DatabaseConfig{
DbConfig: rest.DbConfig{
Guest: &auth.PrincipalConfig{Disabled: base.BoolPtr(false)},
UserFunctions: &kUserN1QLFunctionsAuthTestConfig,
},
},
}
})
defer rt.Close()

sendReqFn := rt.SendRequest

Expand All @@ -231,7 +235,6 @@ func TestN1QLFunctionAsGuest(t *testing.T) {
})

t.Run("user required", func(t *testing.T) {
t.Skip("Does not work with SG_TEST_USE_DEFAULT_COLLECTION=true CBG-2702")
response := sendReqFn("POST", "/db/_function/square", `{"numero": 16}`)
rest.RequireStatus(t, response, http.StatusUnauthorized)
assert.Contains(t, response.BodyString(), "login required")
Expand Down

0 comments on commit 860f00b

Please sign in to comment.