Search in sources :

Example 1 with UserStore

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");
}
Also used : User(codeu.model.data.User) UserStore(codeu.model.store.basic.UserStore) HttpSession(javax.servlet.http.HttpSession) Test(org.junit.Test)

Example 2 with UserStore

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);
}
Also used : User(codeu.model.data.User) UserStore(codeu.model.store.basic.UserStore) HttpSession(javax.servlet.http.HttpSession) Test(org.junit.Test)

Example 3 with UserStore

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");
}
Also used : UserStore(codeu.model.store.basic.UserStore) User(codeu.model.data.User) HttpSession(javax.servlet.http.HttpSession) Test(org.junit.Test)

Example 4 with UserStore

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");
}
Also used : UserStore(codeu.model.store.basic.UserStore) User(codeu.model.data.User) HttpSession(javax.servlet.http.HttpSession) Test(org.junit.Test)

Example 5 with UserStore

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);
}
Also used : UserStore(codeu.model.store.basic.UserStore) Test(org.junit.Test)

Aggregations

UserStore (codeu.model.store.basic.UserStore)17 Test (org.junit.Test)17 HttpSession (javax.servlet.http.HttpSession)14 User (codeu.model.data.User)12 Activity (codeu.model.data.Activity)2 ActivityStore (codeu.model.store.basic.ActivityStore)2 Instant (java.time.Instant)1 UUID (java.util.UUID)1