code.gitea.io/gitea@v1.22.3/services/webhook/slack_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webhook
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	webhook_model "code.gitea.io/gitea/models/webhook"
    11  	"code.gitea.io/gitea/modules/json"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	webhook_module "code.gitea.io/gitea/modules/webhook"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestSlackPayload(t *testing.T) {
    20  	sc := slackConvertor{}
    21  
    22  	t.Run("Create", func(t *testing.T) {
    23  		p := createTestPayload()
    24  
    25  		pl, err := sc.Create(p)
    26  		require.NoError(t, err)
    27  
    28  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1", pl.Text)
    29  	})
    30  
    31  	t.Run("Delete", func(t *testing.T) {
    32  		p := deleteTestPayload()
    33  
    34  		pl, err := sc.Delete(p)
    35  		require.NoError(t, err)
    36  
    37  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:test] branch deleted by user1", pl.Text)
    38  	})
    39  
    40  	t.Run("Fork", func(t *testing.T) {
    41  		p := forkTestPayload()
    42  
    43  		pl, err := sc.Fork(p)
    44  		require.NoError(t, err)
    45  
    46  		assert.Equal(t, "<http://localhost:3000/test/repo2|test/repo2> is forked to <http://localhost:3000/test/repo|test/repo>", pl.Text)
    47  	})
    48  
    49  	t.Run("Push", func(t *testing.T) {
    50  		p := pushTestPayload()
    51  
    52  		pl, err := sc.Push(p)
    53  		require.NoError(t, err)
    54  
    55  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1", pl.Text)
    56  	})
    57  
    58  	t.Run("Issue", func(t *testing.T) {
    59  		p := issueTestPayload()
    60  
    61  		p.Action = api.HookIssueOpened
    62  		pl, err := sc.Issue(p)
    63  		require.NoError(t, err)
    64  
    65  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Issue opened: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>", pl.Text)
    66  
    67  		p.Action = api.HookIssueClosed
    68  		pl, err = sc.Issue(p)
    69  		require.NoError(t, err)
    70  
    71  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Issue closed: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>", pl.Text)
    72  	})
    73  
    74  	t.Run("IssueComment", func(t *testing.T) {
    75  		p := issueCommentTestPayload()
    76  
    77  		pl, err := sc.IssueComment(p)
    78  		require.NoError(t, err)
    79  
    80  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New comment on issue <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>", pl.Text)
    81  	})
    82  
    83  	t.Run("PullRequest", func(t *testing.T) {
    84  		p := pullRequestTestPayload()
    85  
    86  		pl, err := sc.PullRequest(p)
    87  		require.NoError(t, err)
    88  
    89  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Pull request opened: <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>", pl.Text)
    90  	})
    91  
    92  	t.Run("PullRequestComment", func(t *testing.T) {
    93  		p := pullRequestCommentTestPayload()
    94  
    95  		pl, err := sc.IssueComment(p)
    96  		require.NoError(t, err)
    97  
    98  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New comment on pull request <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>", pl.Text)
    99  	})
   100  
   101  	t.Run("Review", func(t *testing.T) {
   102  		p := pullRequestTestPayload()
   103  		p.Action = api.HookIssueReviewed
   104  
   105  		pl, err := sc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
   106  		require.NoError(t, err)
   107  
   108  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by <https://try.gitea.io/user1|user1>", pl.Text)
   109  	})
   110  
   111  	t.Run("Repository", func(t *testing.T) {
   112  		p := repositoryTestPayload()
   113  
   114  		pl, err := sc.Repository(p)
   115  		require.NoError(t, err)
   116  
   117  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.Text)
   118  	})
   119  
   120  	t.Run("Package", func(t *testing.T) {
   121  		p := packageTestPayload()
   122  
   123  		pl, err := sc.Package(p)
   124  		require.NoError(t, err)
   125  
   126  		assert.Equal(t, "Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by <https://try.gitea.io/user1|user1>", pl.Text)
   127  	})
   128  
   129  	t.Run("Wiki", func(t *testing.T) {
   130  		p := wikiTestPayload()
   131  
   132  		p.Action = api.HookWikiCreated
   133  		pl, err := sc.Wiki(p)
   134  		require.NoError(t, err)
   135  
   136  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New wiki page '<http://localhost:3000/test/repo/wiki/index|index>' (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.Text)
   137  
   138  		p.Action = api.HookWikiEdited
   139  		pl, err = sc.Wiki(p)
   140  		require.NoError(t, err)
   141  
   142  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' edited (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.Text)
   143  
   144  		p.Action = api.HookWikiDeleted
   145  		pl, err = sc.Wiki(p)
   146  		require.NoError(t, err)
   147  
   148  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' deleted by <https://try.gitea.io/user1|user1>", pl.Text)
   149  	})
   150  
   151  	t.Run("Release", func(t *testing.T) {
   152  		p := pullReleaseTestPayload()
   153  
   154  		pl, err := sc.Release(p)
   155  		require.NoError(t, err)
   156  
   157  		assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Release created: <http://localhost:3000/test/repo/releases/tag/v1.0|v1.0> by <https://try.gitea.io/user1|user1>", pl.Text)
   158  	})
   159  }
   160  
   161  func TestSlackJSONPayload(t *testing.T) {
   162  	p := pushTestPayload()
   163  	data, err := p.JSONPayload()
   164  	require.NoError(t, err)
   165  
   166  	hook := &webhook_model.Webhook{
   167  		RepoID:     3,
   168  		IsActive:   true,
   169  		Type:       webhook_module.SLACK,
   170  		URL:        "https://slack.example.com/",
   171  		Meta:       `{}`,
   172  		HTTPMethod: "POST",
   173  	}
   174  	task := &webhook_model.HookTask{
   175  		HookID:         hook.ID,
   176  		EventType:      webhook_module.HookEventPush,
   177  		PayloadContent: string(data),
   178  		PayloadVersion: 2,
   179  	}
   180  
   181  	req, reqBody, err := newSlackRequest(context.Background(), hook, task)
   182  	require.NotNil(t, req)
   183  	require.NotNil(t, reqBody)
   184  	require.NoError(t, err)
   185  
   186  	assert.Equal(t, "POST", req.Method)
   187  	assert.Equal(t, "https://slack.example.com/", req.URL.String())
   188  	assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
   189  	assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
   190  	var body SlackPayload
   191  	err = json.NewDecoder(req.Body).Decode(&body)
   192  	assert.NoError(t, err)
   193  	assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1", body.Text)
   194  }
   195  
   196  func TestIsValidSlackChannel(t *testing.T) {
   197  	tt := []struct {
   198  		channelName string
   199  		expected    bool
   200  	}{
   201  		{"gitea", true},
   202  		{"#gitea", true},
   203  		{"  ", false},
   204  		{"#", false},
   205  		{" #", false},
   206  		{"gitea   ", false},
   207  		{"  gitea", false},
   208  	}
   209  
   210  	for _, v := range tt {
   211  		assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName))
   212  	}
   213  }