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
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\TwoFactorBackupCodes\Tests\Unit\BackgroundJob;
use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\BackgroundJob\IJobList;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use Test\TestCase;
class RememberBackupCodesJobTest extends TestCase {
/** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
private $registry;
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
private $userManager;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $time;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
private $notificationManager;
/** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
private $jobList;
/** @var RememberBackupCodesJob */
private $job;
public function setUp(): void {
parent::setUp();
$this->registry = $this->createMock(IRegistry::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->time->method('getTime')
->willReturn(10000000);
$this->notificationManager = $this->createMock(IManager::class);
$this->jobList = $this->createMock(IJobList::class);
$this->job = new RememberBackupCodesJob(
$this->registry,
$this->userManager,
$this->time,
$this->notificationManager,
$this->jobList
);
}
public function testInvalidUID() {
$this->userManager->method('get')
->with('invalidUID')
->willReturn(null);
$this->notificationManager->expects($this->never())
->method($this->anything());
$this->jobList->expects($this->never())
->method($this->anything());
$this->invokePrivate($this->job, 'run', [['uid' => 'invalidUID']]);
}
public function testBackupCodesGenerated() {
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('validUID');
$this->userManager->method('get')
->with('validUID')
->willReturn($user);
$this->registry->method('getProviderStates')
->with($user)
->willReturn([
'backup_codes' => true
]);
$this->jobList->expects($this->once())
->method('remove')
->with(
RememberBackupCodesJob::class,
['uid' => 'validUID']
);
$this->notificationManager->expects($this->never())
->method($this->anything());
$this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
}
public function testNoActiveProvider() {
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('validUID');
$this->userManager->method('get')
->with('validUID')
->willReturn($user);
$this->registry->method('getProviderStates')
->with($user)
->willReturn([
'backup_codes' => false,
'foo' => false,
]);
$this->jobList->expects($this->once())
->method('remove')
->with(
RememberBackupCodesJob::class,
['uid' => 'validUID']
);
$this->notificationManager->expects($this->never())
->method($this->anything());
$this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
}
public function testNotificationSend() {
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('validUID');
$this->userManager->method('get')
->with('validUID')
->willReturn($user);
$this->registry->method('getProviderStates')
->with($user)
->willReturn([
'backup_codes' => false,
'foo' => true,
]);
$this->jobList->expects($this->never())
->method($this->anything());
$date = new \DateTime();
$date->setTimestamp($this->time->getTime());
$this->notificationManager->method('createNotification')
->willReturn(\OC::$server->query(IManager::class)->createNotification());
$this->notificationManager->expects($this->once())
->method('notify')
->with($this->callback(function (INotification $n) {
return $n->getApp() === 'twofactor_backupcodes' &&
$n->getUser() === 'validUID' &&
$n->getDateTime()->getTimestamp() === 10000000 &&
$n->getObjectType() === 'create' &&
$n->getObjectId() === 'codes' &&
$n->getSubject() === 'create_backupcodes';
}));
$this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
}
}
|