This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreceipts_test.go
142 lines (126 loc) · 3.62 KB
/
receipts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package starling
import (
"context"
"encoding/json"
"net/http"
"reflect"
"testing"
)
var rcptTestCases = []struct {
name string
receipt Receipt
status int
mock string
}{
{
name: "valid receipt",
receipt: Receipt{
UID: "9af397e9-63e8-4a72-b3f6-4f0b068b2ed0",
EventUID: "feada116-963e-4246-8fac-ed5e499b03d5",
MetadataSource: "tests",
ReceiptIdentifier: "0987654321",
MerchantIdentifier: "2b03a13a-bbfc-4479-8d4d-abb6a9119d27",
MerchantAddress: "1 Merchant Way, Go Tests, UK",
TotalAmount: 1.23,
TotalTax: 0.45,
TaxReference: "1234567890",
AuthCode: "639682",
CardLast4: "9012",
ProviderName: "demo",
Items: []ReceiptItem{
{
UID: "d74b17d5-f114-42d2-8adb-a6ddedc29298",
Description: "Large coffee",
Quantity: 2,
Amount: 12.34,
Tax: 1.23,
URL: "https://crossenvsync",
},
},
Notes: []ReceiptNote{
{
UID: "152dcc19-c86b-4f03-ba46-82aadbdcc957",
Description: "Large coffee",
URL: "https://crossenvsync",
},
},
},
status: http.StatusAccepted,
mock: ``,
},
}
func TestCreateReceipt(t *testing.T) {
for _, tc := range rcptTestCases {
t.Run(tc.name, func(st *testing.T) {
testCreateReceipt(st, tc.name, tc.receipt, tc.mock, tc.status)
})
}
}
func testCreateReceipt(t *testing.T, name string, rcpt Receipt, mock string, status int) {
client, mux, _, teardown := setup()
defer teardown()
txnUID := "ab4b76ef-8e98-4181-8be9-164d73e16d99"
mux.HandleFunc("/api/v1/transactions/mastercard/"+txnUID+"/receipt", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPost)
var reqRcpt = Receipt{}
err := json.NewDecoder(r.Body).Decode(&reqRcpt)
if err != nil {
t.Fatal("should send a request that the API can parse", cross, err)
}
if !reflect.DeepEqual(rcpt, reqRcpt) {
t.Error("should send a receipt that matches the mock", cross)
}
w.WriteHeader(http.StatusAccepted)
})
resp, err := client.CreateReceipt(context.Background(), txnUID, rcpt)
if status <= 299 {
checkNoError(t, err)
} else {
checkHasError(t, err)
}
checkStatus(t, resp, status)
}
func TestCreateReceiptForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/transactions/mastercard/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusForbidden)
})
receipt := Receipt{
UID: "9af397e9-63e8-4a72-b3f6-4f0b068b2ed0",
EventUID: "feada116-963e-4246-8fac-ed5e499b03d5",
MetadataSource: "tests",
ReceiptIdentifier: "0987654321",
MerchantIdentifier: "2b03a13a-bbfc-4479-8d4d-abb6a9119d27",
MerchantAddress: "1 Merchant Way, Go Tests, UK",
TotalAmount: 1.23,
TotalTax: 0.45,
TaxReference: "1234567890",
AuthCode: "639682",
CardLast4: "9012",
ProviderName: "demo",
Items: []ReceiptItem{
{
UID: "d74b17d5-f114-42d2-8adb-a6ddedc29298",
Description: "Large coffee",
Quantity: 2,
Amount: 12.34,
Tax: 1.23,
URL: "https://crossenvsync",
},
},
Notes: []ReceiptNote{
{
UID: "152dcc19-c86b-4f03-ba46-82aadbdcc957",
Description: "Large coffee",
URL: "https://crossenvsync",
},
},
}
resp, err := client.CreateReceipt(context.Background(), "949404bd-d32e-4f1e-9759-4d6caee3137c", receipt)
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
}