forked from fabric8-services/fabric8-wit
/
workitemtype_blackbox_test.go
370 lines (318 loc) · 13.7 KB
/
workitemtype_blackbox_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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main_test
import (
"bytes"
"fmt"
"net/http"
"os"
"testing"
. "github.com/almighty/almighty-core"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/app/test"
"github.com/almighty/almighty-core/configuration"
"github.com/almighty/almighty-core/gormapplication"
"github.com/almighty/almighty-core/gormsupport"
"github.com/almighty/almighty-core/gormsupport/cleaner"
"github.com/almighty/almighty-core/jsonapi"
"github.com/almighty/almighty-core/migration"
"github.com/almighty/almighty-core/models"
"github.com/almighty/almighty-core/resource"
"github.com/almighty/almighty-core/workitem"
jwt "github.com/dgrijalva/jwt-go"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"golang.org/x/net/context"
)
//-----------------------------------------------------------------------------
// Test Suite setup
//-----------------------------------------------------------------------------
// The WorkItemTypeTestSuite has state the is relevant to all tests.
// It implements these interfaces from the suite package: SetupAllSuite, SetupTestSuite, TearDownAllSuite, TearDownTestSuite
type workItemTypeSuite struct {
gormsupport.DBTestSuite
typeCtrl *WorkitemtypeController
linkTypeCtrl *WorkItemLinkTypeController
linkCatCtrl *WorkItemLinkCategoryController
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestSuiteWorkItemType(t *testing.T) {
resource.Require(t, resource.Database)
suite.Run(t, &workItemTypeSuite{
DBTestSuite: gormsupport.NewDBTestSuite(""),
})
}
// The SetupSuite method will run before the tests in the suite are run.
// It sets up a database connection for all the tests in this suite without polluting global space.
func (s *workItemTypeSuite) SetupSuite() {
s.DBTestSuite.SetupSuite()
// Make sure the database is populated with the correct types (e.g. bug etc.)
if _, c := os.LookupEnv(resource.Database); c != false {
if err := models.Transactional(s.DB, func(tx *gorm.DB) error {
return migration.PopulateCommonTypes(context.Background(), tx, workitem.NewWorkItemTypeRepository(tx))
}); err != nil {
panic(err.Error())
}
}
}
// The SetupTest method will be run before every test in the suite.
func (s *workItemTypeSuite) SetupTest() {
svc := goa.New("workItemTypeSuite-Service")
assert.NotNil(s.T(), svc)
s.typeCtrl = NewWorkitemtypeController(svc, gormapplication.NewGormDB(s.DB))
assert.NotNil(s.T(), s.typeCtrl)
s.linkTypeCtrl = NewWorkItemLinkTypeController(svc, gormapplication.NewGormDB(DB))
require.NotNil(s.T(), s.linkTypeCtrl)
s.linkCatCtrl = NewWorkItemLinkCategoryController(svc, gormapplication.NewGormDB(DB))
require.NotNil(s.T(), s.linkCatCtrl)
}
//-----------------------------------------------------------------------------
// helper method
//-----------------------------------------------------------------------------
// createWorkItemTypeAnimal defines a work item type "animal" that consists of
// two fields ("animal-type" and "color"). The type is mandatory but the color is not.
func (s *workItemTypeSuite) createWorkItemTypeAnimal() (http.ResponseWriter, *app.WorkItemType) {
// Create an enumeration of animal names
typeStrings := []string{"elephant", "blue whale", "Tyrannosaurus rex"}
// Convert string slice to slice of interface{} in O(n) time.
typeEnum := make([]interface{}, len(typeStrings))
for i := range typeStrings {
typeEnum[i] = typeStrings[i]
}
// Create the type for "animal-type" field based on the enum above
stString := "string"
typeFieldDef := app.FieldDefinition{
Required: true,
Type: &app.FieldType{
BaseType: &stString,
Kind: "enum",
Values: typeEnum,
},
}
// Create the type for the "color" field
colorFieldDef := app.FieldDefinition{
Required: false,
Type: &app.FieldType{
Kind: "string",
},
}
// Use the goa generated code to create a work item type
payload := app.CreateWorkItemTypePayload{
Fields: map[string]*app.FieldDefinition{
"animal_type": &typeFieldDef,
"color": &colorFieldDef,
},
Name: "animal",
}
return test.CreateWorkitemtypeCreated(s.T(), nil, nil, s.typeCtrl, &payload)
}
// createWorkItemTypePerson defines a work item type "person" that consists of
// a required "name" field.
func (s *workItemTypeSuite) createWorkItemTypePerson() (http.ResponseWriter, *app.WorkItemType) {
// Create the type for the "color" field
nameFieldDef := app.FieldDefinition{
Required: true,
Type: &app.FieldType{
Kind: "string",
},
}
// Use the goa generated code to create a work item type
payload := app.CreateWorkItemTypePayload{
Fields: map[string]*app.FieldDefinition{
"name": &nameFieldDef,
},
Name: "person",
}
return test.CreateWorkitemtypeCreated(s.T(), nil, nil, s.typeCtrl, &payload)
}
//-----------------------------------------------------------------------------
// Actual tests
//-----------------------------------------------------------------------------
// TestCreateWorkItemType tests if we can create two work item types: "animal" and "person"
func (s *workItemTypeSuite) TestCreateWorkItemType() {
defer cleaner.DeleteCreatedEntities(s.DB)()
_, wit := s.createWorkItemTypeAnimal()
require.NotNil(s.T(), wit)
require.Equal(s.T(), "animal", wit.Name)
_, wit = s.createWorkItemTypePerson()
require.NotNil(s.T(), wit)
require.Equal(s.T(), "person", wit.Name)
}
// TestShowWorkItemType tests if we can fetch the work item type "animal".
func (s *workItemTypeSuite) TestShowWorkItemType() {
defer cleaner.DeleteCreatedEntities(s.DB)()
// Create the work item type first and try to read it back in
_, wit := s.createWorkItemTypeAnimal()
require.NotNil(s.T(), wit)
_, wit2 := test.ShowWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, wit.Name)
require.NotNil(s.T(), wit2)
require.EqualValues(s.T(), wit, wit2)
}
// TestListWorkItemType tests if we can find the work item types
// "person" and "animal" in the list of work item types
func (s *workItemTypeSuite) TestListWorkItemType() {
defer cleaner.DeleteCreatedEntities(s.DB)()
// Create the work item type first and try to read it back in
_, witAnimal := s.createWorkItemTypeAnimal()
require.NotNil(s.T(), witAnimal)
_, witPerson := s.createWorkItemTypePerson()
require.NotNil(s.T(), witPerson)
// Fetch a single work item type
// Paging in the format <start>,<limit>"
page := "0,-1"
_, witCollection := test.ListWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, &page)
require.NotNil(s.T(), witCollection)
require.Nil(s.T(), witCollection.Validate())
// Check the number of found work item types
require.Condition(s.T(), func() bool {
return (len(witCollection) >= 2)
}, "At least two work item types must exist (animal and person), but only %d exist.", len(witCollection))
// Search for the work item types that must exist at minimum
toBeFound := 2
for i := 0; i < len(witCollection) && toBeFound > 0; i++ {
if witCollection[i].Name == "person" || witCollection[i].Name == "animal" {
s.T().Log("Found work item type in collection: ", witCollection[i].Name)
toBeFound--
}
}
require.Exactly(s.T(), 0, toBeFound, "Not all required work item types (animal and person) where found.")
}
// TestListSourceAndTargetLinkTypes tests if we can find the work item link
// types for a given WIT.
func (s *workItemTypeSuite) TestListSourceAndTargetLinkTypes() {
defer cleaner.DeleteCreatedEntities(s.DB)()
// Create the work item type first and try to read it back in
_, witAnimal := s.createWorkItemTypeAnimal()
require.NotNil(s.T(), witAnimal)
_, witPerson := s.createWorkItemTypePerson()
require.NotNil(s.T(), witPerson)
// Create work item link category
linkCatPayload := CreateWorkItemLinkCategory("some-link-category")
_, linkCat := test.CreateWorkItemLinkCategoryCreated(s.T(), nil, nil, s.linkCatCtrl, linkCatPayload)
require.NotNil(s.T(), linkCat)
// Create work item link type
animalLinksToBugStr := "animal-links-to-bug"
linkTypePayload := CreateWorkItemLinkType(animalLinksToBugStr, "animal", workitem.SystemBug, *linkCat.Data.ID)
_, linkType := test.CreateWorkItemLinkTypeCreated(s.T(), nil, nil, s.linkTypeCtrl, linkTypePayload)
require.NotNil(s.T(), linkType)
// Create another work item link type
bugLinksToAnimalStr := "bug-links-to-animal"
linkTypePayload = CreateWorkItemLinkType(bugLinksToAnimalStr, workitem.SystemBug, "animal", *linkCat.Data.ID)
_, linkType = test.CreateWorkItemLinkTypeCreated(s.T(), nil, nil, s.linkTypeCtrl, linkTypePayload)
require.NotNil(s.T(), linkType)
// Fetch source link types
_, wiltCollection := test.ListSourceLinkTypesWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, "animal")
require.NotNil(s.T(), wiltCollection)
assert.Nil(s.T(), wiltCollection.Validate())
// Check the number of found work item link types
require.Len(s.T(), wiltCollection.Data, 1)
require.Equal(s.T(), animalLinksToBugStr, *wiltCollection.Data[0].Attributes.Name)
// Fetch target link types
_, wiltCollection = test.ListTargetLinkTypesWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, "animal")
require.NotNil(s.T(), wiltCollection)
require.Nil(s.T(), wiltCollection.Validate())
// Check the number of found work item link types
require.Len(s.T(), wiltCollection.Data, 1)
require.Equal(s.T(), bugLinksToAnimalStr, *wiltCollection.Data[0].Attributes.Name)
}
// TestListSourceAndTargetLinkTypesEmpty tests that no link type is returned for
// WITs that don't have link types associated to them
func (s *workItemTypeSuite) TestListSourceAndTargetLinkTypesEmpty() {
defer cleaner.DeleteCreatedEntities(s.DB)()
_, witPerson := s.createWorkItemTypePerson()
require.NotNil(s.T(), witPerson)
_, wiltCollection := test.ListSourceLinkTypesWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, "person")
require.NotNil(s.T(), wiltCollection)
require.Nil(s.T(), wiltCollection.Validate())
require.Len(s.T(), wiltCollection.Data, 0)
_, wiltCollection = test.ListTargetLinkTypesWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, "person")
require.NotNil(s.T(), wiltCollection)
require.Nil(s.T(), wiltCollection.Validate())
require.Len(s.T(), wiltCollection.Data, 0)
}
// TestListSourceAndTargetLinkTypesNotFound tests that a NotFound error is
// returned when you query a non existing WIT.
func (s *workItemTypeSuite) TestListSourceAndTargetLinkTypesNotFound() {
defer cleaner.DeleteCreatedEntities(s.DB)()
_, jerrors := test.ListSourceLinkTypesWorkitemtypeNotFound(s.T(), nil, nil, s.typeCtrl, "not-existing-WIT")
require.NotNil(s.T(), jerrors)
_, jerrors = test.ListTargetLinkTypesWorkitemtypeNotFound(s.T(), nil, nil, s.typeCtrl, "not-existing-WIT")
require.NotNil(s.T(), jerrors)
}
func getWorkItemTypeTestData(t *testing.T) []testSecureAPI {
privatekey, err := jwt.ParseRSAPrivateKeyFromPEM((configuration.GetTokenPrivateKey()))
if err != nil {
t.Fatal("Could not parse Key ", err)
}
differentPrivatekey, err := jwt.ParseRSAPrivateKeyFromPEM(([]byte(RSADifferentPrivateKeyTest)))
require.Nil(t, err)
createWITPayloadString := bytes.NewBuffer([]byte(`{"fields": {"system.administrator": {"Required": true,"Type": {"Kind": "string"}}},"name": "Epic"}`))
return []testSecureAPI{
// Create Work Item API with different parameters
{
method: http.MethodPost,
url: endpointWorkItemTypes,
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWITPayloadString,
jwtToken: getExpiredAuthHeader(t, privatekey),
}, {
method: http.MethodPost,
url: endpointWorkItemTypes,
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWITPayloadString,
jwtToken: getMalformedAuthHeader(t, privatekey),
}, {
method: http.MethodPost,
url: endpointWorkItemTypes,
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWITPayloadString,
jwtToken: getValidAuthHeader(t, differentPrivatekey),
}, {
method: http.MethodPost,
url: endpointWorkItemTypes,
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWITPayloadString,
jwtToken: "",
},
// Try fetching a random work Item Type
// We do not have security on GET hence this should return 404 not found
{
method: http.MethodGet,
url: endpointWorkItemTypes + "/someRandomTestWIT8712",
expectedStatusCode: http.StatusNotFound,
expectedErrorCode: jsonapi.ErrorCodeNotFound,
payload: nil,
jwtToken: "",
}, {
method: http.MethodGet,
url: fmt.Sprintf(endpointWorkItemTypesSourceLinkTypes, "someNotExistingWIT"),
expectedStatusCode: http.StatusNotFound,
expectedErrorCode: jsonapi.ErrorCodeNotFound,
payload: nil,
jwtToken: "",
}, {
method: http.MethodGet,
url: fmt.Sprintf(endpointWorkItemTypesTargetLinkTypes, "someNotExistingWIT"),
expectedStatusCode: http.StatusNotFound,
expectedErrorCode: jsonapi.ErrorCodeNotFound,
payload: nil,
jwtToken: "",
},
}
}
// This test case will check authorized access to Create/Update/Delete APIs
func TestUnauthorizeWorkItemTypeCreate(t *testing.T) {
UnauthorizeCreateUpdateDeleteTest(t, getWorkItemTypeTestData, func() *goa.Service {
return goa.New("TestUnauthorizedCreateWIT-Service")
}, func(service *goa.Service) error {
controller := NewWorkitemtypeController(service, gormapplication.NewGormDB(DB))
app.MountWorkitemtypeController(service, controller)
return nil
})
}