forked from fabric8-services/fabric8-wit
/
work-item-comments_test.go
213 lines (191 loc) · 7.25 KB
/
work-item-comments_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
package main_test
import (
"html"
"testing"
"time"
"golang.org/x/net/context"
. "github.com/almighty/almighty-core"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/app/test"
"github.com/almighty/almighty-core/application"
"github.com/almighty/almighty-core/comment"
"github.com/almighty/almighty-core/gormapplication"
"github.com/almighty/almighty-core/gormsupport"
"github.com/almighty/almighty-core/gormsupport/cleaner"
"github.com/almighty/almighty-core/rendering"
"github.com/almighty/almighty-core/resource"
testsupport "github.com/almighty/almighty-core/test"
almtoken "github.com/almighty/almighty-core/token"
"github.com/almighty/almighty-core/workitem"
"github.com/goadesign/goa"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type TestCommentREST struct {
gormsupport.DBTestSuite
db *gormapplication.GormDB
clean func()
}
func TestRunCommentREST(t *testing.T) {
suite.Run(t, &TestCommentREST{DBTestSuite: gormsupport.NewDBTestSuite("config.yaml")})
}
func (rest *TestCommentREST) SetupTest() {
resource.Require(rest.T(), resource.Database)
rest.db = gormapplication.NewGormDB(rest.DB)
rest.clean = cleaner.DeleteCreatedEntities(rest.DB)
}
func (rest *TestCommentREST) TearDownTest() {
rest.clean()
}
func (rest *TestCommentREST) SecuredController() (*goa.Service, *WorkItemCommentsController) {
priv, _ := almtoken.ParsePrivateKey([]byte(almtoken.RSAPrivateKey))
svc := testsupport.ServiceAsUser("WorkItemComment-Service", almtoken.NewManagerWithPrivateKey(priv), testsupport.TestIdentity)
return svc, NewWorkItemCommentsController(svc, rest.db)
}
func (rest *TestCommentREST) UnSecuredController() (*goa.Service, *WorkItemCommentsController) {
svc := goa.New("WorkItemComment-Service")
return svc, NewWorkItemCommentsController(svc, rest.db)
}
func (rest *TestCommentREST) newCreateWorkItemCommentsPayload(body string, markup *string) *app.CreateWorkItemCommentsPayload {
return &app.CreateWorkItemCommentsPayload{
Data: &app.CreateComment{
Type: "comments",
Attributes: &app.CreateCommentAttributes{
Body: body,
Markup: markup,
},
},
}
}
func (rest *TestCommentREST) createDefaultWorkItem() string {
var wiid string
err := application.Transactional(rest.db, func(appl application.Application) error {
repo := appl.WorkItems()
wi, err := repo.Create(
context.Background(),
workitem.SystemBug,
map[string]interface{}{
workitem.SystemTitle: "A",
workitem.SystemState: "new",
},
uuid.NewV4().String())
if err != nil {
return errors.WithStack(err)
}
wiid = wi.ID
return nil
})
require.Nil(rest.T(), err)
return wiid
}
func (rest *TestCommentREST) assertComment(c *app.Comment, expectedBody string, expectedMarkup string) {
assert.NotNil(rest.T(), c)
assert.Equal(rest.T(), "comments", c.Type)
assert.NotNil(rest.T(), c.ID)
require.NotNil(rest.T(), c.Attributes)
assert.Equal(rest.T(), expectedBody, *c.Attributes.Body)
assert.Equal(rest.T(), expectedMarkup, *c.Attributes.Markup)
assert.Equal(rest.T(), rendering.RenderMarkupToHTML(html.EscapeString(expectedBody), expectedMarkup), *c.Attributes.BodyRendered)
require.NotNil(rest.T(), c.Attributes.CreatedAt)
assert.WithinDuration(rest.T(), time.Now(), *c.Attributes.CreatedAt, 2*time.Second)
require.NotNil(rest.T(), c.Relationships)
require.NotNil(rest.T(), c.Relationships.CreatedBy)
require.NotNil(rest.T(), c.Relationships.CreatedBy.Data)
assert.Equal(rest.T(), "identities", c.Relationships.CreatedBy.Data.Type)
assert.NotNil(rest.T(), c.Relationships.CreatedBy.Data.ID)
}
func (rest *TestCommentREST) TestSuccessCreateSingleCommentWithMarkup() {
// given
wiid := rest.createDefaultWorkItem()
// when
markup := rendering.SystemMarkupMarkdown
p := rest.newCreateWorkItemCommentsPayload("Test", &markup)
svc, ctrl := rest.SecuredController()
_, c := test.CreateWorkItemCommentsOK(rest.T(), svc.Context, svc, ctrl, wiid, p)
// then
rest.assertComment(c.Data, "Test", markup)
}
func (rest *TestCommentREST) TestSuccessCreateSingleCommentWithDefaultMarkup() {
// given
wiid := rest.createDefaultWorkItem()
// when
p := rest.newCreateWorkItemCommentsPayload("Test", nil)
svc, ctrl := rest.SecuredController()
_, c := test.CreateWorkItemCommentsOK(rest.T(), svc.Context, svc, ctrl, wiid, p)
// then
rest.assertComment(c.Data, "Test", rendering.SystemMarkupDefault)
}
func (rest *TestCommentREST) TestListCommentsByParentWorkItem() {
// given
wiid := rest.createDefaultWorkItem()
application.Transactional(rest.db, func(app application.Application) error {
repo := app.Comments()
repo.Create(context.Background(), &comment.Comment{ParentID: wiid, Body: "Test 1", CreatedBy: uuid.NewV4()})
repo.Create(context.Background(), &comment.Comment{ParentID: wiid, Body: "Test 2", CreatedBy: uuid.NewV4()})
repo.Create(context.Background(), &comment.Comment{ParentID: wiid, Body: "Test 3", CreatedBy: uuid.NewV4()})
repo.Create(context.Background(), &comment.Comment{ParentID: wiid + "_other", Body: "Test 1", CreatedBy: uuid.NewV4()})
return nil
})
// when
svc, ctrl := rest.UnSecuredController()
offset := "0"
limit := 3
_, cs := test.ListWorkItemCommentsOK(rest.T(), svc.Context, svc, ctrl, wiid, &limit, &offset)
// then
require.Equal(rest.T(), 3, len(cs.Data))
rest.assertComment(cs.Data[0], "Test 3", rendering.SystemMarkupDefault) // items are returned in reverse order or creation
// given
wiid2 := rest.createDefaultWorkItem()
// when
_, cs2 := test.ListWorkItemCommentsOK(rest.T(), svc.Context, svc, ctrl, wiid2, &limit, &offset)
// then
assert.Equal(rest.T(), 0, len(cs2.Data))
}
func (rest *TestCommentREST) TestEmptyListCommentsByParentWorkItem() {
// given
wiid := rest.createDefaultWorkItem()
// when
svc, ctrl := rest.UnSecuredController()
offset := "0"
limit := 1
_, cs := test.ListWorkItemCommentsOK(rest.T(), svc.Context, svc, ctrl, wiid, &limit, &offset)
// then
assert.Equal(rest.T(), 0, len(cs.Data))
}
func (rest *TestCommentREST) TestCreateSingleCommentMissingWorkItem() {
// given
p := rest.newCreateWorkItemCommentsPayload("Test", nil)
// when/then
svc, ctrl := rest.SecuredController()
test.CreateWorkItemCommentsNotFound(rest.T(), svc.Context, svc, ctrl, "0000000", p)
}
func (rest *TestCommentREST) TestCreateSingleNoAuthorized() {
// given
wiid := rest.createDefaultWorkItem()
// when/then
p := rest.newCreateWorkItemCommentsPayload("Test", nil)
svc, ctrl := rest.UnSecuredController()
test.CreateWorkItemCommentsUnauthorized(rest.T(), svc.Context, svc, ctrl, wiid, p)
}
// Can not be tested via normal Goa testing framework as setting empty body on CreateCommentAttributes is
// validated before Request to service is made. Validate model and assume it will be validated before hitting
// service when mounted. Test to show intent.
func (rest *TestCommentREST) TestShouldNotCreateEmptyBody() {
// given
p := rest.newCreateWorkItemCommentsPayload("", nil)
// when
err := p.Validate()
// then
require.NotNil(rest.T(), err)
}
func (rest *TestCommentREST) TestListCommentsByMissingParentWorkItem() {
// given
svc, ctrl := rest.SecuredController()
// when/then
offset := "0"
limit := 1
test.ListWorkItemCommentsNotFound(rest.T(), svc.Context, svc, ctrl, "0000000", &limit, &offset)
}