@@ -7,25 +7,163 @@ package webhook
7
7
import (
8
8
"testing"
9
9
10
+ "code.gitea.io/gitea/models"
10
11
api "code.gitea.io/gitea/modules/structs"
12
+
11
13
"github.com/stretchr/testify/assert"
12
14
"github.com/stretchr/testify/require"
13
15
)
14
16
15
- func TestGetDingTalkIssuesPayload (t * testing.T ) {
16
- p := issueTestPayload ()
17
- d := new (DingtalkPayload )
18
- p .Action = api .HookIssueOpened
19
- pl , err := d .Issue (p )
20
- require .NoError (t , err )
21
- require .NotNil (t , pl )
22
- assert .Equal (t , "#2 crash" , pl .(* DingtalkPayload ).ActionCard .Title )
23
- assert .Equal (t , "[test/repo] Issue opened: #2 crash by user1\r \n \r \n " , pl .(* DingtalkPayload ).ActionCard .Text )
24
-
25
- p .Action = api .HookIssueClosed
26
- pl , err = d .Issue (p )
27
- require .NoError (t , err )
28
- require .NotNil (t , pl )
29
- assert .Equal (t , "#2 crash" , pl .(* DingtalkPayload ).ActionCard .Title )
30
- assert .Equal (t , "[test/repo] Issue closed: #2 crash by user1\r \n \r \n " , pl .(* DingtalkPayload ).ActionCard .Text )
17
+ func TestDingTalkPayload (t * testing.T ) {
18
+ t .Run ("Create" , func (t * testing.T ) {
19
+ p := createTestPayload ()
20
+
21
+ d := new (DingtalkPayload )
22
+ pl , err := d .Create (p )
23
+ require .NoError (t , err )
24
+ require .NotNil (t , pl )
25
+ require .IsType (t , & DingtalkPayload {}, pl )
26
+
27
+ assert .Equal (t , "[test/repo] branch test created" , pl .(* DingtalkPayload ).ActionCard .Text )
28
+ assert .Equal (t , "[test/repo] branch test created" , pl .(* DingtalkPayload ).ActionCard .Title )
29
+ assert .Equal (t , "view ref test" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
30
+ assert .Equal (t , "http://localhost:3000/test/repo/src/test" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
31
+ })
32
+
33
+ t .Run ("Delete" , func (t * testing.T ) {
34
+ p := deleteTestPayload ()
35
+
36
+ d := new (DingtalkPayload )
37
+ pl , err := d .Delete (p )
38
+ require .NoError (t , err )
39
+ require .NotNil (t , pl )
40
+ require .IsType (t , & DingtalkPayload {}, pl )
41
+
42
+ assert .Equal (t , "[test/repo] branch test deleted" , pl .(* DingtalkPayload ).ActionCard .Text )
43
+ assert .Equal (t , "[test/repo] branch test deleted" , pl .(* DingtalkPayload ).ActionCard .Title )
44
+ assert .Equal (t , "view ref test" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
45
+ assert .Equal (t , "http://localhost:3000/test/repo/src/test" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
46
+ })
47
+
48
+ t .Run ("Fork" , func (t * testing.T ) {
49
+ p := forkTestPayload ()
50
+
51
+ d := new (DingtalkPayload )
52
+ pl , err := d .Fork (p )
53
+ require .NoError (t , err )
54
+ require .NotNil (t , pl )
55
+ require .IsType (t , & DingtalkPayload {}, pl )
56
+
57
+ assert .Equal (t , "test/repo2 is forked to test/repo" , pl .(* DingtalkPayload ).ActionCard .Text )
58
+ assert .Equal (t , "test/repo2 is forked to test/repo" , pl .(* DingtalkPayload ).ActionCard .Title )
59
+ assert .Equal (t , "view forked repo test/repo" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
60
+ assert .Equal (t , "http://localhost:3000/test/repo" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
61
+ })
62
+
63
+ t .Run ("Push" , func (t * testing.T ) {
64
+ p := pushTestPayload ()
65
+
66
+ d := new (DingtalkPayload )
67
+ pl , err := d .Push (p )
68
+ require .NoError (t , err )
69
+ require .NotNil (t , pl )
70
+ require .IsType (t , & DingtalkPayload {}, pl )
71
+
72
+ assert .Equal (t , "[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1\n [2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1" , pl .(* DingtalkPayload ).ActionCard .Text )
73
+ assert .Equal (t , "[test/repo:test] 2 new commits" , pl .(* DingtalkPayload ).ActionCard .Title )
74
+ assert .Equal (t , "view commit 2020558...2020558" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
75
+ assert .Equal (t , "http://localhost:3000/test/repo/src/test" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
76
+ })
77
+
78
+ t .Run ("Issue" , func (t * testing.T ) {
79
+ p := issueTestPayload ()
80
+
81
+ d := new (DingtalkPayload )
82
+ p .Action = api .HookIssueOpened
83
+ pl , err := d .Issue (p )
84
+ require .NoError (t , err )
85
+ require .NotNil (t , pl )
86
+ require .IsType (t , & DingtalkPayload {}, pl )
87
+
88
+ assert .Equal (t , "[test/repo] Issue opened: #2 crash by user1\r \n \r \n issue body" , pl .(* DingtalkPayload ).ActionCard .Text )
89
+ assert .Equal (t , "#2 crash" , pl .(* DingtalkPayload ).ActionCard .Title )
90
+ assert .Equal (t , "view issue" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
91
+ assert .Equal (t , "http://localhost:3000/test/repo/issues/2" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
92
+ })
93
+
94
+ t .Run ("IssueComment" , func (t * testing.T ) {
95
+ p := issueCommentTestPayload ()
96
+
97
+ d := new (DingtalkPayload )
98
+ pl , err := d .IssueComment (p )
99
+ require .NoError (t , err )
100
+ require .NotNil (t , pl )
101
+ require .IsType (t , & DingtalkPayload {}, pl )
102
+
103
+ assert .Equal (t , "[test/repo] New comment on issue #2 crash by user1\r \n \r \n more info needed" , pl .(* DingtalkPayload ).ActionCard .Text )
104
+ assert .Equal (t , "#2 crash" , pl .(* DingtalkPayload ).ActionCard .Title )
105
+ assert .Equal (t , "view issue comment" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
106
+ assert .Equal (t , "http://localhost:3000/test/repo/issues/2#issuecomment-4" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
107
+ })
108
+
109
+ t .Run ("PullRequest" , func (t * testing.T ) {
110
+ p := pullRequestTestPayload ()
111
+
112
+ d := new (DingtalkPayload )
113
+ pl , err := d .PullRequest (p )
114
+ require .NoError (t , err )
115
+ require .NotNil (t , pl )
116
+ require .IsType (t , & DingtalkPayload {}, pl )
117
+
118
+ assert .Equal (t , "[test/repo] Pull request opened: #12 Fix bug by user1\r \n \r \n fixes bug #2" , pl .(* DingtalkPayload ).ActionCard .Text )
119
+ assert .Equal (t , "#12 Fix bug" , pl .(* DingtalkPayload ).ActionCard .Title )
120
+ assert .Equal (t , "view pull request" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
121
+ assert .Equal (t , "http://localhost:3000/test/repo/pulls/12" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
122
+ })
123
+
124
+ t .Run ("Review" , func (t * testing.T ) {
125
+ p := pullRequestTestPayload ()
126
+ p .Action = api .HookIssueReviewed
127
+
128
+ d := new (DingtalkPayload )
129
+ pl , err := d .Review (p , models .HookEventPullRequestReviewApproved )
130
+ require .NoError (t , err )
131
+ require .NotNil (t , pl )
132
+ require .IsType (t , & DingtalkPayload {}, pl )
133
+
134
+ assert .Equal (t , "[test/repo] Pull request review approved : #12 Fix bug\r \n \r \n good job" , pl .(* DingtalkPayload ).ActionCard .Text )
135
+ assert .Equal (t , "[test/repo] Pull request review approved : #12 Fix bug" , pl .(* DingtalkPayload ).ActionCard .Title )
136
+ assert .Equal (t , "view pull request" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
137
+ assert .Equal (t , "http://localhost:3000/test/repo/pulls/12" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
138
+ })
139
+
140
+ t .Run ("Repository" , func (t * testing.T ) {
141
+ p := repositoryTestPayload ()
142
+
143
+ d := new (DingtalkPayload )
144
+ pl , err := d .Repository (p )
145
+ require .NoError (t , err )
146
+ require .NotNil (t , pl )
147
+ require .IsType (t , & DingtalkPayload {}, pl )
148
+
149
+ assert .Equal (t , "[test/repo] Repository created" , pl .(* DingtalkPayload ).ActionCard .Text )
150
+ assert .Equal (t , "[test/repo] Repository created" , pl .(* DingtalkPayload ).ActionCard .Title )
151
+ assert .Equal (t , "view repository" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
152
+ assert .Equal (t , "http://localhost:3000/test/repo" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
153
+ })
154
+
155
+ t .Run ("Release" , func (t * testing.T ) {
156
+ p := pullReleaseTestPayload ()
157
+
158
+ d := new (DingtalkPayload )
159
+ pl , err := d .Release (p )
160
+ require .NoError (t , err )
161
+ require .NotNil (t , pl )
162
+ require .IsType (t , & DingtalkPayload {}, pl )
163
+
164
+ assert .Equal (t , "[test/repo] Release created: v1.0 by user1" , pl .(* DingtalkPayload ).ActionCard .Text )
165
+ assert .Equal (t , "[test/repo] Release created: v1.0 by user1" , pl .(* DingtalkPayload ).ActionCard .Title )
166
+ assert .Equal (t , "view release" , pl .(* DingtalkPayload ).ActionCard .SingleTitle )
167
+ assert .Equal (t , "http://localhost:3000/api/v1/repos/test/repo/releases/2" , pl .(* DingtalkPayload ).ActionCard .SingleURL )
168
+ })
31
169
}
0 commit comments