forked from jitsucom/jitsu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
211 lines (189 loc) · 5.34 KB
/
main_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bytes"
"github.com/ksensehq/eventnative/destinations"
"github.com/ksensehq/eventnative/events"
"github.com/ksensehq/eventnative/logging"
"github.com/ksensehq/eventnative/middleware"
"github.com/ksensehq/eventnative/telemetry"
"github.com/ksensehq/eventnative/test"
"time"
"bou.ke/monkey"
"github.com/ksensehq/eventnative/appconfig"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"testing"
)
func SetTestDefaultParams() {
viper.Set("log.path", "")
viper.Set("server.auth", `{"tokens":[{"id":"id1","client_secret":"c2stoken","server_secret":"s2stoken","origins":["whiteorigin*"]}]}`)
}
func TestApiEvent(t *testing.T) {
SetTestDefaultParams()
tests := []struct {
name string
reqUrn string
reqOrigin string
reqBodyPath string
expectedJsonPath string
xAuthToken string
expectedHttpCode int
expectedErrMsg string
}{
{
"Unauthorized c2s endpoint",
"/api/v1/event?token=wrongtoken",
"",
"test_data/event_input.json",
"",
"",
http.StatusUnauthorized,
"",
},
{
"Unauthorized s2s endpoint",
"/api/v1/s2s/event?token=wrongtoken",
"",
"test_data/api_event_input.json",
"",
"",
http.StatusUnauthorized,
"The token isn't a server token. Please use s2s integration token\n",
},
{
"Unauthorized c2s endpoint with s2s token",
"/api/v1/event?token=s2stoken",
"",
"test_data/event_input.json",
"",
"",
http.StatusUnauthorized,
"",
},
{
"Unauthorized s2s wrong origin",
"/api/v1/s2s/event?token=s2stoken",
"http://ksense.com",
"test_data/api_event_input.json",
"",
"",
http.StatusUnauthorized,
"",
},
{
"C2S Api event consuming test",
"/api/v1/event?token=c2stoken",
"https://whiteorigin.com/",
"test_data/event_input.json",
"test_data/fact_output.json",
"",
http.StatusOK,
"",
},
{
"S2S Api event consuming test",
"/api/v1/s2s/event",
"https://whiteorigin.com/",
"test_data/api_event_input.json",
"test_data/api_fact_output.json",
"s2stoken",
http.StatusOK,
"",
},
{
"S2S Api malformed event test",
"/api/v1/s2s/event",
"https://whiteorigin.com/",
"test_data/malformed_input.json",
"",
"s2stoken",
http.StatusBadRequest,
`{"message":"Failed to parse body","error":{"Offset":2}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
telemetry.Init("test", "test", "test", true)
httpAuthority, _ := getLocalAuthority()
err := appconfig.Init()
require.NoError(t, err)
defer appconfig.Instance.Close()
inmemWriter := logging.InitInMemoryWriter()
router := SetupRouter(destinations.NewTestService(
map[string]map[string]events.Consumer{
"id1": {"id1": events.NewAsyncLogger(inmemWriter, false)},
}, map[string]map[string]events.StorageProxy{}), "")
freezeTime := time.Date(2020, 06, 16, 23, 0, 0, 0, time.UTC)
patch := monkey.Patch(time.Now, func() time.Time { return freezeTime })
defer patch.Unpatch()
server := &http.Server{
Addr: httpAuthority,
Handler: middleware.Cors(router),
ReadTimeout: time.Second * 60,
ReadHeaderTimeout: time.Second * 60,
IdleTimeout: time.Second * 65,
}
go func() {
log.Fatal(server.ListenAndServe())
}()
logging.Info("Started listen and serve " + httpAuthority)
//check ping endpoint
resp, err := test.RenewGet("http://" + httpAuthority + "/ping")
require.NoError(t, err)
b, err := ioutil.ReadFile(tt.reqBodyPath)
require.NoError(t, err)
//check http OPTIONS
optReq, err := http.NewRequest("OPTIONS", "http://"+httpAuthority+tt.reqUrn, bytes.NewBuffer(b))
require.NoError(t, err)
optResp, err := http.DefaultClient.Do(optReq)
require.NoError(t, err)
require.Equal(t, 200, optResp.StatusCode)
//check http POST
apiReq, err := http.NewRequest("POST", "http://"+httpAuthority+tt.reqUrn, bytes.NewBuffer(b))
require.NoError(t, err)
if tt.reqOrigin != "" {
apiReq.Header.Add("Origin", tt.reqOrigin)
}
if tt.xAuthToken != "" {
apiReq.Header.Add("x-auth-token", tt.xAuthToken)
}
apiReq.Header.Add("x-real-ip", "95.82.232.185")
resp, err = http.DefaultClient.Do(apiReq)
require.NoError(t, err)
if tt.expectedHttpCode != 200 {
require.Equal(t, tt.expectedHttpCode, resp.StatusCode, "Http cods aren't equal")
b, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
resp.Body.Close()
require.Equal(t, tt.expectedErrMsg, string(b))
} else {
require.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin"), "Cors header ACAO is empty")
require.Equal(t, http.StatusOK, resp.StatusCode, "Http code isn't 200")
resp.Body.Close()
time.Sleep(200 * time.Millisecond)
data := logging.InstanceMock.Data
require.Equal(t, 1, len(data))
fBytes, err := ioutil.ReadFile(tt.expectedJsonPath)
require.NoError(t, err)
test.JsonBytesEqual(t, fBytes, data[0], "Logged facts aren't equal")
}
})
}
}
func getLocalAuthority() (string, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return "", err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return "", err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).IP.String() + ":" + strconv.Itoa(l.Addr().(*net.TCPAddr).Port), nil
}