Search in sources :

Example 71 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class DefaultDataStore method addRandomMessages.

private void addRandomMessages() {
    for (int i = 0; i < DEFAULT_MESSAGE_COUNT; i++) {
        Conversation conversation = getRandomElement(conversations);
        User author = getRandomElement(users);
        String content = getRandomMessageContent();
        Message message = new Message(UUID.randomUUID(), conversation.getId(), author.getId(), content, Instant.now());
        PersistentStorageAgent.getInstance().writeThrough(message);
        messages.add(message);
    }
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation)

Example 72 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class DefaultDataStore method addRandomUsers.

private void addRandomUsers() {
    List<String> randomUsernames = getRandomUsernames();
    Collections.shuffle(randomUsernames);
    for (int i = 0; i < DEFAULT_USER_COUNT; i++) {
        User user = new User(UUID.randomUUID(), randomUsernames.get(i), BCrypt.hashpw("password", BCrypt.gensalt()), Instant.now());
        PersistentStorageAgent.getInstance().writeThrough(user);
        users.add(user);
    }
}
Also used : User(codeu.model.data.User)

Example 73 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

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

Example 74 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class ConversationServletTest method testDoPost_NewConversation.

@Test
public void testDoPost_NewConversation() 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(false);
    conversationServlet.doPost(mockRequest, mockResponse);
    ArgumentCaptor<Conversation> conversationArgumentCaptor = ArgumentCaptor.forClass(Conversation.class);
    Mockito.verify(mockConversationStore).addConversation(conversationArgumentCaptor.capture());
    Assert.assertEquals(conversationArgumentCaptor.getValue().getTitle(), "test_conversation");
    Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
Also used : User(codeu.model.data.User) Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 75 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

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");
    String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());
    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, passwordHash, Instant.now());
    userStore.addUser(user);
    response.sendRedirect("/login");
}
Also used : User(codeu.model.data.User)

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