package httpapi import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/wangjia/pangolin/pay/internal/pay" "github.com/wangjia/pangolin/pay/internal/store" ) const testXpub = "xpub6D1AabNHCupeiLM65ZR9UStMhJ1vCpyV4XbZdyhMZBiJXALQtmn9p42VTQckoHVn8WNqS7dqnJokZHAHcHGoaQgmv8D45oNUKx6DZMNZBCd" func TestCreateAndGetOrder(t *testing.T) { st, _ := store.Open(":memory:") t.Cleanup(func() { _ = st.Close() }) srv := httptest.NewServer(New(pay.New(st, pay.Config{AccountXpub: testXpub}))) t.Cleanup(srv.Close) body, _ := json.Marshal(map[string]any{"sku": "pro-year", "amount": 5_000000}) resp, err := http.Post(srv.URL+"/order", "application/json", bytes.NewReader(body)) if err != nil { t.Fatal(err) } if resp.StatusCode != http.StatusCreated { t.Fatalf("create status %d", resp.StatusCode) } var created orderResp _ = json.NewDecoder(resp.Body).Decode(&created) _ = resp.Body.Close() if created.Address != "TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH" || created.Status != "pending" { t.Fatalf("create resp: %+v", created) } r2, _ := http.Get(srv.URL + "/order/" + created.OrderNo) if r2.StatusCode != http.StatusOK { t.Fatalf("get status %d", r2.StatusCode) } var got orderResp _ = json.NewDecoder(r2.Body).Decode(&got) _ = r2.Body.Close() if got.OrderNo != created.OrderNo || got.Address != created.Address { t.Fatalf("get mismatch: %+v vs %+v", got, created) } r3, _ := http.Get(srv.URL + "/order/NOPE") if r3.StatusCode != http.StatusNotFound { t.Fatalf("want 404, got %d", r3.StatusCode) } _ = r3.Body.Close() r4, _ := http.Post(srv.URL+"/order", "application/json", bytes.NewReader([]byte(`{"sku":"x","amount":0}`))) if r4.StatusCode != http.StatusBadRequest { t.Fatalf("want 400 for bad amount, got %d", r4.StatusCode) } _ = r4.Body.Close() }