use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class ChatServletTest method testDoPost_CleansHtmlContent.
@Test
public void testDoPost_CleansHtmlContent() throws IOException, ServletException {
Mockito.when(mockRequest.getRequestURI()).thenReturn("/chat/test_conversation");
Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
User fakeUser = new User(UUID.randomUUID(), "test_username", "test_password", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
Conversation fakeConversation = new Conversation(UUID.randomUUID(), UUID.randomUUID(), "test_conversation", Instant.now());
Mockito.when(mockConversationStore.getConversationWithTitle("test_conversation")).thenReturn(fakeConversation);
Mockito.when(mockRequest.getParameter("message")).thenReturn("Contains <b>html</b> and <script>JavaScript</script> content.");
chatServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
Mockito.verify(mockMessageStore).addMessage(messageArgumentCaptor.capture());
Assert.assertEquals("Contains html and content.", messageArgumentCaptor.getValue().getContent());
Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
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", "test_password", 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-Spring-2018 by jwang281.
the class ConversationServletTest method testDoPost_ConversationNameTaken.
@Test
public void testDoPost_ConversationNameTaken() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("conversationTitle")).thenReturn("test_conversation");
Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
User fakeUser = new User(UUID.randomUUID(), "test_username", "test_password", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
Mockito.when(mockConversationStore.isTitleTaken("test_conversation")).thenReturn(true);
conversationServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockConversationStore, Mockito.never()).addConversation(Mockito.any(Conversation.class));
Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
use of codeu.model.data.User 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");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class UserStoreTest method testGetUser_byId_notFound.
@Test
public void testGetUser_byId_notFound() {
User resultUser = userStore.getUser(UUID.randomUUID());
Assert.assertNull(resultUser);
}
Aggregations