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

     1  // Copyright 2020 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 TestMatrixPayload(t *testing.T) {
    20  	mc := matrixConvertor{
    21  		MsgType: "m.text",
    22  	}
    23  
    24  	t.Run("Create", func(t *testing.T) {
    25  		p := createTestPayload()
    26  
    27  		pl, err := mc.Create(p)
    28  		require.NoError(t, err)
    29  		require.NotNil(t, pl)
    30  
    31  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo):[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1", pl.Body)
    32  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>:<a href="http://localhost:3000/test/repo/src/branch/test">test</a>] branch created by user1`, pl.FormattedBody)
    33  	})
    34  
    35  	t.Run("Delete", func(t *testing.T) {
    36  		p := deleteTestPayload()
    37  
    38  		pl, err := mc.Delete(p)
    39  		require.NoError(t, err)
    40  		require.NotNil(t, pl)
    41  
    42  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo):test] branch deleted by user1", pl.Body)
    43  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>:test] branch deleted by user1`, pl.FormattedBody)
    44  	})
    45  
    46  	t.Run("Fork", func(t *testing.T) {
    47  		p := forkTestPayload()
    48  
    49  		pl, err := mc.Fork(p)
    50  		require.NoError(t, err)
    51  		require.NotNil(t, pl)
    52  
    53  		assert.Equal(t, "[test/repo2](http://localhost:3000/test/repo2) is forked to [test/repo](http://localhost:3000/test/repo)", pl.Body)
    54  		assert.Equal(t, `<a href="http://localhost:3000/test/repo2">test/repo2</a> is forked to <a href="http://localhost:3000/test/repo">test/repo</a>`, pl.FormattedBody)
    55  	})
    56  
    57  	t.Run("Push", func(t *testing.T) {
    58  		p := pushTestPayload()
    59  
    60  		pl, err := mc.Push(p)
    61  		require.NoError(t, err)
    62  		require.NotNil(t, pl)
    63  
    64  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1", pl.Body)
    65  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] user1 pushed 2 commits to <a href="http://localhost:3000/test/repo/src/branch/test">test</a>:<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1`, pl.FormattedBody)
    66  	})
    67  
    68  	t.Run("Issue", func(t *testing.T) {
    69  		p := issueTestPayload()
    70  
    71  		p.Action = api.HookIssueOpened
    72  		pl, err := mc.Issue(p)
    73  		require.NoError(t, err)
    74  		require.NotNil(t, pl)
    75  
    76  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Issue opened: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)", pl.Body)
    77  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
    78  
    79  		p.Action = api.HookIssueClosed
    80  		pl, err = mc.Issue(p)
    81  		require.NoError(t, err)
    82  		require.NotNil(t, pl)
    83  
    84  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Issue closed: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)", pl.Body)
    85  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
    86  	})
    87  
    88  	t.Run("IssueComment", func(t *testing.T) {
    89  		p := issueCommentTestPayload()
    90  
    91  		pl, err := mc.IssueComment(p)
    92  		require.NoError(t, err)
    93  		require.NotNil(t, pl)
    94  
    95  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New comment on issue [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)", pl.Body)
    96  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
    97  	})
    98  
    99  	t.Run("PullRequest", func(t *testing.T) {
   100  		p := pullRequestTestPayload()
   101  
   102  		pl, err := mc.PullRequest(p)
   103  		require.NoError(t, err)
   104  		require.NotNil(t, pl)
   105  
   106  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.Body)
   107  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   108  	})
   109  
   110  	t.Run("PullRequestComment", func(t *testing.T) {
   111  		p := pullRequestCommentTestPayload()
   112  
   113  		pl, err := mc.IssueComment(p)
   114  		require.NoError(t, err)
   115  		require.NotNil(t, pl)
   116  
   117  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.Body)
   118  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   119  	})
   120  
   121  	t.Run("Review", func(t *testing.T) {
   122  		p := pullRequestTestPayload()
   123  		p.Action = api.HookIssueReviewed
   124  
   125  		pl, err := mc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
   126  		require.NoError(t, err)
   127  		require.NotNil(t, pl)
   128  
   129  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.Body)
   130  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request review approved: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   131  	})
   132  
   133  	t.Run("Repository", func(t *testing.T) {
   134  		p := repositoryTestPayload()
   135  
   136  		pl, err := mc.Repository(p)
   137  		require.NoError(t, err)
   138  		require.NotNil(t, pl)
   139  
   140  		assert.Equal(t, `[[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1)`, pl.Body)
   141  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   142  	})
   143  
   144  	t.Run("Package", func(t *testing.T) {
   145  		p := packageTestPayload()
   146  
   147  		pl, err := mc.Package(p)
   148  		require.NoError(t, err)
   149  		require.NotNil(t, pl)
   150  
   151  		assert.Equal(t, `[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1)`, pl.Body)
   152  		assert.Equal(t, `[<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   153  	})
   154  
   155  	t.Run("Wiki", func(t *testing.T) {
   156  		p := wikiTestPayload()
   157  
   158  		p.Action = api.HookWikiCreated
   159  		pl, err := mc.Wiki(p)
   160  		require.NoError(t, err)
   161  		require.NotNil(t, pl)
   162  
   163  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New wiki page '[index](http://localhost:3000/test/repo/wiki/index)' (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.Body)
   164  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   165  
   166  		p.Action = api.HookWikiEdited
   167  		pl, err = mc.Wiki(p)
   168  		require.NoError(t, err)
   169  		require.NotNil(t, pl)
   170  
   171  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' edited (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.Body)
   172  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   173  
   174  		p.Action = api.HookWikiDeleted
   175  		pl, err = mc.Wiki(p)
   176  		require.NoError(t, err)
   177  		require.NotNil(t, pl)
   178  
   179  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' deleted by [user1](https://try.gitea.io/user1)", pl.Body)
   180  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   181  	})
   182  
   183  	t.Run("Release", func(t *testing.T) {
   184  		p := pullReleaseTestPayload()
   185  
   186  		pl, err := mc.Release(p)
   187  		require.NoError(t, err)
   188  		require.NotNil(t, pl)
   189  
   190  		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/releases/tag/v1.0) by [user1](https://try.gitea.io/user1)", pl.Body)
   191  		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0">v1.0</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
   192  	})
   193  }
   194  
   195  func TestMatrixJSONPayload(t *testing.T) {
   196  	p := pushTestPayload()
   197  	data, err := p.JSONPayload()
   198  	require.NoError(t, err)
   199  
   200  	hook := &webhook_model.Webhook{
   201  		RepoID:   3,
   202  		IsActive: true,
   203  		Type:     webhook_module.MATRIX,
   204  		URL:      "https://matrix.example.com/_matrix/client/r0/rooms/ROOM_ID/send/m.room.message",
   205  		Meta:     `{"message_type":0}`, // text
   206  	}
   207  	task := &webhook_model.HookTask{
   208  		HookID:         hook.ID,
   209  		EventType:      webhook_module.HookEventPush,
   210  		PayloadContent: string(data),
   211  		PayloadVersion: 2,
   212  	}
   213  
   214  	req, reqBody, err := newMatrixRequest(context.Background(), hook, task)
   215  	require.NotNil(t, req)
   216  	require.NotNil(t, reqBody)
   217  	require.NoError(t, err)
   218  
   219  	assert.Equal(t, "PUT", req.Method)
   220  	assert.Equal(t, "/_matrix/client/r0/rooms/ROOM_ID/send/m.room.message/6db5dc1e282529a8c162c7fe93dd2667494eeb51", req.URL.Path)
   221  	assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
   222  	assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
   223  	var body MatrixPayload
   224  	err = json.NewDecoder(req.Body).Decode(&body)
   225  	assert.NoError(t, err)
   226  	assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1", body.Body)
   227  }
   228  
   229  func Test_getTxnID(t *testing.T) {
   230  	type args struct {
   231  		payload []byte
   232  	}
   233  	tests := []struct {
   234  		name    string
   235  		args    args
   236  		want    string
   237  		wantErr bool
   238  	}{
   239  		{
   240  			name:    "dummy payload",
   241  			args:    args{payload: []byte("Hello World")},
   242  			want:    "0a4d55a8d778e5022fab701977c5d840bbc486d0",
   243  			wantErr: false,
   244  		},
   245  	}
   246  	for _, tt := range tests {
   247  		t.Run(tt.name, func(t *testing.T) {
   248  			got, err := getMatrixTxnID(tt.args.payload)
   249  			if (err != nil) != tt.wantErr {
   250  				t.Errorf("getMatrixTxnID() error = %v, wantErr %v", err, tt.wantErr)
   251  				return
   252  			}
   253  			assert.Equal(t, tt.want, got)
   254  		})
   255  	}
   256  }