3333use OCP \IUserManager ;
3434use OCP \Notification \IManager ;
3535use OCP \Notification \INotification ;
36+ use OCP \Server ;
3637use Test \TestCase ;
3738
3839class RememberBackupCodesJobTest extends TestCase {
@@ -82,16 +83,25 @@ public function testInvalidUID() {
8283
8384 $ this ->notificationManager ->expects ($ this ->never ())
8485 ->method ($ this ->anything ());
86+ $ this ->jobList ->expects ($ this ->once ())
87+ ->method ('remove ' )
88+ ->with (
89+ RememberBackupCodesJob::class,
90+ ['uid ' => 'invalidUID ' ]
91+ );
8592 $ this ->jobList ->expects ($ this ->never ())
86- ->method ($ this -> anything () );
93+ ->method (' add ' );
8794
88- $ this -> invokePrivate ($ this ->job , 'run ' , [['uid ' => 'invalidUID ' ]]);
95+ self :: invokePrivate ($ this ->job , 'run ' , [['uid ' => 'invalidUID ' ]]);
8996 }
9097
9198 public function testBackupCodesGenerated () {
9299 $ user = $ this ->createMock (IUser::class);
93100 $ user ->method ('getUID ' )
94101 ->willReturn ('validUID ' );
102+ $ user ->method ('isEnabled ' )
103+ ->willReturn (true );
104+
95105 $ this ->userManager ->method ('get ' )
96106 ->with ('validUID ' )
97107 ->willReturn ($ user );
@@ -112,7 +122,7 @@ public function testBackupCodesGenerated() {
112122 $ this ->notificationManager ->expects ($ this ->never ())
113123 ->method ($ this ->anything ());
114124
115- $ this -> invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
125+ self :: invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
116126 }
117127
118128 public function testNoActiveProvider () {
@@ -140,13 +150,15 @@ public function testNoActiveProvider() {
140150 $ this ->notificationManager ->expects ($ this ->never ())
141151 ->method ($ this ->anything ());
142152
143- $ this -> invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
153+ self :: invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
144154 }
145155
146156 public function testNotificationSend () {
147157 $ user = $ this ->createMock (IUser::class);
148158 $ user ->method ('getUID ' )
149159 ->willReturn ('validUID ' );
160+ $ user ->method ('isEnabled ' )
161+ ->willReturn (true );
150162 $ this ->userManager ->method ('get ' )
151163 ->with ('validUID ' )
152164 ->willReturn ($ user );
@@ -165,7 +177,7 @@ public function testNotificationSend() {
165177 $ date ->setTimestamp ($ this ->time ->getTime ());
166178
167179 $ this ->notificationManager ->method ('createNotification ' )
168- ->willReturn (\ OC :: $ server -> query (IManager::class)->createNotification ());
180+ ->willReturn (Server:: get (IManager::class)->createNotification ());
169181
170182 $ this ->notificationManager ->expects ($ this ->once ())
171183 ->method ('notify ' )
@@ -178,6 +190,52 @@ public function testNotificationSend() {
178190 $ n ->getSubject () === 'create_backupcodes ' ;
179191 }));
180192
181- $ this ->invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
193+ self ::invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
194+ }
195+
196+ public function testNotificationNotSendForDisabledUser () {
197+ $ user = $ this ->createMock (IUser::class);
198+ $ user ->method ('getUID ' )
199+ ->willReturn ('validUID ' );
200+ $ user ->method ('isEnabled ' )
201+ ->willReturn (false );
202+ $ this ->userManager ->method ('get ' )
203+ ->with ('validUID ' )
204+ ->willReturn ($ user );
205+
206+ $ this ->registry ->method ('getProviderStates ' )
207+ ->with ($ user )
208+ ->willReturn ([
209+ 'backup_codes ' => false ,
210+ 'foo ' => true ,
211+ ]);
212+
213+ $ this ->jobList ->expects ($ this ->once ())
214+ ->method ('remove ' )
215+ ->with (
216+ RememberBackupCodesJob::class,
217+ ['uid ' => 'validUID ' ]
218+ );
219+
220+ $ date = new \DateTime ();
221+ $ date ->setTimestamp ($ this ->time ->getTime ());
222+
223+ $ this ->notificationManager ->method ('createNotification ' )
224+ ->willReturn (Server::get (IManager::class)->createNotification ());
225+
226+ $ this ->notificationManager ->expects ($ this ->once ())
227+ ->method ('markProcessed ' )
228+ ->with ($ this ->callback (function (INotification $ n ) {
229+ return $ n ->getApp () === 'twofactor_backupcodes ' &&
230+ $ n ->getUser () === 'validUID ' &&
231+ $ n ->getObjectType () === 'create ' &&
232+ $ n ->getObjectId () === 'codes ' &&
233+ $ n ->getSubject () === 'create_backupcodes ' ;
234+ }));
235+
236+ $ this ->notificationManager ->expects ($ this ->never ())
237+ ->method ('notify ' );
238+
239+ self ::invokePrivate ($ this ->job , 'run ' , [['uid ' => 'validUID ' ]]);
182240 }
183241}
0 commit comments