use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.
the class LoginServletTest method testDoPost_ExistingUser.
@Test
public void testDoPost_ExistingUser() throws IOException, ServletException {
User mockUser = Mockito.mock(User.class);
Mockito.when(mockUser.getName()).thenReturn("username_test");
Mockito.when(mockUser.getPassword()).thenReturn("valid_password");
Mockito.when(mockRequest.getParameter("username")).thenReturn("username_test");
Mockito.when(mockRequest.getParameter("password")).thenReturn("valid_password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("username_test")).thenReturn(true);
Mockito.when(mockUserStore.getUser("username_test")).thenReturn(mockUser);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockResponse).sendRedirect("/conversations");
}
use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.
the class LoginServletTest method testDoPost_WrongPassword.
@Test
public void testDoPost_WrongPassword() throws IOException, ServletException {
User mockUser = Mockito.mock(User.class);
Mockito.when(mockUser.getName()).thenReturn("username_test");
Mockito.when(mockUser.getPassword()).thenReturn("valid_password");
Mockito.when(mockRequest.getParameter("username")).thenReturn("username_test");
Mockito.when(mockRequest.getParameter("password")).thenReturn("invalid_password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("username_test")).thenReturn(true);
Mockito.when(mockUserStore.getUser("username_test")).thenReturn(mockUser);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("error", "Invalid password.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.
the class RegisterServletTest method testDoPost_NewUser.
@Test
public void testDoPost_NewUser() 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);
registerServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
registerServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
Mockito.verify(mockUserStore).addUser(userArgumentCaptor.capture());
Assert.assertEquals(userArgumentCaptor.getValue().getName(), "test username");
Mockito.verify(mockUserStore, Mockito.atLeastOnce()).addUser(Mockito.any(User.class));
Mockito.verify(mockResponse).sendRedirect("/login");
}
use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by maksymko.
the class LoginServletTest method testDoPost_ExistingUserCorrectPassword.
@Test
public void testDoPost_ExistingUserCorrectPassword() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test username");
Mockito.when(mockRequest.getParameter("password")).thenReturn("correct password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
loginServlet.setUserStore(mockUserStore);
Mockito.when(mockUserStore.isUserRegistered("test username")).thenReturn(true);
User mockUser = Mockito.mock(User.class);
Mockito.when(mockUserStore.getUser("test username")).thenReturn(mockUser);
Mockito.when(mockUser.getPassword()).thenReturn(BCrypt.hashpw("correct password", BCrypt.gensalt()));
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-Spring-2018 by maksymko.
the class LoginServletTest method testDoPost_NonExistingUser.
@Test
public void testDoPost_NonExistingUser() 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);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("error", "That username was not found.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Aggregations