use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.
the class PersistentDataStore method loadUsers.
/**
* Loads all User objects from the Datastore service and returns them in a List.
*
* @throws PersistentDataStoreException if an error was detected during the load from the
* Datastore service
*/
public List<User> loadUsers() throws PersistentDataStoreException {
List<User> users = new ArrayList<>();
// Retrieve all users from the datastore.
Query query = new Query("chat-users");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
String userName = (String) entity.getProperty("username");
String password = (String) entity.getProperty("password");
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
User user = new User(uuid, userName, password, creationTime);
users.add(user);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return users;
}
use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.
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_passord", 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 maksymko.
the class ChatServletTest method testDoPost_ConversationNotFound.
@Test
public void testDoPost_ConversationNotFound() 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);
Mockito.when(mockConversationStore.getConversationWithTitle("test_conversation")).thenReturn(null);
chatServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockMessageStore, Mockito.never()).addMessage(Mockito.any(Message.class));
Mockito.verify(mockResponse).sendRedirect("/conversations");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.
the class ChatServletTest method testDoPost_StoresMessage.
@Test
public void testDoPost_StoresMessage() 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("Test message.");
chatServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
Mockito.verify(mockMessageStore).addMessage(messageArgumentCaptor.capture());
Assert.assertEquals("Test message.", messageArgumentCaptor.getValue().getContent());
Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
use of codeu.model.data.User 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");
}
Aggregations