1616use OCP \Files \IAppData ;
1717use OCP \Files \SimpleFS \ISimpleFile ;
1818use OCP \Files \SimpleFS \ISimpleFolder ;
19+ use OCP \ICache ;
20+ use OCP \ICacheFactory ;
1921use OCP \IConfig ;
2022use OCP \IUser ;
2123use OCP \Security \ICrypto ;
@@ -36,6 +38,10 @@ class ManagerTest extends TestCase {
3638 private $ config ;
3739 /** @var LoggerInterface|MockObject */
3840 private $ logger ;
41+ /** @var LoggerInterface|ICacheFactory */
42+ private $ cacheFactory ;
43+ /** @var LoggerInterface|ICache */
44+ private $ cache ;
3945
4046 protected function setUp (): void {
4147 parent ::setUp ();
@@ -49,6 +55,12 @@ protected function setUp(): void {
4955 ->with ('identityproof ' )
5056 ->willReturn ($ this ->appData );
5157 $ this ->logger = $ this ->createMock (LoggerInterface::class);
58+ $ this ->cacheFactory = $ this ->createMock (ICacheFactory::class);
59+ $ this ->cache = $ this ->createMock (ICache::class);
60+
61+ $ this ->cacheFactory ->expects ($ this ->any ())
62+ ->method ('createDistributed ' )
63+ ->willReturn ($ this ->cache );
5264
5365 $ this ->crypto = $ this ->createMock (ICrypto::class);
5466 $ this ->manager = $ this ->getManager (['generateKeyPair ' ]);
@@ -66,15 +78,17 @@ protected function getManager($setMethods = []) {
6678 $ this ->factory ,
6779 $ this ->crypto ,
6880 $ this ->config ,
69- $ this ->logger
81+ $ this ->logger ,
82+ $ this ->cacheFactory ,
7083 );
7184 } else {
7285 return $ this ->getMockBuilder (Manager::class)
7386 ->setConstructorArgs ([
7487 $ this ->factory ,
7588 $ this ->crypto ,
7689 $ this ->config ,
77- $ this ->logger
90+ $ this ->logger ,
91+ $ this ->cacheFactory ,
7892 ])
7993 ->onlyMethods ($ setMethods )
8094 ->getMock ();
@@ -115,6 +129,28 @@ public function testGetKeyWithExistingKey(): void {
115129 ->method ('getFolder ' )
116130 ->with ('user-MyUid ' )
117131 ->willReturn ($ folder );
132+ $ this ->cache ->expects ($ this ->exactly (2 ))
133+ ->method ('get ' )
134+ ->willReturnOnConsecutiveCalls (null , null );
135+
136+ $ expected = new Key ('MyPublicKey ' , 'MyPrivateKey ' );
137+ $ this ->assertEquals ($ expected , $ this ->manager ->getKey ($ user ));
138+ }
139+
140+ public function testGetKeyWithExistingKeyCached (): void {
141+ $ user = $ this ->createMock (IUser::class);
142+ $ user
143+ ->expects ($ this ->once ())
144+ ->method ('getUID ' )
145+ ->willReturn ('MyUid ' );
146+ $ this ->crypto
147+ ->expects ($ this ->once ())
148+ ->method ('decrypt ' )
149+ ->with ('EncryptedPrivateKey ' )
150+ ->willReturn ('MyPrivateKey ' );
151+ $ this ->cache ->expects ($ this ->exactly (2 ))
152+ ->method ('get ' )
153+ ->willReturnOnConsecutiveCalls ('MyPublicKey ' , 'EncryptedPrivateKey ' );
118154
119155 $ expected = new Key ('MyPublicKey ' , 'MyPrivateKey ' );
120156 $ this ->assertEquals ($ expected , $ this ->manager ->getKey ($ user ));
0 commit comments