return new SimpleMethodInvocation(targetObject, method, args);
return new SimpleMethodInvocation(targetObject, method, args);
+ "'"); mi = new SimpleMethodInvocation(null, m);
+ "'"); mi = new SimpleMethodInvocation(null, m);
@Test(expected = IllegalArgumentException.class) public void testRejectsNulls2() { new AuthorizedEvent(new SimpleMethodInvocation(), null, new UsernamePasswordAuthenticationToken("foo", "bar")); }
@Test(expected = IllegalArgumentException.class) public void rejectsNullAuthentication() { new AuthorizationFailureEvent(new SimpleMethodInvocation(), attributes, null, exception); }
@Test(expected = IllegalArgumentException.class) public void rejectsNullException() { new AuthorizationFailureEvent(new SimpleMethodInvocation(), attributes, foo, null); }
@Test(expected = IllegalArgumentException.class) public void rejectsNullAttributesList() { new AuthorizationFailureEvent(new SimpleMethodInvocation(), null, foo, exception); }
@Test(expected = IllegalArgumentException.class) public void testRejectsNulls3() { new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"), null); } }
@Test(expected = IllegalArgumentException.class) public void testRejectsNulls3() { new AuthorizedEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"), null); } }
@Test(expected = IllegalArgumentException.class) public void testRejectsNulls2() { new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(), null, new AuthenticationCredentialsNotFoundException("test")); }
@Test public void returnsEmptyListIfDelegateReturnsNull() throws Exception { List sources = new ArrayList(); MethodSecurityMetadataSource delegate = mock(MethodSecurityMetadataSource.class); when(delegate.getAttributes(Matchers.<Method> any(), Matchers.any(Class.class))) .thenReturn(null); sources.add(delegate); mds = new DelegatingMethodSecurityMetadataSource(sources); assertThat(mds.getMethodSecurityMetadataSources()).isSameAs(sources); assertThat(mds.getAllConfigAttributes().isEmpty()).isTrue(); MethodInvocation mi = new SimpleMethodInvocation(null, String.class.getMethod("toString")); assertThat(mds.getAttributes(mi)).isEqualTo(Collections.emptyList()); // Exercise the cached case assertThat(mds.getAttributes(mi)).isEqualTo(Collections.emptyList()); }
@Test public void hasRoleExpressionDeniesUserWithoutRole() throws Exception { List<ConfigAttribute> cad = new ArrayList<>(1); cad.add(new PreInvocationExpressionAttribute(null, null, "hasRole('joedoesnt')")); MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAnArray()); assertThat(am.vote(joe, mi, cad)).isEqualTo(AccessDecisionVoter.ACCESS_DENIED); }
@Test public void returnsDelegateAttributes() throws Exception { List sources = new ArrayList(); MethodSecurityMetadataSource delegate = mock(MethodSecurityMetadataSource.class); ConfigAttribute ca = mock(ConfigAttribute.class); List attributes = Arrays.asList(ca); Method toString = String.class.getMethod("toString"); when(delegate.getAttributes(toString, String.class)).thenReturn(attributes); sources.add(delegate); mds = new DelegatingMethodSecurityMetadataSource(sources); assertThat(mds.getMethodSecurityMetadataSources()).isSameAs(sources); assertThat(mds.getAllConfigAttributes().isEmpty()).isTrue(); MethodInvocation mi = new SimpleMethodInvocation("", toString); assertThat(mds.getAttributes(mi)).isSameAs(attributes); // Exercise the cached case assertThat(mds.getAttributes(mi)).isSameAs(attributes); assertThat(mds.getAttributes( new SimpleMethodInvocation(null, String.class.getMethod("length")))).isEmpty(); }
@Test(expected = IllegalArgumentException.class) public void detectsIfInvocationPassedIncompatibleSecureObject() throws Exception { MockSecurityInterceptorWhichOnlySupportsStrings si = new MockSecurityInterceptorWhichOnlySupportsStrings(); si.setRunAsManager(mock(RunAsManager.class)); si.setAuthenticationManager(mock(AuthenticationManager.class)); si.setAfterInvocationManager(mock(AfterInvocationManager.class)); si.setAccessDecisionManager(mock(AccessDecisionManager.class)); si.setSecurityMetadataSource(mock(SecurityMetadataSource.class)); si.beforeInvocation(new SimpleMethodInvocation()); }
private ContextPropagatingRemoteInvocation getRemoteInvocation() throws Exception { Class<TargetObject> clazz = TargetObject.class; Method method = clazz.getMethod("makeLowerCase", new Class[] { String.class }); MethodInvocation mi = new SimpleMethodInvocation(new TargetObject(), method, "SOME_STRING"); ContextPropagatingRemoteInvocationFactory factory = new ContextPropagatingRemoteInvocationFactory(); return (ContextPropagatingRemoteInvocation) factory.createRemoteInvocation(mi); }
@Test public void testOperation() { List<ConfigAttribute> attr = SecurityConfig.createList("FOO"); MethodInvocation mi = new SimpleMethodInvocation(); SecurityContext ctx = SecurityContextHolder.createEmptyContext(); InterceptorStatusToken token = new InterceptorStatusToken(ctx, true, attr, mi); assertThat(token.isContextHolderRefreshRequired()).isTrue(); assertThat(token.getAttributes()).isEqualTo(attr); assertThat(token.getSecureObject()).isEqualTo(mi); assertThat(token.getSecurityContext()).isSameAs(ctx); } }
@Test public void hasRoleExpressionAllowsUserWithRole() throws Exception { MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAnArray()); assertThat(am.vote(joe, mi, createAttributes(new PreInvocationExpressionAttribute(null, null, "hasRole('blah')")))).isEqualTo(AccessDecisionVoter.ACCESS_GRANTED); }
@Test public void matchingArgAgainstAuthenticationNameIsSuccessful() throws Exception { MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAString(), "joe"); assertThat(am.vote(joe, mi, createAttributes(new PreInvocationExpressionAttribute(null, null, "(#argument == principal) and (principal == 'joe')")))) .isEqualTo(AccessDecisionVoter.ACCESS_GRANTED); }
@Test public void ruleDefinedInAClassMethodIsApplied() throws Exception { MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAString(), "joe"); assertThat( am.vote(joe, mi, createAttributes(new PreInvocationExpressionAttribute(null, null, "T(org.springframework.security.access.expression.method.SecurityRules).isJoe(#argument)")))) .isEqualTo(AccessDecisionVoter.ACCESS_GRANTED); }