-
Notifications
You must be signed in to change notification settings - Fork 0
/
vies_test.go
168 lines (149 loc) · 5.79 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
package vies_test
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/macabu/vies"
)
const (
validMockedResponse string = `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><checkVatResponse xmlns="urn:ec.europa.eu:taxud:vies:services:checkVat:types"><countryCode>NL</countryCode><vatNumber>100</vatNumber><requestDate>2021-04-10+02:00</requestDate><valid>true</valid><name>John Doe</name><address>123 Main St, Anytown, UK</address></checkVatResponse></soap:Body></soap:Envelope>`
invalidMockedResponse string = `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>INVALID_REQUESTER_INFO</faultstring></soap:Fault></soap:Body></soap:Envelope>`
)
func TestCheckVAT(t *testing.T) {
validReq := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if validReq {
fmt.Fprintln(w, validMockedResponse)
} else {
fmt.Fprintln(w, invalidMockedResponse)
}
}))
defer ts.Close()
svc := vies.NewService(ts.URL)
vat, err := svc.CheckVAT("NL1234")
if err != nil {
t.Errorf("expected err to be nil, got %v", err.Error())
}
if vat == nil {
t.Error("expected vat to not be nil, got nil")
}
validReq = false
svc = vies.NewServiceWithTimeout(ts.URL, time.Minute)
vat, err = svc.CheckVAT("NL1234")
if err == nil {
t.Errorf("expected err to not be nil, got nil")
}
if !errors.Is(err, vies.ErrInvalidRequesterInfo) {
t.Errorf("expected err to be %v, got %v", vies.ErrInvalidRequesterInfo, err.Error())
}
if vat != nil {
t.Errorf("expected vat to be nil, got %+v", vat)
}
}
func TestCheckVATWithCountry(t *testing.T) {
validReq := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if validReq {
fmt.Fprintln(w, validMockedResponse)
} else {
fmt.Fprintln(w, invalidMockedResponse)
}
}))
defer ts.Close()
svc := vies.NewService(ts.URL)
vat, err := svc.CheckVATWithCountry("NL", "NL1234")
if err != nil {
t.Errorf("expected err to be nil, got %v", err.Error())
}
if vat == nil {
t.Error("expected vat to not be nil, got nil")
}
validReq = false
svc = vies.NewServiceWithTimeout(ts.URL, time.Minute)
vat, err = svc.CheckVATWithCountry("NL", "NL1234")
if err == nil {
t.Errorf("expected err to not be nil, got nil")
}
if !errors.Is(err, vies.ErrInvalidRequesterInfo) {
t.Errorf("expected err to be %v, got %v", vies.ErrInvalidRequesterInfo, err.Error())
}
if vat != nil {
t.Errorf("expected vat to be nil, got %+v", vat)
}
}
func TestCheckVATWithTestEndpoint(t *testing.T) {
if testing.Short() {
t.Skip()
}
svc := vies.NewService(vies.TestEndpoint)
testCases := []struct {
vatNumber string
expectedValid bool
expectedError error
}{
{vatNumber: "NL100", expectedValid: true},
{vatNumber: "NL201", expectedError: vies.ErrInvalidInput},
{vatNumber: "NL202", expectedError: vies.ErrInvalidRequesterInfo},
{vatNumber: "NL301", expectedError: vies.ErrMsUnavailable},
{vatNumber: "NL302", expectedError: vies.ErrTimeout},
{vatNumber: "NL400", expectedError: vies.ErrVATBlocked},
{vatNumber: "NL401", expectedError: vies.ErrIpBlocked},
{vatNumber: "NL500", expectedError: vies.ErrGlobalMaxConcurrentReq},
{vatNumber: "NL501", expectedError: vies.ErrGlobalMaxConcurrentReqTime},
{vatNumber: "NL600", expectedError: vies.ErrMsMaxConcurrentReq},
{vatNumber: "NL601", expectedError: vies.ErrMsMaxConcurrentReqTime},
{vatNumber: "NLanythingelse", expectedError: vies.ErrServiceUnavailable},
}
for _, tc := range testCases {
t.Run("with vat "+tc.vatNumber, func(t *testing.T) {
vat, err := svc.CheckVAT(tc.vatNumber)
if tc.expectedError != nil && !errors.Is(err, tc.expectedError) {
t.Errorf("expected err to be %v, got %v", tc.expectedError, err.Error())
}
if vat != nil && vat.Valid != tc.expectedValid {
t.Errorf("expected vat validation flag to be %v, got %v", tc.expectedValid, vat.Valid)
}
<-time.After(500 * time.Millisecond) // don't stress VIES
})
}
}
func TestCheckVATWithCountryWithTestEndpoint(t *testing.T) {
if testing.Short() {
t.Skip()
}
svc := vies.NewService(vies.TestEndpoint)
testCases := []struct {
countryCode string
vatNumber string
expectedValid bool
expectedError error
}{
{countryCode: "NL", vatNumber: "100", expectedValid: true},
{countryCode: "NL", vatNumber: "201", expectedError: vies.ErrInvalidInput},
{countryCode: "NL", vatNumber: "202", expectedError: vies.ErrInvalidRequesterInfo},
{countryCode: "NL", vatNumber: "301", expectedError: vies.ErrMsUnavailable},
{countryCode: "NL", vatNumber: "302", expectedError: vies.ErrTimeout},
{countryCode: "NL", vatNumber: "400", expectedError: vies.ErrVATBlocked},
{countryCode: "NL", vatNumber: "401", expectedError: vies.ErrIpBlocked},
{countryCode: "NL", vatNumber: "500", expectedError: vies.ErrGlobalMaxConcurrentReq},
{countryCode: "NL", vatNumber: "501", expectedError: vies.ErrGlobalMaxConcurrentReqTime},
{countryCode: "NL", vatNumber: "600", expectedError: vies.ErrMsMaxConcurrentReq},
{countryCode: "NL", vatNumber: "601", expectedError: vies.ErrMsMaxConcurrentReqTime},
{countryCode: "NL", vatNumber: "anythingelse", expectedError: vies.ErrServiceUnavailable},
}
for _, tc := range testCases {
t.Run("with country "+tc.countryCode+" and vat "+tc.vatNumber, func(t *testing.T) {
vat, err := svc.CheckVATWithCountry(tc.countryCode, tc.vatNumber)
if tc.expectedError != nil && !errors.Is(err, tc.expectedError) {
t.Errorf("expected err to be %v, got %v", tc.expectedError, err.Error())
}
if vat != nil && vat.Valid != tc.expectedValid {
t.Errorf("expected vat validation flag to be %v, got %v", tc.expectedValid, vat.Valid)
}
<-time.After(500 * time.Millisecond) // don't stress VIES
})
}
}