TestSMTPAPI_Codes verifies OTP code extraction.
(t *testing.T)
| 386 | |
| 387 | // TestSMTPAPI_Codes verifies OTP code extraction. |
| 388 | func TestSMTPAPI_Codes(t *testing.T) { |
| 389 | mux, store, _ := setupAPITest(t) |
| 390 | |
| 391 | storeSMTPEvent(t, store, "code-uuid1", ParsedEmail{ |
| 392 | Subject: "Your OTP", |
| 393 | Text: "Your code is 123456. Do not share it.", |
| 394 | }, 1000.0, "default") |
| 395 | |
| 396 | t.Run("default pattern", func(t *testing.T) { |
| 397 | r := httptest.NewRequest("GET", "/api/smtp/message/code-uuid1/codes", nil) |
| 398 | w := httptest.NewRecorder() |
| 399 | mux.ServeHTTP(w, r) |
| 400 | |
| 401 | if w.Code != 200 { |
| 402 | t.Fatalf("status = %d", w.Code) |
| 403 | } |
| 404 | var resp map[string]any |
| 405 | json.NewDecoder(w.Body).Decode(&resp) |
| 406 | data := resp["data"].([]any) |
| 407 | found := false |
| 408 | for _, c := range data { |
| 409 | if c.(string) == "123456" { |
| 410 | found = true |
| 411 | } |
| 412 | } |
| 413 | if !found { |
| 414 | t.Errorf("expected to find 123456 in %v", data) |
| 415 | } |
| 416 | }) |
| 417 | |
| 418 | t.Run("custom pattern", func(t *testing.T) { |
| 419 | r := httptest.NewRequest("GET", "/api/smtp/message/code-uuid1/codes?pattern=\\d{6}", nil) |
| 420 | w := httptest.NewRecorder() |
| 421 | mux.ServeHTTP(w, r) |
| 422 | |
| 423 | var resp map[string]any |
| 424 | json.NewDecoder(w.Body).Decode(&resp) |
| 425 | data := resp["data"].([]any) |
| 426 | if len(data) == 0 { |
| 427 | t.Error("expected at least one code with custom pattern") |
| 428 | } |
| 429 | }) |
| 430 | |
| 431 | t.Run("invalid pattern", func(t *testing.T) { |
| 432 | r := httptest.NewRequest("GET", "/api/smtp/message/code-uuid1/codes?pattern=[invalid", nil) |
| 433 | w := httptest.NewRecorder() |
| 434 | mux.ServeHTTP(w, r) |
| 435 | |
| 436 | if w.Code != http.StatusBadRequest { |
| 437 | t.Errorf("expected 400, got %d", w.Code) |
| 438 | } |
| 439 | }) |
| 440 | |
| 441 | t.Run("html body codes", func(t *testing.T) { |
| 442 | storeSMTPEvent(t, store, "code-uuid2", ParsedEmail{ |
| 443 | Subject: "HTML OTP", |
| 444 | HTML: "<p>Your verification code: <strong>654321</strong></p>", |
| 445 | }, 1001.0, "default") |
nothing calls this directly
no test coverage detected