-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathAvatarControllerTest.php
139 lines (111 loc) Β· 4.07 KB
/
AvatarControllerTest.php
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
<?php
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Tests\Unit\Controller;
use OCA\Mail\Contracts\IAvatarService;
use OCA\Mail\Controller\AvatarsController;
use OCA\Mail\Http\AvatarDownloadResponse;
use OCA\Mail\Service\Avatar\Avatar;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
use PHPUnit_Framework_MockObject_MockObject;
use Test\TestCase;
class AvatarControllerTest extends TestCase {
/** @var IAvatarService|PHPUnit_Framework_MockObject_MockObject */
private $avatarService;
/** @var AvatarsController */
private $controller;
/** @var ITimeFactory */
private $oldFactory;
protected function setUp(): void {
parent::setUp();
$request = $this->createMock(IRequest::class);
$this->avatarService = $this->createMock(IAvatarService::class);
$timeFactory = $this->createMocK(ITimeFactory::class);
$timeFactory->expects($this->any())
->method('getTime')
->willReturn(10000);
$this->oldFactory = \OC::$server->offsetGet(ITimeFactory::class);
\OC::$server->registerService(ITimeFactory::class, function () use ($timeFactory) {
return $timeFactory;
});
$this->controller = new AvatarsController('mail', $request, $this->avatarService, 'jane');
}
protected function tearDown(): void {
parent::tearDown();
\OC::$server->offsetUnset(ITimeFactory::class);
\OC::$server->offsetSet(ITimeFactory::class, $this->oldFactory);
}
public function testGetUrl() {
$email = '[email protected]';
$avatar = new Avatar('https://doe.com/favicon.ico');
$this->avatarService->expects($this->once())
->method('getAvatar')
->with($email, 'jane')
->willReturn($avatar);
$resp = $this->controller->url($email);
$expected = new JSONResponse($avatar);
$expected->cacheFor(7 * 24 * 60 * 60, false, true);
$this->assertEquals($expected, $resp);
}
public function testGetUrlNoAvatarFound() {
$email = '[email protected]';
$this->avatarService->expects($this->once())
->method('getAvatar')
->with($email, 'jane')
->willReturn(null);
$resp = $this->controller->url($email);
$expected = new JSONResponse([], Http::STATUS_NO_CONTENT);
$expected->cacheFor(60 * 60, false, true);
$this->assertEquals($expected, $resp);
}
public function testGetImage() {
$email = '[email protected]';
$this->avatarService->expects($this->once())
->method('getAvatarImage')
->with($email, 'jane')
$resp = $this->controller->image($email);
$expected = new AvatarDownloadResponse('data');
$expected->addHeader('Content-Type', 'image/jpeg');
$expected->cacheFor(7 * 24 * 60 * 60, false, true);
$this->assertEquals($expected, $resp);
}
public function testGetImageNotFound() {
$email = '[email protected]';
$this->avatarService->expects($this->once())
->method('getAvatarImage')
->with($email, 'jane')
->willReturn(null);
$resp = $this->controller->image($email);
$expected = new Response();
$expected->setStatus(Http::STATUS_NO_CONTENT);
$expected->cacheFor(0);
$this->assertEquals($expected, $resp);
}
public function testNormalizeEmailWithExtraSpaces() {
$email = ' [email protected] ';
$normalizedEmail = $this->invokePrivate($this->controller, 'normalizeEmail', [$email]);
}
public function testNormalizeEmailWithSingleQuotes() {
$email = "'[email protected]'";
$normalizedEmail = $this->invokePrivate($this->controller, 'normalizeEmail', [$email]);
}
public function testValidateEmailWithValidEmail() {
$email = '[email protected]';
$isValid = $this->invokePrivate($this->controller, 'validateEmail', [$email]);
$this->assertTrue($isValid);
}
public function testValidateEmailWithInvalidEmail() {
$email = 'valid.email.example.com';
$isValid = $this->invokePrivate($this->controller, 'validateEmail', [$email]);
$this->assertFalse($isValid);
}
}