@Test public void testEqualsWhenEqual() { AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test", ROLES_12); AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key", "Test", ROLES_12); assertThat(token2).isEqualTo(token1); }
@Test public void testNotEqualsDueToAbstractParentEqualsCheck() { AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test", ROLES_12); AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key", "DIFFERENT_PRINCIPAL", ROLES_12); assertThat(token1.equals(token2)).isFalse(); }
@Test(expected = IllegalArgumentException.class) public void constructorWhenPrincipalIsEmptyStringThenThrowIllegalArgumentException() throws Exception { new AnonymousAuthenticationToken("key", "", ROLES_12); } }
@Test public void testNotEqualsDueToKey() { AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test", ROLES_12); AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken( "DIFFERENT_KEY", "Test", ROLES_12); assertThat(token1.equals(token2)).isFalse(); }
private Authentication createAnonymous() { return new AnonymousAuthenticationToken("ignored", "ignored", AuthorityUtils.createAuthorityList("ignored")); }
private Authentication createAnonymousPrincipal() { return new AnonymousAuthenticationToken("key-1234", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); } }
private Authentication createAnonymousPrincipal() { return new AnonymousAuthenticationToken("key-1234", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); } }
@Test(expected = IllegalArgumentException.class) public void constructorWhenEmptyAuthoritiesThenThrowIllegalArgumentException() throws Exception { new AnonymousAuthenticationToken("key", "principal", Collections.<GrantedAuthority>emptyList()); }
@Test public void testNotEqualsDueToDifferentAuthenticationClass() { AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test", ROLES_12); UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken( "Test", "Password", ROLES_12); assertThat(token1.equals(token2)).isFalse(); }
@Test public void testSetAuthenticatedIgnored() { AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key", "Test", ROLES_12); assertThat(token.isAuthenticated()).isTrue(); token.setAuthenticated(false); assertThat(!token.isAuthenticated()).isTrue(); }
@Test public void principalIsEmptyForAnonymousUser() { AuthenticationSource source = new SpringSecurityAuthenticationSource(); SecurityContextHolder.getContext().setAuthentication( new AnonymousAuthenticationToken("key", "anonUser", AuthorityUtils .createAuthorityList("ignored"))); assertThat(source.getPrincipal()).isEqualTo(""); }
@Test public void testGetters() { AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key", "Test", ROLES_12); assertThat(token.getKeyHash()).isEqualTo("key".hashCode()); assertThat(token.getPrincipal()).isEqualTo("Test"); assertThat(token.getCredentials()).isEqualTo(""); assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains( "ROLE_ONE", "ROLE_TWO"); assertThat(token.isAuthenticated()).isTrue(); }
@Test public void testNormalOperation() throws Exception { AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider( "qwerty"); AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("qwerty", "Test", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO")); Authentication result = aap.authenticate(token); assertThat(token).isEqualTo(result); }
@Test public void testNullContextHolderWhenAnonymous() throws Exception { AnonymousAuthenticationToken anonymous = new AnonymousAuthenticationToken("key", "principal", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); SecurityContextHolder.getContext().setAuthentication(anonymous); // Create a connection and ensure our executor sets its // properties correctly AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor(); HttpURLConnection conn = new MockHttpURLConnection(new URL("http://localhost/")); executor.prepareConnection(conn, 10); // Check connection properties (shouldn't be an Authorization header) assertThat(conn.getRequestProperty("Authorization")).isNull(); }
@Test public void serializeAnonymousAuthenticationTokenTest() throws JsonProcessingException, JSONException { User user = createDefaultUser(); AnonymousAuthenticationToken token = new AnonymousAuthenticationToken( HASH_KEY, user, user.getAuthorities() ); String actualJson = mapper.writeValueAsString(token); JSONAssert.assertEquals(ANONYMOUS_JSON, actualJson, true); }
@Test public void testCorrectOperationIsAnonymous() { AuthenticationTrustResolverImpl trustResolver = new AuthenticationTrustResolverImpl(); assertThat(trustResolver.isAnonymous(new AnonymousAuthenticationToken("ignored", "ignored", AuthorityUtils.createAuthorityList("ignored")))).isTrue(); assertThat(trustResolver.isAnonymous(new TestingAuthenticationToken("ignored", "ignored", AuthorityUtils.createAuthorityList("ignored")))).isFalse(); }
@Test public void serializeAnonymousAuthenticationTokenMixinAfterEraseCredentialTest() throws JsonProcessingException, JSONException { User user = createDefaultUser(); AnonymousAuthenticationToken token = new AnonymousAuthenticationToken( HASH_KEY, user, user.getAuthorities() ); token.eraseCredentials(); String actualJson = mapper.writeValueAsString(token); JSONAssert.assertEquals(ANONYMOUS_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true); } }
@Before public void setup() { handler = new DefaultMessageSecurityExpressionHandler<>(); message = new GenericMessage<>(""); authentication = new AnonymousAuthenticationToken("key", "anonymous", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); }
@Before public void setup() { authentication = new TestingAuthenticationToken("user", "pass", "ROLE_USER"); messageBuilder = MessageBuilder.withPayload("payload"); expectedAnonymous = new AnonymousAuthenticationToken("key", "anonymous", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); interceptor = new SecurityContextChannelInterceptor(); }
@Test public void preSendUsesCustomAnonymous() throws Exception { expectedAnonymous = new AnonymousAuthenticationToken("customKey", "customAnonymous", AuthorityUtils.createAuthorityList("ROLE_CUSTOM")); interceptor.setAnonymousAuthentication(expectedAnonymous); interceptor.preSend(messageBuilder.build(), channel); assertAnonymous(); }