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 ;
2426use Test \TestCase ;
2527
2628class ManagerTest extends TestCase {
27- /** @var Factory|MockObject */
28- private $ factory ;
29- /** @var IAppData|MockObject */
30- private $ appData ;
31- /** @var ICrypto|MockObject */
32- private $ crypto ;
33- /** @var Manager|MockObject */
34- private $ manager ;
35- /** @var IConfig|MockObject */
36- private $ config ;
37- /** @var LoggerInterface|MockObject */
38- private $ logger ;
29+ private Factory &MockObject $ factory ;
30+ private IAppData &MockObject $ appData ;
31+ private ICrypto &MockObject $ crypto ;
32+ private Manager &MockObject $ manager ;
33+ private IConfig &MockObject $ config ;
34+ private LoggerInterface &MockObject $ logger ;
35+ private ICacheFactory &MockObject $ cacheFactory ;
36+ private ICache &MockObject $ cache ;
3937
4038 protected function setUp (): void {
4139 parent ::setUp ();
@@ -49,6 +47,12 @@ protected function setUp(): void {
4947 ->with ('identityproof ' )
5048 ->willReturn ($ this ->appData );
5149 $ this ->logger = $ this ->createMock (LoggerInterface::class);
50+ $ this ->cacheFactory = $ this ->createMock (ICacheFactory::class);
51+ $ this ->cache = $ this ->createMock (ICache::class);
52+
53+ $ this ->cacheFactory ->expects ($ this ->any ())
54+ ->method ('createDistributed ' )
55+ ->willReturn ($ this ->cache );
5256
5357 $ this ->crypto = $ this ->createMock (ICrypto::class);
5458 $ this ->manager = $ this ->getManager (['generateKeyPair ' ]);
@@ -66,15 +70,17 @@ protected function getManager($setMethods = []) {
6670 $ this ->factory ,
6771 $ this ->crypto ,
6872 $ this ->config ,
69- $ this ->logger
73+ $ this ->logger ,
74+ $ this ->cacheFactory ,
7075 );
7176 } else {
7277 return $ this ->getMockBuilder (Manager::class)
7378 ->setConstructorArgs ([
7479 $ this ->factory ,
7580 $ this ->crypto ,
7681 $ this ->config ,
77- $ this ->logger
82+ $ this ->logger ,
83+ $ this ->cacheFactory ,
7884 ])->setMethods ($ setMethods )->getMock ();
7985 }
8086 }
@@ -117,6 +123,33 @@ public function testGetKeyWithExistingKey(): void {
117123 ->method ('getFolder ' )
118124 ->with ('user-MyUid ' )
119125 ->willReturn ($ folder );
126+ $ this ->cache
127+ ->expects ($ this ->exactly (2 ))
128+ ->method ('get ' )
129+ ->willReturn (null );
130+
131+ $ expected = new Key ('MyPublicKey ' , 'MyPrivateKey ' );
132+ $ this ->assertEquals ($ expected , $ this ->manager ->getKey ($ user ));
133+ }
134+
135+ public function testGetKeyWithExistingKeyCached (): void {
136+ $ user = $ this ->createMock (IUser::class);
137+ $ user
138+ ->expects ($ this ->once ())
139+ ->method ('getUID ' )
140+ ->willReturn ('MyUid ' );
141+ $ this ->crypto
142+ ->expects ($ this ->once ())
143+ ->method ('decrypt ' )
144+ ->with ('EncryptedPrivateKey ' )
145+ ->willReturn ('MyPrivateKey ' );
146+ $ this ->cache
147+ ->expects ($ this ->exactly (2 ))
148+ ->method ('get ' )
149+ ->willReturnMap ([
150+ ['user-MyUid-public ' , 'MyPublicKey ' ],
151+ ['user-MyUid-private ' , 'EncryptedPrivateKey ' ],
152+ ]);
120153
121154 $ expected = new Key ('MyPublicKey ' , 'MyPrivateKey ' );
122155 $ this ->assertEquals ($ expected , $ this ->manager ->getKey ($ user ));
0 commit comments