// Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package webhook import ( "testing" webhook_model "gitea.dev/models/webhook" "gitea.dev/modules/json" api "gitea.dev/modules/structs" webhook_module "gitea.dev/modules/webhook" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestSlackPayload(t *testing.T) { sc := slackConvertor{} for _, testCase := range []struct { name string call func() (SlackPayload, error) text string attachments bool }{ { "Create", func() (SlackPayload, error) { return sc.Create(createTestPayload()) }, "[:] branch created by user1", false, }, { "Delete", func() (SlackPayload, error) { return sc.Delete(deleteTestPayload()) }, "[:test] branch deleted by user1", false, }, { "Fork", func() (SlackPayload, error) { return sc.Fork(forkTestPayload()) }, " is forked to ", false, }, { "Push", func() (SlackPayload, error) { return sc.Push(pushTestPayload()) }, "[:] 2 new commits pushed by user1", true, }, { "IssueComment", func() (SlackPayload, error) { return sc.IssueComment(issueCommentTestPayload()) }, "[] New comment on issue by ", true, }, { "PullRequest", func() (SlackPayload, error) { return sc.PullRequest(pullRequestTestPayload()) }, "[] Pull request opened: by ", true, }, { "PullRequestComment", func() (SlackPayload, error) { return sc.IssueComment(pullRequestCommentTestPayload()) }, "[] New comment on pull request by ", false, }, { "Review", func() (SlackPayload, error) { p := pullRequestTestPayload() p.Action = api.HookIssueReviewed return sc.Review(p, webhook_module.HookEventPullRequestReviewApproved) }, "[] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by ", false, }, { "Package", func() (SlackPayload, error) { return sc.Package(packageTestPayload()) }, "Package created: by ", false, }, { "Release", func() (SlackPayload, error) { return sc.Release(pullReleaseTestPayload()) }, "[] Release created: by ", false, }, } { t.Run(testCase.name, func(t *testing.T) { pl, err := testCase.call() require.NoError(t, err) assert.Equal(t, testCase.text, pl.Text) if testCase.attachments { require.Len(t, pl.Attachments, 1) assert.Equal(t, "user1", pl.Attachments[0].AuthorName) assert.Equal(t, "http://localhost:3000/user1/avatar", pl.Attachments[0].AuthorIcon) assert.Equal(t, "http://localhost:3000/user1", pl.Attachments[0].AuthorLink) } }) } t.Run("Issue", func(t *testing.T) { p := issueTestPayload() p.Action = api.HookIssueOpened pl, err := sc.Issue(p) require.NoError(t, err) assert.Equal(t, "[] Issue opened: by ", pl.Text) require.Len(t, pl.Attachments, 1) assert.Equal(t, "user1", pl.Attachments[0].AuthorName) assert.Equal(t, "http://localhost:3000/user1/avatar", pl.Attachments[0].AuthorIcon) assert.Equal(t, "http://localhost:3000/user1", pl.Attachments[0].AuthorLink) p.Action = api.HookIssueClosed pl, err = sc.Issue(p) require.NoError(t, err) assert.Equal(t, "[] Issue closed: by ", pl.Text) }) t.Run("Repository", func(t *testing.T) { p := repositoryTestPayload() pl, err := sc.Repository(p) require.NoError(t, err) assert.Equal(t, "[] Repository created by ", pl.Text) p.Action = api.HookRepoRenamed p.Changes = &api.ChangesPayload{Name: &api.ChangesFromPayload{From: "old-repo"}} pl, err = sc.Repository(p) require.NoError(t, err) assert.Equal(t, "[] Repository renamed from old-repo by ", pl.Text) }) t.Run("Wiki", func(t *testing.T) { p := wikiTestPayload() for _, testCase := range []struct { action api.HookWikiAction text string }{ {api.HookWikiCreated, "[] New wiki page '' (Wiki change comment) by "}, {api.HookWikiEdited, "[] Wiki page '' edited (Wiki change comment) by "}, {api.HookWikiDeleted, "[] Wiki page '' deleted by "}, } { p.Action = testCase.action pl, err := sc.Wiki(p) require.NoError(t, err) assert.Equal(t, testCase.text, pl.Text) } }) } func TestSlackJSONPayload(t *testing.T) { p := pushTestPayload() data, err := p.JSONPayload() require.NoError(t, err) hook := &webhook_model.Webhook{ RepoID: 3, IsActive: true, Type: webhook_module.SLACK, URL: "https://slack.example.com/", Meta: `{}`, HTTPMethod: "POST", } task := &webhook_model.HookTask{ HookID: hook.ID, EventType: webhook_module.HookEventPush, PayloadContent: string(data), PayloadVersion: 2, } req, reqBody, err := newSlackRequest(t.Context(), hook, task) require.NotNil(t, req) require.NotNil(t, reqBody) require.NoError(t, err) assert.Equal(t, "POST", req.Method) assert.Equal(t, "https://slack.example.com/", req.URL.String()) assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256")) assert.Equal(t, "application/json", req.Header.Get("Content-Type")) var body SlackPayload err = json.NewDecoder(req.Body).Decode(&body) assert.NoError(t, err) assert.Equal(t, "[:] 2 new commits pushed by user1", body.Text) }