Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. package se.nabor.component.user.impl;
  2.  
  3. import org.apache.commons.lang.StringUtils;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.mockito.InjectMocks;
  7. import org.mockito.Mock;
  8. import org.mockito.runners.MockitoJUnitRunner;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.ldap.ServiceUnavailableException;
  11. import org.springframework.security.authentication.BadCredentialsException;
  12. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  13. import org.springframework.security.core.Authentication;
  14. import org.springframework.security.core.AuthenticationException;
  15. import se.nabor.component.audit.AuditService;
  16. import se.nabor.component.audit.model.data.MessageData;
  17. import se.nabor.component.audit.model.data.auth.LoginFailMessageData;
  18. import se.nabor.component.authority.AuthorityService;
  19. import se.nabor.component.common.ApplicationSettingsService;
  20. import se.nabor.component.common.log.syslog.SyslogNotificationPreparation;
  21. import se.nabor.component.common.log.syslog.SyslogNotificationsService;
  22. import se.nabor.component.security.ad.ActiveDirectoryLdapAuthenticationProvider;
  23. import se.nabor.component.user.UserService;
  24. import se.nabor.exception.authentication.WrongPassword;
  25.  
  26. import static org.junit.Assert.assertEquals;
  27. import static org.junit.Assert.assertSame;
  28. import static org.junit.Assert.fail;
  29. import static org.mockito.Matchers.anyString;
  30. import static org.mockito.Mockito.*;
  31.  
  32. @RunWith(MockitoJUnitRunner.class)
  33. public class MainAuthenticationProviderTest {
  34.  
  35.     private static final String LOGIN_THROUGH_ACTIVE_DIRECTORY = "ActiveDirectory";
  36.     private static final String LOGIN_THROUGH_DATABASE = "Database";
  37.     private static final String DOMAIN = "domain.com";
  38.     private static final String URL1 = "ldap://localhost";
  39.     private static final String URL2 = "ldap://localhost";
  40.     private static final String AUTHENTICATION_NAME = "test name";
  41.     private static final String AUTHENTICATION_CREDENTIALS = "test credentials";
  42.     public static final String AUTHENTICATION_FAILED_ERROR_MESSAGE = "Authentication failed";
  43.     public static final String BAD_CREDENTIALS_ERROR_MESSAGE = "Bad credentials";
  44.  
  45.     @Mock
  46.     private ActiveDirectoryLdapAuthenticationProvider ldapAuthProvider;
  47.  
  48.     @Mock
  49.     private ActiveDirectoryLdapAuthenticationProvider ldapAuthProviderReserve;
  50.  
  51.     @Mock
  52.     private AuthorityService authorityService;
  53.  
  54.     @Mock
  55.     private UserService userService;
  56.  
  57.     @Mock
  58.     private DaoAuthenticationProvider daoAuthenticationProvider;
  59.  
  60.     @Mock
  61.     private AuditService auditService;
  62.  
  63.     @Mock
  64.     private SyslogNotificationsService syslogNotificationsService;
  65.  
  66.     @InjectMocks
  67.     private final MainAuthenticationProvider adAuthProvider = getMainAuthenticationProvider(LOGIN_THROUGH_ACTIVE_DIRECTORY, DOMAIN, URL1, URL2);
  68.  
  69.     @InjectMocks
  70.     private final MainAuthenticationProvider dbAuthProvider = getMainAuthenticationProvider(LOGIN_THROUGH_DATABASE, DOMAIN, URL1, URL2);
  71.  
  72.     @Test
  73.     public void testInvalidSuppliedPassword() {
  74.         MainAuthenticationProvider mainAuthenticationProvider = adAuthProvider;
  75.  
  76.         Authentication authentication = mock(Authentication.class);
  77.         when(authentication.getCredentials()).thenReturn(StringUtils.EMPTY);
  78.         when(authentication.getName()).thenReturn(AUTHENTICATION_NAME);
  79.  
  80.         try {
  81.             mainAuthenticationProvider.authenticate(authentication);
  82.  
  83.             fail();
  84.         } catch (WrongPassword e) {
  85.             assertEquals("Empty password.", e.getMessage());
  86.         }
  87.  
  88.         verify(auditService).addMessage(new LoginFailMessageData(anyString(), "AD1",
  89.                 "Supplied password was invalid."));
  90.     }
  91.  
  92.     @Test
  93.     public void testLdapBadCredentials() {
  94.         MainAuthenticationProvider mainAuthenticationProvider = adAuthProvider;
  95.  
  96.         Authentication authentication = mock(Authentication.class);
  97.         when(authentication.getCredentials()).thenReturn(AUTHENTICATION_CREDENTIALS);
  98.         when(authentication.getName()).thenReturn(AUTHENTICATION_NAME);
  99.  
  100.         when(ldapAuthProvider.authenticate(authentication)).thenThrow(
  101.                 new BadCredentialsException(BAD_CREDENTIALS_ERROR_MESSAGE));
  102.  
  103.         try {
  104.             mainAuthenticationProvider.authenticate(authentication);
  105.  
  106.             fail();
  107.         } catch (BadCredentialsException e) {
  108.             assertEquals(BAD_CREDENTIALS_ERROR_MESSAGE, e.getMessage());
  109.         }
  110.  
  111.         verify(auditService).addMessage(new LoginFailMessageData(anyString(), "AD1",
  112.                 "User has invalid registration data"));
  113.     }
  114.  
  115.     @Test
  116.     public void testDbAuthenticationFailed() {
  117.         MainAuthenticationProvider mainAuthenticationProvider = dbAuthProvider;
  118.  
  119.         Authentication authentication = mock(Authentication.class);
  120.         when(authentication.getCredentials()).thenReturn(AUTHENTICATION_CREDENTIALS);
  121.         when(authentication.getName()).thenReturn(AUTHENTICATION_NAME);
  122.  
  123.         AuthenticationException authenticationException = mock(AuthenticationException.class);
  124.         when(authenticationException.getMessage()).thenReturn(AUTHENTICATION_FAILED_ERROR_MESSAGE);
  125.  
  126.         when(daoAuthenticationProvider.authenticate(authentication)).thenThrow(authenticationException);
  127.  
  128.         try {
  129.             mainAuthenticationProvider.authenticate(authentication);
  130.  
  131.             fail();
  132.         } catch (AuthenticationException e) {
  133.             assertSame(authenticationException, e);
  134.         }
  135.  
  136.         verify(auditService).addMessage(new LoginFailMessageData(anyString(), "DB", AUTHENTICATION_FAILED_ERROR_MESSAGE));
  137.     }
  138.  
  139.     @Test
  140.     public void testLdapBadCredentialsUrl2() {
  141.         MainAuthenticationProvider mainAuthenticationProvider = adAuthProvider;
  142.  
  143.         SyslogNotificationPreparation syslogNotificationPreparation = mock(SyslogNotificationPreparation.class);
  144.         when(syslogNotificationPreparation.setProperty("ADName", "AD1")).thenReturn(syslogNotificationPreparation);
  145.  
  146.         when(syslogNotificationsService.prepareNotification("0232")).thenReturn(syslogNotificationPreparation);
  147.  
  148.         Authentication authentication = mock(Authentication.class);
  149.         when(authentication.getCredentials()).thenReturn(AUTHENTICATION_CREDENTIALS);
  150.         when(authentication.getName()).thenReturn(AUTHENTICATION_NAME);
  151.  
  152.         when(ldapAuthProvider.authenticate(authentication)).thenThrow(
  153.                 new ServiceUnavailableException(new javax.naming.ServiceUnavailableException()));
  154.  
  155.         when(ldapAuthProviderReserve.authenticate(authentication)).thenThrow(
  156.                 new BadCredentialsException(BAD_CREDENTIALS_ERROR_MESSAGE));
  157.  
  158.         try {
  159.             mainAuthenticationProvider.authenticate(authentication);
  160.  
  161.             fail();
  162.         } catch (BadCredentialsException e) {
  163.             assertEquals(BAD_CREDENTIALS_ERROR_MESSAGE, e.getMessage());
  164.         }
  165.  
  166.         verify(auditService, times(2)).addMessage((MessageData) any());
  167.     }
  168.  
  169.     private MainAuthenticationProvider getMainAuthenticationProvider(String loginThrough, String domain,
  170.                                                                      String url1, String url2) {
  171.         ApplicationSettingsService applicationSettingsService = mock(ApplicationSettingsService.class);
  172.  
  173.         when(applicationSettingsService.getValue(ApplicationSettingsService.LOGIN_THROUGH_KEY, false))
  174.                 .thenReturn(loginThrough);
  175.  
  176.         when(applicationSettingsService.getValue(ApplicationSettingsService.DOMAIN_AD_KEY, false)).thenReturn(domain);
  177.         when(applicationSettingsService.getValue(ApplicationSettingsService.URL_AD_KEY, false)).thenReturn(url1);
  178.         when(applicationSettingsService.getValue(ApplicationSettingsService.URL_AD_KEY2, false)).thenReturn(url2);
  179.  
  180.         return new MainAuthenticationProvider(applicationSettingsService);
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement