Search in sources :

Example 36 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

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", "password", "test biography", 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 37 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class LoginServletTest method testDoPost_CorrectPass.

@Test
public void testDoPost_CorrectPass() throws IOException, ServletException {
    Mockito.when(mockRequest.getParameter("username")).thenReturn("test_username");
    Mockito.when(mockRequest.getParameter("password")).thenReturn("password");
    UserStore mockUserStore = Mockito.mock(UserStore.class);
    Mockito.when(mockUserStore.isUserRegistered("test_username")).thenReturn(true);
    String hashedPassword = BCrypt.hashpw("password", BCrypt.gensalt());
    User testUser = new User(UUID.randomUUID(), "test_username", hashedPassword, "test biography", Instant.now());
    Mockito.when(mockUserStore.getUser("test_username")).thenReturn(testUser);
    loginServlet.setUserStore(mockUserStore);
    HttpSession mockSession = Mockito.mock(HttpSession.class);
    Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
    loginServlet.doPost(mockRequest, mockResponse);
    Mockito.verify(mockUserStore, Mockito.never()).addUser(Mockito.any(User.class));
    Mockito.verify(mockSession).setAttribute("user", "test_username");
    Mockito.verify(mockResponse).sendRedirect("/conversations");
}
Also used : UserStore(codeu.model.store.basic.UserStore) User(codeu.model.data.User) HttpSession(javax.servlet.http.HttpSession) Test(org.junit.Test)

Example 38 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class ProfileServletTest method testDoGet.

@Test
public void testDoGet() throws IOException, ServletException {
    Mockito.when(mockRequest.getRequestURI()).thenReturn("/profile/test_user");
    User testUser = new User(UUID.randomUUID(), "test_user", "password", null, Instant.now());
    Mockito.when(mockUserStore.getUser("test_user")).thenReturn(testUser);
    List<Message> fakeMessageList = new ArrayList<>();
    fakeMessageList.add(new Message(UUID.randomUUID(), UUID.randomUUID(), testUser.getId(), "test message 1", Instant.ofEpochMilli(2000)));
    fakeMessageList.add(new Message(UUID.randomUUID(), UUID.randomUUID(), testUser.getId(), "test message 2", Instant.ofEpochMilli(1000)));
    Mockito.when(mockMessageStore.getMessagesByAuthorSorted(testUser.getId())).thenReturn(fakeMessageList);
    profileServlet.doGet(mockRequest, mockResponse);
    Mockito.verify(mockRequest).setAttribute("user", testUser);
    Mockito.verify(mockRequest).setAttribute("messages", fakeMessageList);
    Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 39 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class RegisterServletTest method testDoPost_NewUser.

@Test
public void testDoPost_NewUser() throws IOException, ServletException {
    Mockito.when(mockRequest.getParameter("username")).thenReturn("test username");
    Mockito.when(mockRequest.getParameter("password")).thenReturn("test password");
    Mockito.when(mockRequest.getParameter("confirmPassword")).thenReturn("test password");
    UserStore mockUserStore = Mockito.mock(UserStore.class);
    Mockito.when(mockUserStore.isUserRegistered("test username")).thenReturn(false);
    registerServlet.setUserStore(mockUserStore);
    ActivityStore mockActivityStore = Mockito.mock(ActivityStore.class);
    registerServlet.setActivityStore(mockActivityStore);
    HttpSession mockSession = Mockito.mock(HttpSession.class);
    Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
    registerServlet.doPost(mockRequest, mockResponse);
    ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
    ArgumentCaptor<Activity> activityArgumentCaptor = ArgumentCaptor.forClass(Activity.class);
    Mockito.verify(mockUserStore).addUser(userArgumentCaptor.capture());
    Assert.assertEquals(userArgumentCaptor.getValue().getName(), "test username");
    Mockito.verify(mockActivityStore).addActivity(activityArgumentCaptor.capture());
    Assert.assertEquals(activityArgumentCaptor.getValue().getUserId(), userArgumentCaptor.getValue().getId());
    Mockito.verify(mockResponse).sendRedirect("/login");
}
Also used : ActivityStore(codeu.model.store.basic.ActivityStore) UserStore(codeu.model.store.basic.UserStore) User(codeu.model.data.User) HttpSession(javax.servlet.http.HttpSession) Activity(codeu.model.data.Activity) Test(org.junit.Test)

Example 40 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class SearchServletTest method testDoGet.

@Test
public void testDoGet() throws IOException, ServletException {
    Mockito.when(mockRequest.getParameter("search")).thenReturn("te");
    User testUser = new User(UUID.randomUUID(), "test_user", "password", "test biography", Instant.now());
    ArrayList<User> users = new ArrayList<User>();
    users.add(testUser);
    Mockito.when(mockUserStore.searchUsersSorted("te")).thenReturn(users);
    searchServlet.doGet(mockRequest, mockResponse);
    Mockito.verify(mockRequest).setAttribute("users", users);
    Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Also used : User(codeu.model.data.User) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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