1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Christoph Wurst <[email protected]> |
5
|
|
|
* @author Lukas Reschke <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Mail |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
24
|
|
|
use OCP\AppFramework\Http; |
25
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
26
|
|
|
use OCA\Mail\Controller\MessagesController; |
27
|
|
|
use OCA\Mail\Http\AttachmentDownloadResponse; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tests for lib/controller/messagescontroller |
31
|
|
|
* |
32
|
|
|
* @author Christoph Wurst |
33
|
|
|
*/ |
34
|
|
|
class MessagesControllerTest extends \Test\TestCase { |
35
|
|
|
|
36
|
|
|
private $appName; |
37
|
|
|
private $request; |
38
|
|
|
private $accountService; |
39
|
|
|
private $userId; |
40
|
|
|
private $userFolder; |
41
|
|
|
private $contactIntegration; |
42
|
|
|
private $logger; |
43
|
|
|
private $l10n; |
44
|
|
|
private $controller; |
45
|
|
|
private $account; |
46
|
|
|
private $mailbox; |
47
|
|
|
private $message; |
48
|
|
|
private $attachment; |
49
|
|
|
private $mimeTypeDetector; |
50
|
|
|
private $urlGenerator; |
51
|
|
|
|
52
|
|
|
protected function setUp() { |
53
|
|
|
parent::setUp(); |
54
|
|
|
|
55
|
|
|
$this->appName = 'mail'; |
56
|
|
|
$this->request = $this->getMockBuilder('\OCP\IRequest')->getMock(); |
57
|
|
|
$this->accountService = $this->getMockBuilder('\OCA\Mail\Service\AccountService') |
58
|
|
|
->disableOriginalConstructor() |
59
|
|
|
->getMock(); |
60
|
|
|
$this->userId = 'john'; |
61
|
|
|
$this->userFolder = $this->getMockBuilder('\OCP\Files\Folder') |
62
|
|
|
->disableOriginalConstructor() |
63
|
|
|
->getMock(); |
64
|
|
|
$this->request = $this->getMockBuilder('\OC\AppFramework\Http\Request') |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMock(); |
67
|
|
|
$this->contactIntegration = $this->getMockBuilder('\OCA\Mail\Service\ContactsIntegration') |
68
|
|
|
->disableOriginalConstructor() |
69
|
|
|
->getMock(); |
70
|
|
|
$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger') |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->getMock(); |
73
|
|
|
$this->l10n = $this->getMockBuilder('\OCP\IL10N')->getMock(); |
74
|
|
|
$this->mimeTypeDetector = $this->getMockBuilder('OCP\Files\IMimeTypeDetector')->getMock(); |
75
|
|
|
$this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')->getMock(); |
76
|
|
|
|
77
|
|
|
$this->controller = new MessagesController( |
78
|
|
|
$this->appName, |
79
|
|
|
$this->request, |
80
|
|
|
$this->accountService, |
81
|
|
|
$this->userId, |
82
|
|
|
$this->userFolder, |
83
|
|
|
$this->contactIntegration, |
84
|
|
|
$this->logger, |
85
|
|
|
$this->l10n, |
86
|
|
|
$this->mimeTypeDetector, |
87
|
|
|
$this->urlGenerator); |
88
|
|
|
|
89
|
|
|
$this->account = $this->getMockBuilder('\OCA\Mail\Account') |
90
|
|
|
->disableOriginalConstructor() |
91
|
|
|
->getMock(); |
92
|
|
|
$this->mailbox = $this->getMockBuilder('\OCA\Mail\Mailbox') |
93
|
|
|
->disableOriginalConstructor() |
94
|
|
|
->getMock(); |
95
|
|
|
$this->message = $this->getMockBuilder('\OCA\Mail\Model\IMAPMessage') |
96
|
|
|
->disableOriginalConstructor() |
97
|
|
|
->getMock(); |
98
|
|
|
$this->attachment = $this->getMockBuilder('\OCA\Mail\Attachment') |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->getMock(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testIndex() { |
104
|
|
|
// TODO: write test |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testShow() { |
108
|
|
|
// TODO: write test |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testShowMessageNotFound() { |
112
|
|
|
// TODO: write test |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetHtmlBody() { |
116
|
|
|
$accountId = 17; |
117
|
|
|
$folderId = 'testfolder'; |
118
|
|
|
$messageId = 4321; |
119
|
|
|
|
120
|
|
|
$this->accountService->expects($this->once()) |
121
|
|
|
->method('find') |
122
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
123
|
|
|
->will($this->returnValue($this->account)); |
124
|
|
|
$this->account->expects($this->once()) |
125
|
|
|
->method('getMailbox') |
126
|
|
|
->with($this->equalTo($folderId)) |
127
|
|
|
->will($this->returnValue($this->mailbox)); |
128
|
|
|
$this->mailbox->expects($this->once()) |
129
|
|
|
->method('getMessage') |
130
|
|
|
->with($this->equalTo($messageId), $this->equalTo(true)) |
131
|
|
|
->will($this->returnValue($this->message)); |
132
|
|
|
|
133
|
|
|
$expectedResponse = new \OCA\Mail\Http\HtmlResponse(null); |
134
|
|
|
$expectedResponse->cacheFor(3600); |
135
|
|
|
$expectedResponse->addHeader('Pragma', 'cache'); |
136
|
|
|
if(class_exists('\OCP\AppFramework\Http\ContentSecurityPolicy')) { |
137
|
|
|
$policy = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
138
|
|
|
$policy->allowEvalScript(false); |
139
|
|
|
$policy->disallowScriptDomain('\'self\''); |
140
|
|
|
$policy->disallowConnectDomain('\'self\''); |
141
|
|
|
$policy->disallowFontDomain('\'self\''); |
142
|
|
|
$policy->disallowMediaDomain('\'self\''); |
143
|
|
|
$expectedResponse->setContentSecurityPolicy($policy); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$actualResponse = $this->controller->getHtmlBody($accountId, base64_encode($folderId), $messageId); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($expectedResponse, $actualResponse); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testDownloadAttachment() { |
152
|
|
|
$accountId = 17; |
153
|
|
|
$folderId = base64_encode('my folder'); |
154
|
|
|
$messageId = 123; |
155
|
|
|
$attachmentId = 3; |
156
|
|
|
|
157
|
|
|
// Attachment data |
158
|
|
|
$contents = 'abcdef'; |
159
|
|
|
$name = 'cat.jpg'; |
160
|
|
|
$type = 'image/jpg'; |
161
|
|
|
|
162
|
|
|
$this->accountService->expects($this->once()) |
163
|
|
|
->method('find') |
164
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
165
|
|
|
->will($this->returnValue($this->account)); |
166
|
|
|
$this->account->expects($this->once()) |
167
|
|
|
->method('getMailbox') |
168
|
|
|
->with(base64_decode($folderId)) |
169
|
|
|
->will($this->returnValue($this->mailbox)); |
170
|
|
|
$this->mailbox->expects($this->once()) |
171
|
|
|
->method('getAttachment') |
172
|
|
|
->with($messageId, $attachmentId) |
173
|
|
|
->will($this->returnValue($this->attachment)); |
174
|
|
|
$this->attachment->expects($this->once()) |
175
|
|
|
->method('getContents') |
176
|
|
|
->will($this->returnValue($contents)); |
177
|
|
|
$this->attachment->expects($this->once()) |
178
|
|
|
->method('getName') |
179
|
|
|
->will($this->returnValue($name)); |
180
|
|
|
$this->attachment->expects($this->once()) |
181
|
|
|
->method('getType') |
182
|
|
|
->will($this->returnValue($type)); |
183
|
|
|
|
184
|
|
|
$expected = new AttachmentDownloadResponse($contents, $name, $type); |
185
|
|
|
$response = $this->controller->downloadAttachment($accountId, $folderId, |
186
|
|
|
$messageId, $attachmentId); |
187
|
|
|
|
188
|
|
|
$this->assertEquals($expected, $response); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testSaveSingleAttachment() { |
192
|
|
|
$accountId = 17; |
193
|
|
|
$folderId = base64_encode('my folder'); |
194
|
|
|
$messageId = 123; |
195
|
|
|
$attachmentId = 3; |
196
|
|
|
$targetPath = 'Downloads'; |
197
|
|
|
|
198
|
|
|
$this->accountService->expects($this->once()) |
199
|
|
|
->method('find') |
200
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
201
|
|
|
->will($this->returnValue($this->account)); |
202
|
|
|
$this->account->expects($this->once()) |
203
|
|
|
->method('getMailbox') |
204
|
|
|
->with(base64_decode($folderId)) |
205
|
|
|
->will($this->returnValue($this->mailbox)); |
206
|
|
|
$this->mailbox->expects($this->once()) |
207
|
|
|
->method('getAttachment') |
208
|
|
|
->with($messageId, $attachmentId) |
209
|
|
|
->will($this->returnValue($this->attachment)); |
210
|
|
|
$this->attachment->expects($this->once()) |
211
|
|
|
->method('getName') |
212
|
|
|
->with() |
213
|
|
|
->will($this->returnValue('cat.jpg')); |
214
|
|
|
$this->userFolder->expects($this->once()) |
215
|
|
|
->method('nodeExists') |
216
|
|
|
->with("Downloads/cat.jpg") |
217
|
|
|
->will($this->returnValue(false)); |
218
|
|
|
$file = $this->getMockBuilder('\OCP\Files\File') |
219
|
|
|
->disableOriginalConstructor() |
220
|
|
|
->getMock(); |
221
|
|
|
$this->userFolder->expects($this->once()) |
222
|
|
|
->method('newFile') |
223
|
|
|
->with("Downloads/cat.jpg") |
224
|
|
|
->will($this->returnValue($file)); |
225
|
|
|
$file->expects($this->once()) |
226
|
|
|
->method('putContent') |
227
|
|
|
->with('abcdefg'); |
228
|
|
|
$this->attachment->expects($this->once()) |
229
|
|
|
->method('getContents') |
230
|
|
|
->will($this->returnValue('abcdefg')); |
231
|
|
|
|
232
|
|
|
$expected = new JSONResponse(); |
233
|
|
|
$response = $this->controller->saveAttachment($accountId, $folderId, |
234
|
|
|
$messageId, $attachmentId, $targetPath); |
235
|
|
|
|
236
|
|
|
$this->assertEquals($expected, $response); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testSaveAllAttachments() { |
240
|
|
|
$accountId = 17; |
241
|
|
|
$folderId = base64_encode('my folder'); |
242
|
|
|
$messageId = 123; |
243
|
|
|
$attachmentId = 3; |
244
|
|
|
$targetPath = 'Downloads'; |
245
|
|
|
|
246
|
|
|
$this->accountService->expects($this->once()) |
247
|
|
|
->method('find') |
248
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
249
|
|
|
->will($this->returnValue($this->account)); |
250
|
|
|
$this->account->expects($this->once()) |
251
|
|
|
->method('getMailbox') |
252
|
|
|
->with(base64_decode($folderId)) |
253
|
|
|
->will($this->returnValue($this->mailbox)); |
254
|
|
|
$this->mailbox->expects($this->once()) |
255
|
|
|
->method('getMessage') |
256
|
|
|
->with($messageId) |
257
|
|
|
->will($this->returnValue($this->message)); |
258
|
|
|
$this->message->attachments = [ |
259
|
|
|
[ |
260
|
|
|
'id' => $attachmentId |
261
|
|
|
] |
262
|
|
|
]; |
263
|
|
|
|
264
|
|
|
$this->mailbox->expects($this->once()) |
265
|
|
|
->method('getAttachment') |
266
|
|
|
->with($messageId, $attachmentId) |
267
|
|
|
->will($this->returnValue($this->attachment)); |
268
|
|
|
$this->attachment->expects($this->once()) |
269
|
|
|
->method('getName') |
270
|
|
|
->with() |
271
|
|
|
->will($this->returnValue('cat.jpg')); |
272
|
|
|
$this->userFolder->expects($this->once()) |
273
|
|
|
->method('nodeExists') |
274
|
|
|
->with("Downloads/cat.jpg") |
275
|
|
|
->will($this->returnValue(false)); |
276
|
|
|
$file = $this->getMockBuilder('\OCP\Files\File') |
277
|
|
|
->disableOriginalConstructor() |
278
|
|
|
->getMock(); |
279
|
|
|
$this->userFolder->expects($this->once()) |
280
|
|
|
->method('newFile') |
281
|
|
|
->with("Downloads/cat.jpg") |
282
|
|
|
->will($this->returnValue($file)); |
283
|
|
|
$file->expects($this->once()) |
284
|
|
|
->method('putContent') |
285
|
|
|
->with('abcdefg'); |
286
|
|
|
$this->attachment->expects($this->once()) |
287
|
|
|
->method('getContents') |
288
|
|
|
->will($this->returnValue('abcdefg')); |
289
|
|
|
|
290
|
|
|
$expected = new JSONResponse(); |
291
|
|
|
$response = $this->controller->saveAttachment($accountId, $folderId, |
292
|
|
|
$messageId, 0, $targetPath); |
293
|
|
|
|
294
|
|
|
$this->assertEquals($expected, $response); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testSetFlagsUnseen() { |
298
|
|
|
$accountId = 17; |
299
|
|
|
$folderId = base64_encode('my folder'); |
300
|
|
|
$messageId = 123; |
301
|
|
|
$flags = [ |
302
|
|
|
'unseen' => false |
303
|
|
|
]; |
304
|
|
|
|
305
|
|
|
$this->accountService->expects($this->once()) |
306
|
|
|
->method('find') |
307
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
308
|
|
|
->will($this->returnValue($this->account)); |
309
|
|
|
$this->account->expects($this->once()) |
310
|
|
|
->method('getMailbox') |
311
|
|
|
->with(base64_decode($folderId)) |
312
|
|
|
->will($this->returnValue($this->mailbox)); |
313
|
|
|
$this->mailbox->expects($this->once()) |
314
|
|
|
->method('setMessageFlag') |
315
|
|
|
->with($messageId, '\\seen', true); |
316
|
|
|
|
317
|
|
|
$expected = new JSONResponse(); |
318
|
|
|
$response = $this->controller->setFlags($accountId, $folderId, $messageId, |
319
|
|
|
$flags); |
320
|
|
|
|
321
|
|
|
$this->assertEquals($expected, $response); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function testSetFlagsFlagged() { |
325
|
|
|
$accountId = 17; |
326
|
|
|
$folderId = base64_encode('my folder'); |
327
|
|
|
$messageId = 123; |
328
|
|
|
$flags = [ |
329
|
|
|
'flagged' => true |
330
|
|
|
]; |
331
|
|
|
|
332
|
|
|
$this->accountService->expects($this->once()) |
333
|
|
|
->method('find') |
334
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
335
|
|
|
->will($this->returnValue($this->account)); |
336
|
|
|
$this->account->expects($this->once()) |
337
|
|
|
->method('getMailbox') |
338
|
|
|
->with(base64_decode($folderId)) |
339
|
|
|
->will($this->returnValue($this->mailbox)); |
340
|
|
|
$this->mailbox->expects($this->once()) |
341
|
|
|
->method('setMessageFlag') |
342
|
|
|
->with($messageId, '\\flagged', true); |
343
|
|
|
|
344
|
|
|
$expected = new JSONResponse(); |
345
|
|
|
$response = $this->controller->setFlags($accountId, $folderId, $messageId, |
346
|
|
|
$flags); |
347
|
|
|
|
348
|
|
|
$this->assertEquals($expected, $response); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testDestroy() { |
352
|
|
|
$accountId = 17; |
353
|
|
|
$folderId = base64_encode('my folder'); |
354
|
|
|
$messageId = 123; |
355
|
|
|
|
356
|
|
|
$this->accountService->expects($this->once()) |
357
|
|
|
->method('find') |
358
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
359
|
|
|
->will($this->returnValue($this->account)); |
360
|
|
|
|
361
|
|
|
$this->account->expects($this->once()) |
362
|
|
|
->method('deleteMessage') |
363
|
|
|
->with(base64_decode($folderId), $messageId); |
364
|
|
|
|
365
|
|
|
$expected = new JSONResponse(); |
366
|
|
|
$result = $this->controller->destroy($accountId, $folderId, $messageId); |
367
|
|
|
|
368
|
|
|
$this->assertEquals($expected, $result); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function testDestroyWithAccountNotFound() { |
372
|
|
|
$accountId = 17; |
373
|
|
|
$folderId = base64_encode('my folder'); |
374
|
|
|
$messageId = 123; |
375
|
|
|
|
376
|
|
|
$this->accountService->expects($this->once()) |
377
|
|
|
->method('find') |
378
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
379
|
|
|
->will($this->throwException(new DoesNotExistException(''))); |
380
|
|
|
|
381
|
|
|
$expected = new JSONResponse([], Http::STATUS_NOT_FOUND); |
382
|
|
|
$result = $this->controller->destroy($accountId, $folderId, $messageId); |
383
|
|
|
|
384
|
|
|
$this->assertEquals($expected, $result); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function testDestroyWithFolderOrMessageNotFound() { |
388
|
|
|
$accountId = 17; |
389
|
|
|
$folderId = base64_encode('my folder'); |
390
|
|
|
$messageId = 123; |
391
|
|
|
|
392
|
|
|
$this->accountService->expects($this->once()) |
393
|
|
|
->method('find') |
394
|
|
|
->with($this->equalTo($this->userId), $this->equalTo($accountId)) |
395
|
|
|
->will($this->returnValue($this->account)); |
396
|
|
|
|
397
|
|
|
$this->account->expects($this->once()) |
398
|
|
|
->method('deleteMessage') |
399
|
|
|
->with(base64_decode($folderId), $messageId) |
400
|
|
|
->will($this->throwException(new DoesNotExistException(''))); |
401
|
|
|
|
402
|
|
|
$expected = new JSONResponse([], Http::STATUS_NOT_FOUND); |
403
|
|
|
$result = $this->controller->destroy($accountId, $folderId, $messageId); |
404
|
|
|
|
405
|
|
|
$this->assertEquals($expected, $result); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
} |
409
|
|
|
|