Skip to content

Commit

Permalink
test for #175
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Furer committed Nov 3, 2021
1 parent d8c6541 commit 066c3a7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import io.grpc.examples.CalculatorGrpc;
import io.grpc.examples.CalculatorOuterClass;
import io.grpc.examples.SecuredCalculatorGrpc;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.annotation.Secured;

@Configuration
public class DemoAppConfiguration {
Expand All @@ -21,7 +19,7 @@ public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObse


}
static CalculatorOuterClass.CalculatorResponse calculate(CalculatorOuterClass.CalculatorRequest request){
public static CalculatorOuterClass.CalculatorResponse calculate(CalculatorOuterClass.CalculatorRequest request){
CalculatorOuterClass.CalculatorResponse.Builder resultBuilder = CalculatorOuterClass.CalculatorResponse.newBuilder();
switch (request.getOperation()){
case ADD:
Expand All @@ -45,17 +43,5 @@ static CalculatorOuterClass.CalculatorResponse calculate(CalculatorOuterClass.Ca

}

@GRpcService(interceptors = NotSpringBeanInterceptor.class)
@Secured({})
public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase{
@Override
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
responseObserver.onNext(CalculatorService.calculate(request));
responseObserver.onCompleted();


}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.CalculatorOuterClass;
import io.grpc.examples.SecuredCalculatorGrpc;
import io.grpc.examples.tasks.Assignment;
import io.grpc.examples.tasks.Person;
import io.grpc.examples.tasks.TaskServiceGrpc;
Expand All @@ -10,9 +12,12 @@
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.GRpcService;
import org.lognet.springboot.grpc.GrpcServerTestBase;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.demo.DemoAppConfiguration;
import org.lognet.springboot.grpc.demo.ITaskService;
import org.lognet.springboot.grpc.demo.NotSpringBeanInterceptor;
import org.lognet.springboot.grpc.security.AuthCallCredentials;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.GrpcSecurity;
Expand All @@ -22,6 +27,7 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.test.context.junit4.SpringRunner;
Expand Down Expand Up @@ -75,6 +81,17 @@ public List<T> get(Duration duration) throws Throwable {

@TestConfiguration
static class TestCfg extends GrpcSecurityConfigurerAdapter {
@GRpcService(interceptors = NotSpringBeanInterceptor.class)
@PreAuthorize("isAuthenticated()")
public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase{
@Override
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
responseObserver.onNext(DemoAppConfiguration.CalculatorService.calculate(request));
responseObserver.onCompleted();


}
}
@Override
public void configure(GrpcSecurity builder) throws Exception {
builder.authorizeRequests()
Expand Down Expand Up @@ -119,6 +136,33 @@ public void configure(GrpcSecurity builder) throws Exception {
@MockBean
private ITaskService service;

@Test
public void preAuthAnnotationOnClassTest() {


final SecuredCalculatorGrpc.SecuredCalculatorBlockingStub stub = SecuredCalculatorGrpc
.newBlockingStub(selectedChanel);

final CalculatorOuterClass.CalculatorResponse response = stub
.withCallCredentials(user2Credentials())
.calculate(CalculatorOuterClass.CalculatorRequest.newBuilder()
.setNumber1(1)
.setNumber2(1)
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
.build());
assertThat(response.getResult(),Matchers.is(2d));

final StatusRuntimeException statusRuntimeException = Assert.assertThrows(StatusRuntimeException.class, () -> {
stub.withCallCredentials(unAuthUserCredentials())
.calculate(CalculatorOuterClass.CalculatorRequest.newBuilder()
.setNumber1(1)
.setNumber2(1)
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
.build());
});
assertThat(statusRuntimeException.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED));
}

@Test
public void unaryPreAuthorizeCallTest() {
final TaskServiceGrpc.TaskServiceBlockingStub stub = TaskServiceGrpc.newBlockingStub(getChannel())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import io.grpc.examples.CalculatorOuterClass;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.SecuredCalculatorGrpc;
import io.grpc.stub.StreamObserver;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.GRpcService;
import org.lognet.springboot.grpc.GrpcServerTestBase;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.demo.DemoAppConfiguration;
import org.lognet.springboot.grpc.demo.NotSpringBeanInterceptor;
import org.lognet.springboot.grpc.security.AuthClientInterceptor;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.GrpcSecurity;
Expand All @@ -24,6 +28,7 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
Expand All @@ -45,6 +50,18 @@ public class UserDetailsAuthTest extends GrpcServerTestBase {

@TestConfiguration
static class TestCfg extends GrpcSecurityConfigurerAdapter {

@GRpcService(interceptors = NotSpringBeanInterceptor.class)
@Secured({})
public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase{
@Override
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
responseObserver.onNext(DemoAppConfiguration.CalculatorService.calculate(request));
responseObserver.onCompleted();


}
}
static final String pwd="strongPassword1";

@Bean
Expand Down

0 comments on commit 066c3a7

Please sign in to comment.