mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
Added additional tests for UserService login
This commit is contained in:
@@ -103,8 +103,10 @@ public class UserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* should triggers after successful login
|
* should triggers after successful login
|
||||||
|
*
|
||||||
|
* Note: Visibility changed to protected to enabled spying on this method
|
||||||
*/
|
*/
|
||||||
private void afterLogin(User user) {
|
protected void afterLogin(User user) {
|
||||||
Date lastLogin = user.getLastLogin();
|
Date lastLogin = user.getLastLogin();
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import static org.mockito.Matchers.anyString;
|
|||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -104,5 +106,72 @@ public class UserServiceTest {
|
|||||||
|
|
||||||
verify(encryptionService).authenticate("password", encryptedPassword, salt);
|
verify(encryptionService).authenticate("password", encryptedPassword, salt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void
|
||||||
|
calling_login_should_not_return_user_object_on_unsuccessful_authentication() {
|
||||||
|
// Make a user who is not disabled
|
||||||
|
User user = new User();
|
||||||
|
user.setDisabled(false);
|
||||||
|
|
||||||
|
// Set the encryptedPassword on the user
|
||||||
|
byte[] encryptedPassword = new byte[]{1,2,3};
|
||||||
|
user.setPassword(encryptedPassword);
|
||||||
|
|
||||||
|
// Set a salt for this user
|
||||||
|
byte[] salt = new byte[]{4,5,6};
|
||||||
|
user.setSalt(salt);
|
||||||
|
|
||||||
|
// Mock DAO to return the user
|
||||||
|
UserDAO dao = mock(UserDAO.class);
|
||||||
|
when(dao.findByName("test")).thenReturn(user);
|
||||||
|
|
||||||
|
// Mock PasswordEncryptionService
|
||||||
|
PasswordEncryptionService encryptionService = mock(PasswordEncryptionService.class);
|
||||||
|
when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(false);
|
||||||
|
|
||||||
|
// Create service with mocks
|
||||||
|
UserService service = new UserService(null, dao, null, null, encryptionService, null);
|
||||||
|
|
||||||
|
// Try to login as the user
|
||||||
|
Optional<User> authenticatedUser = service.login("test", "password");
|
||||||
|
|
||||||
|
Assert.assertFalse(authenticatedUser.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void
|
||||||
|
calling_login_should_return_user_object_on_successful_authentication() {
|
||||||
|
// Make a user who is not disabled
|
||||||
|
User user = new User();
|
||||||
|
user.setDisabled(false);
|
||||||
|
|
||||||
|
// Set the encryptedPassword on the user
|
||||||
|
byte[] encryptedPassword = new byte[]{1,2,3};
|
||||||
|
user.setPassword(encryptedPassword);
|
||||||
|
|
||||||
|
// Set a salt for this user
|
||||||
|
byte[] salt = new byte[]{4,5,6};
|
||||||
|
user.setSalt(salt);
|
||||||
|
|
||||||
|
// Mock DAO to return the user
|
||||||
|
UserDAO dao = mock(UserDAO.class);
|
||||||
|
when(dao.findByName("test")).thenReturn(user);
|
||||||
|
|
||||||
|
// Mock PasswordEncryptionService
|
||||||
|
PasswordEncryptionService encryptionService = mock(PasswordEncryptionService.class);
|
||||||
|
when(encryptionService.authenticate(anyString(), any(byte[].class), any(byte[].class))).thenReturn(true);
|
||||||
|
|
||||||
|
// Create service with mocks
|
||||||
|
UserService service = new UserService(null, dao, null, null, encryptionService, null);
|
||||||
|
|
||||||
|
// Skip afterLogin activities
|
||||||
|
UserService spy = spy(service);
|
||||||
|
doNothing().when(spy).afterLogin(any(User.class));
|
||||||
|
|
||||||
|
// Try to login as the user
|
||||||
|
Optional<User> authenticatedUser = spy.login("test", "password");
|
||||||
|
|
||||||
|
Assert.assertTrue(authenticatedUser.isPresent());
|
||||||
|
Assert.assertEquals(user, authenticatedUser.get());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user