feat(t230): use OCR for ERP session login
This commit is contained in:
@@ -166,6 +166,66 @@ func TestSessionManagerSerializesCaptchaRequests(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionManagerEnsureAuthenticatedUsesRecognizerOnce(t *testing.T) {
|
||||
var captchaCalls, loginCalls int
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case CaptchaPath:
|
||||
captchaCalls++
|
||||
http.SetCookie(w, &http.Cookie{Name: "captcha", Value: "ready", Path: "/"})
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
_, _ = w.Write([]byte("captcha-image"))
|
||||
case LoginPath:
|
||||
loginCalls++
|
||||
http.SetCookie(w, &http.Cookie{Name: "authenticated", Value: "yes", Path: "/"})
|
||||
_, _ = w.Write([]byte(`{"status":true,"data":{"user":{"id":12}}}`))
|
||||
case UserPath:
|
||||
_, _ = w.Write([]byte(`{"status":true,"data":{"id":12}}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
recognizer := &fixedRecognizer{code: "1234"}
|
||||
manager := testSessionManager(t, server.URL, "test-user", "test-password")
|
||||
manager.recognizer = recognizer
|
||||
if err := manager.EnsureAuthenticated(context.Background()); err != nil {
|
||||
t.Fatalf("EnsureAuthenticated() error = %v", err)
|
||||
}
|
||||
if !manager.Status().Authenticated || recognizer.calls != 1 || captchaCalls != 1 || loginCalls != 1 {
|
||||
t.Fatalf("state/calls = %+v / %d / %d / %d", manager.Status(), recognizer.calls, captchaCalls, loginCalls)
|
||||
}
|
||||
if err := manager.EnsureAuthenticated(context.Background()); err != nil {
|
||||
t.Fatalf("second EnsureAuthenticated() error = %v", err)
|
||||
}
|
||||
if recognizer.calls != 1 || captchaCalls != 1 || loginCalls != 1 {
|
||||
t.Fatalf("second call repeated OCR/login = %d / %d / %d", recognizer.calls, captchaCalls, loginCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionManagerEnsureAuthenticatedRequiresRecognizer(t *testing.T) {
|
||||
manager := testSessionManager(t, "https://erp.example.test", "test-user", "test-password")
|
||||
err := manager.EnsureAuthenticated(context.Background())
|
||||
if !errors.Is(err, domain.ErrFreightSourceOCRInvalid) {
|
||||
t.Fatalf("EnsureAuthenticated() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type fixedRecognizer struct {
|
||||
code string
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (recognizer *fixedRecognizer) Recognize(
|
||||
context.Context,
|
||||
[]byte,
|
||||
string,
|
||||
) (string, error) {
|
||||
recognizer.calls++
|
||||
return recognizer.code, recognizer.err
|
||||
}
|
||||
|
||||
func testSessionManager(
|
||||
t *testing.T,
|
||||
baseURL, username, password string,
|
||||
|
||||
Reference in New Issue
Block a user