Skip to content

Commit 0934d1b

Browse files
ethantkoeniglunny
authored andcommitted
Bug fixes and unit tests for models/webhook (#751)
1 parent a6832c2 commit 0934d1b

File tree

5 files changed

+312
-20
lines changed

5 files changed

+312
-20
lines changed

models/fixtures/hook_task.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-
2+
id: 1
3+
repo_id: 1
4+
hook_id: 1
5+
uuid: uuid1

models/fixtures/webhook.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-
2+
id: 1
3+
repo_id: 1
4+
url: www.example.com/url1
5+
content_type: 1 # json
6+
events: '{"push_only":true,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":false}}'
7+
is_active: true
8+
9+
-
10+
id: 2
11+
repo_id: 1
12+
url: www.example.com/url2
13+
content_type: 1 # json
14+
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
15+
is_active: false
16+
17+
-
18+
id: 3
19+
org_id: 3
20+
repo_id: 3
21+
url: www.example.com/url3
22+
content_type: 1 # json
23+
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
24+
is_active: true

models/setup_for_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"gopkg.in/testfixtures.v2"
1717
)
1818

19+
const NonexistentID = 9223372036854775807
20+
1921
func TestMain(m *testing.M) {
2022
if err := CreateTestEngine(); err != nil {
2123
fmt.Printf("Error creating test engine: %v\n", err)

models/webhook.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,8 @@ func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
231231
// GetActiveWebhooksByRepoID returns all active webhooks of repository.
232232
func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
233233
webhooks := make([]*Webhook, 0, 5)
234-
return webhooks, x.Find(&webhooks, &Webhook{
235-
RepoID: repoID,
236-
IsActive: true,
237-
})
234+
return webhooks, x.Where("is_active=?", true).
235+
Find(&webhooks, &Webhook{RepoID: repoID})
238236
}
239237

240238
// GetWebhooksByRepoID returns all webhooks of a repository.
@@ -243,6 +241,21 @@ func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
243241
return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
244242
}
245243

244+
// GetActiveWebhooksByOrgID returns all active webhooks for an organization.
245+
func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
246+
err = x.
247+
Where("org_id=?", orgID).
248+
And("is_active=?", true).
249+
Find(&ws)
250+
return ws, err
251+
}
252+
253+
// GetWebhooksByOrgID returns all webhooks for an organization.
254+
func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
255+
err = x.Find(&ws, &Webhook{OrgID: orgID})
256+
return ws, err
257+
}
258+
246259
// UpdateWebhook updates information of webhook.
247260
func UpdateWebhook(w *Webhook) error {
248261
_, err := x.Id(w.ID).AllCols().Update(w)
@@ -285,21 +298,6 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
285298
})
286299
}
287300

288-
// GetWebhooksByOrgID returns all webhooks for an organization.
289-
func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
290-
err = x.Find(&ws, &Webhook{OrgID: orgID})
291-
return ws, err
292-
}
293-
294-
// GetActiveWebhooksByOrgID returns all active webhooks for an organization.
295-
func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
296-
err = x.
297-
Where("org_id=?", orgID).
298-
And("is_active=?", true).
299-
Find(&ws)
300-
return ws, err
301-
}
302-
303301
// ___ ___ __ ___________ __
304302
// / | \ ____ ____ | | _\__ ___/____ _____| | __
305303
// / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
@@ -505,7 +503,7 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
505503
}
506504
}
507505

508-
// Use separate objects so modifcations won't be made on payload on non-Gogs type hooks.
506+
// Use separate objects so modifications won't be made on payload on non-Gogs type hooks.
509507
switch w.HookTaskType {
510508
case SLACK:
511509
payloader, err = GetSlackPayload(p, event, w.Meta)

models/webhook_test.go

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"encoding/json"
9+
"testing"
10+
11+
api "code.gitea.io/sdk/gitea"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestHookContentType_Name(t *testing.T) {
17+
assert.Equal(t, "json", ContentTypeJSON.Name())
18+
assert.Equal(t, "form", ContentTypeForm.Name())
19+
}
20+
21+
func TestIsValidHookContentType(t *testing.T) {
22+
assert.True(t, IsValidHookContentType("json"))
23+
assert.True(t, IsValidHookContentType("form"))
24+
assert.False(t, IsValidHookContentType("invalid"))
25+
}
26+
27+
func TestWebhook_GetSlackHook(t *testing.T) {
28+
w := &Webhook{
29+
Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
30+
}
31+
slackHook := w.GetSlackHook()
32+
assert.Equal(t, *slackHook, SlackMeta{
33+
Channel: "foo",
34+
Username: "username",
35+
Color: "blue",
36+
})
37+
}
38+
39+
func TestWebhook_History(t *testing.T) {
40+
assert.NoError(t, PrepareTestDatabase())
41+
webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
42+
tasks, err := webhook.History(0)
43+
assert.NoError(t, err)
44+
assert.Len(t, tasks, 1)
45+
assert.Equal(t, int64(1), tasks[0].ID)
46+
47+
webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
48+
tasks, err = webhook.History(0)
49+
assert.NoError(t, err)
50+
assert.Len(t, tasks, 0)
51+
}
52+
53+
func TestWebhook_UpdateEvent(t *testing.T) {
54+
assert.NoError(t, PrepareTestDatabase())
55+
webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
56+
hookEvent := &HookEvent{
57+
PushOnly: true,
58+
SendEverything: false,
59+
ChooseEvents: false,
60+
HookEvents: HookEvents{
61+
Create: false,
62+
Push: true,
63+
PullRequest: false,
64+
},
65+
}
66+
webhook.HookEvent = hookEvent
67+
assert.NoError(t, webhook.UpdateEvent())
68+
assert.NotEmpty(t, webhook.Events)
69+
actualHookEvent := &HookEvent{}
70+
assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
71+
assert.Equal(t, *hookEvent, *actualHookEvent)
72+
}
73+
74+
func TestWebhook_EventsArray(t *testing.T) {
75+
assert.Equal(t, []string{"create", "push", "pull_request"},
76+
(&Webhook{
77+
HookEvent: &HookEvent{SendEverything: true},
78+
}).EventsArray(),
79+
)
80+
81+
assert.Equal(t, []string{"push"},
82+
(&Webhook{
83+
HookEvent: &HookEvent{PushOnly: true},
84+
}).EventsArray(),
85+
)
86+
}
87+
88+
func TestCreateWebhook(t *testing.T) {
89+
hook := &Webhook{
90+
RepoID: 3,
91+
URL: "www.example.com/unit_test",
92+
ContentType: ContentTypeJSON,
93+
Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`,
94+
}
95+
AssertNotExistsBean(t, hook)
96+
assert.NoError(t, CreateWebhook(hook))
97+
AssertExistsAndLoadBean(t, hook)
98+
}
99+
100+
func TestGetWebhookByRepoID(t *testing.T) {
101+
assert.NoError(t, PrepareTestDatabase())
102+
hook, err := GetWebhookByRepoID(1, 1)
103+
assert.NoError(t, err)
104+
assert.Equal(t, int64(1), hook.ID)
105+
106+
_, err = GetWebhookByRepoID(NonexistentID, NonexistentID)
107+
assert.Error(t, err)
108+
assert.True(t, IsErrWebhookNotExist(err))
109+
}
110+
111+
func TestGetWebhookByOrgID(t *testing.T) {
112+
assert.NoError(t, PrepareTestDatabase())
113+
hook, err := GetWebhookByOrgID(3, 3)
114+
assert.NoError(t, err)
115+
assert.Equal(t, int64(3), hook.ID)
116+
117+
_, err = GetWebhookByOrgID(NonexistentID, NonexistentID)
118+
assert.Error(t, err)
119+
assert.True(t, IsErrWebhookNotExist(err))
120+
}
121+
122+
func TestGetActiveWebhooksByRepoID(t *testing.T) {
123+
assert.NoError(t, PrepareTestDatabase())
124+
hooks, err := GetActiveWebhooksByRepoID(1)
125+
assert.NoError(t, err)
126+
assert.Len(t, hooks, 1)
127+
assert.Equal(t, int64(1), hooks[0].ID)
128+
assert.True(t, hooks[0].IsActive)
129+
}
130+
131+
func TestGetWebhooksByRepoID(t *testing.T) {
132+
assert.NoError(t, PrepareTestDatabase())
133+
hooks, err := GetWebhooksByRepoID(1)
134+
assert.NoError(t, err)
135+
assert.Len(t, hooks, 2)
136+
assert.Equal(t, int64(1), hooks[0].ID)
137+
assert.Equal(t, int64(2), hooks[1].ID)
138+
}
139+
140+
func TestGetActiveWebhooksByOrgID(t *testing.T) {
141+
assert.NoError(t, PrepareTestDatabase())
142+
hooks, err := GetActiveWebhooksByOrgID(3)
143+
assert.NoError(t, err)
144+
assert.Len(t, hooks, 1)
145+
assert.Equal(t, int64(3), hooks[0].ID)
146+
assert.True(t, hooks[0].IsActive)
147+
}
148+
149+
func TestGetWebhooksByOrgID(t *testing.T) {
150+
assert.NoError(t, PrepareTestDatabase())
151+
hooks, err := GetWebhooksByOrgID(3)
152+
assert.NoError(t, err)
153+
assert.Len(t, hooks, 1)
154+
assert.Equal(t, int64(3), hooks[0].ID)
155+
assert.True(t, hooks[0].IsActive)
156+
157+
}
158+
159+
func TestUpdateWebhook(t *testing.T) {
160+
assert.NoError(t, PrepareTestDatabase())
161+
hook := AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
162+
hook.IsActive = true
163+
hook.ContentType = ContentTypeForm
164+
AssertNotExistsBean(t, hook)
165+
assert.NoError(t, UpdateWebhook(hook))
166+
AssertExistsAndLoadBean(t, hook)
167+
}
168+
169+
func TestDeleteWebhookByRepoID(t *testing.T) {
170+
assert.NoError(t, PrepareTestDatabase())
171+
AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1})
172+
assert.NoError(t, DeleteWebhookByRepoID(1, 2))
173+
AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1})
174+
175+
err := DeleteWebhookByRepoID(NonexistentID, NonexistentID)
176+
assert.Error(t, err)
177+
assert.True(t, IsErrWebhookNotExist(err))
178+
}
179+
180+
func TestDeleteWebhookByOrgID(t *testing.T) {
181+
assert.NoError(t, PrepareTestDatabase())
182+
AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3})
183+
assert.NoError(t, DeleteWebhookByOrgID(3, 3))
184+
AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3})
185+
186+
err := DeleteWebhookByOrgID(NonexistentID, NonexistentID)
187+
assert.Error(t, err)
188+
assert.True(t, IsErrWebhookNotExist(err))
189+
}
190+
191+
func TestToHookTaskType(t *testing.T) {
192+
assert.Equal(t, GOGS, ToHookTaskType("gogs"))
193+
assert.Equal(t, SLACK, ToHookTaskType("slack"))
194+
}
195+
196+
func TestHookTaskType_Name(t *testing.T) {
197+
assert.Equal(t, "gogs", GOGS.Name())
198+
assert.Equal(t, "slack", SLACK.Name())
199+
}
200+
201+
func TestIsValidHookTaskType(t *testing.T) {
202+
assert.True(t, IsValidHookTaskType("gogs"))
203+
assert.True(t, IsValidHookTaskType("slack"))
204+
assert.False(t, IsValidHookTaskType("invalid"))
205+
}
206+
207+
func TestHookTasks(t *testing.T) {
208+
assert.NoError(t, PrepareTestDatabase())
209+
hookTasks, err := HookTasks(1, 1)
210+
assert.NoError(t, err)
211+
assert.Len(t, hookTasks, 1)
212+
assert.Equal(t, int64(1), hookTasks[0].ID)
213+
214+
hookTasks, err = HookTasks(NonexistentID, 1)
215+
assert.NoError(t, err)
216+
assert.Len(t, hookTasks, 0)
217+
}
218+
219+
func TestCreateHookTask(t *testing.T) {
220+
assert.NoError(t, PrepareTestDatabase())
221+
hookTask := &HookTask{
222+
RepoID: 3,
223+
HookID: 3,
224+
Type: GOGS,
225+
URL: "http://www.example.com/unit_test",
226+
Payloader: &api.PushPayload{},
227+
}
228+
AssertNotExistsBean(t, hookTask)
229+
assert.NoError(t, CreateHookTask(hookTask))
230+
AssertExistsAndLoadBean(t, hookTask)
231+
}
232+
233+
func TestUpdateHookTask(t *testing.T) {
234+
assert.NoError(t, PrepareTestDatabase())
235+
236+
hook := AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask)
237+
hook.PayloadContent = "new payload content"
238+
hook.DeliveredString = "new delivered string"
239+
hook.IsDelivered = true
240+
AssertNotExistsBean(t, hook)
241+
assert.NoError(t, UpdateHookTask(hook))
242+
AssertExistsAndLoadBean(t, hook)
243+
}
244+
245+
func TestPrepareWebhooks(t *testing.T) {
246+
assert.NoError(t, PrepareTestDatabase())
247+
248+
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
249+
hookTasks := []*HookTask{
250+
{RepoID: repo.ID, HookID: 1, EventType: HookEventPush},
251+
}
252+
for _, hookTask := range hookTasks {
253+
AssertNotExistsBean(t, hookTask)
254+
}
255+
assert.NoError(t, PrepareWebhooks(repo, HookEventPush, &api.PushPayload{}))
256+
for _, hookTask := range hookTasks {
257+
AssertExistsAndLoadBean(t, hookTask)
258+
}
259+
}
260+
261+
// TODO TestHookTask_deliver
262+
263+
// TODO TestDeliverHooks

0 commit comments

Comments
 (0)