MCPcopy
hub / github.com/pocketbase/pocketbase / TestRecordAuthResponseAuthRuleCheck

Function TestRecordAuthResponseAuthRuleCheck

apis/record_helpers_test.go:266–338  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

264}
265
266func TestRecordAuthResponseAuthRuleCheck(t *testing.T) {
267 app, _ := tests.NewTestApp()
268 defer app.Cleanup()
269
270 event := new(core.RequestEvent)
271 event.App = app
272 event.Request = httptest.NewRequest(http.MethodGet, "/", nil)
273 event.Response = httptest.NewRecorder()
274
275 user, err := app.FindAuthRecordByEmail("users", "test@example.com")
276 if err != nil {
277 t.Fatal(err)
278 }
279
280 scenarios := []struct {
281 name string
282 rule *string
283 expectError bool
284 }{
285 {
286 "admin only rule",
287 nil,
288 true,
289 },
290 {
291 "empty rule",
292 types.Pointer(""),
293 false,
294 },
295 {
296 "false rule",
297 types.Pointer("1=2"),
298 true,
299 },
300 {
301 "true rule",
302 types.Pointer("1=1"),
303 false,
304 },
305 }
306
307 for _, s := range scenarios {
308 t.Run(s.name, func(t *testing.T) {
309 user.Collection().AuthRule = s.rule
310
311 err := apis.RecordAuthResponse(event, user, "", nil)
312
313 hasErr := err != nil
314 if s.expectError != hasErr {
315 t.Fatalf("Expected hasErr %v, got %v (%v)", s.expectError, hasErr, err)
316 }
317
318 // in all cases login alert shouldn't be send because of the empty auth method
319 if app.TestMailer.TotalSend() != 0 {
320 t.Fatalf("Expected no emails send, got %d:\n%v", app.TestMailer.TotalSend(), app.TestMailer.LastMessage().HTML)
321 }
322
323 if !hasErr {

Callers

nothing calls this directly

Calls 9

CleanupMethod · 0.95
NewTestAppFunction · 0.92
PointerFunction · 0.92
RecordAuthResponseFunction · 0.92
CollectionMethod · 0.80
TotalSendMethod · 0.80
LastMessageMethod · 0.80
FindAuthRecordByEmailMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…