Skip to content

[MM-979]: Added more testcase for webhook.go #858

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

Open
wants to merge 3 commits into
base: MM-956
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
229 changes: 206 additions & 23 deletions server/plugin/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package plugin

import (
"context"
"crypto/hmac"
"crypto/sha1" // #nosec G505
"encoding/hex"
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/google/go-github/v54/github"
Expand All @@ -15,22 +19,30 @@ import (
)

const (
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/"
MockUserID = "mockUserID"
MockUsername = "mockUsername"
MockAccessToken = "mockAccessToken"
MockChannelID = "mockChannelID"
MockCreatorID = "mockCreatorID"
MockWebhookSecret = "mockWebhookSecret" // #nosec G101
MockBotID = "mockBotID"
MockOrg = "mockOrg"
MockSender = "mockSender"
MockPostMessage = "mockPostMessage"
MockOrgRepo = "mockOrg/mockRepo"
MockHead = "mockHead"
MockPRTitle = "mockPRTitle"
MockProfileUsername = "@username"
MockPostID = "mockPostID"
MockRepoName = "mockRepoName"
MockEventReference = "refs/heads/main"
MockUserLogin = "mockUser"
MockBranch = "mockBranch"
MockRepo = "mockRepo"
MockLabel = "mockLabel"
MockValidLabel = "validLabel"
MockIssueAuthor = "issueAuthor"
GithubBaseURL = "https://github.com/"
)

type GitHubUserResponse struct {
Expand Down Expand Up @@ -91,6 +103,104 @@ func GetMockUserContext(p *Plugin, mockLogger *mocks.MockLogger) (*UserContext,
return mockUserContext, nil
}

func generateSignature(secret, body []byte) string {
h := hmac.New(sha1.New, secret)
h.Write(body)
return "sha1=" + hex.EncodeToString(h.Sum(nil))
}

func GetMockPingEvent() *github.PingEvent {
return &github.PingEvent{
Zen: github.String("Keep it logically awesome."),
HookID: github.Int64(123456),
Hook: &github.Hook{
Type: github.String("Repository"),
ID: github.Int64(654321),
Config: map[string]interface{}{
"url": "https://example.com/webhook",
"content_type": "json",
"secret": "mocksecret",
"insecure_ssl": "0",
},
Active: github.Bool(true),
},
Repo: &github.Repository{
Name: github.String(MockRepoName),
FullName: github.String(MockOrgRepo),
Private: github.Bool(false),
HTMLURL: github.String(fmt.Sprintf("%s/%s", GithubBaseURL, MockOrgRepo)),
},
Org: &github.Organization{
Login: github.String("mockorg"),
ID: github.Int64(12345),
URL: github.String(fmt.Sprintf("%s/mockorg", GithubBaseURL)),
},
Sender: &github.User{
Login: github.String(MockUserLogin),
ID: github.Int64(98765),
URL: github.String(fmt.Sprintf("%s/users/%s", GithubBaseURL, MockUserLogin)),
},
Installation: &github.Installation{
ID: github.Int64(246810),
NodeID: github.String("MDQ6VXNlcjE="),
},
}
}

func GetMockPRDescriptionEvent(repo, org, sender, prUser, action, label string) *github.PullRequestEvent {
return &github.PullRequestEvent{
Action: github.String(action),
PullRequest: &github.PullRequest{
Title: github.String(MockPRTitle),
Body: github.String("Mock PR description with label: " + label),
State: github.String("open"),
User: &github.User{Login: github.String(prUser)},
Head: &github.PullRequestBranch{Ref: github.String(MockBranch)},
Base: &github.PullRequestBranch{Ref: github.String("main")},
HTMLURL: github.String(GithubBaseURL + org + "/" + repo + "/pull/1"),
Number: github.Int(1),
},
Repo: &github.Repository{
Name: github.String(repo),
Owner: &github.User{Login: github.String(org)},
FullName: github.String(org + "/" + repo),
},
Sender: &github.User{
Login: github.String(sender),
},
}
}

func GetMockIssueEvent(repo, org, sender, action, label string) *github.IssuesEvent {
event := &github.IssuesEvent{
Repo: &github.Repository{
Name: github.String(repo),
Owner: &github.User{Login: github.String(org)},
FullName: github.String(fmt.Sprintf("%s/%s", repo, org)),
},
Sender: &github.User{Login: github.String(sender)},
Issue: &github.Issue{
Number: github.Int(123),
Labels: []*github.Label{
{Name: github.String(label)},
},
},
Action: github.String(action),
}

if action == actionLabeled || action == "unlabeled" {
event.Label = &github.Label{Name: github.String(label)}
}

return event
}

func GetMockIssueEventWithTimeDiff(repo, org, sender, action, label string, timeDiff time.Duration) *github.IssuesEvent {
event := GetMockIssueEvent(repo, org, sender, action, label)
event.Issue.CreatedAt = &github.Timestamp{Time: time.Now().Add(timeDiff)}
return event
}

func GetMockPushEvent() *github.PushEvent {
return &github.PushEvent{
PushID: github.Int64(1),
Expand Down Expand Up @@ -253,22 +363,25 @@ func GetMockDeleteEventWithInvalidType() *github.DeleteEvent {
}
}

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

Expand Down Expand Up @@ -346,9 +459,10 @@ func GetMockIssueCommentEventWithAssignees(eventType, action, body, sender strin
}
}

func GetMockPullRequestEvent(action, repoName string, isPrivate bool, sender, user, assignee string) *github.PullRequestEvent {
func GetMockPullRequestEvent(action, repoName, eventLabel string, isPrivate bool, sender, user, assignee string) *github.PullRequestEvent {
return &github.PullRequestEvent{
Action: github.String(action),
Label: &github.Label{Name: github.String(eventLabel)},
Repo: &github.Repository{
Name: github.String(repoName),
FullName: github.String(fmt.Sprintf("mockOrg/%s", repoName)),
Expand All @@ -359,10 +473,79 @@ func GetMockPullRequestEvent(action, repoName string, isPrivate bool, sender, us
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)}},
Labels: []*github.Label{{Name: github.String("validLabel")}},
Draft: github.Bool(true),
},
Sender: &github.User{
Login: github.String(sender),
},
RequestedReviewer: &github.User{Login: github.String(user)},
}
}

func GetMockIssuesEvent(action, repoName string, isPrivate bool, author, sender, assignee string) *github.IssuesEvent {
return &github.IssuesEvent{
Action: &action,
Repo: &github.Repository{FullName: &repoName, Private: &isPrivate},
Issue: &github.Issue{User: &github.User{Login: &author}},
Sender: &github.User{Login: &sender},
Assignee: func() *github.User {
if assignee == "" {
return nil
}
return &github.User{Login: &assignee}
}(),
}
}

func GetMockStarEvent(repo, org string, isPrivate bool, sender string) *github.StarEvent {
return &github.StarEvent{
Repo: &github.Repository{
Name: github.String(repo),
Private: github.Bool(isPrivate),
FullName: github.String(fmt.Sprintf("%s/%s", repo, org)),
},
Sender: &github.User{Login: github.String(sender)},
}
}

func GetMockReleaseEvent(repo, org, action, sender string) *github.ReleaseEvent {
return &github.ReleaseEvent{
Action: &action,
Repo: &github.Repository{
Name: github.String(repo),
Owner: &github.User{Login: github.String(org)},
FullName: github.String(fmt.Sprintf("%s/%s", repo, org)),
},
Sender: &github.User{Login: github.String(sender)},
}
}

func GetMockDiscussionEvent(repo, org, sender string) *github.DiscussionEvent {
return &github.DiscussionEvent{
Repo: &github.Repository{
Name: github.String(repo),
Owner: &github.User{Login: github.String(org)},
FullName: github.String(fmt.Sprintf("%s/%s", repo, org)),
},
Sender: &github.User{Login: github.String(sender)},
Discussion: &github.Discussion{
Number: github.Int(123),
},
}
}

func GetMockDiscussionCommentEvent(repo, org, action, sender string) *github.DiscussionCommentEvent {
return &github.DiscussionCommentEvent{
Action: &action,
Repo: &github.Repository{
Name: github.String(repo),
Owner: &github.User{Login: github.String(org)},
FullName: github.String(fmt.Sprintf("%s/%s", repo, org)),
},
Sender: &github.User{Login: github.String(sender)},
Comment: &github.CommentDiscussion{
ID: github.Int64(456),
},
}
}
15 changes: 15 additions & 0 deletions server/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,18 @@ func lastN(s string, n int) string {

return string(out)
}

func GetMockSubscriptionWithLabel(repo string, feature string) *Subscriptions {
return &Subscriptions{
Repositories: map[string][]*Subscription{
repo: {
{
ChannelID: MockChannelID,
CreatorID: MockCreatorID,
Features: Features(feature),
Repository: MockRepo,
},
},
},
}
}
Loading