http_test.go85 lines, 2.5 KB
| 1 | package transfer_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/fiston-user/payments/internal/platform/postgres" |
| 12 | "github.com/fiston-user/payments/internal/transfer" |
| 13 | "github.com/google/uuid" |
| 14 | ) |
| 15 | |
| 16 | // newAPI serves the transfer API on the package's database. Nothing runs |
| 17 | // the sagas: we only look at what the API answers. |
| 18 | func newAPI(t *testing.T) http.Handler { |
| 19 | t.Helper() |
| 20 | if testing.Short() { |
| 21 | t.Skip("integration test") |
| 22 | } |
| 23 | pool, err := postgres.NewPool(t.Context(), postgresURL, 4) |
| 24 | if err != nil { |
| 25 | t.Fatalf("connect: %v", err) |
| 26 | } |
| 27 | t.Cleanup(pool.Close) |
| 28 | mux := http.NewServeMux() |
| 29 | transfer.NewHandler(transfer.NewStore(pool), func() {}).Register(mux) |
| 30 | return mux |
| 31 | } |
| 32 | |
| 33 | // body is a transfer request. |
| 34 | func body(from, to string, amount int64, currency string) string { |
| 35 | return fmt.Sprintf(`{"from": %q, "to": %q, "amount": %d, "currency": %q}`, from, to, amount, currency) |
| 36 | } |
| 37 | |
| 38 | func TestCreateValidation(t *testing.T) { |
| 39 | t.Parallel() |
| 40 | api := newAPI(t) |
| 41 | from, to := uuid.NewString(), uuid.NewString() |
| 42 | |
| 43 | // One valid request, then every way to get one wrong, one at a time. |
| 44 | tests := []struct { |
| 45 | name string |
| 46 | body string |
| 47 | field string // the one field that's wrong: none for a valid request |
| 48 | }{ |
| 49 | {"valid", body(from, to, 1500, "USD"), ""}, |
| 50 | {"from is not an ID", body("alice", to, 1500, "USD"), "from"}, |
| 51 | {"to is not an ID", body(from, "bob", 1500, "USD"), "to"}, |
| 52 | {"to is from", body(from, from, 1500, "USD"), "to"}, |
| 53 | {"zero amount", body(from, to, 0, "USD"), "amount"}, |
| 54 | {"negative amount", body(from, to, -1500, "USD"), "amount"}, |
| 55 | {"over a million dollars", body(from, to, 100_000_001, "USD"), "amount"}, |
| 56 | {"lowercase currency", body(from, to, 1500, "usd"), "currency"}, |
| 57 | {"no currency", body(from, to, 1500, ""), "currency"}, |
| 58 | } |
| 59 | for _, tt := range tests { |
| 60 | t.Run(tt.name, func(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | req := httptest.NewRequest("POST", "/v1/transfers", strings.NewReader(tt.body)) |
| 63 | req.Header.Set("Idempotency-Key", uuid.NewString()) |
| 64 | rr := httptest.NewRecorder() |
| 65 | api.ServeHTTP(rr, req) |
| 66 | |
| 67 | want := http.StatusAccepted |
| 68 | if tt.field != "" { |
| 69 | want = http.StatusUnprocessableEntity |
| 70 | } |
| 71 | if rr.Code != want { |
| 72 | t.Fatalf("status = %d, want %d: %s", rr.Code, want, rr.Body) |
| 73 | } |
| 74 | var resp struct { |
| 75 | Error struct { |
| 76 | Fields map[string]string `json:"fields"` |
| 77 | } `json:"error"` |
| 78 | } |
| 79 | json.Unmarshal(rr.Body.Bytes(), &resp) |
| 80 | if f := resp.Error.Fields; tt.field != "" && (len(f) != 1 || f[tt.field] == "") { |
| 81 | t.Errorf("errors %v, want one, about %s", f, tt.field) |
| 82 | } |
| 83 | }) |
| 84 | } |
| 85 | } |