Skip to content

Commit 900ec67

Browse files
committed
Added tests for Matrix.
1 parent 4aae581 commit 900ec67

File tree

1 file changed

+140
-51
lines changed

1 file changed

+140
-51
lines changed

services/webhook/matrix_test.go

Lines changed: 140 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,160 @@ import (
1414
"github.com/stretchr/testify/require"
1515
)
1616

17-
func TestMatrixIssuesPayloadOpened(t *testing.T) {
18-
p := issueTestPayload()
19-
m := new(MatrixPayloadUnsafe)
17+
func TestMatrixPayload(t *testing.T) {
18+
t.Run("Create", func(t *testing.T) {
19+
p := createTestPayload()
2020

21-
p.Action = api.HookIssueOpened
22-
pl, err := m.Issue(p)
23-
require.NoError(t, err)
24-
require.NotNil(t, pl)
25-
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)
26-
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)
21+
d := new(MatrixPayloadUnsafe)
22+
pl, err := d.Create(p)
23+
require.NoError(t, err)
24+
require.NotNil(t, pl)
25+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
2726

28-
p.Action = api.HookIssueClosed
29-
pl, err = m.Issue(p)
30-
require.NoError(t, err)
31-
require.NotNil(t, pl)
32-
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)
33-
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)
34-
}
27+
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)
28+
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)
29+
})
3530

36-
func TestMatrixIssueCommentPayload(t *testing.T) {
37-
p := issueCommentTestPayload()
38-
m := new(MatrixPayloadUnsafe)
31+
t.Run("Delete", func(t *testing.T) {
32+
p := deleteTestPayload()
3933

40-
pl, err := m.IssueComment(p)
41-
require.NoError(t, err)
42-
require.NotNil(t, pl)
34+
d := new(MatrixPayloadUnsafe)
35+
pl, err := d.Delete(p)
36+
require.NoError(t, err)
37+
require.NotNil(t, pl)
38+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
4339

44-
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)
45-
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)
46-
}
40+
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo):test] branch deleted by user1", pl.(*MatrixPayloadUnsafe).Body)
41+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>:test] branch deleted by user1`, pl.(*MatrixPayloadUnsafe).FormattedBody)
42+
})
4743

48-
func TestMatrixPullRequestCommentPayload(t *testing.T) {
49-
p := pullRequestCommentTestPayload()
50-
m := new(MatrixPayloadUnsafe)
44+
t.Run("Fork", func(t *testing.T) {
45+
p := forkTestPayload()
5146

52-
pl, err := m.IssueComment(p)
53-
require.NoError(t, err)
54-
require.NotNil(t, pl)
47+
d := new(MatrixPayloadUnsafe)
48+
pl, err := d.Fork(p)
49+
require.NoError(t, err)
50+
require.NotNil(t, pl)
51+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
5552

56-
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)
57-
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)
58-
}
53+
assert.Equal(t, "[test/repo2](http://localhost:3000/test/repo2) is forked to [test/repo](http://localhost:3000/test/repo)", pl.(*MatrixPayloadUnsafe).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.(*MatrixPayloadUnsafe).FormattedBody)
55+
})
5956

60-
func TestMatrixReleasePayload(t *testing.T) {
61-
p := pullReleaseTestPayload()
62-
m := new(MatrixPayloadUnsafe)
57+
t.Run("Push", func(t *testing.T) {
58+
p := pushTestPayload()
6359

64-
pl, err := m.Release(p)
65-
require.NoError(t, err)
66-
require.NotNil(t, pl)
60+
d := new(MatrixPayloadUnsafe)
61+
pl, err := d.Push(p)
62+
require.NoError(t, err)
63+
require.NotNil(t, pl)
64+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
6765

68-
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)
69-
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)
70-
}
66+
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)
67+
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)
68+
})
7169

72-
func TestMatrixPullRequestPayload(t *testing.T) {
73-
p := pullRequestTestPayload()
74-
m := new(MatrixPayloadUnsafe)
70+
t.Run("Issue", func(t *testing.T) {
71+
p := issueTestPayload()
7572

76-
pl, err := m.PullRequest(p)
77-
require.NoError(t, err)
78-
require.NotNil(t, pl)
73+
d := new(MatrixPayloadUnsafe)
74+
p.Action = api.HookIssueOpened
75+
pl, err := d.Issue(p)
76+
require.NoError(t, err)
77+
require.NotNil(t, pl)
78+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
79+
80+
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)
81+
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)
82+
83+
p.Action = api.HookIssueClosed
84+
pl, err = d.Issue(p)
85+
require.NoError(t, err)
86+
require.NotNil(t, pl)
87+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
88+
89+
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)
90+
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)
91+
})
92+
93+
t.Run("IssueComment", func(t *testing.T) {
94+
p := issueCommentTestPayload()
95+
96+
d := new(MatrixPayloadUnsafe)
97+
pl, err := d.IssueComment(p)
98+
require.NoError(t, err)
99+
require.NotNil(t, pl)
100+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
101+
102+
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)
103+
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)
104+
})
105+
106+
t.Run("PullRequest", func(t *testing.T) {
107+
p := pullRequestTestPayload()
108+
109+
d := new(MatrixPayloadUnsafe)
110+
pl, err := d.PullRequest(p)
111+
require.NoError(t, err)
112+
require.NotNil(t, pl)
113+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
114+
115+
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)
116+
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)
117+
})
118+
119+
t.Run("PullRequestComment", func(t *testing.T) {
120+
p := pullRequestCommentTestPayload()
121+
122+
d := new(MatrixPayloadUnsafe)
123+
pl, err := d.IssueComment(p)
124+
require.NoError(t, err)
125+
require.NotNil(t, pl)
126+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
127+
128+
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)
129+
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)
130+
})
131+
132+
t.Run("Review", func(t *testing.T) {
133+
p := pullRequestTestPayload()
134+
p.Action = api.HookIssueReviewed
135+
136+
d := new(MatrixPayloadUnsafe)
137+
pl, err := d.Review(p, models.HookEventPullRequestReviewApproved)
138+
require.NoError(t, err)
139+
require.NotNil(t, pl)
140+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
141+
142+
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)
143+
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)
144+
})
145+
146+
t.Run("Repository", func(t *testing.T) {
147+
p := repositoryTestPayload()
148+
149+
d := new(MatrixPayloadUnsafe)
150+
pl, err := d.Repository(p)
151+
require.NoError(t, err)
152+
require.NotNil(t, pl)
153+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
154+
155+
assert.Equal(t, `[[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1)`, pl.(*MatrixPayloadUnsafe).Body)
156+
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)
157+
})
158+
159+
t.Run("Release", func(t *testing.T) {
160+
p := pullReleaseTestPayload()
161+
162+
d := new(MatrixPayloadUnsafe)
163+
pl, err := d.Release(p)
164+
require.NoError(t, err)
165+
require.NotNil(t, pl)
166+
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
79167

80-
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)
81-
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)
168+
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)
169+
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)
170+
})
82171
}
83172

84173
func TestMatrixHookRequest(t *testing.T) {

0 commit comments

Comments
 (0)