forked from hostwithquantum/golexoffice
/
errors_test.go
77 lines (67 loc) · 2.37 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
package golexoffice_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
lexoffice "github.com/karitham/go-lexoffice"
"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()
config := lexoffice.NewClient("api-key", lexoffice.WithBaseUrl(server.URL))
t.Run("errors=legacy", func(t *testing.T) {
_, err := config.CreateContact(context.Background(), lexoffice.ContactBody{
Company: &lexoffice.ContactBodyCompany{
Name: "company",
VatRegistrationId: "",
TaxNumber: "",
},
})
assert.Error(t, err)
})
t.Run("errors=new", func(t *testing.T) {
_, err := config.CreateInvoice(context.Background(), lexoffice.CreateInvoiceOptions{})
assert.Error(t, err)
})
}
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)
}))
}