Search in sources :

Example 16 with UserStore

use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.

the class RegisterServletTest method testDoPost_NewUser_2.

@Test
public void testDoPost_NewUser_2() throws IOException, ServletException {
    Mockito.when(mockRequest.getParameter("username")).thenReturn("test 2 username");
    UserStore mockUserStore = Mockito.mock(UserStore.class);
    Mockito.when(mockUserStore.isUserRegistered("test 2 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 2 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 17 with UserStore

use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.

the class RegisterServletTest method testDoPost_ExistingUser.

@Test
public void testDoPost_ExistingUser() throws IOException, ServletException {
    Mockito.when(mockRequest.getParameter("username")).thenReturn("test username");
    UserStore mockUserStore = Mockito.mock(UserStore.class);
    Mockito.when(mockUserStore.isUserRegistered("test username")).thenReturn(true);
    registerServlet.setUserStore(mockUserStore);
    HttpSession mockSession = Mockito.mock(HttpSession.class);
    Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
    registerServlet.doPost(mockRequest, mockResponse);
    Mockito.verify(mockUserStore, Mockito.never()).addUser(Mockito.any(User.class));
    Mockito.verify(mockRequest).setAttribute("error", "That username is already taken.");
    Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Also used : UserStore(codeu.model.store.basic.UserStore) User(codeu.model.data.User) HttpSession(javax.servlet.http.HttpSession) 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