(t *testing.T)
| 264 | } |
| 265 | |
| 266 | func 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 { |
nothing calls this directly
no test coverage detected
searching dependent graphs…