private void testAuthenticate(JaasAuthenticationProvider p1) { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "user", "password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO")); Authentication auth = p1.authenticate(token); assertThat(auth).isNotNull(); }
@Test public void testNullDefaultAuthorities() { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "user", "password"); assertThat(jaasProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue(); Authentication auth = jaasProvider.authenticate(token); assertThat(auth .getAuthorities()).withFailMessage("Only ROLE_TEST1 and ROLE_TEST2 should have been returned").hasSize(2); }
@Test public void testUnsupportedAuthenticationObjectReturnsNull() { assertThat(jaasProvider.authenticate(new TestingAuthenticationToken("foo", "bar", AuthorityUtils.NO_AUTHORITIES))).isNull(); }
@Test public void testBadPassword() { try { jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")); fail("LoginException should have been thrown for the bad password"); } catch (AuthenticationException e) { } assertThat(eventCheck.failedEvent).as("Failure event not fired").isNotNull(); assertThat(eventCheck.failedEvent.getException()).withFailMessage("Failure event exception was null").isNotNull(); assertThat(eventCheck.successEvent).as("Success event was fired").isNull(); }
@Test public void testLoginExceptionResolver() { assertThat(jaasProvider.getLoginExceptionResolver()).isNotNull(); jaasProvider.setLoginExceptionResolver(new LoginExceptionResolver() { public AuthenticationException resolveException(LoginException e) { return new LockedException("This is just a test!"); } }); try { jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); } catch (LockedException e) { } catch (Exception e) { fail("LockedException should have been thrown and caught"); } }
@Test public void testBadUser() { try { jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")); fail("LoginException should have been thrown for the bad user"); } catch (AuthenticationException e) { } assertThat(eventCheck.failedEvent).as("Failure event not fired").isNotNull(); assertThat(eventCheck.failedEvent.getException()).withFailMessage("Failure event exception was null").isNotNull(); assertThat(eventCheck.successEvent).as("Success event was fired").isNull(); }
@Test public void testFull() throws Exception { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "user", "password", AuthorityUtils.createAuthorityList("ROLE_ONE")); assertThat(jaasProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue(); Authentication auth = jaasProvider.authenticate(token); assertThat(jaasProvider.getAuthorityGranters()).isNotNull(); assertThat(jaasProvider.getCallbackHandlers()).isNotNull(); assertThat(jaasProvider.getLoginConfig()).isNotNull(); assertThat(jaasProvider.getLoginContextName()).isNotNull(); Collection<? extends GrantedAuthority> list = auth.getAuthorities(); Set<String> set = AuthorityUtils.authorityListToSet(list); assertThat(set.contains("ROLE_ONE")).withFailMessage("GrantedAuthorities should not contain ROLE_ONE").isFalse(); assertThat(set.contains("ROLE_TEST1")).withFailMessage("GrantedAuthorities should contain ROLE_TEST1").isTrue(); assertThat(set.contains("ROLE_TEST2")).withFailMessage("GrantedAuthorities should contain ROLE_TEST2").isTrue(); boolean foundit = false; for (GrantedAuthority a : list) { if (a instanceof JaasGrantedAuthority) { JaasGrantedAuthority grant = (JaasGrantedAuthority) a; assertThat(grant.getPrincipal()).withFailMessage("Principal was null on JaasGrantedAuthority").isNotNull(); foundit = true; } } assertThat(foundit).as("Could not find a JaasGrantedAuthority").isTrue(); assertThat(eventCheck.successEvent).as("Success event should be fired").isNotNull(); assertThat(eventCheck.successEvent.getAuthentication()).withFailMessage("Auth objects should be equal").isEqualTo(auth); assertThat(eventCheck.failedEvent).as("Failure event should not be fired").isNull(); }