|
23 | 23 | import org.lognet.springboot.grpc.GRpcService; |
24 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 25 | import org.springframework.boot.test.context.SpringBootTest; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.annotation.Import; |
| 29 | +import org.springframework.context.annotation.Primary; |
26 | 30 | import org.springframework.core.env.Environment; |
27 | 31 | import org.springframework.test.context.ActiveProfiles; |
28 | 32 | import org.springframework.test.context.junit4.SpringRunner; |
29 | 33 |
|
30 | 34 | import java.io.IOException; |
31 | 35 | import java.lang.reflect.Field; |
| 36 | +import java.lang.reflect.InvocationTargetException; |
| 37 | +import java.lang.reflect.Method; |
32 | 38 |
|
33 | 39 | @RunWith(SpringRunner.class) |
34 | 40 | @SpringBootTest |
@@ -312,37 +318,38 @@ public void testEmptyUserIdInToken() throws IOException { |
312 | 318 | } |
313 | 319 |
|
314 | 320 | @Test |
315 | | - public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException { |
| 321 | + public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException, |
| 322 | + NoSuchMethodException, InvocationTargetException, InterruptedException { |
316 | 323 |
|
317 | 324 | final GrpcJwtProperties customProperties = new GrpcJwtProperties(); |
318 | 325 | final Field field = customProperties.getClass().getDeclaredField("expirationSec"); |
319 | 326 | field.setAccessible(true); |
320 | | - field.set(customProperties, -10L); |
| 327 | + field.set(customProperties, 1L); |
321 | 328 |
|
| 329 | + final Field propertyField = jwtService.getClass().getDeclaredField("properties"); |
| 330 | + propertyField.setAccessible(true); |
| 331 | + final GrpcJwtProperties existingProperties = (GrpcJwtProperties) propertyField.get(jwtService); |
| 332 | + propertyField.set(jwtService, customProperties); |
322 | 333 |
|
323 | | - final JwtService customJwtService = new JwtService(environment, customProperties); |
324 | | - final String token = customJwtService.generate(new JwtData("lala", Sets.newHashSet(ExampleService.ADMIN))); |
| 334 | + final Method refreshMethod = jwtService.getClass().getDeclaredMethod("refreshInternalToken"); |
| 335 | + refreshMethod.setAccessible(true); |
| 336 | + |
| 337 | + refreshMethod.invoke(jwtService); |
325 | 338 |
|
326 | 339 | final ManagedChannel channel = initTestServer(new ExampleService()); |
327 | 340 | final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor); |
328 | 341 | final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel); |
329 | | - |
330 | | - final Metadata header = new Metadata(); |
331 | | - header.put(GrpcHeader.AUTHORIZATION, token); |
332 | | - |
333 | | - final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header); |
334 | 342 | final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder() |
335 | 343 | .setUserId("other-user-id").build(); |
336 | 344 |
|
337 | | - Status status = Status.OK; |
| 345 | + Thread.sleep(2000); |
338 | 346 |
|
339 | | - try { |
340 | | - final Empty ignore = injectedStub.getExample(request); |
341 | | - } catch (StatusRuntimeException e) { |
342 | | - status = e.getStatus(); |
343 | | - } |
| 347 | + final Empty response = stub.getExample(request); |
344 | 348 |
|
345 | | - Assert.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode()); |
| 349 | + Assert.assertNotNull(response); |
| 350 | + |
| 351 | + propertyField.set(jwtService, existingProperties); |
| 352 | + refreshMethod.invoke(jwtService); |
346 | 353 | } |
347 | 354 |
|
348 | 355 | @Test |
|
0 commit comments