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);
}
}
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);
}
}
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);
}
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");
}
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");
}
Aggregations