use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class ConversationServletTest method testDoPost_BadConversationName.
@Test
public void testDoPost_BadConversationName() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("conversationTitle")).thenReturn("bad !@#$% name");
Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
User fakeUser = new User(UUID.randomUUID(), "test_username", "password", "test biography", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
conversationServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockConversationStore, Mockito.never()).addConversation(Mockito.any(Conversation.class));
Mockito.verify(mockRequest).setAttribute("error", "Please enter only letters and numbers.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
use of codeu.model.data.User 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.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class ProfileServletTest method testDoGet.
@Test
public void testDoGet() throws IOException, ServletException {
Mockito.when(mockRequest.getRequestURI()).thenReturn("/profile/test_user");
User testUser = new User(UUID.randomUUID(), "test_user", "password", null, Instant.now());
Mockito.when(mockUserStore.getUser("test_user")).thenReturn(testUser);
List<Message> fakeMessageList = new ArrayList<>();
fakeMessageList.add(new Message(UUID.randomUUID(), UUID.randomUUID(), testUser.getId(), "test message 1", Instant.ofEpochMilli(2000)));
fakeMessageList.add(new Message(UUID.randomUUID(), UUID.randomUUID(), testUser.getId(), "test message 2", Instant.ofEpochMilli(1000)));
Mockito.when(mockMessageStore.getMessagesByAuthorSorted(testUser.getId())).thenReturn(fakeMessageList);
profileServlet.doGet(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("user", testUser);
Mockito.verify(mockRequest).setAttribute("messages", fakeMessageList);
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
use of codeu.model.data.User 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.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class SearchServletTest method testDoGet.
@Test
public void testDoGet() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("search")).thenReturn("te");
User testUser = new User(UUID.randomUUID(), "test_user", "password", "test biography", Instant.now());
ArrayList<User> users = new ArrayList<User>();
users.add(testUser);
Mockito.when(mockUserStore.searchUsersSorted("te")).thenReturn(users);
searchServlet.doGet(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("users", users);
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Aggregations