use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class PersistentDataStoreTest method testSaveAndLoadUsersPassHash.
@Test
public void testSaveAndLoadUsersPassHash() throws PersistentDataStoreException {
UUID idOne = UUID.randomUUID();
String nameOne = "test_username_one";
String passwordOne = "test_password_one";
String hashedPasswordOne = BCrypt.hashpw(passwordOne, BCrypt.gensalt());
Instant creationOne = Instant.ofEpochMilli(1000);
User inputUserOne = new User(idOne, nameOne, hashedPasswordOne, null, creationOne);
UUID idTwo = UUID.randomUUID();
String nameTwo = "test_username_two";
String passwordTwo = "test_password_two";
String hashedPasswordTwo = BCrypt.hashpw(passwordTwo, BCrypt.gensalt());
Instant creationTwo = Instant.ofEpochMilli(2000);
User inputUserTwo = new User(idTwo, nameTwo, hashedPasswordTwo, null, creationTwo);
// save
persistentDataStore.writeThrough(inputUserOne);
persistentDataStore.writeThrough(inputUserTwo);
// load
List<User> resultUsers = persistentDataStore.loadUsers();
// confirm that what we saved matches what we loaded
User resultUserOne = resultUsers.get(0);
Assert.assertEquals(idOne, resultUserOne.getId());
Assert.assertEquals(nameOne, resultUserOne.getName());
Assert.assertTrue(BCrypt.checkpw(passwordOne, resultUserOne.getPassword()));
Assert.assertEquals(creationOne, resultUserOne.getCreationTime());
User resultUserTwo = resultUsers.get(1);
Assert.assertEquals(idTwo, resultUserTwo.getId());
Assert.assertEquals(nameTwo, resultUserTwo.getName());
Assert.assertTrue(BCrypt.checkpw(passwordTwo, resultUserTwo.getPassword()));
Assert.assertEquals(creationTwo, resultUserTwo.getCreationTime());
}
use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class PersistentStorageAgentTest method testUpdateUser.
@Test
public void testUpdateUser() {
User user = new User(UUID.randomUUID(), "test_username", "password", "testbio", Instant.now());
persistentStorageAgent.updateEntity(user);
Mockito.verify(mockPersistentDataStore).updateEntity(user);
}
use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class ChatServlet method doGet.
/**
* This function fires when a user navigates to the chat page. It gets the conversation title from
* the URL, finds the corresponding Conversation, and fetches the messages in that Conversation.
* It then forwards to chat.jsp for rendering.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
System.out.println("Conversation was null: " + conversationTitle);
response.sendRedirect("/conversations");
return;
}
UUID conversationId = conversation.getId();
List<Message> messages = messageStore.getMessagesInConversation(conversationId);
List<User> conversationUsers = conversation.getConversationUsers();
request.setAttribute("conversation", conversation);
request.setAttribute("messages", messages);
request.setAttribute("conversationUsers", conversationUsers);
request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
}
use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class ChatServlet method doPost.
/**
* This function fires when a user submits the form on the chat page. It gets the logged-in
* username from the session, the conversation title from the URL, and the chat message from the
* submitted form data. It creates a new Message from that data, adds it to the model, and then
* redirects back to the chat page.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String button = request.getParameter("button");
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
// user is not logged in, don't let them add a message
response.sendRedirect("/login");
return;
}
User user = userStore.getUser(username);
if (user == null) {
// user was not found, don't let them add a message
response.sendRedirect("/login");
return;
}
String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
response.sendRedirect("/conversations");
return;
}
if ("joinButton".equals(button)) {
conversation.getConversationUsers().add(user);
String activityMessage = " joined " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>.";
Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "joinedConvo", activityMessage);
activityStore.addActivity(activity);
}
if ("leaveButton".equals(button)) {
conversation.getConversationUsers().remove(user);
String activityMessage = " left " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>.";
Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "leftConvo", activityMessage);
activityStore.addActivity(activity);
}
if (button == null && conversation.getConversationUsers().contains(user)) {
String messageContent = request.getParameter("message");
// this removes any HTML from the message content
String cleanedMessageContent = Jsoup.clean(messageContent, "", Whitelist.none(), new OutputSettings().prettyPrint(false));
String finalMessageContent = TextFormatter.formatForDisplay(cleanedMessageContent);
Message message = new Message(UUID.randomUUID(), conversation.getId(), user.getId(), finalMessageContent, Instant.now());
messageStore.addMessage(message);
String activityMessage = " sent a message in " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>" + ": " + finalMessageContent;
Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "messageSent", activityMessage);
activityStore.addActivity(activity);
}
// redirect to a GET request
response.sendRedirect("/chat/" + conversationTitle);
}
use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.
the class LoginServlet method doPost.
/**
* This function fires when a user submits the login form. It gets the username from the submitted
* form data, and then adds it to the session so we know the user is logged in.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (userStore.isUserRegistered(username)) {
User user = userStore.getUser(username);
if (BCrypt.checkpw(password, user.getPassword())) {
request.getSession().setAttribute("user", username);
response.sendRedirect("/conversations");
} else {
request.setAttribute("error", "Invalid password.");
request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "That username was not found.");
request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
}
}
Aggregations