forked from noebs/noebs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebs_service_test.go
204 lines (164 loc) · 4.8 KB
/
ebs_service_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"
"github.com/adonese/noebs/ebs_fields"
"github.com/gin-gonic/gin"
)
func TestEnv(t *testing.T) {
// Test that we can read environmental variables correctly.
key := "MYKEY"
val := "MYVA"
if err := os.Setenv(key, val); err != nil {
t.Errorf("An error occured: %s", err.Error())
}
if got := os.Getenv(key); got != val {
t.Errorf("environmental variable is incorrect. Wanted %s, Got: %s", key, got)
}
}
func TestWorkingKey(t *testing.T) {
var workingKeyFields ebs_fields.WorkingKeyFields
workingKeyFields.ClientID = "noebs"
workingKeyFields.TerminalID = "12345678"
workingKeyFields.TranDateTime = time.Now().UTC().String()
workingKeyFields.SystemTraceAuditNumber = rand.Intn(99999)
payload, err := json.Marshal(workingKeyFields)
if err != nil {
t.Fatal()
}
w := httptest.NewRecorder()
route := GetMainEngine()
fmt.Println(w.Body.String(), "Why is it.")
// well, assuming that the server is running. Eh?
// Mock data BTW...
req := httptest.NewRequest("POST", "/test", bytes.NewBuffer(payload))
route.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected: %d, got: %d", 200, w.Code)
}
// I'm really not sure why this would ever work.
// suddenly, things starting to make sense.
}
func TestPurchase(t *testing.T) {
// always returns t.Fatal...
// test a missing field always returns 400.
route := GetMainEngine()
t.Run("Test all fields passed", func(t *testing.T) {
fields := populatePurchaseFields(false)
now := time.Now()
iso8601 := now.Format(time.RFC3339)
fields.TranDateTime = iso8601
buff, err := json.Marshal(&fields)
if err != nil {
t.Fatalf("Error in marshalling %v", err)
}
req, _ := http.NewRequest(http.MethodPost, "/purchase", bytes.NewBuffer(buff))
w := httptest.NewRecorder()
route.ServeHTTP(w, req)
got := w.Code
want := 500
if got != want {
t.Errorf("got '%d', want '%d'", got, want)
t.Errorf(w.Body.String())
}
})
t.Run("Test missing field(s)", func(t *testing.T) {
fields := populatePurchaseFields(true)
buff, err := json.Marshal(&fields)
if err != nil {
t.Fatalf("Error in marshalling %v", err)
}
req, _ := http.NewRequest(http.MethodPost, "/purchase", bytes.NewBuffer(buff))
w := httptest.NewRecorder()
route.ServeHTTP(w, req)
got := w.Code
want := 400
if got != want {
t.Errorf("got '%d', want '%d'", got, want)
}
})
}
func TestEBSHttpClient(t *testing.T) {
// always return wrong
// i need to mock up EBS server (which is really challenging!
//t.Fatalf("Something went wrong")
t.Run("Testing wrong content-types", func(t *testing.T) {
url := "https://example.com"
payload := getSuccessfulPurchasePayload(ebs_fields.PurchaseFields{})
fmt.Println(string(payload))
_, _, err := ebs_fields.EBSHttpClient(url, payload)
if err != ebs_fields.ContentTypeErr {
t.Fatalf("Returned error is not of the correct type, %v. Wanted %v", err, ebs_fields.ContentTypeErr)
}
})
t.Run("Check the return error type is EBSFailedTransactionErr", func(t *testing.T) {
url := "https://212.0.129.118/terminal_api/transactions/purchase/"
payload := getFailedPurchasePayload(t, ebs_fields.PurchaseFields{})
fmt.Println(string(payload))
_, _, err := ebs_fields.EBSHttpClient(url, payload)
fmt.Print(reflect.TypeOf(err))
if err != ebs_fields.EbsFailedTransaction {
t.Fatalf("Returned error is not of the correct type. Got: (%s). Wanted: (%s)", reflect.TypeOf(err), ebs_fields.ContentTypeErr)
}
})
}
func TestCardTransfer(t *testing.T) {
route := GetMainEngine()
t.Run("Test all CardTransfer passed", func(t *testing.T) {
fields := populateCardTransferFields()
now := time.Now()
iso8601 := now.Format(time.RFC3339)
fields.TranDateTime = iso8601
buff, err := json.Marshal(&fields)
if err != nil {
t.Fatalf("Error in marshalling %v", err)
}
req, _ := http.NewRequest(http.MethodPost, "/cardTransfer", bytes.NewBuffer(buff))
w := httptest.NewRecorder()
route.ServeHTTP(w, req)
got := w.Code
want := 500
if got != want {
t.Errorf("got '%d', want '%d'", got, want)
t.Errorf(w.Body.String())
}
})
t.Run("Test missing field(s)", func(t *testing.T) {
fields := populateCardTransferFields()
buff, err := json.Marshal(&fields)
if err != nil {
t.Fatalf("Error in marshalling %v", err)
}
req, _ := http.NewRequest(http.MethodPost, "/cardTransfer", bytes.NewBuffer(buff))
w := httptest.NewRecorder()
route.ServeHTTP(w, req)
got := w.Code
want := 400
if got != want {
t.Errorf("got '%d', want '%d'", got, want)
}
})
}
func TestPurchase1(t *testing.T) {
type args struct {
c *gin.Context
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
})
}
}