99
1010namespace  Test \Security \IdentityProof ;
1111
12+ use  Hoa \Iterator \Mock ;
1213use  OC \Files \AppData \AppData ;
1314use  OC \Files \AppData \Factory ;
1415use  OC \Security \IdentityProof \Key ;
1516use  OC \Security \IdentityProof \Manager ;
17+ use  OCA \WorkflowEngine \Service \Logger ;
1618use  OCP \Files \IAppData ;
1719use  OCP \Files \SimpleFS \ISimpleFile ;
1820use  OCP \Files \SimpleFS \ISimpleFolder ;
21+ use  OCP \ICache ;
22+ use  OCP \ICacheFactory ;
1923use  OCP \IConfig ;
2024use  OCP \IUser ;
2125use  OCP \Security \ICrypto ;
2428use  Test \TestCase ;
2529
2630class  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
31+ 	private  Factory MockObject $ factory
32+ 	private  IAppData MockObject $ appData
33+ 	private  ICrypto MockObject $ crypto
34+ 	private  Manager MockObject $ manager
35+ 	private  IConfig MockObject $ config
36+ 	private  LoggerInterface MockObject $ logger
37+ 	private  ICacheFactory MockObject $ cacheFactory
38+ 	private  ICache MockObject $ cache
3939
4040	protected  function  setUp (): void  {
4141		parent ::setUp ();
@@ -49,6 +49,12 @@ protected function setUp(): void {
4949			->with ('identityproof ' )
5050			->willReturn ($ this appData );
5151		$ this logger  = $ this createMock (LoggerInterface::class);
52+ 		$ this cacheFactory  = $ this createMock (ICacheFactory::class);
53+ 		$ this cache  = $ this createMock (ICache::class);
54+ 
55+ 		$ this cacheFactory ->expects ($ this any ())
56+ 			->method ('createDistributed ' )
57+ 			->willReturn ($ this cache );
5258
5359		$ this crypto  = $ this createMock (ICrypto::class);
5460		$ this manager  = $ this getManager (['generateKeyPair ' ]);
@@ -66,15 +72,17 @@ protected function getManager($setMethods = []) {
6672				$ this factory ,
6773				$ this crypto ,
6874				$ this config ,
69- 				$ this logger 
75+ 				$ this logger ,
76+ 				$ this cacheFactory ,
7077			);
7178		} else  {
7279			return  $ this getMockBuilder (Manager::class)
7380				->setConstructorArgs ([
7481					$ this factory ,
7582					$ this crypto ,
7683					$ this config ,
77- 					$ this logger 
84+ 					$ this logger ,
85+ 					$ this cacheFactory ,
7886				])
7987				->onlyMethods ($ setMethods
8088				->getMock ();
@@ -115,6 +123,33 @@ public function testGetKeyWithExistingKey(): void {
115123			->method ('getFolder ' )
116124			->with ('user-MyUid ' )
117125			->willReturn ($ folder
126+ 		$ this cache 
127+ 			->expects ($ this exactly (2 ))
128+ 			->method ('get ' )
129+ 			->willReturn (null );
130+ 
131+ 		$ expectednew  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+ 			]);
118153
119154		$ expectednew  Key ('MyPublicKey ' , 'MyPrivateKey ' );
120155		$ this assertEquals ($ expected$ this manager ->getKey ($ user
0 commit comments