-
Notifications
You must be signed in to change notification settings - Fork 12
/
adyen_test.go
144 lines (115 loc) · 2.95 KB
/
adyen_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
143
144
package adyen
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/joho/godotenv"
)
func TestMain(m *testing.M) {
// Set environment variables for subsequent tests.
if err := godotenv.Load(".default.env"); err != nil {
log.Fatalf("error loading .env file: %v", err)
}
os.Exit(m.Run())
}
func TestNewWithTimeout(t *testing.T) {
const timeout = time.Second * 123
act := New(Testing, "un", "pw", WithTimeout(timeout))
equals(t, timeout, act.client.Timeout)
}
func TestNewWithCurrency(t *testing.T) {
const currency = "USD"
act := New(Testing, "un", "pw", WithCurrency(currency))
equals(t, currency, act.Currency)
}
func TestNewWithCustomOptions(t *testing.T) {
const merchant, currency, timeout = "merch", "JPY", time.Second * 21
f1 := func(a *Adyen) {
a.Currency = currency
a.client.Timeout = timeout
}
f2 := func(a *Adyen) {
a.MerchantAccount = merchant
}
act := New(Testing, "un", "pw", f1, f2)
equals(t, merchant, act.MerchantAccount)
equals(t, currency, act.Currency)
equals(t, timeout, act.client.Timeout)
}
func equals(tb *testing.T, exp interface{}, act interface{}) {
_, fullPath, line, _ := runtime.Caller(1)
file := filepath.Base(fullPath)
if !reflect.DeepEqual(exp, act) {
fmt.Printf("%s:%d:\n\texp: %[3]v (%[3]T)\n\tgot: %[4]v (%[4]T)\n", file, line, exp, act)
tb.FailNow()
}
}
func assert(tb *testing.T, cond bool, message string) {
_, fullPath, line, _ := runtime.Caller(1)
file := filepath.Base(fullPath)
if !cond {
fmt.Printf("%s:%d:\n\t%s\n", file, line, message)
tb.FailNow()
}
}
// getTestInstance - instanciate adyen for tests
func getTestInstance() *Adyen {
instance := New(
Testing,
os.Getenv("ADYEN_USERNAME"),
os.Getenv("ADYEN_PASSWORD"))
return instance
}
// getTestInstanceWithHPP - instanciate adyen for tests
func getTestInstanceWithHPP() *Adyen {
instance := NewWithHMAC(
Testing,
os.Getenv("ADYEN_USERNAME"),
os.Getenv("ADYEN_PASSWORD"),
os.Getenv("ADYEN_HMAC"))
return instance
}
// randInt - get random integer from a given range
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
// randomString - generate randorm string of given length
// note: not for use in live code
func randomString(l int) string {
rand.Seed(time.Now().UTC().UnixNano())
bytes := make([]byte, l)
for i := 0; i < l; i++ {
bytes[i] = byte(randInt(65, 90))
}
return string(bytes)
}
// createTestResponse - create response object for tests
func createTestResponse(input, status string, code int) (*Response, error) {
body := strings.NewReader(input)
resp := &http.Response{
Status: status,
StatusCode: code,
ContentLength: int64(body.Len()),
Body: ioutil.NopCloser(body),
}
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
response := &Response{
Response: resp,
Body: buf.Bytes(),
}
return response, nil
}