forked from NXRSE/bank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpApiHandlers_test.go
137 lines (109 loc) · 3.26 KB
/
httpApiHandlers_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/ksred/bank/accounts"
"github.com/ksred/bank/appauth"
"github.com/ksred/bank/configuration"
"github.com/ksred/bank/payments"
"github.com/ksred/bank/push"
)
func TestLoadConfiguration(t *testing.T) {
// Load app config
_, err := configuration.LoadConfig()
if err != nil {
t.Errorf("loadDatabase does not pass. Configuration does not load, looking for %v, got %v", nil, err)
}
}
func loadAllConfig(t *testing.T) {
// Load app config
Config, err := configuration.LoadConfig()
if err != nil {
t.Errorf("loadDatabase does not pass. Configuration does not load, looking for %v, got %v", nil, err)
}
// Set config in packages
accounts.SetConfig(&Config)
payments.SetConfig(&Config)
appauth.SetConfig(&Config)
push.SetConfig(&Config)
}
func TestIndex(t *testing.T) {
loadAllConfig(t)
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(Index)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
//expected := `{"alive": true}`
//if rr.Body.String() != expected {
// t.Errorf("handler returned unexpected body: got %v want %v",
// rr.Body.String(), expected)
//}
}
// Extend token
func TestAuthIndexFailNoToken(t *testing.T) {
loadAllConfig(t)
req, err := http.NewRequest("POST", "/auth", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(AuthIndex)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestAuthIndexFailIncorrectToken(t *testing.T) {
loadAllConfig(t)
req, err := http.NewRequest("POST", "/auth", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Auth-Token", "incorrect-token")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(AuthIndex)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
/*
func TestAccountCreate(t *testing.T) {
loadAllConfig(t)
user := map[string]string{
"AccountHolderGivenName": "Test",
"AccountHolderFamilyName": "Account",
"AccountHolderDateOfBirth": "01011990",
"AccountHolderIdentificationNumber": "1234567890",
"AccountHolderContactNumber1": "180012345",
"AccountHolderContactNumber2": "",
"AccountHolderEmailAddress": "test@email.com",
"AccountHolderAddressLine1": "Address Line 1",
"AccountHolderAddressLine2": "Address Line 2",
"AccountHolderAddressLine3": "Address Line 3",
"AccountHolderPostalCode": "AB1 CD2",
}
req, err := http.NewRequest("POST", "/account", user)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(AuthIndex)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
*/