|
25 | 25 |
|
26 | 26 | import com.google.api.server.spi.EndpointMethod; |
27 | 27 | import com.google.api.server.spi.auth.common.User; |
| 28 | +import com.google.api.server.spi.config.AuthLevel; |
28 | 29 | import com.google.api.server.spi.config.Named; |
29 | 30 | import com.google.api.server.spi.config.Nullable; |
| 31 | +import com.google.api.server.spi.config.model.ApiMethodConfig; |
| 32 | +import com.google.api.server.spi.response.UnauthorizedException; |
30 | 33 | import com.google.api.server.spi.testing.TestEndpoint; |
31 | 34 | import com.google.api.server.spi.testing.TestEndpoint.Request; |
32 | 35 | import com.google.api.server.spi.types.DateAndTime; |
|
39 | 42 | import org.junit.Test; |
40 | 43 | import org.junit.runner.RunWith; |
41 | 44 | import org.mockito.Mock; |
| 45 | +import org.mockito.Mockito; |
42 | 46 | import org.mockito.runners.MockitoJUnitRunner; |
43 | 47 |
|
44 | 48 | import java.io.ByteArrayInputStream; |
@@ -640,7 +644,7 @@ public void user(TestUser user) {} |
640 | 644 | final TestUser user = new TestUser("test"); |
641 | 645 | Method method = TestUserEndpoint.class.getDeclaredMethod("user", TestUser.class); |
642 | 646 | ParamReader reader = new ServletRequestParamReader( |
643 | | - EndpointMethod.create(method.getDeclaringClass(), method), request, context, null) { |
| 647 | + EndpointMethod.create(method.getDeclaringClass(), method), request, context, null, null) { |
644 | 648 | @Override |
645 | 649 | User getUser() { |
646 | 650 | return user; |
@@ -768,19 +772,75 @@ public void prettyPrint(@Named("prettyPrint") String prettyPrint) {} |
768 | 772 | assertEquals(true, params[0]); |
769 | 773 | } |
770 | 774 |
|
| 775 | + @Test |
| 776 | + public void testUserInjectionThrowsExceptionIfRequired() throws Exception { |
| 777 | + @SuppressWarnings("unused") |
| 778 | + class TestUser { |
| 779 | + @SuppressWarnings("unused") |
| 780 | + public void getUser(User user) { } |
| 781 | + } |
| 782 | + ApiMethodConfig methodConfig = Mockito.mock(ApiMethodConfig.class); |
| 783 | + when(methodConfig.getAuthLevel()).thenReturn(AuthLevel.REQUIRED); |
| 784 | + methodConfig.setAuthLevel(AuthLevel.REQUIRED); |
| 785 | + try { |
| 786 | + Method method = TestUser.class.getDeclaredMethod("getUser", User.class); |
| 787 | + readParameters( |
| 788 | + "{}", EndpointMethod.create(method.getDeclaringClass(), method), |
| 789 | + methodConfig, |
| 790 | + null, |
| 791 | + null); |
| 792 | + fail("expected unauthorized method exception"); |
| 793 | + } catch (UnauthorizedException ex) { |
| 794 | + // expected |
| 795 | + } |
| 796 | + } |
| 797 | + |
| 798 | + @Test |
| 799 | + public void testAppEngineUserInjectionThrowsExceptionIfRequired() throws Exception { |
| 800 | + @SuppressWarnings("unused") |
| 801 | + class TestUser { |
| 802 | + @SuppressWarnings("unused") |
| 803 | + public void getUser(com.google.appengine.api.users.User user) { } |
| 804 | + } |
| 805 | + ApiMethodConfig methodConfig = Mockito.mock(ApiMethodConfig.class); |
| 806 | + when(methodConfig.getAuthLevel()).thenReturn(AuthLevel.REQUIRED); |
| 807 | + methodConfig.setAuthLevel(AuthLevel.REQUIRED); |
| 808 | + try { |
| 809 | + Method method = TestUser.class |
| 810 | + .getDeclaredMethod("getUser", com.google.appengine.api.users.User.class); |
| 811 | + readParameters( |
| 812 | + "{}", |
| 813 | + EndpointMethod.create(method.getDeclaringClass(), method), |
| 814 | + methodConfig, |
| 815 | + null, |
| 816 | + null); |
| 817 | + fail("expected unauthorized method exception"); |
| 818 | + } catch (UnauthorizedException ex) { |
| 819 | + // expected |
| 820 | + } |
| 821 | + } |
| 822 | + |
771 | 823 | private Object[] readParameters(String input, Method method) throws Exception { |
772 | 824 | return readParameters(input, EndpointMethod.create(method.getDeclaringClass(), method)); |
773 | 825 | } |
774 | 826 |
|
775 | 827 | private Object[] readParameters(final String input, EndpointMethod method) throws Exception { |
776 | | - ParamReader reader = new ServletRequestParamReader(method, request, context, null) { |
| 828 | + return readParameters(input, method, null, USER, APP_ENGINE_USER); |
| 829 | + } |
| 830 | + |
| 831 | + private Object[] readParameters(final String input, EndpointMethod method, |
| 832 | + ApiMethodConfig methodConfig, final User user, |
| 833 | + final com.google.appengine.api.users.User appEngineUser) |
| 834 | + throws Exception { |
| 835 | + ParamReader reader = new ServletRequestParamReader( |
| 836 | + method, request, context, null, methodConfig) { |
777 | 837 | @Override |
778 | 838 | User getUser() { |
779 | | - return USER; |
| 839 | + return user; |
780 | 840 | } |
781 | 841 | @Override |
782 | 842 | com.google.appengine.api.users.User getAppEngineUser() { |
783 | | - return APP_ENGINE_USER; |
| 843 | + return appEngineUser; |
784 | 844 | } |
785 | 845 | }; |
786 | 846 | return readParameters(input, reader); |
|
0 commit comments