-
Notifications
You must be signed in to change notification settings - Fork 6
/
vies_test.go
193 lines (140 loc) · 4.45 KB
/
vies_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
package finance_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/pieterclaerhout/go-finance"
)
const nameApple = "SRL APPLE RETAIL BELGIUM"
const addrApple = "Avenue du Port 86C/204\n1000 Bruxelles"
func TestCheck(t *testing.T) {
type test struct {
name string
vatNumber string
expectedCountryCode string
expectedVATnumber string
expectedName string
expectedAddress string
expectedIsValid bool
expectedError error
}
var tests = []test{
{"empty", "", "", "", "", "", false, finance.ErrVATNumberTooShort},
{"valid-spaces", "BE 0836 157 420", "BE", "0836157420", nameApple, addrApple, true, nil},
{"valid-nospaces", "BE0836157420", "BE", "0836157420", nameApple, addrApple, true, nil},
{"valid-dots", "BE 0836.157.420", "BE", "0836157420", nameApple, addrApple, true, nil},
{"valid-ie", "IE6388047V", "IE", "6388047V", "GOOGLE IRELAND LIMITED", "3RD FLOOR, GORDON HOUSE, BARROW STREET, DUBLIN 4", true, nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
time.Sleep(1 * time.Second)
result, err := finance.CheckVAT(tc.vatNumber)
if tc.expectedError != nil {
assert.Nil(t, result)
assert.Error(t, err)
} else {
assert.NotNil(t, result)
assert.NoError(t, err)
if result != nil {
assert.Equal(t, tc.expectedCountryCode, result.CountryCode, "country-code")
assert.Equal(t, tc.expectedVATnumber, result.VATNumber, "vat-number")
assert.Equal(t, tc.expectedIsValid, result.IsValid, "is-valid")
assert.Equal(t, tc.expectedName, result.Name, "name")
assert.Equal(t, tc.expectedAddress, result.Address, "address")
}
}
})
}
}
func TestCheckVATInvalidURL(t *testing.T) {
finance.VATServiceURL = "ht&@-tp://:aa"
defer func() {
finance.VATServiceURL = finance.DefaultVATServiceURL
}()
result, err := finance.CheckVAT("BE0836157420")
assert.Nil(t, result)
assert.Error(t, err)
}
func TestCheckVATTimeout(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(500 * time.Millisecond)
w.Header().Set("Content-Type", "text/xml")
w.Write([]byte("hello"))
}),
)
defer s.Close()
finance.VATTimeout = 250 * time.Millisecond
finance.VATServiceURL = s.URL
defer func() {
finance.VATTimeout = finance.DefaultVATTimeout
finance.VATServiceURL = finance.DefaultVATServiceURL
}()
result, err := finance.CheckVAT("BE0836157420")
assert.Nil(t, result)
assert.Error(t, err)
}
func TestCheckVATReadBodyError(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Length", "1")
}),
)
defer s.Close()
finance.VATServiceURL = s.URL
defer func() {
finance.VATServiceURL = finance.DefaultVATServiceURL
}()
result, err := finance.CheckVAT("BE0836157420")
assert.Nil(t, result)
assert.Error(t, err)
}
func TestCheckVATInvalidInput(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("INVALIDINPUT"))
}),
)
defer s.Close()
finance.VATServiceURL = s.URL
defer func() {
finance.VATServiceURL = finance.DefaultVATServiceURL
}()
result, err := finance.CheckVAT("BE0836157420")
assert.Nil(t, result)
assert.Error(t, err)
assert.Equal(t, finance.ErrVATnumberNotValid, err)
}
func TestCheckVATInvalidXML(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("<vies>"))
}),
)
defer s.Close()
finance.VATServiceURL = s.URL
defer func() {
finance.VATServiceURL = finance.DefaultVATServiceURL
}()
result, err := finance.CheckVAT("BE0836157420")
assert.Nil(t, result)
assert.Error(t, err)
}
func TestCheckVATSoapFault(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>error</faultstring></soap:Fault></soap:Body></soap:Envelope>`))
}),
)
defer s.Close()
finance.VATServiceURL = s.URL
defer func() {
finance.VATServiceURL = finance.DefaultVATServiceURL
}()
result, err := finance.CheckVAT("BE0836157420")
assert.Nil(t, result)
assert.Error(t, err)
assert.Equal(t, finance.ErrVATserviceError+"error", err.Error(), "error-message")
}