code.gitea.io/gitea@v1.22.3/services/webhook/telegram_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 TestTelegramPayload(t *testing.T) {
    20  	tc := telegramConvertor{}
    21  
    22  	t.Run("Correct webhook params", func(t *testing.T) {
    23  		p := createTelegramPayload("testMsg ")
    24  
    25  		assert.Equal(t, "HTML", p.ParseMode)
    26  		assert.Equal(t, true, p.DisableWebPreview)
    27  		assert.Equal(t, "testMsg", p.Message)
    28  	})
    29  
    30  	t.Run("Create", func(t *testing.T) {
    31  		p := createTestPayload()
    32  
    33  		pl, err := tc.Create(p)
    34  		require.NoError(t, err)
    35  
    36  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> created`, pl.Message)
    37  	})
    38  
    39  	t.Run("Delete", func(t *testing.T) {
    40  		p := deleteTestPayload()
    41  
    42  		pl, err := tc.Delete(p)
    43  		require.NoError(t, err)
    44  
    45  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> deleted`, pl.Message)
    46  	})
    47  
    48  	t.Run("Fork", func(t *testing.T) {
    49  		p := forkTestPayload()
    50  
    51  		pl, err := tc.Fork(p)
    52  		require.NoError(t, err)
    53  
    54  		assert.Equal(t, `test/repo2 is forked to <a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>`, pl.Message)
    55  	})
    56  
    57  	t.Run("Push", func(t *testing.T) {
    58  		p := pushTestPayload()
    59  
    60  		pl, err := tc.Push(p)
    61  		require.NoError(t, err)
    62  
    63  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
    64  [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
    65  [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, pl.Message)
    66  	})
    67  
    68  	t.Run("Issue", func(t *testing.T) {
    69  		p := issueTestPayload()
    70  
    71  		p.Action = api.HookIssueOpened
    72  		pl, err := tc.Issue(p)
    73  		require.NoError(t, err)
    74  
    75  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
    76  
    77  issue body`, pl.Message)
    78  
    79  		p.Action = api.HookIssueClosed
    80  		pl, err = tc.Issue(p)
    81  		require.NoError(t, err)
    82  
    83  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
    84  	})
    85  
    86  	t.Run("IssueComment", func(t *testing.T) {
    87  		p := issueCommentTestPayload()
    88  
    89  		pl, err := tc.IssueComment(p)
    90  		require.NoError(t, err)
    91  
    92  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
    93  more info needed`, pl.Message)
    94  	})
    95  
    96  	t.Run("PullRequest", func(t *testing.T) {
    97  		p := pullRequestTestPayload()
    98  
    99  		pl, err := tc.PullRequest(p)
   100  		require.NoError(t, err)
   101  
   102  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
   103  fixes bug #2`, pl.Message)
   104  	})
   105  
   106  	t.Run("PullRequestComment", func(t *testing.T) {
   107  		p := pullRequestCommentTestPayload()
   108  
   109  		pl, err := tc.IssueComment(p)
   110  		require.NoError(t, err)
   111  
   112  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
   113  changes requested`, pl.Message)
   114  	})
   115  
   116  	t.Run("Review", func(t *testing.T) {
   117  		p := pullRequestTestPayload()
   118  		p.Action = api.HookIssueReviewed
   119  
   120  		pl, err := tc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
   121  		require.NoError(t, err)
   122  
   123  		assert.Equal(t, `[test/repo] Pull request review approved: #12 Fix bug
   124  good job`, pl.Message)
   125  	})
   126  
   127  	t.Run("Repository", func(t *testing.T) {
   128  		p := repositoryTestPayload()
   129  
   130  		pl, err := tc.Repository(p)
   131  		require.NoError(t, err)
   132  
   133  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Repository created`, pl.Message)
   134  	})
   135  
   136  	t.Run("Package", func(t *testing.T) {
   137  		p := packageTestPayload()
   138  
   139  		pl, err := tc.Package(p)
   140  		require.NoError(t, err)
   141  
   142  		assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest" rel="nofollow">GiteaContainer:latest</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
   143  	})
   144  
   145  	t.Run("Wiki", func(t *testing.T) {
   146  		p := wikiTestPayload()
   147  
   148  		p.Action = api.HookWikiCreated
   149  		pl, err := tc.Wiki(p)
   150  		require.NoError(t, err)
   151  
   152  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
   153  
   154  		p.Action = api.HookWikiEdited
   155  		pl, err = tc.Wiki(p)
   156  		require.NoError(t, err)
   157  
   158  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; edited (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
   159  
   160  		p.Action = api.HookWikiDeleted
   161  		pl, err = tc.Wiki(p)
   162  		require.NoError(t, err)
   163  
   164  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; deleted by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
   165  	})
   166  
   167  	t.Run("Release", func(t *testing.T) {
   168  		p := pullReleaseTestPayload()
   169  
   170  		pl, err := tc.Release(p)
   171  		require.NoError(t, err)
   172  
   173  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0" rel="nofollow">v1.0</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
   174  	})
   175  }
   176  
   177  func TestTelegramJSONPayload(t *testing.T) {
   178  	p := pushTestPayload()
   179  	data, err := p.JSONPayload()
   180  	require.NoError(t, err)
   181  
   182  	hook := &webhook_model.Webhook{
   183  		RepoID:     3,
   184  		IsActive:   true,
   185  		Type:       webhook_module.TELEGRAM,
   186  		URL:        "https://telegram.example.com/",
   187  		Meta:       ``,
   188  		HTTPMethod: "POST",
   189  	}
   190  	task := &webhook_model.HookTask{
   191  		HookID:         hook.ID,
   192  		EventType:      webhook_module.HookEventPush,
   193  		PayloadContent: string(data),
   194  		PayloadVersion: 2,
   195  	}
   196  
   197  	req, reqBody, err := newTelegramRequest(context.Background(), hook, task)
   198  	require.NotNil(t, req)
   199  	require.NotNil(t, reqBody)
   200  	require.NoError(t, err)
   201  
   202  	assert.Equal(t, "POST", req.Method)
   203  	assert.Equal(t, "https://telegram.example.com/", req.URL.String())
   204  	assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
   205  	assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
   206  	var body TelegramPayload
   207  	err = json.NewDecoder(req.Body).Decode(&body)
   208  	assert.NoError(t, err)
   209  	assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
   210  [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
   211  [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, body.Message)
   212  }