use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class UserStoreTest method testGetUser_byUsername_notFound.
@Test
public void testGetUser_byUsername_notFound() {
User resultUser = userStore.getUser("fake username");
Assert.assertNull(resultUser);
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class UserStoreTest method testGetUser_byId_found.
@Test
public void testGetUser_byId_found() {
User resultUser = userStore.getUser(USER_ONE.getId());
assertEquals(USER_ONE, resultUser);
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class LoginServlet method doPost.
/**
* This function fires when a user submits the login form. It gets the username and password from
* the submitted form data, checks that they're valid, and either adds the user to the session
* so we know the user is logged in or shows an error to the user.
*/
@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 (password.equals(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);
}
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class RegisterServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (!username.matches("[\\w*\\s*]*")) {
request.setAttribute("error", "Please enter only letters, numbers, and spaces.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
if (userStore.isUserRegistered(username)) {
request.setAttribute("error", "That username is already taken.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
User user = new User(UUID.randomUUID(), username, password, Instant.now());
userStore.addUser(user);
response.sendRedirect("/login");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class DefaultDataStore method addRandomConversations.
private void addRandomConversations() {
for (int i = 1; i <= DEFAULT_CONVERSATION_COUNT; i++) {
User user = getRandomElement(users);
String title = "Conversation_" + i;
Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), title, Instant.now());
PersistentStorageAgent.getInstance().writeThrough(conversation);
conversations.add(conversation);
}
}
Aggregations