1313use OCP \IAppConfig ;
1414use OCP \IConfig ;
1515use PHPUnit \Framework \MockObject \MockObject ;
16+ use Psr \Log \LoggerInterface ;
1617use Test \TestCase ;
1718
1819class ResetTokenTest extends TestCase {
20+ private ITimeFactory |MockObject $ timeFactory ;
1921 private IConfig |MockObject $ config ;
2022 private IAppConfig |MockObject $ appConfig ;
21- private ITimeFactory |MockObject $ timeFactory ;
23+ private LoggerInterface |MockObject $ logger ;
2224 private BackgroundJobResetToken $ resetTokenBackgroundJob ;
2325
2426 protected function setUp (): void {
2527 parent ::setUp ();
26- $ this ->appConfig = $ this ->createMock (IAppConfig::class);
27- $ this ->config = $ this ->createMock (IConfig::class);
2828 $ this ->timeFactory = $ this ->createMock (ITimeFactory::class);
29+ $ this ->config = $ this ->createMock (IConfig::class);
30+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
31+ $ this ->logger = $ this ->createMock (LoggerInterface::class);
2932 $ this ->resetTokenBackgroundJob = new BackgroundJobResetToken (
3033 $ this ->timeFactory ,
3134 $ this ->config ,
3235 $ this ->appConfig ,
36+ $ this ->logger ,
3337 );
3438 }
3539
36- public function testRunWithNotExpiredToken (): void {
40+ public function testRunWithNotExpiredToken (): void { // Affirm if updater.secret.created <48 hours ago then `updater.secret` is left alone
3741 $ this ->timeFactory
3842 ->expects ($ this ->atLeastOnce ())
3943 ->method ('getTime ' )
40- ->willReturn (123 );
44+ ->willReturn (1733069649 ); // "Sun, 01 Dec 2024 16:14:09 +0000"
4145 $ this ->appConfig
4246 ->expects ($ this ->once ())
4347 ->method ('getValueInt ' )
44- ->with ('core ' , 'updater.secret.created ' , 123 );
48+ ->with ('core ' , 'updater.secret.created ' , 1733069649 )
49+ ->willReturn (1733069649 - 1 * 24 * 60 * 60 ); // 24h prior: "Sat, 30 Nov 2024 16:14:09 +0000"
4550 $ this ->config
4651 ->expects ($ this ->once ())
4752 ->method ('getSystemValueBool ' )
@@ -50,29 +55,53 @@ public function testRunWithNotExpiredToken(): void {
5055 $ this ->config
5156 ->expects ($ this ->never ())
5257 ->method ('deleteSystemValue ' );
58+ $ this ->appConfig
59+ ->expects ($ this ->never ())
60+ ->method ('deleteKey ' );
61+ $ this ->logger
62+ ->expects ($ this ->never ())
63+ ->method ('warning ' );
64+ $ this ->logger
65+ ->expects ($ this ->once ())
66+ ->method ('debug ' );
5367
54- static :: invokePrivate ($ this ->resetTokenBackgroundJob , 'run ' , [null ]);
68+ $ this -> invokePrivate ($ this ->resetTokenBackgroundJob , 'run ' , [null ]);
5569 }
5670
57- public function testRunWithExpiredToken (): void {
71+ public function testRunWithExpiredToken (): void { // Affirm if updater.secret.created >48 hours ago then `updater.secret` is removed
5872 $ this ->timeFactory
59- ->expects ($ this ->once ())
73+ ->expects ($ this ->atLeastOnce ())
6074 ->method ('getTime ' )
61- ->willReturn (1455045234 );
75+ ->willReturn (1455045234 ); // "Tue, 09 Feb 2016 19:13:54 +0000"
6276 $ this ->appConfig
6377 ->expects ($ this ->once ())
6478 ->method ('getValueInt ' )
6579 ->with ('core ' , 'updater.secret.created ' , 1455045234 )
66- ->willReturn (2 * 24 * 60 * 60 + 1 ); // over 2 days
80+ ->willReturn (1455045234 - 3 * 24 * 60 * 60 ); // 72h prior: "Sat, 06 Feb 2016 19:13:54 +0000"
81+ $ this ->config
82+ ->expects ($ this ->once ())
83+ ->method ('getSystemValueBool ' )
84+ ->with ('config_is_read_only ' )
85+ ->willReturn (false );
6786 $ this ->config
6887 ->expects ($ this ->once ())
6988 ->method ('deleteSystemValue ' )
7089 ->with ('updater.secret ' );
90+ $ this ->appConfig
91+ ->expects ($ this ->once ())
92+ ->method ('deleteKey ' )
93+ ->with ('core ' , 'updater.secret.created ' );
94+ $ this ->logger
95+ ->expects ($ this ->once ())
96+ ->method ('warning ' );
97+ $ this ->logger
98+ ->expects ($ this ->never ())
99+ ->method ('debug ' );
71100
72- static :: invokePrivate ($ this ->resetTokenBackgroundJob , 'run ' , [null ]);
101+ $ this -> invokePrivate ($ this ->resetTokenBackgroundJob , 'run ' , [null ]);
73102 }
74103
75- public function testRunWithExpiredTokenAndReadOnlyConfigFile (): void {
104+ public function testRunWithExpiredTokenAndReadOnlyConfigFile (): void { // Affirm if config_is_read_only is set that the secret is never reset
76105 $ this ->timeFactory
77106 ->expects ($ this ->never ())
78107 ->method ('getTime ' );
@@ -87,7 +116,16 @@ public function testRunWithExpiredTokenAndReadOnlyConfigFile(): void {
87116 $ this ->config
88117 ->expects ($ this ->never ())
89118 ->method ('deleteSystemValue ' );
119+ $ this ->appConfig
120+ ->expects ($ this ->never ())
121+ ->method ('deleteKey ' );
122+ $ this ->logger
123+ ->expects ($ this ->never ())
124+ ->method ('warning ' );
125+ $ this ->logger
126+ ->expects ($ this ->once ())
127+ ->method ('debug ' );
90128
91- static :: invokePrivate ($ this ->resetTokenBackgroundJob , 'run ' , [null ]);
129+ $ this -> invokePrivate ($ this ->resetTokenBackgroundJob , 'run ' , [null ]);
92130 }
93131}
0 commit comments