1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-20 22:36:00 +02:00
pocketbase/apis/middlewares_test.go

629 lines
24 KiB
Go
Raw Normal View History

2022-07-07 00:19:05 +03:00
package apis_test
import (
"net/http"
"testing"
"github.com/pocketbase/pocketbase/apis"
2024-09-29 19:23:19 +03:00
"github.com/pocketbase/pocketbase/core"
2022-07-07 00:19:05 +03:00
"github.com/pocketbase/pocketbase/tests"
)
func TestPanicRecover(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "panic from route",
Method: http.MethodGet,
URL: "/my/test",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
panic("123")
})
},
ExpectedStatus: 500,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
},
{
Name: "panic from middleware",
Method: http.MethodGet,
URL: "/my/test",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(http.StatusOK, "test")
}).BindFunc(func(e *core.RequestEvent) error {
panic(123)
})
},
ExpectedStatus: 500,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
2022-07-07 00:19:05 +03:00
func TestRequireGuestOnly(t *testing.T) {
t.Parallel()
2024-09-29 19:23:19 +03:00
beforeTestFunc := func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireGuestOnly())
}
2022-07-07 00:19:05 +03:00
scenarios := []tests.ApiScenario{
{
2024-09-29 19:23:19 +03:00
Name: "valid regular user token",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: beforeTestFunc,
2022-07-07 00:19:05 +03:00
ExpectedStatus: 400,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid superuser auth token",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: beforeTestFunc,
2022-07-07 00:19:05 +03:00
ExpectedStatus: 400,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
Name: "expired/invalid token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoxNjQwOTkxNjYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.2D3tmqPn3vc5LoqqCz8V-iCDVXo9soYiH0d32G7FQT4",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: beforeTestFunc,
2022-07-07 00:19:05 +03:00
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "guest",
Method: http.MethodGet,
URL: "/my/test",
BeforeTestFunc: beforeTestFunc,
2022-07-07 00:19:05 +03:00
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
2024-09-29 19:23:19 +03:00
func TestRequireAuth(t *testing.T) {
t.Parallel()
2022-07-07 00:19:05 +03:00
scenarios := []tests.ApiScenario{
{
Name: "guest",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth())
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "expired token",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoxNjQwOTkxNjYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.2D3tmqPn3vc5LoqqCz8V-iCDVXo9soYiH0d32G7FQT4",
},
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth())
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "invalid token",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsImV4cCI6MjUyNDYwNDQ2MSwidHlwZSI6ImZpbGUiLCJjb2xsZWN0aW9uSWQiOiJwYmNfMzE0MjYzNTgyMyJ9.Lupz541xRvrktwkrl55p5pPCF77T69ZRsohsIcb2dxc",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth())
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token with no collection restrictions",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
// regular user
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth())
2022-10-30 10:28:14 +02:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record static auth token",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
// regular user
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6ZmFsc2V9.4IsO6YMsR19crhwl_YWzvRH8pfq2Ri4Gv2dzGyneLak",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth())
2022-10-30 10:28:14 +02:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token with collection not in the restricted list",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
// superuser
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth("users", "demo1"))
2022-10-30 10:28:14 +02:00
},
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-10-30 10:28:14 +02:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token with collection in the restricted list",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
// superuser
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireAuth("users", core.CollectionNameSuperusers))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
2024-09-29 19:23:19 +03:00
func TestRequireSuperuserAuth(t *testing.T) {
t.Parallel()
2022-07-07 00:19:05 +03:00
scenarios := []tests.ApiScenario{
{
Name: "guest",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuth())
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
Name: "expired/invalid token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjE2NDA5OTE2NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.0pDcBPGDpL2Khh76ivlRi7ugiLBSYvasct3qpHV3rfs",
2024-09-29 19:23:19 +03:00
},
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuth())
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid regular user auth token",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuth())
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
2022-07-07 00:19:05 +03:00
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid superuser auth token",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuth())
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
2024-09-29 19:23:19 +03:00
func TestRequireSuperuserAuthOnlyIfAny(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
2024-09-29 19:23:19 +03:00
Name: "guest (while having at least 1 existing superuser)",
Method: http.MethodGet,
URL: "/my/test",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuthOnlyIfAny())
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
},
{
2024-09-29 19:23:19 +03:00
Name: "guest (while having 0 existing superusers)",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
// delete all superusers
_, err := app.DB().NewQuery("DELETE FROM {{" + core.CollectionNameSuperusers + "}}").Execute()
if err != nil {
t.Fatal(err)
}
2024-09-29 19:23:19 +03:00
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuthOnlyIfAny())
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
{
Name: "expired/invalid token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjE2NDA5OTE2NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.0pDcBPGDpL2Khh76ivlRi7ugiLBSYvasct3qpHV3rfs",
2024-09-29 19:23:19 +03:00
},
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuthOnlyIfAny())
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
},
{
2024-09-29 19:23:19 +03:00
Name: "valid regular user token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuthOnlyIfAny())
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
},
{
2024-09-29 19:23:19 +03:00
Name: "valid superuser auth token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserAuthOnlyIfAny())
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
2024-09-29 19:23:19 +03:00
func TestRequireSuperuserOrOwnerAuth(t *testing.T) {
t.Parallel()
2022-07-07 00:19:05 +03:00
scenarios := []tests.ApiScenario{
{
Name: "guest",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/4q1xlclmfloku33",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth(""))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
Name: "expired/invalid token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/4q1xlclmfloku33",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjE2NDA5OTE2NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.0pDcBPGDpL2Khh76ivlRi7ugiLBSYvasct3qpHV3rfs",
2024-09-29 19:23:19 +03:00
},
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth(""))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (different user)",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/oap640cot4yru2s",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth(""))
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
2022-10-30 10:28:14 +02:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (owner)",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/4q1xlclmfloku33",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth(""))
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
2022-10-30 10:28:14 +02:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (owner + non-matching custom owner param)",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/4q1xlclmfloku33",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth("test"))
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (owner + matching custom owner param)",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/4q1xlclmfloku33",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{test}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth("test"))
2022-10-30 10:28:14 +02:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
{
2024-09-29 19:23:19 +03:00
Name: "valid superuser auth token",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/4q1xlclmfloku33",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth(""))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
2024-09-29 19:23:19 +03:00
func TestRequireSameCollectionContextAuth(t *testing.T) {
t.Parallel()
2022-07-07 00:19:05 +03:00
scenarios := []tests.ApiScenario{
{
Name: "guest",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/_pb_users_auth_",
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{collection}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSameCollectionContextAuth(""))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
Name: "expired/invalid token",
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/_pb_users_auth_",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoxNjQwOTkxNjYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.2D3tmqPn3vc5LoqqCz8V-iCDVXo9soYiH0d32G7FQT4",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{collection}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSameCollectionContextAuth(""))
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 401,
2022-10-30 10:28:14 +02:00
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-10-30 10:28:14 +02:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (different collection)",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/clients",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{collection}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSameCollectionContextAuth(""))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-07-07 00:19:05 +03:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (same collection)",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/_pb_users_auth_",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{collection}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSameCollectionContextAuth(""))
2022-07-07 00:19:05 +03:00
},
ExpectedStatus: 200,
ExpectedContent: []string{"test123"},
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (non-matching/missing collection param)",
2022-07-07 00:19:05 +03:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/_pb_users_auth_",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{id}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth(""))
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
2022-10-30 10:28:14 +02:00
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-10-30 10:28:14 +02:00
},
{
2024-09-29 19:23:19 +03:00
Name: "valid record auth token (matching custom collection param)",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/_pb_users_auth_",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{test}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSuperuserOrOwnerAuth("test"))
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
2022-10-30 10:28:14 +02:00
},
{
2024-09-29 19:23:19 +03:00
Name: "superuser no exception check",
2022-10-30 10:28:14 +02:00
Method: http.MethodGet,
2024-09-29 19:23:19 +03:00
URL: "/my/test/_pb_users_auth_",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
e.Router.GET("/my/test/{collection}", func(e *core.RequestEvent) error {
return e.String(200, "test123")
}).Bind(apis.RequireSameCollectionContextAuth(""))
2022-10-30 10:28:14 +02:00
},
2024-09-29 19:23:19 +03:00
ExpectedStatus: 403,
2022-10-30 10:28:14 +02:00
ExpectedContent: []string{`"data":{}`},
2024-09-29 19:23:19 +03:00
ExpectedEvents: map[string]int{"*": 0},
2022-10-30 10:28:14 +02:00
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}