|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package webhook |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models" |
| 11 | + api "code.gitea.io/gitea/modules/structs" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestFeishuPayload(t *testing.T) { |
| 18 | + t.Run("Create", func(t *testing.T) { |
| 19 | + p := createTestPayload() |
| 20 | + |
| 21 | + d := new(FeishuPayload) |
| 22 | + pl, err := d.Create(p) |
| 23 | + require.NoError(t, err) |
| 24 | + require.NotNil(t, pl) |
| 25 | + require.IsType(t, &FeishuPayload{}, pl) |
| 26 | + |
| 27 | + assert.Equal(t, `[test/repo] branch test created`, pl.(*FeishuPayload).Content.Text) |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("Delete", func(t *testing.T) { |
| 31 | + p := deleteTestPayload() |
| 32 | + |
| 33 | + d := new(FeishuPayload) |
| 34 | + pl, err := d.Delete(p) |
| 35 | + require.NoError(t, err) |
| 36 | + require.NotNil(t, pl) |
| 37 | + require.IsType(t, &FeishuPayload{}, pl) |
| 38 | + |
| 39 | + assert.Equal(t, `[test/repo] branch test deleted`, pl.(*FeishuPayload).Content.Text) |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("Fork", func(t *testing.T) { |
| 43 | + p := forkTestPayload() |
| 44 | + |
| 45 | + d := new(FeishuPayload) |
| 46 | + pl, err := d.Fork(p) |
| 47 | + require.NoError(t, err) |
| 48 | + require.NotNil(t, pl) |
| 49 | + require.IsType(t, &FeishuPayload{}, pl) |
| 50 | + |
| 51 | + assert.Equal(t, `test/repo2 is forked to test/repo`, pl.(*FeishuPayload).Content.Text) |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("Push", func(t *testing.T) { |
| 55 | + p := pushTestPayload() |
| 56 | + |
| 57 | + d := new(FeishuPayload) |
| 58 | + pl, err := d.Push(p) |
| 59 | + require.NoError(t, err) |
| 60 | + require.NotNil(t, pl) |
| 61 | + require.IsType(t, &FeishuPayload{}, pl) |
| 62 | + |
| 63 | + assert.Equal(t, "[test/repo:test] \r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1\r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1", pl.(*FeishuPayload).Content.Text) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("Issue", func(t *testing.T) { |
| 67 | + p := issueTestPayload() |
| 68 | + |
| 69 | + d := new(FeishuPayload) |
| 70 | + p.Action = api.HookIssueOpened |
| 71 | + pl, err := d.Issue(p) |
| 72 | + require.NoError(t, err) |
| 73 | + require.NotNil(t, pl) |
| 74 | + require.IsType(t, &FeishuPayload{}, pl) |
| 75 | + |
| 76 | + assert.Equal(t, "#2 crash\r\n[test/repo] Issue opened: #2 crash by user1\r\n\r\nissue body", pl.(*FeishuPayload).Content.Text) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("IssueComment", func(t *testing.T) { |
| 80 | + p := issueCommentTestPayload() |
| 81 | + |
| 82 | + d := new(FeishuPayload) |
| 83 | + pl, err := d.IssueComment(p) |
| 84 | + require.NoError(t, err) |
| 85 | + require.NotNil(t, pl) |
| 86 | + require.IsType(t, &FeishuPayload{}, pl) |
| 87 | + |
| 88 | + assert.Equal(t, "#2 crash\r\n[test/repo] New comment on issue #2 crash by user1\r\n\r\nmore info needed", pl.(*FeishuPayload).Content.Text) |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("PullRequest", func(t *testing.T) { |
| 92 | + p := pullRequestTestPayload() |
| 93 | + |
| 94 | + d := new(FeishuPayload) |
| 95 | + pl, err := d.PullRequest(p) |
| 96 | + require.NoError(t, err) |
| 97 | + require.NotNil(t, pl) |
| 98 | + require.IsType(t, &FeishuPayload{}, pl) |
| 99 | + |
| 100 | + assert.Equal(t, "#12 Fix bug\r\n[test/repo] Pull request opened: #12 Fix bug by user1\r\n\r\nfixes bug #2", pl.(*FeishuPayload).Content.Text) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("Review", func(t *testing.T) { |
| 104 | + p := pullRequestTestPayload() |
| 105 | + p.Action = api.HookIssueReviewed |
| 106 | + |
| 107 | + d := new(FeishuPayload) |
| 108 | + pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) |
| 109 | + require.NoError(t, err) |
| 110 | + require.NotNil(t, pl) |
| 111 | + require.IsType(t, &FeishuPayload{}, pl) |
| 112 | + |
| 113 | + assert.Equal(t, "[test/repo] Pull request review approved : #12 Fix bug\r\n\r\ngood job", pl.(*FeishuPayload).Content.Text) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("Repository", func(t *testing.T) { |
| 117 | + p := repositoryTestPayload() |
| 118 | + |
| 119 | + d := new(FeishuPayload) |
| 120 | + pl, err := d.Repository(p) |
| 121 | + require.NoError(t, err) |
| 122 | + require.NotNil(t, pl) |
| 123 | + require.IsType(t, &FeishuPayload{}, pl) |
| 124 | + |
| 125 | + assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text) |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("Release", func(t *testing.T) { |
| 129 | + p := pullReleaseTestPayload() |
| 130 | + |
| 131 | + d := new(FeishuPayload) |
| 132 | + pl, err := d.Release(p) |
| 133 | + require.NoError(t, err) |
| 134 | + require.NotNil(t, pl) |
| 135 | + require.IsType(t, &FeishuPayload{}, pl) |
| 136 | + |
| 137 | + assert.Equal(t, "[test/repo] Release created: v1.0 by user1", pl.(*FeishuPayload).Content.Text) |
| 138 | + }) |
| 139 | +} |
0 commit comments