Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-956]: Added server testcase for webhook.go #855

Open
wants to merge 4 commits into
base: MM-918
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 294 additions & 7 deletions server/plugin/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package plugin

import (
"context"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/go-github/v54/github"
"golang.org/x/oauth2"

"github.com/mattermost/mattermost-plugin-github/server/mocks"
Expand All @@ -13,13 +15,22 @@ import (
)

const (
MockUserID = "mockUserID"
MockUsername = "mockUsername"
MockAccessToken = "mockAccessToken"
MockChannelID = "mockChannelID"
MockCreatorID = "mockCreatorID"
MockBotID = "mockBotID"
MockPostMessage = "mockPostMessage"
MockUserID = "mockUserID"
MockUsername = "mockUsername"
MockAccessToken = "mockAccessToken"
MockChannelID = "mockChannelID"
MockCreatorID = "mockCreatorID"
MockBotID = "mockBotID"
MockPostMessage = "mockPostMessage"
MockOrgRepo = "mockOrg/mockRepo"
MockHead = "mockHead"
MockRepoName = "mockRepoName"
MockEventReference = "refs/heads/main"
MockUserLogin = "mockUser"
MockBranch = "mockBranch"
MockRepo = "mockRepo"
MockIssueAuthor = "issueAuthor"
GithubBaseURL = "https://github.com/"
)

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

return mockUserContext, nil
}

func GetMockPushEvent() *github.PushEvent {
return &github.PushEvent{
PushID: github.Int64(1),
Head: github.String(MockHead),
Repo: &github.PushEventRepository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s/%s", GithubBaseURL, MockOrgRepo)),
},
Ref: github.String(MockEventReference),
Compare: github.String("%s%s/compare/old...new"),
Sender: &github.User{
Login: github.String(MockUserLogin),
},
Commits: []*github.HeadCommit{
{
ID: github.String("abcdef123456"),
URL: github.String(fmt.Sprintf("%s%s/commit/abcdef123456", GithubBaseURL, MockOrgRepo)),
Message: github.String("Initial commit"),
Author: &github.CommitAuthor{
Name: github.String("John Doe"),
},
},
{
ID: github.String("123456abcdef"),
URL: github.String(fmt.Sprintf("%s%s/commit/123456abcdef", GithubBaseURL, MockOrgRepo)),
Message: github.String("Update README"),
Author: &github.CommitAuthor{
Name: github.String("Jane Smith"),
},
},
},
}
}

func GetMockPushEventWithoutCommit() *github.PushEvent {
return &github.PushEvent{
PushID: github.Int64(1),
Head: github.String(MockHead),
Repo: &github.PushEventRepository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Ref: github.String(MockEventReference),
Compare: github.String(fmt.Sprintf("%s%s/compare/old...new", GithubBaseURL, MockOrgRepo)),
Sender: &github.User{
Login: github.String(MockUserLogin),
},
}
}

func GetMockSubscriptions() *Subscriptions {
return &Subscriptions{
Repositories: map[string][]*Subscription{
"mockorg/mockrepo": {
{
ChannelID: "channel1",
CreatorID: "user1",
Features: Features("pushes"),
Flags: SubscriptionFlags{},
Repository: MockOrgRepo,
},
{
ChannelID: "channel2",
CreatorID: "user2",
Features: Features("creates"),
Flags: SubscriptionFlags{},
Repository: MockOrgRepo,
},
{
ChannelID: "channel2",
CreatorID: "user3",
Features: Features("deletes"),
Flags: SubscriptionFlags{},
Repository: MockOrgRepo,
},
{
ChannelID: "channel4",
CreatorID: "user4",
Features: Features("issue_comments"),
Flags: SubscriptionFlags{},
Repository: MockOrgRepo,
},
{
ChannelID: "channel5",
CreatorID: "user5",
Features: Features("pull_reviews"),
Flags: SubscriptionFlags{},
Repository: MockOrgRepo,
},
},
},
}
}

func GetMockCreateEvent() *github.CreateEvent {
return &github.CreateEvent{
Ref: github.String("v1.0.0"),
RefType: github.String("tag"),
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
},
}
}

func GetMockCreateEventWithUnsupportedRefType() *github.CreateEvent {
return &github.CreateEvent{
Ref: github.String("feature/new-feature"),
RefType: github.String("unsupported"),
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
},
}
}

func GetMockDeleteEvent() *github.DeleteEvent {
return &github.DeleteEvent{
Ref: github.String(MockBranch),
RefType: github.String("branch"),
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
},
}
}

func GetMockDeleteEventWithInvalidType() *github.DeleteEvent {
return &github.DeleteEvent{
Ref: github.String(MockBranch),
RefType: github.String("invalidType"),
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
},
}
}

func GetMockPullRequestReviewEvent(action, state string) *github.PullRequestReviewEvent {
return &github.PullRequestReviewEvent{
Action: github.String(action),
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Review: &github.PullRequestReview{
State: github.String(state),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
},
PullRequest: &github.PullRequest{},
}
}

func GetMockPullRequestReviewCommentEvent() *github.PullRequestReviewCommentEvent {
return &github.PullRequestReviewCommentEvent{
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s%s", GithubBaseURL, MockOrgRepo)),
},
Comment: &github.PullRequestComment{
ID: github.Int64(12345),
Body: github.String("This is a review comment"),
HTMLURL: github.String(fmt.Sprintf("%s%s/pull/1#discussion_r12345", GithubBaseURL, MockOrgRepo)),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
},
PullRequest: &github.PullRequest{},
}
}

func GetMockIssueCommentEvent(action, body, sender string) *github.IssueCommentEvent {
return &github.IssueCommentEvent{
Action: github.String(action),
Repo: &github.Repository{
Name: github.String(MockRepo),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
},
Comment: &github.IssueComment{
Body: github.String(body),
},
Issue: &github.Issue{
User: &github.User{Login: github.String(MockIssueAuthor)},
Assignees: []*github.User{{Login: github.String("assigneeUser")}},
},
Sender: &github.User{
Login: github.String(sender),
},
}
}

func GetMockIssueCommentEventWithURL(action, body, sender, url string) *github.IssueCommentEvent {
event := GetMockIssueCommentEvent(action, body, sender)
event.Issue.HTMLURL = github.String(url)
return event
}

func GetMockIssueCommentEventWithAssignees(eventType, action, body, sender string, assignees []string) *github.IssueCommentEvent {
assigneeUsers := make([]*github.User, len(assignees))
for i, assignee := range assignees {
assigneeUsers[i] = &github.User{Login: github.String(assignee)}
}

return &github.IssueCommentEvent{
Action: github.String(action),
Repo: &github.Repository{
Name: github.String(MockRepo),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
},
Comment: &github.IssueComment{
Body: github.String(body),
},
Issue: &github.Issue{
User: &github.User{Login: github.String(MockIssueAuthor)},
Assignees: assigneeUsers,
HTMLURL: github.String(fmt.Sprintf("%s%s/%s/123", GithubBaseURL, MockOrgRepo, eventType)),
},
Sender: &github.User{
Login: github.String(sender),
},
}
}

func GetMockPullRequestEvent(action, repoName string, isPrivate bool, sender, user, assignee string) *github.PullRequestEvent {
return &github.PullRequestEvent{
Action: github.String(action),
Repo: &github.Repository{
Name: github.String(repoName),
FullName: github.String(fmt.Sprintf("mockOrg/%s", repoName)),
Private: github.Bool(isPrivate),
},
PullRequest: &github.PullRequest{
User: &github.User{Login: github.String(user)},
HTMLURL: github.String(fmt.Sprintf("%s%s/%s/pull/123", GithubBaseURL, MockOrgRepo, repoName)),
Assignee: &github.User{Login: github.String(assignee)},
RequestedReviewers: []*github.User{{Login: github.String(user)}},
},
Sender: &github.User{
Login: github.String(sender),
},
RequestedReviewer: &github.User{Login: github.String(user)},
}
}
Loading