aboutsummaryrefslogtreecommitdiffstats
path: root/internal/accounted/accounted_test.go
diff options
context:
space:
mode:
authorSimen A. W. Olsen <hello@simenandre.no>2026-10-27 17:19:57 +0100
committerSimen A. W. Olsen <hello@simenandre.no>2026-10-27 17:19:57 +0100
commit0571b30b6da27dc4c9e617403deab09b974ac641 (patch)
treef3dfab18e22a48d06c64e563ba9bcf478c01a5bc /internal/accounted/accounted_test.go
downloadaccounted-multicase-intgr-0571b30b6da27dc4c9e617403deab09b974ac641.tar.gz
accounted-multicase-intgr-0571b30b6da27dc4c9e617403deab09b974ac641.zip
feat: send Multicase buntfil vouchers to AccountedHEADmain
Diffstat (limited to 'internal/accounted/accounted_test.go')
-rw-r--r--internal/accounted/accounted_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/internal/accounted/accounted_test.go b/internal/accounted/accounted_test.go
new file mode 100644
index 0000000..01ae42f
--- /dev/null
+++ b/internal/accounted/accounted_test.go
@@ -0,0 +1,65 @@
+package accounted
+
+import (
+ "context"
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func TestMoneyJSON(t *testing.T) {
+ b, _ := json.Marshal(Line{Account: "1930", Debit: 123405, Credit: 7})
+ want := `{"account_number":"1930","debit_amount":1234.05,"credit_amount":0.07}`
+ if string(b) != want {
+ t.Errorf("got %s, want %s", b, want)
+ }
+}
+
+func TestCreateDraftRetriesWithSameKey(t *testing.T) {
+ var keys []string
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ keys = append(keys, r.Header.Get("Idempotency-Key"))
+ if r.URL.Path != "/api/v1/companies/c1/journal-entries" || r.Header.Get("Authorization") != "Bearer k" {
+ t.Errorf("unexpected request %s %s", r.URL.Path, r.Header.Get("Authorization"))
+ }
+ if len(keys) == 1 {
+ w.Header().Set("Retry-After", "0")
+ w.WriteHeader(http.StatusTooManyRequests)
+ io.WriteString(w, `{"error":{"code":"RATE_LIMITED","message_en":"slow down"}}`)
+ return
+ }
+ w.WriteHeader(http.StatusCreated)
+ io.WriteString(w, `{"data":{"id":"je1","status":"draft"},"meta":{"request_id":"req_1"}}`)
+ }))
+ defer srv.Close()
+
+ c := NewClient(srv.URL, "k", "c1")
+ c.Interval = 0
+ id, err := c.CreateDraft(context.Background(), JournalEntry{}, "key-1", false)
+ if err != nil || id != "je1" {
+ t.Fatalf("id = %q, err = %v", id, err)
+ }
+ if len(keys) != 2 || keys[0] != "key-1" || keys[1] != "key-1" {
+ t.Errorf("idempotency keys = %v", keys)
+ }
+}
+
+func TestErrorEnvelope(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Query().Get("dry_run") != "true" {
+ t.Error("dry_run not set")
+ }
+ w.WriteHeader(http.StatusBadRequest)
+ io.WriteString(w, `{"error":{"code":"JOURNAL_ENTRY_NOT_BALANCED","message":"Obalanserad","message_en":"Not balanced","request_id":"req_2"}}`)
+ }))
+ defer srv.Close()
+
+ c := NewClient(srv.URL, "k", "c1")
+ c.Interval = 0
+ _, err := c.CreateDraft(context.Background(), JournalEntry{}, "key-2", true)
+ if !IsCode(err, "JOURNAL_ENTRY_NOT_BALANCED") {
+ t.Fatalf("err = %v", err)
+ }
+}