Skip to content

Commit 17daeee

Browse files
[MM-956]: Added server testcase for webhook.go
1 parent 5c5b74c commit 17daeee

4 files changed

Lines changed: 1182 additions & 10 deletions

File tree

server/plugin/subscriptions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,13 @@ func (p *Plugin) StoreSubscriptions(s *Subscriptions) error {
317317
func (p *Plugin) GetSubscribedChannelsForRepository(repo *github.Repository) []*Subscription {
318318
name := repo.GetFullName()
319319
name = strings.ToLower(name)
320+
fmt.Println("name ", name)
320321
org := strings.Split(name, "/")[0]
321322
subs, err := p.GetSubscriptions()
322323
if err != nil {
323324
return nil
324325
}
326+
fmt.Println("subs ", subs)
325327

326328
// Add subscriptions for the specific repo
327329
subsForRepo := []*Subscription{}
@@ -342,6 +344,7 @@ func (p *Plugin) GetSubscribedChannelsForRepository(repo *github.Repository) []*
342344
subsToReturn := []*Subscription{}
343345

344346
for _, sub := range subsForRepo {
347+
fmt.Println("sub 1", sub)
345348
if repo.GetPrivate() && !p.permissionToRepo(sub.CreatorID, name) {
346349
continue
347350
}

server/plugin/test_utils.go

Lines changed: 294 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package plugin
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/golang/mock/gomock"
9+
"github.com/google/go-github/v54/github"
810
"golang.org/x/oauth2"
911

1012
"github.com/mattermost/mattermost-plugin-github/server/mocks"
@@ -13,13 +15,22 @@ import (
1315
)
1416

1517
const (
16-
MockUserID = "mockUserID"
17-
MockUsername = "mockUsername"
18-
MockAccessToken = "mockAccessToken"
19-
MockChannelID = "mockChannelID"
20-
MockCreatorID = "mockCreatorID"
21-
MockBotID = "mockBotID"
22-
MockPostMessage = "mockPostMessage"
18+
MockUserID = "mockUserID"
19+
MockUsername = "mockUsername"
20+
MockAccessToken = "mockAccessToken"
21+
MockChannelID = "mockChannelID"
22+
MockCreatorID = "mockCreatorID"
23+
MockBotID = "mockBotID"
24+
MockPostMessage = "mockPostMessage"
25+
MockOrgRepo = "mockOrg/mockRepo"
26+
MockHead = "mockHead"
27+
MockRepoName = "mockRepoName"
28+
MockEventReference = "refs/heads/main"
29+
MockUserLogin = "mockUser"
30+
MockBranch = "mockBranch"
31+
MockRepo = "mockRepo"
32+
MockIssueAuthor = "issueAuthor"
33+
GithubBaseURL = "https://github.com/"
2334
)
2435

2536
type GitHubUserResponse struct {
@@ -79,3 +90,279 @@ func GetMockUserContext(p *Plugin, mockLogger *mocks.MockLogger) (*UserContext,
7990

8091
return mockUserContext, nil
8192
}
93+
94+
func GetMockPushEvent() *github.PushEvent {
95+
return &github.PushEvent{
96+
PushID: github.Int64(1),
97+
Head: github.String(MockHead),
98+
Repo: &github.PushEventRepository{
99+
Name: github.String(MockRepoName),
100+
FullName: github.String(MockOrgRepo),
101+
Private: github.Bool(false),
102+
HTMLURL: github.String(fmt.Sprintf("%s/%s", GithubBaseURL, MockOrgRepo)),
103+
},
104+
Ref: github.String(MockEventReference),
105+
Compare: github.String("%s%s/compare/old...new"),
106+
Sender: &github.User{
107+
Login: github.String(MockUserLogin),
108+
},
109+
Commits: []*github.HeadCommit{
110+
{
111+
ID: github.String("abcdef123456"),
112+
URL: github.String(fmt.Sprintf("%s%s/commit/abcdef123456", GithubBaseURL, MockOrgRepo)),
113+
Message: github.String("Initial commit"),
114+
Author: &github.CommitAuthor{
115+
Name: github.String("John Doe"),
116+
},
117+
},
118+
{
119+
ID: github.String("123456abcdef"),
120+
URL: github.String(fmt.Sprintf("%s%s/commit/123456abcdef", GithubBaseURL, MockOrgRepo)),
121+
Message: github.String("Update README"),
122+
Author: &github.CommitAuthor{
123+
Name: github.String("Jane Smith"),
124+
},
125+
},
126+
},
127+
}
128+
}
129+
130+
func GetMockPushEventWithoutCommit() *github.PushEvent {
131+
return &github.PushEvent{
132+
PushID: github.Int64(1),
133+
Head: github.String(MockHead),
134+
Repo: &github.PushEventRepository{
135+
Name: github.String(MockRepoName),
136+
FullName: github.String(MockOrgRepo),
137+
Private: github.Bool(false),
138+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
139+
},
140+
Ref: github.String(MockEventReference),
141+
Compare: github.String(fmt.Sprintf("%s%s/compare/old...new", GithubBaseURL, MockOrgRepo)),
142+
Sender: &github.User{
143+
Login: github.String(MockUserLogin),
144+
},
145+
}
146+
}
147+
148+
func GetMockSubscriptions() *Subscriptions {
149+
return &Subscriptions{
150+
Repositories: map[string][]*Subscription{
151+
"mockorg/mockrepo": {
152+
{
153+
ChannelID: "channel1",
154+
CreatorID: "user1",
155+
Features: Features("pushes"),
156+
Flags: SubscriptionFlags{},
157+
Repository: MockOrgRepo,
158+
},
159+
{
160+
ChannelID: "channel2",
161+
CreatorID: "user2",
162+
Features: Features("creates"),
163+
Flags: SubscriptionFlags{},
164+
Repository: MockOrgRepo,
165+
},
166+
{
167+
ChannelID: "channel2",
168+
CreatorID: "user3",
169+
Features: Features("deletes"),
170+
Flags: SubscriptionFlags{},
171+
Repository: MockOrgRepo,
172+
},
173+
{
174+
ChannelID: "channel4",
175+
CreatorID: "user4",
176+
Features: Features("issue_comments"),
177+
Flags: SubscriptionFlags{},
178+
Repository: MockOrgRepo,
179+
},
180+
{
181+
ChannelID: "channel5",
182+
CreatorID: "user5",
183+
Features: Features("pull_reviews"),
184+
Flags: SubscriptionFlags{},
185+
Repository: MockOrgRepo,
186+
},
187+
},
188+
},
189+
}
190+
}
191+
192+
func GetMockCreateEvent() *github.CreateEvent {
193+
return &github.CreateEvent{
194+
Ref: github.String("v1.0.0"),
195+
RefType: github.String("tag"),
196+
Repo: &github.Repository{
197+
Name: github.String(MockRepoName),
198+
FullName: github.String(MockOrgRepo),
199+
Private: github.Bool(false),
200+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
201+
},
202+
Sender: &github.User{
203+
Login: github.String(MockUserLogin),
204+
},
205+
}
206+
}
207+
208+
func GetMockCreateEventWithUnsupportedRefType() *github.CreateEvent {
209+
return &github.CreateEvent{
210+
Ref: github.String("feature/new-feature"),
211+
RefType: github.String("unsupported"),
212+
Repo: &github.Repository{
213+
Name: github.String(MockRepoName),
214+
FullName: github.String(MockOrgRepo),
215+
Private: github.Bool(false),
216+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
217+
},
218+
Sender: &github.User{
219+
Login: github.String(MockUserLogin),
220+
},
221+
}
222+
}
223+
224+
func GetMockDeleteEvent() *github.DeleteEvent {
225+
return &github.DeleteEvent{
226+
Ref: github.String(MockBranch),
227+
RefType: github.String("branch"),
228+
Repo: &github.Repository{
229+
Name: github.String(MockRepoName),
230+
FullName: github.String(MockOrgRepo),
231+
Private: github.Bool(false),
232+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
233+
},
234+
Sender: &github.User{
235+
Login: github.String(MockUserLogin),
236+
},
237+
}
238+
}
239+
240+
func GetMockDeleteEventWithInvalidType() *github.DeleteEvent {
241+
return &github.DeleteEvent{
242+
Ref: github.String(MockBranch),
243+
RefType: github.String("invalidType"),
244+
Repo: &github.Repository{
245+
Name: github.String(MockRepoName),
246+
FullName: github.String(MockOrgRepo),
247+
Private: github.Bool(false),
248+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
249+
},
250+
Sender: &github.User{
251+
Login: github.String(MockUserLogin),
252+
},
253+
}
254+
}
255+
256+
func GetMockPullRequestReviewEvent(action, state string) *github.PullRequestReviewEvent {
257+
return &github.PullRequestReviewEvent{
258+
Action: github.String(action),
259+
Repo: &github.Repository{
260+
Name: github.String(MockRepoName),
261+
FullName: github.String(MockOrgRepo),
262+
Private: github.Bool(false),
263+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
264+
},
265+
Review: &github.PullRequestReview{
266+
State: github.String(state),
267+
},
268+
Sender: &github.User{
269+
Login: github.String(MockUserLogin),
270+
},
271+
PullRequest: &github.PullRequest{},
272+
}
273+
}
274+
275+
func GetMockPullRequestReviewCommentEvent() *github.PullRequestReviewCommentEvent {
276+
return &github.PullRequestReviewCommentEvent{
277+
Repo: &github.Repository{
278+
Name: github.String(MockRepoName),
279+
FullName: github.String(MockOrgRepo),
280+
Private: github.Bool(false),
281+
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
282+
},
283+
Comment: &github.PullRequestComment{
284+
ID: github.Int64(12345),
285+
Body: github.String("This is a review comment"),
286+
HTMLURL: github.String(fmt.Sprintf("%s%s/pull/1#discussion_r12345", GithubBaseURL, MockOrgRepo)),
287+
},
288+
Sender: &github.User{
289+
Login: github.String(MockUserLogin),
290+
},
291+
PullRequest: &github.PullRequest{},
292+
}
293+
}
294+
295+
func GetMockIssueCommentEvent(action, body, sender string) *github.IssueCommentEvent {
296+
return &github.IssueCommentEvent{
297+
Action: github.String(action),
298+
Repo: &github.Repository{
299+
Name: github.String(MockRepo),
300+
FullName: github.String(MockOrgRepo),
301+
Private: github.Bool(false),
302+
},
303+
Comment: &github.IssueComment{
304+
Body: github.String(body),
305+
},
306+
Issue: &github.Issue{
307+
User: &github.User{Login: github.String(MockIssueAuthor)},
308+
Assignees: []*github.User{{Login: github.String("assigneeUser")}},
309+
},
310+
Sender: &github.User{
311+
Login: github.String(sender),
312+
},
313+
}
314+
}
315+
316+
func GetMockIssueCommentEventWithURL(action, body, sender, url string) *github.IssueCommentEvent {
317+
event := GetMockIssueCommentEvent(action, body, sender)
318+
event.Issue.HTMLURL = github.String(url)
319+
return event
320+
}
321+
322+
func GetMockIssueCommentEventWithAssignees(eventType, action, body, sender string, assignees []string) *github.IssueCommentEvent {
323+
assigneeUsers := make([]*github.User, len(assignees))
324+
for i, assignee := range assignees {
325+
assigneeUsers[i] = &github.User{Login: github.String(assignee)}
326+
}
327+
328+
return &github.IssueCommentEvent{
329+
Action: github.String(action),
330+
Repo: &github.Repository{
331+
Name: github.String(MockRepo),
332+
FullName: github.String(MockOrgRepo),
333+
Private: github.Bool(false),
334+
},
335+
Comment: &github.IssueComment{
336+
Body: github.String(body),
337+
},
338+
Issue: &github.Issue{
339+
User: &github.User{Login: github.String(MockIssueAuthor)},
340+
Assignees: assigneeUsers,
341+
HTMLURL: github.String(fmt.Sprintf("%s%s/%s/123", GithubBaseURL, MockOrgRepo, eventType)),
342+
},
343+
Sender: &github.User{
344+
Login: github.String(sender),
345+
},
346+
}
347+
}
348+
349+
func GetMockPullRequestEvent(action, repoName string, isPrivate bool, sender, user, assignee string) *github.PullRequestEvent {
350+
return &github.PullRequestEvent{
351+
Action: github.String(action),
352+
Repo: &github.Repository{
353+
Name: github.String(repoName),
354+
FullName: github.String(fmt.Sprintf("mockOrg/%s", repoName)),
355+
Private: github.Bool(isPrivate),
356+
},
357+
PullRequest: &github.PullRequest{
358+
User: &github.User{Login: github.String(user)},
359+
HTMLURL: github.String(fmt.Sprintf("%s%s/%s/pull/123", GithubBaseURL, MockOrgRepo, repoName)),
360+
Assignee: &github.User{Login: github.String(assignee)},
361+
RequestedReviewers: []*github.User{{Login: github.String(user)}},
362+
},
363+
Sender: &github.User{
364+
Login: github.String(sender),
365+
},
366+
RequestedReviewer: &github.User{Login: github.String(user)},
367+
}
368+
}

0 commit comments

Comments
 (0)