@@ -131,6 +131,150 @@ function () {
131
131
\expect ($ fooRequest ->getHeaderLine ('Authorization ' ))->not ()->toBe ('Bearer expired-token ' );
132
132
});
133
133
134
+ \it ('uses different cache key if client id is changed ' , function () {
135
+ $ okResponse = new Response (200 );
136
+ $ encrypter = new TrueLayer \Services \Util \Encryption \Encrypter (\hex2bin ('31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' ), TrueLayer \Constants \Encryption::ALGORITHM );
137
+ $ encryptedAccessToken = $ encrypter ->encrypt ([
138
+ 'access_token ' => Mocks \AuthResponse::ACCESS_TOKEN ,
139
+ 'expires_in ' => 3600 ,
140
+ 'retrieved_at ' => (int ) Carbon::now ()->timestamp ,
141
+ ]);
142
+
143
+ $ cacheMock1 = Mockery::mock (Psr \SimpleCache \CacheInterface::class);
144
+ $ cacheMock1 ->shouldReceive ('has ' )->andReturnTrue ();
145
+ $ cacheMock1 ->shouldReceive ('set ' )->andReturnTrue ();
146
+ $ cacheMock1 ->shouldReceive ('get ' )->andReturn ($ encryptedAccessToken );
147
+
148
+ $ client1 = \rawClient ([Mocks \AuthResponse::success (), $ okResponse , $ okResponse ])
149
+ ->cache ($ cacheMock1 , '31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' )
150
+ ->clientId ('client_id_1 ' )
151
+ ->create ()
152
+ ->getApiClient ();
153
+ $ client1 ->request ()->uri ('/foo ' )->post ();
154
+
155
+ $ cacheKey1 = null ;
156
+ $ cacheDefaultValue = 'not-null ' ;
157
+
158
+ $ cacheMock1 ->shouldHaveReceived ('get ' , function (...$ args ) use (&$ cacheKey1 , &$ cacheDefaultValue ) {
159
+ $ cacheKey1 = $ args [0 ];
160
+ $ cacheDefaultValue = $ args [1 ];
161
+ return true ;
162
+ });
163
+ \expect ($ cacheKey1 )->toBeString ();
164
+ \expect ($ cacheKey1 )->not ()->toBeEmpty ();
165
+ \expect ($ cacheDefaultValue )->toBeNull ();
166
+
167
+ $ cacheMock2 = Mockery::mock (Psr \SimpleCache \CacheInterface::class);
168
+ $ cacheMock2 ->shouldReceive ('has ' )->andReturnTrue ();
169
+ $ cacheMock2 ->shouldReceive ('set ' )->andReturnTrue ();
170
+ $ cacheMock2 ->shouldReceive ('get ' )->andReturn ($ encryptedAccessToken );
171
+
172
+ $ client2 = \rawClient ([Mocks \AuthResponse::success (), $ okResponse , $ okResponse ])
173
+ ->cache ($ cacheMock2 , '31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' )
174
+ ->clientId ('client_id_2 ' )
175
+ ->create ()
176
+ ->getApiClient ();
177
+ $ client2 ->request ()->uri ('/foo ' )->post ();
178
+
179
+ $ cacheMock2 ->shouldNotHaveReceived ('get ' , [$ cacheKey1 , $ cacheDefaultValue ]);
180
+ });
181
+
182
+ \it ('uses different cache key if client secret is changed ' , function () {
183
+ $ okResponse = new Response (200 );
184
+ $ encrypter = new TrueLayer \Services \Util \Encryption \Encrypter (\hex2bin ('31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' ), TrueLayer \Constants \Encryption::ALGORITHM );
185
+ $ encryptedAccessToken = $ encrypter ->encrypt ([
186
+ 'access_token ' => Mocks \AuthResponse::ACCESS_TOKEN ,
187
+ 'expires_in ' => 3600 ,
188
+ 'retrieved_at ' => (int ) Carbon::now ()->timestamp ,
189
+ ]);
190
+
191
+ $ cacheMock1 = Mockery::mock (Psr \SimpleCache \CacheInterface::class);
192
+ $ cacheMock1 ->shouldReceive ('has ' )->andReturnTrue ();
193
+ $ cacheMock1 ->shouldReceive ('set ' )->andReturnTrue ();
194
+ $ cacheMock1 ->shouldReceive ('get ' )->andReturn ($ encryptedAccessToken );
195
+
196
+ $ client1 = \rawClient ([Mocks \AuthResponse::success (), $ okResponse , $ okResponse ])
197
+ ->cache ($ cacheMock1 , '31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' )
198
+ ->clientSecret ('client_secret_1 ' )
199
+ ->create ()
200
+ ->getApiClient ();
201
+ $ client1 ->request ()->uri ('/foo ' )->post ();
202
+
203
+ $ cacheKey1 = null ;
204
+ $ cacheDefaultValue = 'not-null ' ;
205
+
206
+ $ cacheMock1 ->shouldHaveReceived ('get ' , function (...$ args ) use (&$ cacheKey1 , &$ cacheDefaultValue ) {
207
+ $ cacheKey1 = $ args [0 ];
208
+ $ cacheDefaultValue = $ args [1 ];
209
+ return true ;
210
+ });
211
+ \expect ($ cacheKey1 )->toBeString ();
212
+ \expect ($ cacheKey1 )->not ()->toBeEmpty ();
213
+ \expect ($ cacheDefaultValue )->toBeNull ();
214
+
215
+ $ cacheMock2 = Mockery::mock (Psr \SimpleCache \CacheInterface::class);
216
+ $ cacheMock2 ->shouldReceive ('has ' )->andReturnTrue ();
217
+ $ cacheMock2 ->shouldReceive ('set ' )->andReturnTrue ();
218
+ $ cacheMock2 ->shouldReceive ('get ' )->andReturn ($ encryptedAccessToken );
219
+
220
+ $ client2 = \rawClient ([Mocks \AuthResponse::success (), $ okResponse , $ okResponse ])
221
+ ->cache ($ cacheMock2 , '31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' )
222
+ ->clientSecret ('client_secret_2 ' )
223
+ ->create ()
224
+ ->getApiClient ();
225
+ $ client2 ->request ()->uri ('/foo ' )->post ();
226
+
227
+ $ cacheMock2 ->shouldNotHaveReceived ('get ' , [$ cacheKey1 , $ cacheDefaultValue ]);
228
+ });
229
+
230
+ \it ('uses different cache key if scopes are changed ' , function () {
231
+ $ okResponse = new Response (200 );
232
+ $ encrypter = new TrueLayer \Services \Util \Encryption \Encrypter (\hex2bin ('31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' ), TrueLayer \Constants \Encryption::ALGORITHM );
233
+ $ encryptedAccessToken = $ encrypter ->encrypt ([
234
+ 'access_token ' => Mocks \AuthResponse::ACCESS_TOKEN ,
235
+ 'expires_in ' => 3600 ,
236
+ 'retrieved_at ' => (int ) Carbon::now ()->timestamp ,
237
+ ]);
238
+
239
+ $ cacheMock1 = Mockery::mock (Psr \SimpleCache \CacheInterface::class);
240
+ $ cacheMock1 ->shouldReceive ('has ' )->andReturnTrue ();
241
+ $ cacheMock1 ->shouldReceive ('set ' )->andReturnTrue ();
242
+ $ cacheMock1 ->shouldReceive ('get ' )->andReturn ($ encryptedAccessToken );
243
+
244
+ $ client1 = \rawClient ([Mocks \AuthResponse::success (), $ okResponse , $ okResponse ])
245
+ ->cache ($ cacheMock1 , '31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' )
246
+ ->scopes ('payments ' )
247
+ ->create ()
248
+ ->getApiClient ();
249
+ $ client1 ->request ()->uri ('/foo ' )->post ();
250
+
251
+ $ cacheKey1 = null ;
252
+ $ cacheDefaultValue = 'not-null ' ;
253
+
254
+ $ cacheMock1 ->shouldHaveReceived ('get ' , function (...$ args ) use (&$ cacheKey1 , &$ cacheDefaultValue ) {
255
+ $ cacheKey1 = $ args [0 ];
256
+ $ cacheDefaultValue = $ args [1 ];
257
+ return true ;
258
+ });
259
+ \expect ($ cacheKey1 )->toBeString ();
260
+ \expect ($ cacheKey1 )->not ()->toBeEmpty ();
261
+ \expect ($ cacheDefaultValue )->toBeNull ();
262
+
263
+ $ cacheMock2 = Mockery::mock (Psr \SimpleCache \CacheInterface::class);
264
+ $ cacheMock2 ->shouldReceive ('has ' )->andReturnTrue ();
265
+ $ cacheMock2 ->shouldReceive ('set ' )->andReturnTrue ();
266
+ $ cacheMock2 ->shouldReceive ('get ' )->andReturn ($ encryptedAccessToken );
267
+
268
+ $ client2 = \rawClient ([Mocks \AuthResponse::success (), $ okResponse , $ okResponse ])
269
+ ->cache ($ cacheMock2 , '31c8d81a110849f83131541b9f67c3cba9c7e0bb103bc4dd19377f0fdf2d924b ' )
270
+ ->scopes ('payments ' , 'accounts ' )
271
+ ->create ()
272
+ ->getApiClient ();
273
+ $ client2 ->request ()->uri ('/foo ' )->post ();
274
+
275
+ $ cacheMock2 ->shouldNotHaveReceived ('get ' , [$ cacheKey1 , $ cacheDefaultValue ]);
276
+ });
277
+
134
278
\it ('uses default scope if none provided ' , function () {
135
279
$ client = \rawClient ([Mocks \AuthResponse::success (), new Response (200 )])->create ();
136
280
$ client ->getApiClient ()->request ()->uri ('/test ' )->post ();
0 commit comments