Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
package erp_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
. "git.breakpilot.com/platform/tenant-registry/internal/erp"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/erpmock"
|
|
)
|
|
|
|
func TestClient_againstMock(t *testing.T) {
|
|
ms := httptest.NewServer(erpmock.New(erpmock.Config{APIKey: "k", APISecret: "s"}))
|
|
defer ms.Close()
|
|
c := New(Config{BaseURL: ms.URL + "/", APIKey: "k", APISecret: "s"})
|
|
ctx := context.Background()
|
|
|
|
if cust, err := c.FindCustomerByTenant(ctx, "t-1"); err != nil || cust != nil {
|
|
t.Fatalf("find before: %v %v", cust, err)
|
|
}
|
|
name, err := c.UpsertCustomer(ctx, Customer{CustomerName: "Acme", TenantID: "t-1"})
|
|
if err != nil || name == "" {
|
|
t.Fatalf("upsert: %q %v", name, err)
|
|
}
|
|
if again, err := c.UpsertCustomer(ctx, Customer{CustomerName: "Acme", TenantID: "t-1"}); err != nil || again != name {
|
|
t.Fatalf("upsert idempotent: %q %v", again, err)
|
|
}
|
|
if _, err := c.UpsertCustomer(ctx, Customer{CustomerName: "x"}); err == nil {
|
|
t.Fatal("tenant_id required")
|
|
}
|
|
if subs, err := c.ListSubscriptions(ctx, name); err != nil || len(subs) != 0 {
|
|
t.Fatalf("subs: %v %v", subs, err)
|
|
}
|
|
if _, err := c.CreateUsageRecord(ctx, UsageRecord{Customer: name, Metric: "m", Qty: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := c.CreateLead(ctx, Lead{LeadName: "Acme"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bad := New(Config{BaseURL: ms.URL, APIKey: "wrong", APISecret: "s"})
|
|
if _, err := bad.FindCustomerByTenant(ctx, "t-1"); err == nil || errors.Is(err, ErrUnavailable) {
|
|
t.Fatalf("401 must be a permanent error: %v", err)
|
|
}
|
|
off := New(Config{})
|
|
if off.Enabled() {
|
|
t.Fatal("empty base url must be disabled")
|
|
}
|
|
if _, err := off.CreateLead(ctx, Lead{}); !errors.Is(err, ErrUnavailable) {
|
|
t.Fatalf("disabled: %v", err)
|
|
}
|
|
down := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(503) }))
|
|
defer down.Close()
|
|
if _, err := New(Config{BaseURL: down.URL}).CreateLead(ctx, Lead{}); !errors.Is(err, ErrUnavailable) {
|
|
t.Fatalf("503: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSignature(t *testing.T) {
|
|
body := []byte(`{"a":1}`)
|
|
sig := Sign("sec", body)
|
|
if !VerifySignature("sec", body, sig) || VerifySignature("other", body, sig) || VerifySignature("sec", body, "") || VerifySignature("", body, sig) {
|
|
t.Fatal("base64 verify")
|
|
}
|
|
if VerifySignature("sec", body, "sha256=00") {
|
|
t.Fatal("bad hex must fail")
|
|
}
|
|
}
|
|
|
|
func TestFrappeDatetime(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"2026-09-30T06:03:53Z": "2026-09-30 08:03:53", // CEST
|
|
"2026-12-01T12:00:00Z": "2026-12-01 13:00:00", // CET
|
|
"2026-09-30T06:03:53.123+02:00": "2026-09-30 06:03:53",
|
|
"2026-09-30 06:03:53": "2026-09-30 06:03:53", // already Frappe form
|
|
"": "",
|
|
} {
|
|
if got := FrappeDatetime(in); got != want {
|
|
t.Errorf("FrappeDatetime(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|