This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
EventHookPropagatorSpec.scala
349 lines (286 loc) · 10 KB
/
EventHookPropagatorSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.softwaremill.codebrag.eventstream
import java.io.StringWriter
import java.net.InetSocketAddress
import akka.actor.ActorSystem
import akka.testkit.{TestActorRef, TestKit}
import com.softwaremill.codebrag.common.ClockSpec
import com.softwaremill.codebrag.dao.commitinfo.CommitInfoDAO
import com.softwaremill.codebrag.dao.events.NewUserRegistered
import com.softwaremill.codebrag.dao.user.UserDAO
import com.softwaremill.codebrag.domain._
import com.softwaremill.codebrag.domain.reactions._
import com.sun.net.httpserver._
import org.bson.types.ObjectId
import org.mockito.Mockito._
import org.scalatest.matchers.MustMatchers
import org.scalatest.mock.MockitoSugar
import org.scalatest.{BeforeAndAfter, WordSpec}
import scala.collection.convert.Wrappers._
class EventHookPropagatorSpec
extends TestKit(ActorSystem("testSystem"))
with WordSpec
with BeforeAndAfter
with MustMatchers
with ClockSpec
with MockitoSugar
{
var remoteHost: HttpServer = null
var remoteHook: StringWriter = null
var headers: Headers = null
var done = false
var mockUserDao = mock[UserDAO]
var mockCommitInfoDao = mock[CommitInfoDAO]
val id = ObjectId.get
val nowUtc = clock.now
val nowUtcStr = nowUtc.toString("yyyy-MM-dd'T'HH:mm:ss'Z'")
val commitId = id
val mockCommitInfo = CommitInfo(id, "test", "szach", "test-message", "mocher", "mocher@domain.com", "mocher", "mocher@domain.com", nowUtc, nowUtc, List())
val userId = id
val mockUser = User(userId, null, "test", "test@domain.com", Set(PlainUserToken("123456789").hashed), admin = false, active = true, null, null, null)
val mockLike = Like(id, commitId, userId, nowUtc, Some("test.txt"), Some(123))
val mockComment = Comment(id, id, id, nowUtc, "test-comment", Some("test2.txt"), Some(321))
val mockPartialCommit = PartialCommitInfo(id, "szach", "commit-loaded", "mocher", "mocher@domain.com", nowUtc)
val repoName = "codebrag"
val branchName = "master"
val hooks = Map(
"like-hook" -> List("http://localhost:8000/"),
"unlike-hook" -> List("http://localhost:8000/"),
"comment-added-hook" -> List("http://localhost:8000/"),
"commit-reviewed-hook" -> List("http://localhost:8000/"),
"all-commits-reviewed-hook" -> List("http://localhost:8000/"),
"new-commits-loaded-hook" -> List("http://localhost:8000/"),
"new-user-registered-hook" -> List("http://localhost:8000/")
)
val actorRef = TestActorRef(new EventHookPropagator(hooks, mockCommitInfoDao, mockUserDao))
"EventHookPropagator" must {
"propagate LikeEvent to remote host" in {
actorRef ! LikeEvent(mockLike)
awaitCond(done)
val expected = s"""
{
"commitInfo": {
"repoName": "test",
"sha": "szach",
"message": "test-message",
"authorName": "mocher",
"authorEmail": "mocher@domain.com",
"committerName": "mocher",
"committerEmail": "mocher@domain.com",
"authorDate": "$nowUtcStr",
"commitDate": "$nowUtcStr",
"parents": []
},
"likedBy": {
"name": "test",
"emailLowerCase": "test@domain.com",
"aliases": null
},
"like": {
"postingTime": "$nowUtcStr",
"fileName": "test.txt",
"lineNumber": 123
},
"hookName": "like-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagate UnlikeEvent to remote host" in {
actorRef ! UnlikeEvent(mockLike)
awaitCond(done)
val expected = s"""
{
"commitInfo": {
"repoName": "test",
"sha": "szach",
"message": "test-message",
"authorName": "mocher",
"authorEmail": "mocher@domain.com",
"committerName": "mocher",
"committerEmail": "mocher@domain.com",
"authorDate": "$nowUtcStr",
"commitDate": "$nowUtcStr",
"parents": []
},
"unlikedBy": {
"name": "test",
"emailLowerCase": "test@domain.com",
"aliases": null
},
"like": {
"postingTime": "$nowUtcStr",
"fileName": "test.txt",
"lineNumber": 123
},
"hookName": "unlike-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagate CommentAddedEvent to remote host" in {
actorRef ! CommentAddedEvent(mockComment)
awaitCond(done)
val expected = s"""
{
"commitInfo": {
"repoName": "test",
"sha": "szach",
"message": "test-message",
"authorName": "mocher",
"authorEmail": "mocher@domain.com",
"committerName": "mocher",
"committerEmail": "mocher@domain.com",
"authorDate": "$nowUtcStr",
"commitDate": "$nowUtcStr",
"parents": []
},
"commentedBy": {
"name": "test",
"emailLowerCase": "test@domain.com",
"aliases": null
},
"comment": {
"message": "test-comment",
"postingTime": "$nowUtcStr",
"fileName": "test2.txt",
"lineNumber": 321
},
"hookName": "comment-added-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagate CommitReviewed to remote host" in {
actorRef ! CommitReviewedEvent(mockCommitInfo, userId)
awaitCond(done)
val expected = s"""
{
"commitInfo": {
"repoName": "test",
"sha": "szach",
"message": "test-message",
"authorName": "mocher",
"authorEmail": "mocher@domain.com",
"committerName": "mocher",
"committerEmail": "mocher@domain.com",
"authorDate": "$nowUtcStr",
"commitDate": "$nowUtcStr",
"parents": []
},
"reviewedBy": {
"name": "test",
"emailLowerCase": "test@domain.com",
"aliases": null
},
"hookName": "commit-reviewed-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagate AllCommitsReviewed to remote host" in {
actorRef ! AllCommitsReviewedEvent(repoName, branchName, userId)
awaitCond(done)
val expected = s"""
{
"repoName": "codebrag",
"branchName": "master",
"reviewedBy": {
"name": "test",
"emailLowerCase": "test@domain.com",
"aliases": null
},
"hookName": "all-commits-reviewed-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagate NewCommitsLoadedEvent to remote host" in {
actorRef ! NewCommitsLoadedEvent(firstTime = false, "test", "szach", List(mockPartialCommit))
awaitCond(done)
val expected = s"""
{
"repoName": "test",
"currentSHA": "szach",
"newCommits": [
{
"sha": "szach",
"message": "commit-loaded",
"authorName": "mocher",
"authorEmail": "mocher@domain.com",
"date": "$nowUtcStr"
}
],
"hookName": "new-commits-loaded-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagate NewUserRegistered to remote host" in {
actorRef ! NewUserRegistered(id, "super", "Master-Disaster", "master@domain.com")
awaitCond(done)
val expected = s"""
{
"newUser": {
"name": "test",
"emailLowerCase": "test@domain.com",
"aliases": null
},
"login": "super",
"fullName": "Master-Disaster",
"hookName": "new-user-registered-hook",
"hookDate": "$nowUtcStr"
}
""".replaceAll(" ", "").replaceAll("\n", "")
remoteHook.toString must include(expected)
}
"propagated hook must be UTF-8 encoded" in {
actorRef ! NewUserRegistered(id, "super", "Master-Disaster", "master@domain.com")
awaitCond(done)
val contentType = JListWrapper(headers.get("Content-Type")).mkString(";")
contentType must include("application/json")
contentType must include("UTF-8")
}
"must not create hook if no listeners were defined" in {
import scala.concurrent.duration._
reset(mockUserDao)
val actorRef = TestActorRef(new EventHookPropagator(Map(), mockCommitInfoDao, mockUserDao))
actorRef ! NewUserRegistered(id, "super", "Master-Disaster", "master@domain.com")
receiveOne(1.second)
verifyZeroInteractions(mockUserDao)
}
}
before {
remoteHook = new StringWriter
remoteHost = HttpServer.create(new InetSocketAddress(8000), 0)
remoteHost.createContext("/", new HttpHandler() {
override def handle(t: HttpExchange): Unit = {
val in = t.getRequestBody
Iterator
.continually(in.read)
.takeWhile(-1 != _)
.foreach(remoteHook.write)
headers = t.getRequestHeaders
done = true
val response = "Ack!"
t.sendResponseHeaders(200, response.length())
val os = t.getResponseBody
os.write(response.getBytes)
os.close()
}
})
remoteHost.setExecutor(null)
remoteHost.start()
when(mockUserDao.findById(userId)).thenReturn(Some(mockUser))
when(mockCommitInfoDao.findByCommitId(commitId)).thenReturn(Some(mockCommitInfo))
}
after{
done = false
remoteHost.stop(0)
}
}