forked from jjideenschmiede/golexoffice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_test.go
172 lines (153 loc) · 5.09 KB
/
errors_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
package golexoffice_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/hostwithquantum/golexoffice"
"github.com/stretchr/testify/assert"
)
// {"requestId":"3fb21ee4-ad26-4e2f-82af-a1197af02d08","IssueList":[{"i18nKey":"invalid_value","source":"company and person","type":"validation_failure"},{"i18nKey":"missing_entity","source":"company.name","type":"validation_failure"}]}
// {"requestId":"75d4dad6-6ccb-40fd-8c22-797f2d421d98","IssueList":[{"i18nKey":"missing_entity","source":"company.vatRegistrationId","type":"validation_failure"},{"i18nKey":"missing_entity","source":"company.taxNumber","type":"validation_failure"}]}
func TestErrorResponse(t *testing.T) {
server := errorMock()
defer server.Close()
lexOffice := golexoffice.NewConfig("token", nil)
lexOffice.SetBaseUrl(server.URL)
t.Run("errors=legacy", func(t *testing.T) {
_, err := lexOffice.AddContact(golexoffice.ContactBody{
Company: &golexoffice.ContactBodyCompany{
Name: "company",
VatRegistrationId: "",
TaxNumber: "",
},
})
assert.Error(t, err)
assert.ErrorContains(t, err, "key: missing_entity (company.vatRegistrationId): validation_failure")
assert.ErrorContains(t, err, "key: missing_entity (company.taxNumber): validation_failure")
})
t.Run("errors=new", func(t *testing.T) {
_, err := lexOffice.AddInvoice(golexoffice.InvoiceBody{})
assert.Error(t, err)
assert.ErrorContains(t, err, "field: lineItems[0].unitPrice.taxRatePercentage (NOTNULL): darf nicht leer sein")
})
}
func TestErrorNoDetails(t *testing.T) {
server := errorMockNoDetails()
defer server.Close()
lexOffice := golexoffice.NewConfig("token", nil)
lexOffice.SetBaseUrl(server.URL)
t.Run("errors=legacy", func(t *testing.T) {
_, err := lexOffice.AddContact(golexoffice.ContactBody{
Company: &golexoffice.ContactBodyCompany{
Name: "company",
VatRegistrationId: "",
TaxNumber: "",
},
})
assert.Error(t, err)
assert.ErrorContains(t, err, "something went wrong")
})
t.Run("errors=new", func(t *testing.T) {
_, err := lexOffice.AddInvoice(golexoffice.InvoiceBody{})
assert.Error(t, err)
assert.ErrorContains(t, err, "Something else went wrong. (406 Not Acceptable)")
})
}
func TestRateLimit(t *testing.T) {
rateLimitHits := 10
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if rateLimitHits > 0 {
rateLimitHits -= 1
w.WriteHeader(http.StatusTooManyRequests)
//nolint:errcheck
w.Write([]byte(`{
"status": 429,
"error": "Too Many Requests",
"message": "Rate limit exceeded"
}`))
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`)) //nolint:errcheck
}
}))
defer server.Close()
lexOffice := golexoffice.NewConfig("token", nil)
lexOffice.SetBaseUrl(server.URL)
t.Run("retry until ok", func(t *testing.T) {
rateLimitHits = 2
_, err := lexOffice.Invoice("tralalala")
assert.NoError(t, err)
})
t.Run("retry until out of retries", func(t *testing.T) {
rateLimitHits = 10
_, err := lexOffice.Invoice("tralalala")
assert.Error(t, err)
assert.ErrorContains(t, err, "Rate limit exceeded")
})
}
func errorMock() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v1/contacts/" {
w.WriteHeader(http.StatusBadRequest)
//nolint:errcheck
w.Write([]byte(`{
"requestId":"75d4dad6-6ccb-40fd-8c22-797f2d421d98",
"IssueList":[
{"i18nKey":"missing_entity","source":"company.vatRegistrationId","type":"validation_failure"},
{"i18nKey":"missing_entity","source":"company.taxNumber","type":"validation_failure"}
]
}`))
return
}
if r.URL.Path == "/v1/invoices" {
w.WriteHeader(http.StatusNotAcceptable)
//nolint:errcheck
w.Write([]byte(`{
"timestamp": "2017-05-11T17:12:31.233+02:00",
"status": 406,
"error": "Not Acceptable",
"path": "/v1/invoices",
"traceId": "90d78d0777be",
"message": "Validation failed for request. Please see details list for specific causes.",
"details": [
{
"violation": "NOTNULL",
"field": "lineItems[0].unitPrice.taxRatePercentage",
"message": "darf nicht leer sein"
}
]
}`))
return
}
w.WriteHeader(http.StatusNotFound)
}))
}
func errorMockNoDetails() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v1/contacts/" {
w.WriteHeader(http.StatusBadRequest)
// unclear if this can actually happen for these legacy errors
//nolint:errcheck
w.Write([]byte(`{
"requestId":"75d4dad6-6ccb-40fd-8c22-797f2d421d98",
"IssueList":[]
}`))
return
}
if r.URL.Path == "/v1/invoices" {
w.WriteHeader(http.StatusNotAcceptable)
// this *can* and *does* happen however (details is optional)
//nolint:errcheck
w.Write([]byte(`{
"timestamp": "2017-05-11T17:12:31.233+02:00",
"status": 406,
"error": "Not Acceptable",
"path": "/v1/invoices",
"traceId": "90d78d0777be",
"message": "Something else went wrong."
}`))
return
}
w.WriteHeader(http.StatusNotFound)
}))
}