@Test public void testNullAuthenticationInSecurityContextIgnored() throws Exception { this.module = new SecurityContextLoginModule(); Map<String, String> options = new HashMap<>(); options.put("ignoreMissingAuthentication", "true"); this.module.initialize(this.subject, null, null, options); SecurityContextHolder.getContext().setAuthentication(null); assertThat(this.module.login()).as("Should return false and ask to be ignored") .isFalse(); }
@Test public void testNullLogout() throws Exception { assertThat(this.module.logout()).isFalse(); } }
@Before public void setUp() throws Exception { this.module = new SecurityContextLoginModule(); this.module.initialize(this.subject, null, null, null); SecurityContextHolder.clearContext(); }
@Test public void testLogout() throws Exception { SecurityContextHolder.getContext().setAuthentication(this.auth); this.module.login(); assertThat(this.module.logout()).as("Should return true as it succeeds").isTrue(); assertThat(this.module.getAuthentication()).as("Authentication should be null") .isNull(); assertThat(this.subject.getPrincipals().contains(this.auth)) .withFailMessage( "Principals should not contain the authentication after logout") .isFalse(); }
@Test public void testLoginSuccess() throws Exception { SecurityContextHolder.getContext().setAuthentication(this.auth); assertThat(this.module.login()) .as("Login should succeed, there is an authentication set").isTrue(); assertThat(this.module.commit()) .withFailMessage( "The authentication is not null, this should return true") .isTrue(); assertThat(this.subject.getPrincipals().contains(this.auth)) .withFailMessage("Principals should contain the authentication").isTrue(); }
@Test public void testNullAuthenticationInSecurityContext() throws Exception { try { SecurityContextHolder.getContext().setAuthentication(null); this.module.login(); fail("LoginException expected, the authentication is null in the SecurityContext"); } catch (Exception e) { } }
@Test public void testLoginException() throws Exception { try { this.module.login(); fail("LoginException expected, there is no Authentication in the SecurityContext"); } catch (LoginException e) { } }