-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadjust_endpoints_test.go
75 lines (57 loc) · 2.07 KB
/
adjust_endpoints_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
package api_test
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"go.uber.org/zap"
"sigs.k8s.io/external-dns/endpoint"
"github.com/stackitcloud/external-dns-stackit-webhook/pkg/api"
mock_provider "github.com/stackitcloud/external-dns-stackit-webhook/pkg/api/mock"
)
func TestWebhook_AdjustEndpoints(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
t.Cleanup(ctrl.Finish)
mockLogger := zap.NewNop()
mockProvider := mock_provider.NewMockProvider(ctrl)
mockMetricsCollector := getTestMockMetricsCollector(ctrl)
app := api.New(mockLogger, mockMetricsCollector, mockProvider)
t.Run("Test provider returns records successfully", func(t *testing.T) {
t.Parallel()
endpoints := []*endpoint.Endpoint{
{
DNSName: "test.com",
RecordType: "A",
},
}
mockProvider.EXPECT().AdjustEndpoints(endpoints).Return(endpoints).Times(1)
body, err := json.Marshal(endpoints)
assert.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/adjustendpoints", bytes.NewReader(body))
req.Header.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
resp, err := app.Test(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
t.Run("Test invalid data send by client", func(t *testing.T) {
t.Parallel()
reqBad := httptest.NewRequest(http.MethodPost, "/adjustendpoints", bytes.NewReader([]byte(`{"bad":"request"}`)))
reqBad.Header.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
respBad, err := app.Test(reqBad)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, respBad.StatusCode)
})
t.Run("Client send invalid JSON", func(t *testing.T) {
t.Parallel()
reqBad := httptest.NewRequest(http.MethodPost, "/adjustendpoints", bytes.NewReader([]byte(`{"wrong:"request"`)))
reqBad.Header.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
respBad, err := app.Test(reqBad)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, respBad.StatusCode)
})
}