Completed
Pull Request — master (#1276)
by Thomas
05:17
created

testCreateAutoDetectFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * ownCloud - Mail
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 *
19
 */
20
use OCA\Mail\Controller\AccountsController;
21
use OCA\Mail\Model\Message;
22
use OC\AppFramework\Http;
23
use OCP\AppFramework\Http\JSONResponse;
24
25
class AccountsControllerTest extends \Test\TestCase {
26
27
	private $appName;
28
	private $request;
29
	private $accountService;
30
	private $userId;
31
	private $userFolder;
32
	private $autoConfig;
33
	private $logger;
34
	private $l10n;
35
	private $crypto;
36
	private $controller;
37
	private $accountId;
38
	private $account;
39
	private $unifiedAccount;
40
41
	protected function setUp() {
42
		parent::setUp();
43
44
		$this->appName = 'mail';
45
		$this->request = $this->getMockBuilder('\OCP\IRequest')
46
			->disableOriginalConstructor()
47
			->getMock();
48
		$this->accountService = $this->getMockBuilder('\OCA\Mail\Service\AccountService')
49
			->disableOriginalConstructor()
50
			->getMock();
51
		$this->userId = 'manfred';
52
		$this->userFolder = $this->getMockBuilder('\OCP\Files\Folder')
53
			->disableOriginalConstructor()
54
			->getMock();
55
		$this->autoConfig = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\AutoConfig')
56
			->disableOriginalConstructor()
57
			->getMock();
58
		$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger')
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
62
			->disableOriginalConstructor()
63
			->getMock();
64
		$this->crypto = $this->getMockBuilder('\OCP\Security\ICrypto')
65
			->disableOriginalConstructor()
66
			->getMock();
67
68
		$this->controller = new AccountsController($this->appName, $this->request,
69
			$this->accountService, $this->userId, $this->userFolder, $this->autoConfig,
70
			$this->logger, $this->l10n, $this->crypto);
71
72
		$this->account = $this->getMockBuilder('\OCA\Mail\Account')
73
			->disableOriginalConstructor()
74
			->getMock();
75
		$this->unifiedAccount = $this->getMockBuilder('\OCA\Mail\Service\UnifiedAccount')
76
			->disableOriginalConstructor()
77
			->getMock();
78
		$this->accountId = 123;
79
	}
80
81
	public function testIndex() {
82
		$this->account->expects($this->once())
83
			->method('getConfiguration')
84
			->will($this->returnValue('conf'));
85
		$this->accountService->expects($this->once())
86
			->method('findByUserId')
87
			->with($this->equalTo($this->userId))
88
			->will($this->returnValue([$this->account]));
89
90
		$response = $this->controller->index();
91
92
		$expectedResponse = new JSONResponse(['conf']);
93
		$this->assertEquals($expectedResponse, $response);
94
	}
95
96 View Code Duplication
	public function testShow() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
		$this->accountService->expects($this->once())
98
			->method('find')
99
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId))
100
			->will($this->returnValue($this->account));
101
		$this->account->expects($this->once())
102
			->method('getConfiguration')
103
			->will($this->returnValue('conf'));
104
105
		$response = $this->controller->show($this->accountId);
106
107
		$expectedResponse = new JSONResponse('conf');
108
		$this->assertEquals($expectedResponse, $response);
109
	}
110
111 View Code Duplication
	public function testShowDoesNotExist() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
		$this->accountService->expects($this->once())
113
			->method('find')
114
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId))
115
			->will($this->returnValue($this->account));
116
		$this->account->expects($this->once())
117
			->method('getConfiguration')
118
			->will($this->throwException(new OCP\AppFramework\Db\DoesNotExistException('test123')));
119
120
		$response = $this->controller->show($this->accountId);
121
122
		$expectedResponse = new JSONResponse([]);
123
		$expectedResponse->setStatus(404);
124
		$this->assertEquals($expectedResponse, $response);
125
	}
126
127 View Code Duplication
	public function testDestroy() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
		$this->accountService->expects($this->once())
129
			->method('delete')
130
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId));
131
132
		$response = $this->controller->destroy($this->accountId);
133
134
		$expectedResponse = new JSONResponse();
135
		$this->assertEquals($expectedResponse, $response);
136
	}
137
138 View Code Duplication
	public function testDestroyDoesNotExist() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
		$this->accountService->expects($this->once())
140
			->method('delete')
141
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId))
142
			->will($this->throwException(new \OCP\AppFramework\Db\DoesNotExistException('test')));
143
144
		$response = $this->controller->destroy($this->accountId);
145
146
		$expectedResponse = new JSONResponse();
147
		$this->assertEquals($expectedResponse, $response);
148
	}
149
150
	public function testCreateAutoDetectSuccess() {
151
		$email = '[email protected]';
152
		$password = '123456';
153
		$accountName = 'John Doe';
154
155
		$this->account->expects($this->exactly(2))
156
			->method('getId')
157
			->will($this->returnValue(135));
158
		$this->autoConfig->expects($this->once())
159
			->method('createAutoDetected')
160
			->with($this->equalTo($email), $this->equalTo($password),
161
				$this->equalTo($accountName))
162
			->will($this->returnValue($this->account));
163
		$this->accountService->expects($this->once())
164
			->method('save')
165
			->with($this->equalTo($this->account));
166
167
		$response = $this->controller->create($accountName, $email, $password, null,
168
			null, null, null, null, null, null, null, null, null, true);
169
170
		$expectedResponse = new JSONResponse([
171
			'data' => [
172
				'id' => 135,
173
			],
174
			], Http::STATUS_CREATED);
175
		$this->assertEquals($expectedResponse, $response);
176
	}
177
178
	public function testCreateAutoDetectFailure() {
179
		$email = '[email protected]';
180
		$password = '123456';
181
		$accountName = 'John Doe';
182
183
		$this->autoConfig->expects($this->once())
184
			->method('createAutoDetected')
185
			->with($this->equalTo($email), $this->equalTo($password),
186
				$this->equalTo($accountName))
187
			->will($this->returnValue(null));
188
		$this->l10n->expects($this->once())
189
			->method('t')
190
			->will($this->returnValue('fail'));
191
192
		$response = $this->controller->create($accountName, $email, $password, null,
193
			null, null, null, null, null, null, null, null, null, true);
194
195
		$expectedResponse = new JSONResponse([
196
			'message' => 'fail',
197
			], Http::STATUS_BAD_REQUEST);
198
		$this->assertEquals($expectedResponse, $response);
199
	}
200
201 View Code Duplication
	public function newMessageDataProvider() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
		return [
203
			[false, false],
204
			[true, false],
205
			[false, true],
206
			[true, true],
207
		];
208
	}
209
210
	/**
211
	 * @dataProvider newMessageDataProvider
212
	 */
213
	public function testSend($isUnifiedInbox, $isReply) {
214
		$account = $isUnifiedInbox ? $this->unifiedAccount : $this->account;
215
		$folderId = base64_encode('My folder');
216
		$subject = 'Hello';
217
		$body = 'Hi!';
218
		$from = '[email protected]';
219
		$to = '[email protected]';
220
		$cc = '"user2" <[email protected]>, [email protected]';
221
		$bcc = '[email protected]';
222
		$draftUID = 45;
223
		$messageId = $isReply ? 123 : null;
224
		$attachmentName = 'kitten.png';
225
		$attachments = [
226
			[
227
				'fileName' => $attachmentName
228
			],
229
		];
230
231
		$this->accountService->expects($this->once())
232
			->method('find')
233
			->with($this->userId, $this->accountId)
234
			->will($this->returnValue($account));
235 View Code Duplication
		if ($isUnifiedInbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
			$this->unifiedAccount->expects($this->once())
237
				->method('resolve')
238
				->with($messageId)
239
				->will($this->returnValue([$this->account, $folderId, $messageId]));
240
		}
241
242
		if ($isReply) {
243
			$message = $this->getMockBuilder('OCA\Mail\Model\ReplyMessage')
244
				->disableOriginalConstructor()
245
				->getMock();
246
			$this->account->expects($this->once())
247
				->method('newReplyMessage')
248
				->will($this->returnValue($message));
249
			$mailbox = $this->getMockBuilder('OCA\Mail\Service\IMailBox')
250
				->disableOriginalConstructor()
251
				->getMock();
252
			$this->account->expects($this->once())
253
				->method('getMailbox')
254
				->with(base64_decode($folderId))
255
				->will($this->returnValue($mailbox));
256
			$reply = new Message();
257
			$mailbox->expects($this->once())
258
				->method('getMessage')
259
				->with($messageId)
260
				->will($this->returnValue($reply));
261
			$message->expects($this->once())
262
				->method('setRepliedMessage')
263
				->with($reply);
264
		} else {
265
			$message = $this->getMockBuilder('OCA\Mail\Model\Message')
266
				->disableOriginalConstructor()
267
				->getMock();
268
			$this->account->expects($this->once())
269
				->method('newMessage')
270
				->will($this->returnValue($message));
271
		}
272
273
		$message->expects($this->once())
274
			->method('setTo')
275
			->with(Message::parseAddressList($to));
276
		$message->expects($this->once())
277
			->method('setSubject')
278
			->with($subject);
279
		$message->expects($this->once())
280
			->method('setFrom')
281
			->with($from);
282
		$message->expects($this->once())
283
			->method('setCC')
284
			->with(Message::parseAddressList($cc));
285
		$message->expects($this->once())
286
			->method('setBcc')
287
			->with(Message::parseAddressList($bcc));
288
		$message->expects($this->once())
289
			->method('setContent')
290
			->with($body);
291
		$this->account->expects($this->once())
292
			->method('getEMailAddress')
293
			->will($this->returnValue($from));
294
		$this->account->expects($this->once())
295
			->method('sendMessage')
296
			->with($message, $draftUID);
297
		$this->userFolder->expects($this->at(0))
298
			->method('nodeExists')
299
			->with($attachmentName)
300
			->will($this->returnValue(true));
301
		$file = $this->getMockBuilder('OCP\Files\File')
302
			->disableOriginalConstructor()
303
			->getMock();
304
		$this->userFolder->expects($this->once())
305
			->method('get')
306
			->with($attachmentName)
307
			->will($this->returnValue($file));
308
309
		$expected = new JSONResponse();
310
		$actual = $this->controller->send($this->accountId, $folderId, $subject,
311
			$body, $to, $cc, $bcc, $draftUID, $messageId, $attachments);
312
313
		$this->assertEquals($expected, $actual);
314
	}
315
316 View Code Duplication
	public function draftDataProvider() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
		return [
318
			[false, false],
319
			[true, true],
320
			[true, false],
321
			[true, true],
322
		];
323
	}
324
325
	/**
326
	 * @dataProvider newMessageDataProvider
327
	 */
328
	public function testDraft($isUnifiedInbox, $withPreviousDraft) {
329
		$account = $isUnifiedInbox ? $this->unifiedAccount : $this->account;
330
		$subject = 'Hello';
331
		$body = 'Hi!';
332
		$from = '[email protected]';
333
		$to = '[email protected]';
334
		$cc = '"user2" <[email protected]>, [email protected]';
335
		$bcc = '[email protected]';
336
		$messageId = 123;
337
		$uid = $withPreviousDraft ? 123 : null;
338
		$newUID = 124;
339
340
		$this->accountService->expects($this->once())
341
			->method('find')
342
			->with($this->userId, $this->accountId)
343
			->will($this->returnValue($account));
344 View Code Duplication
		if ($isUnifiedInbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
			$this->unifiedAccount->expects($this->once())
346
				->method('resolve')
347
				->with($messageId)
348
				->will($this->returnValue([$this->account]));
349
		}
350
351
		$message = $this->getMockBuilder('OCA\Mail\Model\Message')
352
			->disableOriginalConstructor()
353
			->getMock();
354
		$this->account->expects($this->once())
355
			->method('newMessage')
356
			->will($this->returnValue($message));
357
		$message->expects($this->once())
358
			->method('setTo')
359
			->with(Message::parseAddressList($to));
360
		$message->expects($this->once())
361
			->method('setSubject')
362
			->with($subject);
363
		$message->expects($this->once())
364
			->method('setFrom')
365
			->with($from);
366
		$message->expects($this->once())
367
			->method('setCC')
368
			->with(Message::parseAddressList($cc));
369
		$message->expects($this->once())
370
			->method('setBcc')
371
			->with(Message::parseAddressList($bcc));
372
		$message->expects($this->once())
373
			->method('setContent')
374
			->with($body);
375
		$this->account->expects($this->once())
376
			->method('getEMailAddress')
377
			->will($this->returnValue($from));
378
		$this->account->expects($this->once())
379
			->method('saveDraft')
380
			->with($message, $uid)
381
			->will($this->returnValue($newUID));
382
383
		$expected = new JSONResponse([
384
			'uid' => $newUID,
385
		]);
386
		$actual = $this->controller->draft($this->accountId, $subject, $body, $to,
387
			$cc, $bcc, $uid, $messageId);
388
389
		$this->assertEquals($expected, $actual);
390
	}
391
392
}
393