mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Add tests for all webhooks (#16214)
* Added tests for MS Teams. * Added tests for Dingtalk. * Added tests for Telegram. * Added tests for Feishu. * Added tests for Discord. * Added tests for closed issue and pullrequest comment. * Added tests for Matrix. * Trim all spaces. * Added tests for Slack. * Added JSONPayload tests. * Added general tests. * Replaced duplicated code. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		| @@ -14,71 +14,173 @@ import ( | ||||
| 	"github.com/stretchr/testify/require" | ||||
| ) | ||||
|  | ||||
| func TestMatrixIssuesPayloadOpened(t *testing.T) { | ||||
| 	p := issueTestPayload() | ||||
| 	m := new(MatrixPayloadUnsafe) | ||||
| func TestMatrixPayload(t *testing.T) { | ||||
| 	t.Run("Create", func(t *testing.T) { | ||||
| 		p := createTestPayload() | ||||
|  | ||||
| 	p.Action = api.HookIssueOpened | ||||
| 	pl, err := m.Issue(p) | ||||
| 	require.NoError(t, err) | ||||
| 	require.NotNil(t, pl) | ||||
| 	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.(*MatrixPayloadUnsafe).Body) | ||||
| 	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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Create(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 	p.Action = api.HookIssueClosed | ||||
| 	pl, err = m.Issue(p) | ||||
| 	require.NoError(t, err) | ||||
| 	require.NotNil(t, pl) | ||||
| 	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.(*MatrixPayloadUnsafe).Body) | ||||
| 	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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo):[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Delete", func(t *testing.T) { | ||||
| 		p := deleteTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Delete(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo):test] branch deleted by user1", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>:test] branch deleted by user1`, pl.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Fork", func(t *testing.T) { | ||||
| 		p := forkTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Fork(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		assert.Equal(t, "[test/repo2](http://localhost:3000/test/repo2) is forked to [test/repo](http://localhost:3000/test/repo)", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Push", func(t *testing.T) { | ||||
| 		p := pushTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Push(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Issue", func(t *testing.T) { | ||||
| 		p := issueTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		p.Action = api.HookIssueOpened | ||||
| 		pl, err := d.Issue(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
|  | ||||
| 		p.Action = api.HookIssueClosed | ||||
| 		pl, err = d.Issue(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("IssueComment", func(t *testing.T) { | ||||
| 		p := issueCommentTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.IssueComment(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("PullRequest", func(t *testing.T) { | ||||
| 		p := pullRequestTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.PullRequest(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("PullRequestComment", func(t *testing.T) { | ||||
| 		p := pullRequestCommentTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.IssueComment(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Review", func(t *testing.T) { | ||||
| 		p := pullRequestTestPayload() | ||||
| 		p.Action = api.HookIssueReviewed | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		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.(*MatrixPayloadUnsafe).Body) | ||||
| 		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Repository", func(t *testing.T) { | ||||
| 		p := repositoryTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Repository(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		assert.Equal(t, `[[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1)`, pl.(*MatrixPayloadUnsafe).Body) | ||||
| 		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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Release", func(t *testing.T) { | ||||
| 		p := pullReleaseTestPayload() | ||||
|  | ||||
| 		d := new(MatrixPayloadUnsafe) | ||||
| 		pl, err := d.Release(p) | ||||
| 		require.NoError(t, err) | ||||
| 		require.NotNil(t, pl) | ||||
| 		require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 		assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/src/v1.0) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 		assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/src/v1.0">v1.0</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func TestMatrixIssueCommentPayload(t *testing.T) { | ||||
| 	p := issueCommentTestPayload() | ||||
| 	m := new(MatrixPayloadUnsafe) | ||||
| func TestMatrixJSONPayload(t *testing.T) { | ||||
| 	p := pushTestPayload() | ||||
|  | ||||
| 	pl, err := m.IssueComment(p) | ||||
| 	pl, err := new(MatrixPayloadUnsafe).Push(p) | ||||
| 	require.NoError(t, err) | ||||
| 	require.NotNil(t, pl) | ||||
| 	require.IsType(t, &MatrixPayloadUnsafe{}, pl) | ||||
|  | ||||
| 	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.(*MatrixPayloadUnsafe).Body) | ||||
| 	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.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| } | ||||
|  | ||||
| func TestMatrixPullRequestCommentPayload(t *testing.T) { | ||||
| 	p := pullRequestCommentTestPayload() | ||||
| 	m := new(MatrixPayloadUnsafe) | ||||
|  | ||||
| 	pl, err := m.IssueComment(p) | ||||
| 	json, err := pl.JSONPayload() | ||||
| 	require.NoError(t, err) | ||||
| 	require.NotNil(t, pl) | ||||
|  | ||||
| 	assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#2 Fix bug](http://localhost:3000/test/repo/pulls/2) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 	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/2\">#2 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>", pl.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| } | ||||
|  | ||||
| func TestMatrixReleasePayload(t *testing.T) { | ||||
| 	p := pullReleaseTestPayload() | ||||
| 	m := new(MatrixPayloadUnsafe) | ||||
|  | ||||
| 	pl, err := m.Release(p) | ||||
| 	require.NoError(t, err) | ||||
| 	require.NotNil(t, pl) | ||||
|  | ||||
| 	assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/src/v1.0) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 	assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] Release created: <a href=\"http://localhost:3000/test/repo/src/v1.0\">v1.0</a> by <a href=\"https://try.gitea.io/user1\">user1</a>", pl.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| } | ||||
|  | ||||
| func TestMatrixPullRequestPayload(t *testing.T) { | ||||
| 	p := pullRequestTestPayload() | ||||
| 	m := new(MatrixPayloadUnsafe) | ||||
|  | ||||
| 	pl, err := m.PullRequest(p) | ||||
| 	require.NoError(t, err) | ||||
| 	require.NotNil(t, pl) | ||||
|  | ||||
| 	assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#2 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) | ||||
| 	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\">#2 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>", pl.(*MatrixPayloadUnsafe).FormattedBody) | ||||
| 	assert.NotEmpty(t, json) | ||||
| } | ||||
|  | ||||
| func TestMatrixHookRequest(t *testing.T) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user