-
Notifications
You must be signed in to change notification settings - Fork 29
/
bulk_handlers_test.go
321 lines (260 loc) · 8.94 KB
/
bulk_handlers_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"bytes"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/internal/testutils"
"github.com/RedHatInsights/sources-api-go/internal/testutils/fixtures"
"github.com/RedHatInsights/sources-api-go/internal/testutils/request"
"github.com/RedHatInsights/sources-api-go/internal/testutils/templates"
"github.com/RedHatInsights/sources-api-go/kafka"
"github.com/RedHatInsights/sources-api-go/middleware"
h "github.com/RedHatInsights/sources-api-go/middleware/headers"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/service"
"github.com/redhatinsights/platform-go-middlewares/identity"
)
func TestBulkCreateMissingSourceType(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
nameSource := "test"
bulkCreateSource := m.BulkCreateSource{SourceCreateRequest: m.SourceCreateRequest{Name: &nameSource}}
requestBody := m.BulkCreateRequest{Sources: []m.BulkCreateSource{bulkCreateSource}}
testUserId := "testUser"
body, err := json.Marshal(requestBody)
if err != nil {
t.Error("Could not marshal JSON")
}
c, _ := request.CreateTestContext(
http.MethodPost,
"/api/sources/v3.1/bulk_create",
bytes.NewReader(body),
map[string]interface{}{
"tenantID": int64(1),
},
)
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
c.Set(h.ParsedIdentity, &identity.XRHID{Identity: identity.Identity{AccountNumber: fixtures.TestTenantData[0].ExternalTenant}})
user, err := dao.GetUserDao(&fixtures.TestTenantData[0].Id).FindOrCreate(testUserId)
if err != nil {
t.Error(err)
}
c.Set(h.UserID, user.Id)
err = BulkCreate(c)
if !strings.Contains(err.Error(), "no source type present, need either [source_type_name] or [source_type_id]") {
t.Error(err)
}
err = dao.DB.Delete(&m.User{}, "user_id = ?", testUserId).Error
if err != nil {
t.Error(err)
}
}
func TestBulkCreateWithUserCreation(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
testUserId := "testUser"
identityHeader := testutils.IdentityHeaderForUser(testUserId)
nameSource := "test source"
sourceTypeName := "bitbucket"
applicationTypeName := "app-studio"
authenticationResourceType := "application"
requestBody := testutils.SingleResourceBulkCreateRequest(nameSource, sourceTypeName, applicationTypeName, authenticationResourceType)
body, err := json.Marshal(requestBody)
if err != nil {
t.Error("Could not marshal JSON")
}
c, res := request.CreateTestContext(
http.MethodPost,
"/api/sources/v3.1/bulk_create",
bytes.NewReader(body),
map[string]interface{}{
"tenantID": int64(1),
},
)
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
c.Set(h.ParsedIdentity, identityHeader)
var user m.User
err = dao.DB.Model(&m.User{}).Where("user_id = ?", testUserId).First(&user).Error
if err.Error() != "record not found" {
t.Error(err)
}
bulkCreate := middleware.UserCatcher(BulkCreate)
err = bulkCreate(c)
if err != nil {
t.Error(err)
}
err = dao.DB.Model(&m.User{}).Where("user_id = ?", testUserId).First(&user).Error
if err != nil {
t.Error(err)
}
if user.UserID != testUserId {
t.Errorf("expected userid is %s instead of %s", testUserId, user.UserID)
}
var source m.Source
err = dao.DB.Model(&m.Source{}).Where("name = ?", nameSource).First(&source).Error
if err != nil {
t.Error(err)
}
if source.UserID == nil || *source.UserID != user.Id {
t.Error("source user id was not populated correctly")
}
var response m.BulkCreateResponse
err = json.Unmarshal(res.Body.Bytes(), &response)
if err != nil {
t.Error(err)
}
var application m.Application
err = dao.DB.Model(&m.Application{}).Where("id = ?", response.Applications[0].ID).First(&application).Error
if err != nil {
t.Error(err)
}
if application.UserID == nil || *application.UserID != user.Id {
t.Error("application user id was not populated correctly")
}
var authentication m.Authentication
err = dao.DB.Model(&m.Authentication{}).Where("id = ?", response.Authentications[0].ID).First(&authentication).Error
if err != nil {
t.Error(err)
}
if authentication.UserID == nil || *authentication.UserID != user.Id {
t.Error("authentication user id was not populated correctly")
}
var applicationAuthentication m.ApplicationAuthentication
err = dao.DB.Model(&m.ApplicationAuthentication{}).
Where("application_id = ? AND authentication_id = ?", response.Applications[0].ID, response.Authentications[0].ID).
Find(&applicationAuthentication).Error
if err != nil {
t.Error(err)
}
if applicationAuthentication.UserID == nil || *applicationAuthentication.UserID != user.Id {
t.Error(err)
}
err = cleanSourceForTenant(nameSource, &fixtures.TestTenantData[0].Id)
if err != nil {
t.Errorf(`unexpected error received when deleting the source: %s`, err)
}
err = dao.DB.Delete(&m.User{}, "user_id = ?", testUserId).Error
if err != nil {
t.Error(err)
}
}
func TestBulkCreate(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
testUserId := "testUser"
identityHeader := testutils.IdentityHeaderForUser(testUserId)
nameSource := "test source"
sourceTypeName := "bitbucket"
applicationTypeName := "app-studio"
authenticationResourceType := "application"
requestBody := testutils.SingleResourceBulkCreateRequest(nameSource, sourceTypeName, applicationTypeName, authenticationResourceType)
body, err := json.Marshal(requestBody)
if err != nil {
t.Error("Could not marshal JSON")
}
c, res := request.CreateTestContext(
http.MethodPost,
"/api/sources/v3.1/bulk_create",
bytes.NewReader(body),
map[string]interface{}{
"tenantID": int64(1),
},
)
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
c.Set(h.ParsedIdentity, identityHeader)
err = BulkCreate(c)
if err != nil {
t.Error(err)
}
var response m.BulkCreateResponse
err = json.Unmarshal(res.Body.Bytes(), &response)
if err != nil {
t.Error(err)
}
source := response.Sources[0]
if *source.Name != nameSource {
t.Errorf("expected source: %v, got %v", nameSource, source.Name)
}
application := response.Applications[0]
if application.SourceID != source.ID {
t.Errorf("expected source id in application: %v, got %v", source.ID, application.SourceID)
}
endpoint := response.Endpoints[0]
if endpoint.SourceID != source.ID {
t.Errorf("expected source id in endpoint: %v, got %v", source.ID, endpoint.SourceID)
}
authentication := response.Authentications[0]
if authentication.ResourceID != application.ID {
t.Errorf("expected resource id in authentication: %v, got %v", application.ID, authentication.ResourceID)
}
if authentication.ResourceType != "Application" {
t.Errorf("expected resource type in authentication: Application, got %v", authentication.ResourceType)
}
err = cleanSourceForTenant(nameSource, &fixtures.TestTenantData[0].Id)
if err != nil {
t.Errorf(`unexpected error received when deleting the source: %s`, err)
}
}
func TestBulkCreateSourceValidationBadRequest(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
tenantId := int64(1)
// Create a source
reqParams := dao.RequestParams{TenantID: &tenantId}
sourceDao := dao.GetSourceDao(&reqParams)
uid := "bd2ba6d6-4630-40e2-b829-cf09b03bdb9f"
nameSource := "Source for TestBulkCreateSourceValidationBadRequest()"
src := m.Source{
Name: nameSource,
SourceTypeID: 1,
Uid: &uid,
}
err := sourceDao.Create(&src)
if err != nil {
t.Errorf("source not created correctly: %s", err)
}
// Try to create same source via bulk create
sourceTypeName := "bitbucket"
applicationTypeName := "app-studio"
authenticationResourceType := "application"
requestBody := testutils.SingleResourceBulkCreateRequest(nameSource, sourceTypeName, applicationTypeName, authenticationResourceType)
body, err := json.Marshal(requestBody)
if err != nil {
t.Error("Could not marshal JSON")
}
c, res := request.CreateTestContext(
http.MethodPost,
"/api/sources/v3.1/bulk_create",
bytes.NewReader(body),
map[string]interface{}{
"tenantID": tenantId,
},
)
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
c.Set(h.ParsedIdentity, &identity.XRHID{Identity: identity.Identity{AccountNumber: fixtures.TestTenantData[0].ExternalTenant}})
badRequestBulkCreate := ErrorHandlingContext(BulkCreate)
err = badRequestBulkCreate(c)
if err != nil {
t.Error(err)
}
templates.BadRequestTest(t, res)
// Delete created source
deletedSource, err := sourceDao.Delete(&src.ID)
if err != nil {
t.Error(err)
}
if deletedSource.ID != src.ID {
t.Error("wrong source deleted")
}
}
func cleanSourceForTenant(sourceName string, tenantID *int64) error {
source := &m.Source{Name: sourceName}
err := dao.DB.Model(&m.Source{}).Where("name = ?", source.Name).Find(&source).Error
if err != nil {
return err
}
err = service.DeleteCascade(tenantID, nil, "Source", source.ID, []kafka.Header{})
// Have to sleep due to the async nature of this
time.Sleep(2 * time.Second)
return err
}