Search in sources :

Example 6 with UserStore

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

Example 7 with UserStore

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

Example 8 with UserStore

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

Example 9 with UserStore

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

Example 10 with UserStore

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);
}
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