This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathworkitem_blackbox_test.go
3394 lines (3122 loc) · 152 KB
/
workitem_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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package controller_test
import (
"bytes"
"context"
"fmt"
"html"
"net/http"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
jwt "github.com/dgrijalva/jwt-go"
"github.com/fabric8-services/fabric8-common/id"
"github.com/fabric8-services/fabric8-wit/account"
"github.com/fabric8-services/fabric8-wit/app"
"github.com/fabric8-services/fabric8-wit/app/test"
"github.com/fabric8-services/fabric8-wit/area"
"github.com/fabric8-services/fabric8-wit/codebase"
"github.com/fabric8-services/fabric8-wit/configuration"
. "github.com/fabric8-services/fabric8-wit/controller"
"github.com/fabric8-services/fabric8-wit/gormtestsupport"
"github.com/fabric8-services/fabric8-wit/iteration"
"github.com/fabric8-services/fabric8-wit/jsonapi"
"github.com/fabric8-services/fabric8-wit/log"
"github.com/fabric8-services/fabric8-wit/ptr"
"github.com/fabric8-services/fabric8-wit/rendering"
"github.com/fabric8-services/fabric8-wit/resource"
"github.com/fabric8-services/fabric8-wit/rest"
"github.com/fabric8-services/fabric8-wit/search"
"github.com/fabric8-services/fabric8-wit/space"
"github.com/fabric8-services/fabric8-wit/spacetemplate"
testsupport "github.com/fabric8-services/fabric8-wit/test"
notificationsupport "github.com/fabric8-services/fabric8-wit/test/notification"
tf "github.com/fabric8-services/fabric8-wit/test/testfixture"
"github.com/fabric8-services/fabric8-wit/test/token"
"github.com/fabric8-services/fabric8-wit/workitem"
errs "github.com/pkg/errors"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
const none = "none"
func TestSuiteWorkItem1(t *testing.T) {
resource.Require(t, resource.Database)
suite.Run(t, new(WorkItemSuite))
}
type WorkItemSuite struct {
gormtestsupport.DBTestSuite
workitemCtrl app.WorkitemController
workitemsCtrl app.WorkitemsController
spaceCtrl app.SpaceController
svc *goa.Service
wi *app.WorkItem
minimumPayload *app.UpdateWorkitemPayload
testIdentity account.Identity
repoWit workitem.WorkItemRepository
}
func (s *WorkItemSuite) SetupSuite() {
s.DBTestSuite.SetupSuite()
s.repoWit = workitem.NewWorkItemRepository(s.DB)
}
func (s *WorkItemSuite) SetupTest() {
s.DBTestSuite.SetupTest()
// create a test identity
testIdentity, err := testsupport.CreateTestIdentity(s.DB, "WorkItemSuite setup user", "test provider")
require.NoError(s.T(), err)
s.testIdentity = *testIdentity
s.svc = testsupport.ServiceAsUser("TestUpdateWI-Service", s.testIdentity)
s.workitemCtrl = NewWorkitemController(s.svc, s.GormDB, s.Configuration)
s.workitemsCtrl = NewWorkitemsController(s.svc, s.GormDB, s.Configuration)
s.spaceCtrl = NewSpaceController(s.svc, s.GormDB, s.Configuration, &DummyResourceManager{})
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
log.Info(nil, nil, "Creating work item during test setup...")
_, wi := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
log.Info(nil, nil, "Creating work item during test setup: done")
s.wi = wi.Data
s.minimumPayload = getMinimumRequiredUpdatePayload(s.wi)
}
func (s *WorkItemSuite) TestPagingLinks() {
workitemsCtrl := NewWorkitemsController(s.svc, s.GormDB, s.Configuration)
// fxt.Spaces[0] has ONE workitem and fxt.Spaces[1] has TEN workitems and fxt.Spaces[2] has ZERO workitems
fxt := tf.NewTestFixture(s.T(), s.DB, tf.Spaces(3), tf.WorkItems(11, func(fxt *tf.TestFixture, idx int) error {
if idx == 0 {
fxt.WorkItems[idx].SpaceID = fxt.Spaces[0].ID
} else {
fxt.WorkItems[idx].SpaceID = fxt.Spaces[1].ID
}
fxt.WorkItems[idx].Type = fxt.WorkItemTypes[0].ID
return nil
}))
// With only ONE work item
pagingTest := createPagingTest(s.T(), s.svc.Context, workitemsCtrl, &s.repoWit, fxt.Spaces[0].ID, 1)
pagingTest(2, 5, "page[offset]=0&page[limit]=2", "page[offset]=0&page[limit]=2", "page[offset]=0&page[limit]=2", "")
pagingTest = createPagingTest(s.T(), s.svc.Context, workitemsCtrl, &s.repoWit, fxt.Spaces[1].ID, 10)
pagingTest(2, 5, "page[offset]=0&page[limit]=2", "page[offset]=7&page[limit]=5", "page[offset]=0&page[limit]=2", "page[offset]=7&page[limit]=5")
pagingTest(1, 10, "page[offset]=0&page[limit]=1", "page[offset]=1&page[limit]=10", "page[offset]=0&page[limit]=1", "")
pagingTest(0, 4, "page[offset]=0&page[limit]=4", "page[offset]=8&page[limit]=4", "", "page[offset]=4&page[limit]=4")
// With only ZERO work items
pagingTest = createPagingTest(s.T(), s.svc.Context, workitemsCtrl, &s.repoWit, fxt.Spaces[2].ID, 0)
pagingTest(10, 2, "page[offset]=0&page[limit]=0", "page[offset]=0&page[limit]=0", "", "")
pagingTest(0, 2, "page[offset]=0&page[limit]=2", "page[offset]=0&page[limit]=0", "", "")
}
func (s *WorkItemSuite) TestPagingErrors() {
var offset string = "-1"
var limit int = 2
_, result := test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
if !strings.Contains(*result.Links.First, "page[offset]=0") {
assert.Fail(s.T(), "Offset is negative", "Expected offset to be %d, but was %s", 0, *result.Links.First)
}
offset = "0"
limit = 0
_, result = test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
if !strings.Contains(*result.Links.First, "page[limit]=20") {
assert.Fail(s.T(), "Limit is 0", "Expected limit to be default size %d, but was %s", 20, *result.Links.First)
}
offset = "0"
limit = -1
_, result = test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
if !strings.Contains(*result.Links.First, "page[limit]=20") {
assert.Fail(s.T(), "Limit is negative", "Expected limit to be default size %d, but was %s", 20, *result.Links.First)
}
offset = "-3"
limit = -1
_, result = test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
if !strings.Contains(*result.Links.First, "page[limit]=20") {
assert.Fail(s.T(), "Limit is negative", "Expected limit to be default size %d, but was %s", 20, *result.Links.First)
}
if !strings.Contains(*result.Links.First, "page[offset]=0") {
assert.Fail(s.T(), "Offset is negative", "Expected offset to be %d, but was %s", 0, *result.Links.First)
}
offset = "ALPHA"
limit = 40
_, result = test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
if !strings.Contains(*result.Links.First, "page[limit]=40") {
assert.Fail(s.T(), "Limit is within range", "Expected limit to be size %d, but was %s", 40, *result.Links.First)
}
if !strings.Contains(*result.Links.First, "page[offset]=0") {
assert.Fail(s.T(), "Offset is negative", "Expected offset to be %d, but was %s", 0, *result.Links.First)
}
}
func (s *WorkItemSuite) TestPagingLinksHasAbsoluteURL() {
// given
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment(), tf.WorkItems(1))
offset := "10"
limit := 10
// when
_, result := test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, fxt.Spaces[0].ID, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
// then
if !strings.HasPrefix(*result.Links.First, "http://") {
assert.Fail(s.T(), "Not Absolute URL", "Expected link %s to contain absolute URL but was %s", "First", *result.Links.First)
}
if !strings.HasPrefix(*result.Links.Last, "http://") {
assert.Fail(s.T(), "Not Absolute URL", "Expected link %s to contain absolute URL but was %s", "Last", *result.Links.Last)
}
if !strings.HasPrefix(*result.Links.Prev, "http://") {
assert.Fail(s.T(), "Not Absolute URL", "Expected link %s to contain absolute URL but was %s", "Prev", *result.Links.Prev)
}
}
func (s *WorkItemSuite) TestPagingDefaultAndMaxSize() {
// given
offset := "0"
var limit int
// when
_, result := test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, nil, &offset, nil, nil, nil)
// then
if !strings.Contains(*result.Links.First, "page[limit]=20") {
assert.Fail(s.T(), "Limit is nil", "Expected limit to be default size %d, got %v", 20, *result.Links.First)
}
// when
limit = 1000
_, result = test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
// then
if !strings.Contains(*result.Links.First, fmt.Sprintf("page[limit]=%d", PageSizeMax)) {
assert.Fail(s.T(), "Limit is more than max", "Expected limit to be %d, got %v", PageSizeMax, *result.Links.First)
}
// when
limit = 50
_, result = test.ListWorkitemsOK(s.T(), context.Background(), nil, s.workitemsCtrl, space.SystemSpace, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
// then
if !strings.Contains(*result.Links.First, "page[limit]=50") {
assert.Fail(s.T(), "Limit is within range", "Expected limit to be %d, got %v", 50, *result.Links.First)
}
}
func (s *WorkItemSuite) TestGetWorkItemWithLegacyDescription() {
// given
_, wi := test.ShowWorkitemOK(s.T(), nil, nil, s.workitemCtrl, *s.wi.ID, nil, nil)
require.NotNil(s.T(), wi)
assert.Equal(s.T(), s.wi.ID, wi.Data.ID)
assert.NotNil(s.T(), wi.Data.Attributes[workitem.SystemCreatedAt])
assert.Equal(s.T(), s.testIdentity.ID.String(), *wi.Data.Relationships.Creator.Data.ID)
// when
wi.Data.Attributes[workitem.SystemTitle] = "Updated Test WI"
updatedDescription := "= Updated Test WI description"
wi.Data.Attributes[workitem.SystemDescription] = updatedDescription
payload2 := minimumRequiredUpdatePayload()
payload2.Data.ID = wi.Data.ID
payload2.Data.Attributes = wi.Data.Attributes
_, updated := test.UpdateWorkitemOK(s.T(), s.svc.Context, s.svc, s.workitemCtrl, *wi.Data.ID, &payload2)
// then
assert.NotNil(s.T(), updated.Data.Attributes[workitem.SystemCreatedAt])
assert.Equal(s.T(), (s.wi.Attributes["version"].(int) + 1), updated.Data.Attributes["version"])
assert.Equal(s.T(), *s.wi.ID, *updated.Data.ID)
assert.Equal(s.T(), wi.Data.Attributes[workitem.SystemTitle], updated.Data.Attributes[workitem.SystemTitle])
assert.Equal(s.T(), updatedDescription, updated.Data.Attributes[workitem.SystemDescription])
}
func (s *WorkItemSuite) TestCreateWI() {
s.T().Run("ok", func(t *testing.T) {
// given
fxt := tf.NewTestFixture(t, s.DB, tf.CreateWorkItemEnvironment())
svc := testsupport.ServiceAsUser("TestUpdateWI-Service", *fxt.Identities[0])
workitemsCtrl := NewWorkitemsController(svc, s.GormDB, s.Configuration)
// given
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
// when
_, created := test.CreateWorkitemsCreated(t, svc.Context, svc, workitemsCtrl, fxt.Spaces[0].ID, &payload)
// then
require.NotNil(t, created.Data.ID)
assert.NotEmpty(t, *created.Data.ID)
require.NotNil(t, created.Data.Attributes[workitem.SystemCreatedAt])
require.NotNil(t, created.Data.Relationships.Creator.Data)
assert.Equal(t, *created.Data.Relationships.Creator.Data.ID, fxt.Identities[0].ID.String())
require.NotNil(t, created.Data.Relationships.Iteration)
assert.Equal(t, fxt.Iterations[0].ID.String(), *created.Data.Relationships.Iteration.Data.ID)
require.NotNil(t, created.Data.Relationships.Area)
assert.Equal(t, fxt.Areas[0].ID.String(), *created.Data.Relationships.Area.Data.ID)
})
s.T().Run("field types", func(t *testing.T) {
vals := workitem.GetFieldTypeTestData(t)
kinds := vals.GetKinds()
fieldName := "fieldundertest"
// Create a work item type for each kind
fxt := tf.NewTestFixture(t, s.DB,
tf.CreateWorkItemEnvironment(),
tf.WorkItemTypes(len(kinds), func(fxt *tf.TestFixture, idx int) error {
fxt.WorkItemTypes[idx].Name = kinds[idx].String()
// Add one field that is used for testing
fxt.WorkItemTypes[idx].Fields[fieldName] = workitem.FieldDefinition{
Required: true,
Label: kinds[idx].String(),
Description: fmt.Sprintf("This field is used for testing values for the field kind '%s'", kinds[idx]),
Type: workitem.SimpleType{
Kind: kinds[idx],
},
}
return nil
}),
)
svc := testsupport.ServiceAsUser("TestUpdateWI-Service", *fxt.Identities[0])
workitemsCtrl := NewWorkitemsController(svc, s.GormDB, s.Configuration)
workitemCtrl := NewWorkitemController(svc, s.GormDB, s.Configuration)
_ = workitemCtrl
// when
for kind, iv := range vals {
witID := fxt.WorkItemTypeByName(kind.String()).ID
t.Run(kind.String(), func(t *testing.T) {
// Handle cases where the conversion is supposed to work
t.Run("legal", func(t *testing.T) {
for _, expected := range iv.Valid {
t.Run(spew.Sdump(expected), func(t *testing.T) {
payload := minimumRequiredCreateWithTypeAndSpace(witID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = testsupport.CreateRandomValidTestName("Test WI")
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
payload.Data.Attributes[fieldName] = expected
_, created := test.CreateWorkitemsCreated(t, s.svc.Context, svc, workitemsCtrl, fxt.Spaces[0].ID, &payload)
_ = created
require.NotNil(t, created)
require.NotNil(t, created.Data)
require.NotNil(t, created.Data.ID)
_, loadedWi := test.ShowWorkitemOK(t, s.svc.Context, svc, workitemCtrl, *created.Data.ID, nil, nil)
require.NotNil(t, loadedWi)
// // compensate for errors when interpreting ambigous actual values
actual := loadedWi.Data.Attributes[fieldName]
if iv.Compensate != nil {
actual = iv.Compensate(actual)
}
require.Equal(t, expected, actual, "expected no error when loading and comparing the workitem with a '%s': %#v", kind, spew.Sdump(expected))
})
}
})
t.Run("illegal", func(t *testing.T) {
// Handle cases where the conversion is supposed to NOT work
for _, expected := range iv.Invalid {
t.Run(spew.Sdump(expected), func(t *testing.T) {
payload := minimumRequiredCreateWithTypeAndSpace(witID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = testsupport.CreateRandomValidTestName("Test WI")
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
payload.Data.Attributes[fieldName] = expected
_, jerrs := test.CreateWorkitemsBadRequest(t, s.svc.Context, svc, workitemsCtrl, fxt.Spaces[0].ID, &payload)
require.NotNil(t, jerrs, "expected an error when assigning this value to a '%s' field during work item creation: %#v", kind, spew.Sdump(expected))
})
}
})
})
}
})
}
// TestReorderAbove is positive test which tests successful reorder by providing valid input
// This case reorders one workitem -> result3 and places it **above** result2
func (s *WorkItemSuite) TestReorderWorkitemAboveOK() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
// This workitem is created but not used to clearly test that the reorder workitem is moved between **two** workitems i.e. result1 and result2 and not to the **top** of the list
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result2 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result3 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
var dataArray []*app.WorkItem // dataArray contains the workitem(s) that have to be reordered
dataArray = append(dataArray, result3.Data)
payload2.Data = dataArray
payload2.Position.ID = result2.Data.ID // Position.ID specifies the workitem ID above or below which the workitem(s) should be placed
payload2.Position.Direction = string(workitem.DirectionAbove)
_, reordered1 := test.ReorderWorkitemsOK(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2) // Returns the workitems which are reordered
require.Len(s.T(), reordered1.Data, 1) // checks the correct number of workitems reordered
assert.Equal(s.T(), result3.Data.Attributes["version"].(int)+1, reordered1.Data[0].Attributes["version"])
assert.Equal(s.T(), *result3.Data.ID, *reordered1.Data[0].ID)
}
// TestReorder is in error because of version conflict
func (s *WorkItemSuite) TestReorderWorkitemConflict() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
// This workitem is created but not used to clearly test that the reorder workitem is moved between **two** workitems i.e. result1 and result2 and not to the **top** of the list
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result2 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result3 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
var dataArray []*app.WorkItem // dataArray contains the workitem(s) that have to be reordered
result3.Data.Attributes["version"] = 101
dataArray = append(dataArray, result3.Data)
payload2.Data = dataArray
payload2.Position.ID = result2.Data.ID // Position.ID specifies the workitem ID above or below which the workitem(s) should be placed
payload2.Position.Direction = string(workitem.DirectionAbove)
_, jerrs := test.ReorderWorkitemsConflict(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2) // Returns the workitems which are reordered
require.NotNil(s.T(), jerrs)
}
// TestReorderBelow is positive test which tests successful reorder by providing valid input
// This case reorders one workitem -> result1 and places it **below** result1
func (s *WorkItemSuite) TestReorderWorkitemBelowOK() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
_, result1 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result2 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
// This workitem is created but not used to clearly demonstrate that the reorder workitem is moved between **two** workitems i.e. result2 and result3 and not to the **bottom** of the list
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
var dataArray []*app.WorkItem // dataArray contains the workitem(s) that have to be reordered
dataArray = append(dataArray, result1.Data)
payload2.Data = dataArray
payload2.Position.ID = result2.Data.ID // Position.ID specifies the workitem ID above or below which the workitem(s) should be placed
payload2.Position.Direction = string(workitem.DirectionBelow)
_, reordered1 := test.ReorderWorkitemsOK(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2) // Returns the workitems which are reordered
require.Len(s.T(), reordered1.Data, 1) // checks the correct number of workitems reordered
assert.Equal(s.T(), result1.Data.Attributes["version"].(int)+1, reordered1.Data[0].Attributes["version"])
assert.Equal(s.T(), *result1.Data.ID, *reordered1.Data[0].ID)
}
// TestReorderTop is positive test which tests successful reorder by providing valid input
// This case reorders one workitem -> result2 and places it to the top of the list
func (s *WorkItemSuite) TestReorderWorkitemTopOK() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
// There are two workitems in the list -> result1 and result2
// In this case, we reorder result2 to the top of the list i.e. above result1
_, result1 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
var dataArray []*app.WorkItem // dataArray contains the workitem(s) that have to be reordered
dataArray = append(dataArray, result1.Data)
payload2.Data = dataArray
payload2.Position.Direction = string(workitem.DirectionTop)
_, reordered1 := test.ReorderWorkitemsOK(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2) // Returns the workitems which are reordered
require.Len(s.T(), reordered1.Data, 1) // checks the correct number of workitems reordered
assert.Equal(s.T(), result1.Data.Attributes["version"].(int)+1, reordered1.Data[0].Attributes["version"])
assert.Equal(s.T(), *result1.Data.ID, *reordered1.Data[0].ID)
}
// TestReorderBottom is positive test which tests successful reorder by providing valid input
// This case reorders one workitem -> result1 and places it to the bottom of the list
func (s *WorkItemSuite) TestReorderWorkitemBottomOK() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
// There are two workitems in the list -> result1 and result2
// In this case, we reorder result1 to the bottom of the list i.e. below result2
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result2 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
var dataArray []*app.WorkItem // dataArray contains the workitem(s) that have to be reordered
dataArray = append(dataArray, result2.Data)
payload2.Data = dataArray
payload2.Position.Direction = string(workitem.DirectionBottom)
_, reordered1 := test.ReorderWorkitemsOK(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2) // Returns the workitems which are reordered
require.Len(s.T(), reordered1.Data, 1) // checks the correct number of workitems reordered
assert.Equal(s.T(), result2.Data.Attributes["version"].(int)+1, reordered1.Data[0].Attributes["version"])
assert.Equal(s.T(), *result2.Data.ID, *reordered1.Data[0].ID)
}
// TestReorderMultipleWorkitem is positive test which tests successful reorder by providing valid input
// This case reorders two workitems -> result3 and result4 and places them above result2
func (s *WorkItemSuite) TestReorderMultipleWorkitems() {
// given
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result2 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result3 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
_, result4 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
var dataArray []*app.WorkItem // dataArray contains the workitems that have to be reordered
dataArray = append(dataArray, result3.Data, result4.Data)
payload2.Data = dataArray
payload2.Position.ID = result2.Data.ID // Position.ID specifies the workitem ID above or below which the workitem(s) should be placed
payload2.Position.Direction = string(workitem.DirectionAbove)
// when
_, reordered1 := test.ReorderWorkitemsOK(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2) // Returns the workitems which are reordered
// then
require.NotNil(s.T(), reordered1)
require.NotNil(s.T(), reordered1.Data)
require.Len(s.T(), reordered1.Data, 2) // checks the correct number of workitems reordered
assert.Equal(s.T(), result3.Data.Attributes["version"].(int)+1, reordered1.Data[0].Attributes["version"])
assert.Equal(s.T(), result4.Data.Attributes["version"].(int)+1, reordered1.Data[1].Attributes["version"])
assert.Equal(s.T(), *result3.Data.ID, *reordered1.Data[0].ID)
assert.Equal(s.T(), *result4.Data.ID, *reordered1.Data[1].ID)
}
// TestReorderWorkitemBadRequest is negative test which tests unsuccessful reorder by providing invalid input
func (s *WorkItemSuite) TestReorderWorkitemBadRequestOK() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
_, result1 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
// This case gives empty dataArray as input
// Response is Bad Parameter
// Reorder is unsuccessful
var dataArray []*app.WorkItem
payload2.Data = dataArray
payload2.Position.ID = result1.Data.ID
payload2.Position.Direction = string(workitem.DirectionAbove)
test.ReorderWorkitemsBadRequest(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2)
}
// TestReorderWorkitemNotFound is negative test which tests unsuccessful reorder by providing invalid input
func (s *WorkItemSuite) TestReorderWorkitemNotFoundOK() {
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Reorder Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
_, result1 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
payload2 := minimumRequiredReorderPayload()
// This case gives id of workitem in position.ID which is not present in db as input
// Response is Not Found
// Reorder is unsuccessful
var dataArray []*app.WorkItem
dataArray = append(dataArray, result1.Data)
payload2.Data = dataArray
randomID := uuid.NewV4()
payload2.Position.ID = &randomID
payload2.Position.Direction = string(workitem.DirectionAbove)
test.ReorderWorkitemsNotFound(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, fxt.Spaces[0].ID, &payload2)
}
// TestUpdateWorkitemWithoutReorder tests that when workitem is updated, execution order of workitem doesnot change.
func (s *WorkItemSuite) TestUpdateWorkitemWithoutReorder() {
// Create new workitem
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
_, wi := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
// Update the workitem
wi.Data.Attributes[workitem.SystemTitle] = "Updated Test WI"
payload2 := minimumRequiredUpdatePayload()
payload2.Data.ID = wi.Data.ID
payload2.Data.Attributes = wi.Data.Attributes
_, updated := test.UpdateWorkitemOK(s.T(), s.svc.Context, s.svc, s.workitemCtrl, *wi.Data.ID, &payload2)
assert.Equal(s.T(), *wi.Data.ID, *updated.Data.ID)
assert.Equal(s.T(), (s.wi.Attributes["version"].(int) + 1), updated.Data.Attributes["version"])
assert.Equal(s.T(), wi.Data.Attributes[workitem.SystemTitle], updated.Data.Attributes[workitem.SystemTitle])
// Check the execution order
assert.Equal(s.T(), wi.Data.Attributes[workitem.SystemOrder], updated.Data.Attributes[workitem.SystemOrder])
}
func (s *WorkItemSuite) TestCreateWorkItemWithoutContext() {
// given
s.svc = goa.New("TestCreateWorkItemWithoutContext-Service")
payload := minimumRequiredCreateWithType(workitem.SystemBug)
payload.Data.Attributes[workitem.SystemTitle] = "Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
// when/then
test.CreateWorkitemsUnauthorized(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
}
func (s *WorkItemSuite) TestListByFields() {
// given
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment())
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "run integration test"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed
test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
// when
filter := "{\"system.title\":\"run integration test\"}"
offset := "0"
limit := 1
_, result := test.ListWorkitemsOK(s.T(), nil, nil, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &filter, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
// then
require.NotNil(s.T(), result)
require.Equal(s.T(), 1, len(result.Data))
// when
filter = fmt.Sprintf("{\"system.creator\":%q}", s.testIdentity.ID.String())
// then
_, result = test.ListWorkitemsOK(s.T(), nil, nil, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &filter, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
require.NotNil(s.T(), result)
require.Equal(s.T(), 1, len(result.Data))
}
func getWorkItemTestDataFunc(config configuration.Registry) func(t *testing.T) []testSecureAPI {
return func(t *testing.T) []testSecureAPI {
privatekey := token.PrivateKey()
differentPrivatekey, err := jwt.ParseRSAPrivateKeyFromPEM(([]byte(RSADifferentPrivateKeyTest)))
if err != nil {
t.Fatal("Could not parse different private key ", err)
}
createWIPayloadString := bytes.NewBuffer([]byte(`
{
"data": {
"type": "workitems"
"attributes": {
"system.state": "new",
"system.title": "My special story",
"system.description": "description"
}
}
}`))
return []testSecureAPI{
// Create Work Item API with different parameters
{
method: http.MethodPost,
url: fmt.Sprintf(endpointWorkItems, "5b5faa94-7478-4a35-9fdd-e1b5278df331"),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: getExpiredAuthHeader(t, privatekey),
}, {
method: http.MethodPost,
url: fmt.Sprintf(endpointWorkItems, "5b5faa94-7478-4a35-9fdd-e1b5278df331"),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: getMalformedAuthHeader(t, privatekey),
}, {
method: http.MethodPost,
url: fmt.Sprintf(endpointWorkItems, "5b5faa94-7478-4a35-9fdd-e1b5278df331"),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: getValidAuthHeader(t, differentPrivatekey),
}, {
method: http.MethodPost,
url: fmt.Sprintf(endpointWorkItems, "5b5faa94-7478-4a35-9fdd-e1b5278df331"),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: "",
},
// Update Work Item API with different parameters
{
method: http.MethodPatch,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: getExpiredAuthHeader(t, privatekey),
}, {
method: http.MethodPatch,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: getMalformedAuthHeader(t, privatekey),
}, {
method: http.MethodPatch,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: getValidAuthHeader(t, differentPrivatekey),
}, {
method: http.MethodPatch,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: createWIPayloadString,
jwtToken: "",
},
// Delete Work Item API with different parameters
{
method: http.MethodDelete,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: nil,
jwtToken: getExpiredAuthHeader(t, privatekey),
}, {
method: http.MethodDelete,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: nil,
jwtToken: getMalformedAuthHeader(t, privatekey),
}, {
method: http.MethodDelete,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: nil,
jwtToken: getValidAuthHeader(t, differentPrivatekey),
}, {
method: http.MethodDelete,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusUnauthorized,
expectedErrorCode: jsonapi.ErrorCodeJWTSecurityError,
payload: nil,
jwtToken: "",
},
// Try fetching a random work Item
// We do not have security on GET hence this should return 404 not found
{
method: http.MethodGet,
url: endpointWorkItem + "/" + uuid.NewV4().String(),
expectedStatusCode: http.StatusNotFound,
expectedErrorCode: jsonapi.ErrorCodeNotFound,
payload: nil,
jwtToken: "",
},
}
}
}
// This test case will check authorized access to Create/Update/Delete APIs
func (s *WorkItemSuite) TestUnauthorizeWorkItemCUD() {
UnauthorizeCreateUpdateDeleteTest(s.T(), getWorkItemTestDataFunc(*s.Configuration), func() *goa.Service {
return goa.New("TestUnauthorizedCreateWI-Service")
}, func(service *goa.Service) error {
workitemCtrl := NewWorkitemController(service, s.GormDB, s.Configuration)
app.MountWorkitemController(service, workitemCtrl)
workitemsCtrl := NewWorkitemsController(service, s.GormDB, s.Configuration)
app.MountWorkitemsController(service, workitemsCtrl)
return nil
})
}
func createPagingTest(t *testing.T, ctx context.Context, controller *WorkitemsController, repo *workitem.WorkItemRepository, spaceID uuid.UUID, totalCount int) func(start int, limit int, first string, last string, prev string, next string) {
return func(start int, limit int, first string, last string, prev string, next string) {
offset := strconv.Itoa(start)
_, response := test.ListWorkitemsOK(t, ctx, nil, controller, spaceID, nil, nil, nil, nil, nil, nil, nil, nil, &limit, &offset, nil, nil, nil)
assertLink(t, "first", first, response.Links.First)
assertLink(t, "last", last, response.Links.Last)
assertLink(t, "prev", prev, response.Links.Prev)
assertLink(t, "next", next, response.Links.Next)
assert.Equal(t, totalCount, response.Meta.TotalCount)
}
}
func assertLink(t *testing.T, l string, expected string, actual *string) {
if expected == "" {
if actual != nil {
assert.Fail(t, fmt.Sprintf("link %s should be nil but is %s", l, *actual))
}
} else {
if actual == nil {
assert.Fail(t, "link %s should be %s, but is nil", l, expected)
} else {
assert.True(t, strings.HasSuffix(*actual, expected), "link %s should be %s, but is %s", l, expected, *actual)
}
}
}
// ========== helper functions for tests inside WorkItem2Suite ==========
func getMinimumRequiredUpdatePayload(wi *app.WorkItem) *app.UpdateWorkitemPayload {
return &app.UpdateWorkitemPayload{
Data: &app.WorkItem{
Type: APIStringTypeWorkItem,
ID: wi.ID,
Attributes: map[string]interface{}{
"version": wi.Attributes["version"],
},
Relationships: wi.Relationships,
},
}
}
func minimumRequiredUpdatePayload() app.UpdateWorkitemPayload {
return minimumRequiredUpdatePayloadWithSpace(space.SystemSpace)
}
func minimumRequiredUpdatePayloadWithSpace(spaceID uuid.UUID) app.UpdateWorkitemPayload {
spaceSelfURL := rest.AbsoluteURL(&http.Request{Host: "api.service.domain.org"}, app.SpaceHref(spaceID.String()))
return app.UpdateWorkitemPayload{
Data: &app.WorkItem{
Type: APIStringTypeWorkItem,
Attributes: map[string]interface{}{},
Relationships: &app.WorkItemRelationships{
Space: app.NewSpaceRelation(spaceID, spaceSelfURL),
},
},
}
}
func minimumRequiredReorderPayload() app.ReorderWorkitemsPayload {
return app.ReorderWorkitemsPayload{
Data: []*app.WorkItem{},
Position: &app.WorkItemReorderPosition{
ID: nil,
},
}
}
func minimumRequiredCreateWithType(witID uuid.UUID) app.CreateWorkitemsPayload {
c := minimumRequiredCreatePayload()
c.Data.Relationships.BaseType = newRelationBaseType(witID)
return c
}
func minimumRequiredCreateWithTypeAndSpace(witID uuid.UUID, spaceID uuid.UUID) app.CreateWorkitemsPayload {
c := minimumRequiredCreatePayload(spaceID)
c.Data.Relationships.BaseType = newRelationBaseType(witID)
return c
}
func newRelationBaseType(wit uuid.UUID) *app.RelationBaseType {
witRelatedURL := rest.AbsoluteURL(&http.Request{Host: "api.service.domain.org"}, app.WorkitemtypeHref(wit.String()))
return &app.RelationBaseType{
Data: &app.BaseTypeData{
Type: "workitemtypes",
ID: wit,
},
Links: &app.GenericLinks{
Self: &witRelatedURL,
Related: &witRelatedURL,
},
}
}
func minimumRequiredCreatePayload(spaceIDOptional ...uuid.UUID) app.CreateWorkitemsPayload {
spaceID := space.SystemSpace
if len(spaceIDOptional) == 1 {
spaceID = spaceIDOptional[0]
}
spaceSelfURL := rest.AbsoluteURL(&http.Request{Host: "api.service.domain.org"}, app.SpaceHref(spaceID.String()))
return app.CreateWorkitemsPayload{
Data: &app.WorkItem{
Type: APIStringTypeWorkItem,
Attributes: map[string]interface{}{},
Relationships: &app.WorkItemRelationships{
Space: app.NewSpaceRelation(spaceID, spaceSelfURL),
},
},
}
}
func newChildIteration(ctx context.Context, db *gorm.DB, parentIteration *iteration.Iteration) *iteration.Iteration {
iterationRepo := iteration.NewIterationRepository(db)
itr := iteration.Iteration{
ID: uuid.NewV4(),
Name: "Sprint 101",
SpaceID: parentIteration.SpaceID,
}
itr.MakeChildOf(*parentIteration)
err := iterationRepo.Create(ctx, &itr)
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
}, "failed to create iteration")
return nil
}
return &itr
}
// ========== WorkItem2Suite struct that implements SetupSuite, TearDownSuite, SetupTest, TearDownTest ==========
// a normal test function that will kick off WorkItem2Suite
func TestSuiteWorkItem2(t *testing.T) {
resource.Require(t, resource.Database)
suite.Run(t, &WorkItem2Suite{DBTestSuite: gormtestsupport.NewDBTestSuite()})
}
func ident(id uuid.UUID) *app.GenericData {
ut := APIStringTypeUser
i := id.String()
return &app.GenericData{
Type: &ut,
ID: &i,
}
}
type WorkItem2Suite struct {
gormtestsupport.DBTestSuite
workitemCtrl app.WorkitemController
workitemsCtrl app.WorkitemsController
witCtrl app.WorkitemtypeController
linkCtrl app.WorkItemLinkController
linkTypeCtrl app.WorkItemLinkTypeController
spaceCtrl app.SpaceController
svc *goa.Service
wi *app.WorkItem
minimumPayload *app.UpdateWorkitemPayload
notification notificationsupport.FakeNotificationChannel
testDir string
}
func (s *WorkItem2Suite) SetupTest() {
s.DBTestSuite.SetupTest()
s.notification = notificationsupport.FakeNotificationChannel{}
// create identity
testIdentity, err := testsupport.CreateTestIdentity(s.DB, "WorkItem2Suite setup user", "test provider")
require.NoError(s.T(), err)
s.svc = testsupport.ServiceAsUser("TestUpdateWI2-Service", *testIdentity)
s.workitemCtrl = NewNotifyingWorkitemController(s.svc, s.GormDB, &s.notification, s.Configuration)
s.workitemsCtrl = NewNotifyingWorkitemsController(s.svc, s.GormDB, &s.notification, s.Configuration)
s.linkTypeCtrl = NewWorkItemLinkTypeController(s.svc, s.GormDB, s.Configuration)
s.linkCtrl = NewWorkItemLinkController(s.svc, s.GormDB, s.Configuration)
s.spaceCtrl = NewSpaceController(s.svc, s.GormDB, s.Configuration, &DummyResourceManager{})
s.witCtrl = NewWorkitemtypeController(s.svc, s.GormDB, s.Configuration)
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment(), tf.Spaces(1), tf.WorkItemTypes(1))
//payload := minimumRequiredCreateWithType(workitem.SystemBug)
payload := minimumRequiredCreateWithTypeAndSpace(fxt.WorkItemTypes[0].ID, fxt.Spaces[0].ID)
payload.Data.Attributes[workitem.SystemTitle] = "Test WI"
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
_, wi := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload)
s.wi = wi.Data
s.minimumPayload = getMinimumRequiredUpdatePayload(s.wi)
//s.minimumReorderPayload = getMinimumRequiredReorderPayload(s.wi)
s.testDir = filepath.Join("test-files", "work_item")
}
// ========== Actual Test functions ==========
func (s *WorkItem2Suite) TestWI2UpdateOnlyState() {
s.minimumPayload.Data.Attributes[workitem.SystemTitle] = "Test title"
s.minimumPayload.Data.Attributes["system.state"] = "invalid_value"
test.UpdateWorkitemBadRequest(s.T(), s.svc.Context, s.svc, s.workitemCtrl, *s.wi.ID, s.minimumPayload)
newStateValue := "closed"
s.minimumPayload.Data.Attributes[workitem.SystemState] = newStateValue
_, updatedWI := test.UpdateWorkitemOK(s.T(), s.svc.Context, s.svc, s.workitemCtrl, *s.wi.ID, s.minimumPayload)
require.NotNil(s.T(), updatedWI)
assert.Equal(s.T(), updatedWI.Data.Attributes[workitem.SystemState], newStateValue)
}
func (s *WorkItem2Suite) TestWI2UpdateVersionConflict() {
// given
s.minimumPayload.Data.Attributes[workitem.SystemTitle] = "Test title"
test.UpdateWorkitemOK(s.T(), s.svc.Context, s.svc, s.workitemCtrl, *s.wi.ID, s.minimumPayload)
s.minimumPayload.Data.Attributes["version"] = 2398475203
// when/then
test.UpdateWorkitemConflict(s.T(), s.svc.Context, s.svc, s.workitemCtrl, *s.wi.ID, s.minimumPayload)
}
func (s *WorkItem2Suite) TestWI2UpdateWithNonExistentID() {
id := uuid.NewV4()
s.minimumPayload.Data.ID = &id
test.UpdateWorkitemNotFound(s.T(), s.svc.Context, s.svc, s.workitemCtrl, id, s.minimumPayload)
}
func (s *WorkItem2Suite) TestWI2UpdateSetReadOnlyFields() {
// given
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment(), tf.WorkItems(1), tf.WorkItemTypes(1))
u := minimumRequiredUpdatePayload()
u.Data.Attributes[workitem.SystemTitle] = "Test title"
u.Data.Attributes["version"] = fxt.WorkItems[0].Version
u.Data.Attributes[workitem.SystemNumber] = fxt.WorkItems[0].Number + 666
u.Data.ID = &fxt.WorkItems[0].ID
u.Data.Relationships = &app.WorkItemRelationships{
BaseType: newRelationBaseType(fxt.WorkItemTypes[0].ID),
}
// when
_, updatedWI := test.UpdateWorkitemOK(s.T(), s.svc.Context, s.svc, s.workitemCtrl, fxt.WorkItems[0].ID, &u)
s.T().Run("ensure type was not updated", func(t *testing.T) {
require.Equal(t, fxt.WorkItemTypes[0].ID, updatedWI.Data.Relationships.BaseType.Data.ID)
})
s.T().Run("ensure number was not updated", func(t *testing.T) {
require.Equal(t, fxt.WorkItems[0].Number, updatedWI.Data.Attributes[workitem.SystemNumber])
})
}
func (s *WorkItem2Suite) TestWI2UpdateWorkItemType() {
userFullName := []string{"First User", "Second User"}
userUserName := []string{"jon_doe", "lorem_ipsum"}
fxt := tf.NewTestFixture(s.T(), s.DB,
tf.CreateWorkItemEnvironment(),
tf.Users(2, func(fxt *tf.TestFixture, idx int) error {
fxt.Users[idx].FullName = userFullName[idx]
return nil
}),
tf.Identities(2, func(fxt *tf.TestFixture, idx int) error {
fxt.Identities[idx].Username = userUserName[idx]
fxt.Identities[idx].User = *fxt.Users[idx]
return nil
}),
tf.WorkItemTypes(2, func(fxt *tf.TestFixture, idx int) error {
switch idx {
case 0:
fxt.WorkItemTypes[idx].Name = "First WorkItem Type"
fxt.WorkItemTypes[idx].Fields = map[string]workitem.FieldDefinition{
"fooo": {
Label: "Type1 fooo",
Type: &workitem.SimpleType{Kind: workitem.KindFloat},
},
"fooBar": {
Label: "Type1 fooBar",
Type: workitem.EnumType{
BaseType: workitem.SimpleType{Kind: workitem.KindString},
SimpleType: workitem.SimpleType{Kind: workitem.KindEnum},
Values: []interface{}{"open", "done", "closed"},
},
},