-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout_test.go
136 lines (110 loc) · 3.01 KB
/
checkout_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
package hc
import (
"encoding/json"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnconfiguredClientID(t *testing.T) {
assert := assert.New(t)
_, err := Setup("", "efgh")
assert.Error(err)
assert.EqualError(err, "configure client_id")
}
func TestUnconfiguredClientSecret(t *testing.T) {
assert := assert.New(t)
_, err := Setup("abcd", "")
assert.Error(err)
assert.EqualError(err, "configure client_secret")
}
func TestUnconfiguredClientIDAndSecret(t *testing.T) {
assert := assert.New(t)
_, err := Setup("", "")
assert.Error(err)
assert.EqualError(err, "configure client_id and client_secret")
}
func TestAuthKeyGenerator(t *testing.T) {
whiteSpace := " "
assert := assert.New(t)
ak, _ := genAuthKey("abcd", "efgh")
assert.True(strings.HasPrefix(ak, "Basic"))
assert.True(strings.Count(ak, whiteSpace) == 1, "contains < 1 or > 1 whitespace")
assert.True(ak != "BASIC yWJjZDplZmdo")
}
func TestRequestBodyGenerator(t *testing.T) {
assert := assert.New(t)
c, err := Setup("abcd", "efgh")
assert.NoError(err)
body, err := c.genRequestBody()
if assert.NoError(err) {
assert.IsType([]byte{}, body)
eb, _ := json.Marshal(&checkoutRequest{
Invoice: c.Invoice,
Store: c.Store,
Actions: c.Actions,
CustomData: c.CustomData,
})
assert.Exactly(string(eb), string(body))
}
}
func TestCheckoutApiWrapper(t *testing.T) {
clientID := os.Getenv("HUBTEL_ID")
clientSecret := os.Getenv("HUBTEL_SECRET")
assert := assert.New(t)
c, err := Setup(clientID, clientSecret)
assert.NoError(err)
if assert.IsType(&Checkout{}, c) {
// checkout store
checkoutStore := &Store{
Name: "T Shirt Company",
Tagline: "Tagline of the online store",
Phone: "233244124660",
PostAddress: "Box 10770 Accra - Ghana",
LogoURL: "https://company-logo-final.png",
WebsiteURL: "https://company.com",
}
c.Store = checkoutStore
// checkout actions
c.Actions.CancelURL = "http://company.com"
c.Actions.ReturnURL = "http://company.com"
// checkout invoice
c.Invoice.Description = "Invoice Description"
c.Invoice.AddItem("T-Shirt", 2, 35.0, 70.0, "Order of 2 T-Shirts")
c.Invoice.AddItem("Snikers", 1, 50.0, 50.0, "Order of 1 Old Navy Jeans")
c.Invoice.AddTax("Tax on purchase", 0.50)
c.Invoice.TotalAmount = 120.50
// checkout custom data
c.CustomData.Add("email", "[email protected]")
r, err := c.Create()
if assert.NoError(err) {
if r.ResponseCode == "00" {
open(r.ResponseText)
r, err = c.Status(r.Token)
if assert.NoError(err) {
if r.ResponseCode == "00" {
assert.Equal(c.Invoice, r.Invoice)
assert.Equal(c.CustomData, r.CustomData)
}
}
}
}
}
}
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}