Skip to content

Commit d81a405

Browse files
committed
Added test for expired token.
1 parent c0af7bc commit d81a405

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/test/java/io/github/majusko/grpc/jwt/GrpcJwtSpringBootStarterApplicationTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import io.github.majusko.grpc.jwt.data.AuthContextData;
88
import io.github.majusko.grpc.jwt.data.GrpcHeader;
99
import io.github.majusko.grpc.jwt.data.GrpcJwtContext;
10-
import io.github.majusko.grpc.jwt.interceptor.*;
10+
import io.github.majusko.grpc.jwt.interceptor.AllowedCollector;
11+
import io.github.majusko.grpc.jwt.interceptor.AuthClientInterceptor;
12+
import io.github.majusko.grpc.jwt.interceptor.AuthServerInterceptor;
1113
import io.github.majusko.grpc.jwt.interceptor.proto.Example;
1214
import io.github.majusko.grpc.jwt.interceptor.proto.ExampleServiceGrpc;
1315
import io.github.majusko.grpc.jwt.service.GrpcRole;
@@ -317,7 +319,7 @@ public void testEmptyUserIdInToken() throws IOException {
317319
}
318320

319321
@Test
320-
public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException,
322+
public void testExpiredInternalToken() throws IOException, NoSuchFieldException, IllegalAccessException,
321323
NoSuchMethodException, InvocationTargetException, InterruptedException {
322324

323325
final GrpcJwtProperties customProperties = new GrpcJwtProperties();
@@ -351,6 +353,39 @@ public void testExpiredToken() throws IOException, NoSuchFieldException, Illegal
351353
refreshMethod.invoke(jwtService);
352354
}
353355

356+
@Test
357+
public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException {
358+
359+
final GrpcJwtProperties customProperties = new GrpcJwtProperties();
360+
final Field field = customProperties.getClass().getDeclaredField("expirationSec");
361+
field.setAccessible(true);
362+
field.set(customProperties, -10L);
363+
364+
final JwtService customJwtService = new JwtService(environment, customProperties);
365+
final String token = customJwtService.generate(new JwtData("lala", Sets.newHashSet(ExampleService.ADMIN)));
366+
367+
final ManagedChannel channel = initTestServer(new ExampleService());
368+
final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor);
369+
final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel);
370+
371+
final Metadata header = new Metadata();
372+
header.put(GrpcHeader.AUTHORIZATION, token);
373+
374+
final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header);
375+
final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder()
376+
.setUserId("other-user-id").build();
377+
378+
Status status = Status.OK;
379+
380+
try {
381+
final Empty ignore = injectedStub.getExample(request);
382+
} catch (StatusRuntimeException e) {
383+
status = e.getStatus();
384+
}
385+
386+
Assert.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode());
387+
}
388+
354389
@Test
355390
public void testEmptyOwnerFieldInAnnotationSoRolesAreValidated() throws IOException {
356391
final String token = jwtService

0 commit comments

Comments
 (0)