2014-10-08 00:31:49 -04:00
|
|
|
package com.commafeed.backend.service;
|
|
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2014-10-08 00:31:49 -04:00
|
|
|
import static org.mockito.Matchers.any;
|
|
|
|
|
import static org.mockito.Matchers.anyString;
|
2014-10-08 22:18:16 -04:00
|
|
|
import static org.mockito.Mockito.doNothing;
|
2014-10-08 00:31:49 -04:00
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
2014-12-12 10:18:59 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
import org.junit.Before;
|
2014-10-08 00:31:49 -04:00
|
|
|
import org.junit.Test;
|
2014-10-22 22:31:36 -04:00
|
|
|
import org.mockito.Mock;
|
|
|
|
|
import org.mockito.MockitoAnnotations;
|
2014-10-08 00:31:49 -04:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
import com.commafeed.CommaFeedConfiguration;
|
|
|
|
|
import com.commafeed.backend.dao.FeedCategoryDAO;
|
2014-10-08 00:31:49 -04:00
|
|
|
import com.commafeed.backend.dao.UserDAO;
|
2014-10-22 22:31:36 -04:00
|
|
|
import com.commafeed.backend.dao.UserSettingsDAO;
|
2014-10-08 00:31:49 -04:00
|
|
|
import com.commafeed.backend.model.User;
|
2014-10-08 22:18:16 -04:00
|
|
|
import com.commafeed.backend.service.internal.PostLoginActivities;
|
2014-10-08 00:31:49 -04:00
|
|
|
|
|
|
|
|
public class UserServiceTest {
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
private static final byte[] SALT = new byte[] { 1, 2, 3 };
|
|
|
|
|
private static final byte[] ENCRYPTED_PASSWORD = new byte[] { 5, 6, 7 };
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private CommaFeedConfiguration commaFeedConfiguration;
|
|
|
|
|
@Mock
|
|
|
|
|
private FeedCategoryDAO feedCategoryDAO;
|
|
|
|
|
@Mock
|
|
|
|
|
private UserDAO userDAO;
|
|
|
|
|
@Mock
|
|
|
|
|
private UserSettingsDAO userSettingsDAO;
|
|
|
|
|
@Mock
|
|
|
|
|
private PasswordEncryptionService passwordEncryptionService;
|
|
|
|
|
@Mock
|
|
|
|
|
private PostLoginActivities postLoginActivities;
|
|
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
private User disabledUser;
|
|
|
|
|
private User normalUser;
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
private UserService userService;
|
|
|
|
|
|
2014-12-12 10:18:59 +01:00
|
|
|
@Before
|
|
|
|
|
public void before_each_test() {
|
2014-10-22 22:31:36 -04:00
|
|
|
MockitoAnnotations.initMocks(this);
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
userService = new UserService(feedCategoryDAO, userDAO, userSettingsDAO, passwordEncryptionService, commaFeedConfiguration,
|
|
|
|
|
postLoginActivities);
|
|
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
disabledUser = new User();
|
|
|
|
|
disabledUser.setDisabled(true);
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
normalUser = new User();
|
|
|
|
|
normalUser.setDisabled(false);
|
|
|
|
|
normalUser.setSalt(SALT);
|
|
|
|
|
normalUser.setPassword(ENCRYPTED_PASSWORD);
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_not_return_user_object_when_given_null_nameOrEmail() {
|
2014-10-22 22:31:36 -04:00
|
|
|
Optional<User> user = userService.login(null, "password");
|
|
|
|
|
assertFalse(user.isPresent());
|
2014-10-08 00:31:49 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_not_return_user_object_when_given_null_password() {
|
2014-10-22 22:31:36 -04:00
|
|
|
Optional<User> user = userService.login("testusername", null);
|
|
|
|
|
assertFalse(user.isPresent());
|
2014-10-08 00:31:49 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_lookup_user_by_name() {
|
2014-10-22 22:31:36 -04:00
|
|
|
userService.login("test", "password");
|
|
|
|
|
verify(userDAO).findByName("test");
|
2014-10-08 00:31:49 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_lookup_user_by_email_if_lookup_by_name_failed() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test@test.com")).thenReturn(null);
|
|
|
|
|
userService.login("test@test.com", "password");
|
|
|
|
|
verify(userDAO).findByEmail("test@test.com");
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_not_return_user_object_if_could_not_find_user_by_name_or_email() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test@test.com")).thenReturn(null);
|
|
|
|
|
when(userDAO.findByEmail("test@test.com")).thenReturn(null);
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
Optional<User> user = userService.login("test@test.com", "password");
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
assertFalse(user.isPresent());
|
2014-10-08 00:31:49 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_not_return_user_object_if_user_is_disabled() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test")).thenReturn(disabledUser);
|
|
|
|
|
Optional<User> user = userService.login("test", "password");
|
|
|
|
|
assertFalse(user.isPresent());
|
2014-10-08 00:31:49 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_try_to_authenticate_user_who_is_not_disabled() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test")).thenReturn(normalUser);
|
|
|
|
|
when(passwordEncryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false);
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
userService.login("test", "password");
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
verify(passwordEncryptionService).authenticate("password", ENCRYPTED_PASSWORD, SALT);
|
2014-10-08 00:31:49 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_not_return_user_object_on_unsuccessful_authentication() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test")).thenReturn(normalUser);
|
|
|
|
|
when(passwordEncryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false);
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
Optional<User> authenticatedUser = userService.login("test", "password");
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
assertFalse(authenticatedUser.isPresent());
|
2014-10-08 20:59:05 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_execute_post_login_activities_for_user_on_successful_authentication() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test")).thenReturn(normalUser);
|
|
|
|
|
when(passwordEncryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(true);
|
2014-10-08 22:39:32 -04:00
|
|
|
doNothing().when(postLoginActivities).executeFor(any(User.class));
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
userService.login("test", "password");
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
verify(postLoginActivities).executeFor(normalUser);
|
2014-10-08 22:39:32 -04:00
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void calling_login_should_return_user_object_on_successful_authentication() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByName("test")).thenReturn(normalUser);
|
|
|
|
|
when(passwordEncryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(true);
|
2014-10-08 22:18:16 -04:00
|
|
|
doNothing().when(postLoginActivities).executeFor(any(User.class));
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
Optional<User> authenticatedUser = userService.login("test", "password");
|
2014-12-12 10:18:59 +01:00
|
|
|
|
2014-10-22 22:31:36 -04:00
|
|
|
assertTrue(authenticatedUser.isPresent());
|
|
|
|
|
assertEquals(normalUser, authenticatedUser.get());
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void api_login_should_not_return_user_if_apikey_null() {
|
2014-10-22 22:31:36 -04:00
|
|
|
Optional<User> user = userService.login(null);
|
|
|
|
|
assertFalse(user.isPresent());
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void api_login_should_lookup_user_by_apikey() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByApiKey("apikey")).thenReturn(null);
|
|
|
|
|
userService.login("apikey");
|
|
|
|
|
verify(userDAO).findByApiKey("apikey");
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void api_login_should_not_return_user_if_user_not_found_from_lookup_by_apikey() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByApiKey("apikey")).thenReturn(null);
|
|
|
|
|
Optional<User> user = userService.login("apikey");
|
|
|
|
|
assertFalse(user.isPresent());
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void api_login_should_not_return_user_if_user_found_from_apikey_lookup_is_disabled() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByApiKey("apikey")).thenReturn(disabledUser);
|
|
|
|
|
Optional<User> user = userService.login("apikey");
|
|
|
|
|
assertFalse(user.isPresent());
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void api_login_should_perform_post_login_activities_if_user_found_from_apikey_lookup_not_disabled() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByApiKey("apikey")).thenReturn(normalUser);
|
|
|
|
|
userService.login("apikey");
|
|
|
|
|
verify(postLoginActivities).executeFor(normalUser);
|
|
|
|
|
}
|
2014-12-12 10:18:59 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void api_login_should_return_user_if_user_found_from_apikey_lookup_not_disabled() {
|
2014-10-22 22:31:36 -04:00
|
|
|
when(userDAO.findByApiKey("apikey")).thenReturn(normalUser);
|
|
|
|
|
Optional<User> returnedUser = userService.login("apikey");
|
|
|
|
|
assertEquals(normalUser, returnedUser.get());
|
2014-10-08 20:59:05 -04:00
|
|
|
}
|
2014-10-08 00:31:49 -04:00
|
|
|
|
|
|
|
|
}
|