MCPcopy
hub / github.com/pocketbase/pocketbase / TestRequireGuestOnly

Function TestRequireGuestOnly

apis/middlewares_test.go:51–111  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

49}
50
51func TestRequireGuestOnly(t *testing.T) {
52 t.Parallel()
53
54 beforeTestFunc := func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
55 e.Router.GET("/my/test", func(e *core.RequestEvent) error {
56 return e.String(200, "test123")
57 }).Bind(apis.RequireGuestOnly())
58 }
59
60 scenarios := []tests.ApiScenario{
61 {
62 Name: "valid regular user token",
63 Method: http.MethodGet,
64 URL: "/my/test",
65 Headers: map[string]string{
66 "Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
67 },
68 BeforeTestFunc: beforeTestFunc,
69 ExpectedStatus: 400,
70 ExpectedContent: []string{`"data":{}`},
71 ExpectedEvents: map[string]int{"*": 0},
72 },
73 {
74 Name: "valid superuser auth token",
75 Method: http.MethodGet,
76 URL: "/my/test",
77 Headers: map[string]string{
78 "Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
79 },
80 BeforeTestFunc: beforeTestFunc,
81 ExpectedStatus: 400,
82 ExpectedContent: []string{`"data":{}`},
83 ExpectedEvents: map[string]int{"*": 0},
84 },
85 {
86 Name: "expired/invalid token",
87 Method: http.MethodGet,
88 URL: "/my/test",
89 Headers: map[string]string{
90 "Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoxNjQwOTkxNjYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.2D3tmqPn3vc5LoqqCz8V-iCDVXo9soYiH0d32G7FQT4",
91 },
92 BeforeTestFunc: beforeTestFunc,
93 ExpectedStatus: 200,
94 ExpectedContent: []string{"test123"},
95 ExpectedEvents: map[string]int{"*": 0},
96 },
97 {
98 Name: "guest",
99 Method: http.MethodGet,
100 URL: "/my/test",
101 BeforeTestFunc: beforeTestFunc,
102 ExpectedStatus: 200,
103 ExpectedContent: []string{"test123"},
104 ExpectedEvents: map[string]int{"*": 0},
105 },
106 }
107
108 for _, scenario := range scenarios {

Callers

nothing calls this directly

Calls 5

RequireGuestOnlyFunction · 0.92
GETMethod · 0.80
TestMethod · 0.80
BindMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…