|
|
@@ -18,19 +18,8 @@
|
|
|
*/
|
|
|
package org.apache.ambari.logsearch.web.security;
|
|
|
|
|
|
-import static junit.framework.Assert.assertEquals;
|
|
|
-import static junit.framework.Assert.assertSame;
|
|
|
-import static junit.framework.Assert.fail;
|
|
|
-import static org.easymock.EasyMock.expect;
|
|
|
-import static org.easymock.EasyMock.replay;
|
|
|
-import static org.easymock.EasyMock.strictMock;
|
|
|
-import static org.easymock.EasyMock.verify;
|
|
|
-
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
import org.apache.ambari.logsearch.conf.AuthPropsConfig;
|
|
|
+import org.apache.ambari.logsearch.util.CommonUtil;
|
|
|
import org.apache.ambari.logsearch.web.model.User;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
@@ -40,15 +29,24 @@ import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
-import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
-import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
+
|
|
|
+import static junit.framework.Assert.assertEquals;
|
|
|
+import static junit.framework.Assert.assertSame;
|
|
|
+import static junit.framework.Assert.assertTrue;
|
|
|
+import static org.easymock.EasyMock.strictMock;
|
|
|
+import static org.easymock.EasyMock.expect;
|
|
|
+import static org.easymock.EasyMock.replay;
|
|
|
+import static org.easymock.EasyMock.verify;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
private LogsearchFileAuthenticationProvider provider;
|
|
|
private AuthPropsConfig mockAuthPropsConfig;
|
|
|
private UserDetailsService mockUserDetailsService;
|
|
|
- private PasswordEncoder passwordEncoder;
|
|
|
|
|
|
@Before
|
|
|
public void init() throws Exception {
|
|
|
@@ -63,9 +61,6 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
Field userDetailsServiceField = LogsearchFileAuthenticationProvider.class.getDeclaredField("userDetailsService");
|
|
|
userDetailsServiceField.setAccessible(true);
|
|
|
userDetailsServiceField.set(provider, mockUserDetailsService);
|
|
|
-
|
|
|
- passwordEncoder = new BCryptPasswordEncoder();
|
|
|
- provider.setPasswordEncoder(passwordEncoder);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
@@ -90,7 +85,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch(BadCredentialsException e) {
|
|
|
assertEquals("Username can't be null or empty.", e.getMessage());
|
|
|
}
|
|
|
@@ -108,7 +103,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch(BadCredentialsException e) {
|
|
|
assertEquals("Username can't be null or empty.", e.getMessage());
|
|
|
}
|
|
|
@@ -127,7 +122,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch(BadCredentialsException e) {
|
|
|
assertEquals("Password can't be null or empty.", e.getMessage());
|
|
|
}
|
|
|
@@ -145,7 +140,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch(BadCredentialsException e) {
|
|
|
assertEquals("Password can't be null or empty.", e.getMessage());
|
|
|
}
|
|
|
@@ -163,7 +158,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
Authentication authentication = new TestingAuthenticationToken("principal", "credentials");
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch (BadCredentialsException e) {
|
|
|
assertEquals("User not found.", e.getMessage());
|
|
|
}
|
|
|
@@ -173,7 +168,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
@Test
|
|
|
public void testAuthenticationNoPassword() {
|
|
|
- List<GrantedAuthority> grantedAuths = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
+ List<GrantedAuthority> grantedAuths = Arrays.<GrantedAuthority>asList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
User user = new User("principal", null, grantedAuths);
|
|
|
|
|
|
expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true);
|
|
|
@@ -184,7 +179,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
Authentication authentication = new TestingAuthenticationToken("principal", "credentials");
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch (BadCredentialsException e) {
|
|
|
assertEquals("Password can't be null or empty.", e.getMessage());
|
|
|
}
|
|
|
@@ -194,8 +189,8 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
@Test
|
|
|
public void testAuthenticationWrongPassword() {
|
|
|
- List<GrantedAuthority> grantedAuths = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
- User user = new User("principal", passwordEncoder.encode("notCredentials"), grantedAuths);
|
|
|
+ List<GrantedAuthority> grantedAuths = Arrays.<GrantedAuthority>asList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
+ User user = new User("principal", CommonUtil.encryptPassword("principal", "notCredentials"), grantedAuths);
|
|
|
|
|
|
expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true);
|
|
|
expect(mockUserDetailsService.loadUserByUsername("principal")).andReturn(user);
|
|
|
@@ -205,7 +200,7 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
Authentication authentication = new TestingAuthenticationToken("principal", "credentials");
|
|
|
try {
|
|
|
provider.authenticate(authentication);
|
|
|
- fail("Should have thrown BadCredentialsException");
|
|
|
+ assertTrue("Should have thrown BadCredentialsException", false);
|
|
|
} catch (BadCredentialsException e) {
|
|
|
assertEquals("Wrong password.", e.getMessage());
|
|
|
}
|
|
|
@@ -215,9 +210,8 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
|
|
|
@Test
|
|
|
public void testAuthenticationSuccessful() {
|
|
|
- List<GrantedAuthority> grantedAuths = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
- String encodedPassword = passwordEncoder.encode("credentials");
|
|
|
- User user = new User("principal", encodedPassword, grantedAuths);
|
|
|
+ List<GrantedAuthority> grantedAuths = Arrays.<GrantedAuthority>asList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
+ User user = new User("principal", CommonUtil.encryptPassword("principal", "credentials"), grantedAuths);
|
|
|
|
|
|
expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true);
|
|
|
expect(mockUserDetailsService.loadUserByUsername("principal")).andReturn(user);
|
|
|
@@ -225,8 +219,10 @@ public class LogsearchFileAuthenticationProviderTest {
|
|
|
replay(mockAuthPropsConfig, mockUserDetailsService);
|
|
|
|
|
|
Authentication authentication = new TestingAuthenticationToken("principal", "credentials");
|
|
|
+
|
|
|
Authentication authenticationResult = provider.authenticate(authentication);
|
|
|
assertEquals("principal", authenticationResult.getName());
|
|
|
+ assertEquals(CommonUtil.encryptPassword("principal", "credentials"), authenticationResult.getCredentials());
|
|
|
assertEquals(1, authenticationResult.getAuthorities().size());
|
|
|
assertEquals(new SimpleGrantedAuthority("ROLE_USER"), authenticationResult.getAuthorities().iterator().next());
|
|
|
|