use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by maksymko.
the class RegisterServletTest method testDoPost_NotExistingUser.
@Test
public void testDoPost_NotExistingUser() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("testing");
Mockito.when(mockRequest.getParameter("password")).thenReturn("pass");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("testing")).thenReturn(false);
registerServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
registerServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockUserStore, Mockito.times(1)).addUser(Mockito.any(User.class));
}
use of codeu.model.store.basic.UserStore in project codeu-2018-team12 by codeu-2018-team12.
the class LoginServletTest method testDoPost_CorrectPass.
@Test
public void testDoPost_CorrectPass() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test_username");
Mockito.when(mockRequest.getParameter("password")).thenReturn("password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test_username")).thenReturn(true);
String hashedPassword = BCrypt.hashpw("password", BCrypt.gensalt());
User testUser = new User(UUID.randomUUID(), "test_username", hashedPassword, "test biography", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(testUser);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockUserStore, Mockito.never()).addUser(Mockito.any(User.class));
Mockito.verify(mockSession).setAttribute("user", "test_username");
Mockito.verify(mockResponse).sendRedirect("/conversations");
}
use of codeu.model.store.basic.UserStore in project codeu-2018-team12 by codeu-2018-team12.
the class LoginServletTest method testDoPost_NoUser.
@Test
public void testDoPost_NoUser() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test_username");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test_username")).thenReturn(false);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("error", "That username was not found.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
use of codeu.model.store.basic.UserStore in project codeu-2018-team12 by codeu-2018-team12.
the class RegisterServletTest method testDoPost_NewUser.
@Test
public void testDoPost_NewUser() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test username");
Mockito.when(mockRequest.getParameter("password")).thenReturn("test password");
Mockito.when(mockRequest.getParameter("confirmPassword")).thenReturn("test password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test username")).thenReturn(false);
registerServlet.setUserStore(mockUserStore);
ActivityStore mockActivityStore = Mockito.mock(ActivityStore.class);
registerServlet.setActivityStore(mockActivityStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
registerServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
ArgumentCaptor<Activity> activityArgumentCaptor = ArgumentCaptor.forClass(Activity.class);
Mockito.verify(mockUserStore).addUser(userArgumentCaptor.capture());
Assert.assertEquals(userArgumentCaptor.getValue().getName(), "test username");
Mockito.verify(mockActivityStore).addActivity(activityArgumentCaptor.capture());
Assert.assertEquals(activityArgumentCaptor.getValue().getUserId(), userArgumentCaptor.getValue().getId());
Mockito.verify(mockResponse).sendRedirect("/login");
}
use of codeu.model.store.basic.UserStore in project codeu-2018-team12 by codeu-2018-team12.
the class LoginServletTest method testDoPost_BadPass.
@Test
public void testDoPost_BadPass() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test_username");
Mockito.when(mockRequest.getParameter("password")).thenReturn("badpass");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test_username")).thenReturn(true);
String hashedPassword = BCrypt.hashpw("password", BCrypt.gensalt());
User testUser = new User(UUID.randomUUID(), "test_username", hashedPassword, "test biography", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(testUser);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockUserStore, Mockito.never()).addUser(Mockito.any(User.class));
Mockito.verify(mockRequest).setAttribute("error", "Invalid password.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Aggregations