Search in sources :

Example 86 with User

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);
}
Also used : User(codeu.model.data.User) Test(org.junit.Test)

Example 87 with User

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);
}
Also used : User(codeu.model.data.User) Test(org.junit.Test)

Example 88 with User

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);
    }
}
Also used : User(codeu.model.data.User)

Example 89 with User

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");
}
Also used : User(codeu.model.data.User)

Example 90 with User

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);
    }
}
Also used : User(codeu.model.data.User) Conversation(codeu.model.data.Conversation)

Aggregations

User (codeu.model.data.User)91 Test (org.junit.Test)60 Conversation (codeu.model.data.Conversation)36 Message (codeu.model.data.Message)23 UserStore (codeu.model.store.basic.UserStore)9 UUID (java.util.UUID)9 HttpSession (javax.servlet.http.HttpSession)8 Activity (codeu.model.data.Activity)7 Instant (java.time.Instant)7 ArrayList (java.util.ArrayList)7 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 Comparator (java.util.Comparator)2 ActivityStore (codeu.model.store.basic.ActivityStore)1 OutputSettings (org.jsoup.nodes.Document.OutputSettings)1