(t *testing.T)
| 248 | } |
| 249 | |
| 250 | func TestDeleteExpiredOTPs(t *testing.T) { |
| 251 | t.Parallel() |
| 252 | |
| 253 | checkDeletedIds := func(app core.App, t *testing.T, expectedDeletedIds []string) { |
| 254 | if err := tests.StubOTPRecords(app); err != nil { |
| 255 | t.Fatal(err) |
| 256 | } |
| 257 | |
| 258 | deletedIds := []string{} |
| 259 | app.OnRecordAfterDeleteSuccess().BindFunc(func(e *core.RecordEvent) error { |
| 260 | deletedIds = append(deletedIds, e.Record.Id) |
| 261 | return e.Next() |
| 262 | }) |
| 263 | |
| 264 | if err := app.DeleteExpiredOTPs(); err != nil { |
| 265 | t.Fatal(err) |
| 266 | } |
| 267 | |
| 268 | if len(deletedIds) != len(expectedDeletedIds) { |
| 269 | t.Fatalf("Expected deleted ids\n%v\ngot\n%v", expectedDeletedIds, deletedIds) |
| 270 | } |
| 271 | |
| 272 | for _, id := range expectedDeletedIds { |
| 273 | if !slices.Contains(deletedIds, id) { |
| 274 | t.Errorf("Expected to find deleted id %q in %v", id, deletedIds) |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | t.Run("default test collections", func(t *testing.T) { |
| 280 | app, _ := tests.NewTestApp() |
| 281 | defer app.Cleanup() |
| 282 | |
| 283 | checkDeletedIds(app, t, []string{ |
| 284 | "user1_0", |
| 285 | "superuser2_2", |
| 286 | "superuser2_4", |
| 287 | }) |
| 288 | }) |
| 289 | |
| 290 | t.Run("otp collection duration mock", func(t *testing.T) { |
| 291 | app, _ := tests.NewTestApp() |
| 292 | defer app.Cleanup() |
| 293 | |
| 294 | superusers, err := app.FindCollectionByNameOrId(core.CollectionNameSuperusers) |
| 295 | if err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | superusers.OTP.Duration = 60 |
| 299 | if err := app.Save(superusers); err != nil { |
| 300 | t.Fatalf("Failed to mock superusers otp duration: %v", err) |
| 301 | } |
| 302 | |
| 303 | checkDeletedIds(app, t, []string{ |
| 304 | "user1_0", |
| 305 | "superuser2_2", |
| 306 | "superuser2_4", |
| 307 | "superuser3_1", |
nothing calls this directly
no test coverage detected
searching dependent graphs…