-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocti_internal_test.go
218 lines (198 loc) · 4.67 KB
/
gocti_internal_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
212
213
214
215
216
217
218
package gocti
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weisshorn-cyd/gocti/api"
)
//nolint:gochecknoglobals // Test variables
var (
testFile1 = api.File{
Name: "File1.json",
Data: []byte{},
MIME: "application/json",
}
testFile2 = api.File{
Name: "File2.txt",
Data: []byte{},
MIME: "text/plain",
}
testFileNoName = api.File{
Name: "",
Data: []byte{},
MIME: "application/json",
}
testFileNoMIME = api.File{
Name: "File3.txt",
Data: []byte{},
MIME: "",
}
)
func Test_mapFileVariables(t *testing.T) {
t.Parallel()
tests := []struct {
name string
vars map[string]any
wantInput map[string]any
wantFiles []api.File
wantMap map[int][]string
}{
{
name: "No file",
vars: map[string]any{
"string": "string",
"int": 12,
"bool": false,
"list": []string{"val1", "val2"},
"map": map[any]any{1: "1", "2": 2},
},
wantInput: map[string]any{
"string": "string",
"int": 12,
"bool": false,
"list": []string{"val1", "val2"},
"map": map[any]any{1: "1", "2": 2},
},
wantFiles: []api.File{},
wantMap: map[int][]string{},
}, {
name: "Single file",
vars: map[string]any{
"file": testFile1,
},
wantInput: map[string]any{
"file": nil,
},
wantFiles: []api.File{testFile1},
wantMap: map[int][]string{0: {"variables.file"}},
}, {
name: "Multiple files",
vars: map[string]any{
"files": []api.File{
testFile1,
testFile2,
},
},
wantInput: map[string]any{
"files": []any{nil, nil},
},
wantFiles: []api.File{testFile1, testFile2},
wantMap: map[int][]string{
0: {"variables.files.0"},
1: {"variables.files.1"},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
gotFiles, gotMap := mapFileVariables(test.vars)
assert.Len(t, gotMap, len(gotFiles))
assert.Equal(t, test.wantInput, test.vars, "Input changes mismatch")
assert.Equal(t, test.wantFiles, gotFiles, "Output files mismatch")
assert.Equal(t, test.wantMap, gotMap, "Output map mismatch")
})
}
}
func Test_queryBody(t *testing.T) {
t.Parallel()
tests := []struct {
name string
query string
vars map[string]any
wantType string
wantErr bool
}{
{
name: "Standard query - no file var",
query: "query",
vars: map[string]any{
"bool": false,
"int": 12,
"string": "v1",
},
wantType: "application/json",
wantErr: false,
}, {
name: "Upload query - single file",
query: "upload",
vars: map[string]any{
"file": testFile1,
},
wantType: "multipart/form-data",
wantErr: false,
}, {
name: "Upload query - multiple files",
query: "upload",
vars: map[string]any{
"files": []api.File{
testFile1,
testFile2,
},
},
wantType: "multipart/form-data",
wantErr: false,
}, {
name: "Upload query - no file name",
query: "upload",
vars: map[string]any{
"file": testFileNoName,
},
wantType: "multipart/form-data",
wantErr: true,
}, {
name: "Upload query - no MIME type",
query: "upload",
vars: map[string]any{
"file": testFileNoMIME,
},
wantType: "multipart/form-data",
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
_, gotType, err := queryBody(test.query, test.vars)
if test.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Contains(t, gotType, test.wantType)
}
})
}
}
// MockTransport is a RoundTripper that returns a fixed JSON body.
type MockTransport struct {
header http.Header
jsonBody string
}
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
m.header = req.Header.Clone()
resp := &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewBufferString(m.jsonBody)),
Request: req,
}
return resp, nil
}
func TestOpenCTIAPIClient_Impersonate(t *testing.T) {
t.Parallel()
mock := &MockTransport{jsonBody: `{"data":{"users":{"edges":[{"node":{"id":"test-user-id"}}]}}}`}
client, err := NewOpenCTIAPIClient("url", "token", WithTransport(mock))
require.NoError(t, err)
err = client.Impersonate(context.Background(), "test-user")
require.NoError(t, err)
_, err = client.ListUsers(context.Background(), "id", true, nil)
require.NoError(t, err)
assert.Equal(t, "test-user-id", mock.header.Get("Opencti-Applicant-Id"))
_, err = client.ListUsers(context.Background(), "id", true, nil)
require.NoError(t, err)
assert.Equal(t, "", mock.header.Get("Opencti-Applicant-Id"))
}