@Test
 public void verifyClearPassWithUsernameMissingInCache() throws Exception {
   final ClearPassController controller = new ClearPassController(this.map);
   final MockHttpServletRequest req = new MockHttpServletRequest();
   req.setRemoteUser("casuser");
   final ModelAndView mv = controller.handleRequestInternal(req, new MockHttpServletResponse());
   assertEquals(mv.getViewName(), ClearPassController.DEFAULT_SERVICE_FAILURE_VIEW_NAME);
   assertTrue(mv.getModel().containsKey(ClearPassController.MODEL_FAILURE_DESCRIPTION));
 }
  @Test
  public void testEditProfile() throws Exception {
    log.debug("testing edit profile...");
    request = newGet("/userform.html");
    request.setRemoteUser("user");

    user = c.showForm(request, new MockHttpServletResponse());
    assertEquals("Tomcat User", user.getFullName());
  }
  @Test
  public void verifyClearPassSuccess() throws Exception {
    final ClearPassController controller = new ClearPassController(this.map);
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setRemoteUser("casuser");
    this.map.put("casuser", "password");

    final ModelAndView mv = controller.handleRequestInternal(req, new MockHttpServletResponse());
    assertEquals(mv.getViewName(), ClearPassController.DEFAULT_SERVICE_SUCCESS_VIEW_NAME);
    assertTrue(mv.getModel().containsKey(ClearPassController.MODEL_CLEARPASS));
    assertTrue(mv.getModel().containsValue("password"));
  }
  @Test
  public void testAddWithMissingFields() throws Exception {
    request = newPost("/userform.html");
    user = new User();
    user.setFirstName("Jack");
    request.setRemoteUser("user");

    BindingResult errors = new DataBinder(user).getBindingResult();
    c.onSubmit(user, errors, request, new MockHttpServletResponse());

    assertEquals(4, errors.getAllErrors().size());
  }
  @Test
  public void testSave() throws Exception {
    request = newPost("/userform.html");
    // set updated properties first since adding them later will
    // result in multiple parameters with the same name getting sent
    User user = ((UserManager) applicationContext.getBean("userManager")).getUser("-1");
    user.setConfirmPassword(user.getPassword());
    user.setLastName("Updated Last Name");

    request.setRemoteUser(user.getUsername());

    BindingResult errors = new DataBinder(user).getBindingResult();
    c.onSubmit(user, errors, request, new MockHttpServletResponse());

    assertFalse(errors.hasErrors());
    assertNotNull(request.getSession().getAttribute("successMessages"));
  }
  @Test
  public void shouldRemoveASessionAttendeeWhenNotGoing() {
    // ARRANGE
    int conferenceSessionId = 1;
    String emailId = "emailId";

    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setRemoteUser(emailId);

    SessionAttendeeRepository repository = mock(SessionAttendeeRepository.class);

    // ACT
    SessionAttendeeController controller = new SessionAttendeeController(repository);
    controller.notGoing(conferenceSessionId, httpServletRequest);

    // ASSERT
    verify(repository).remove(new SessionAttendee(emailId, conferenceSessionId));
  }
  @Test
  public void shouldCreateASessionAttendeeWhenGoing() {
    // ARRANGE
    int conferenceSessionId = 1;
    String emailId = "emailId";

    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setRemoteUser(emailId);

    SessionAttendeeRepository repository = mock(SessionAttendeeRepository.class);
    SessionAttendee createdConferenceSessionAttendee =
        new SessionAttendee(emailId, conferenceSessionId);
    when(repository.save(isA(SessionAttendee.class))).thenReturn(createdConferenceSessionAttendee);

    // ACT
    SessionAttendeeController controller = new SessionAttendeeController(repository);
    controller.going(conferenceSessionId, httpServletRequest);

    // ASSERT
    assertThat(createdConferenceSessionAttendee.getSessionId(), is(conferenceSessionId));
    assertThat(createdConferenceSessionAttendee.getAttendeeEmail(), is(emailId));
  }
Example #8
0
  @Test
  public void testProcessLoginEvents() throws Exception {
    final IntegerWrapper counter = new IntegerWrapper();

    JAASHelper jaasHelper = JAASHelper.getInstance();

    JAASHelper.setInstance(
        new JAASHelper() {

          @Override
          protected long doGetJaasUserId(long companyId, String name)
              throws PortalException, SystemException {

            try {
              return super.doGetJaasUserId(companyId, name);
            } finally {
              counter.increment();
            }
          }
        });

    if (mainServlet == null) {
      MockServletContext mockServletContext =
          new AutoDeployMockServletContext(getResourceBasePath(), new FileSystemResourceLoader());

      MockServletConfig mockServletConfig = new MockServletConfig(mockServletContext);

      MainServlet mainServlet = new MainServlet();

      try {
        mainServlet.init(mockServletConfig);
      } catch (ServletException se) {
        throw new RuntimeException("The main servlet could not be initialized");
      }
    }

    Date lastLoginDate = _user.getLastLoginDate();

    MockHttpServletRequest mockHttpServletRequest =
        new MockHttpServletRequest(
            mainServlet.getServletContext(), HttpMethods.GET, StringPool.SLASH);

    mockHttpServletRequest.setRemoteUser(String.valueOf(_user.getUserId()));

    JAASAction preJAASAction = new JAASAction();
    JAASAction postJAASAction = new JAASAction();

    try {
      EventsProcessorUtil.registerEvent(PropsKeys.LOGIN_EVENTS_PRE, preJAASAction);
      EventsProcessorUtil.registerEvent(PropsKeys.LOGIN_EVENTS_POST, postJAASAction);

      mainServlet.service(mockHttpServletRequest, new MockHttpServletResponse());

      Assert.assertEquals(2, counter.getValue());
      Assert.assertTrue(preJAASAction.isRan());
      Assert.assertTrue(postJAASAction.isRan());

      _user = UserLocalServiceUtil.getUser(_user.getUserId());

      Assert.assertFalse(lastLoginDate.after(_user.getLastLoginDate()));
    } finally {
      EventsProcessorUtil.unregisterEvent(PropsKeys.LOGIN_EVENTS_PRE, postJAASAction);
      EventsProcessorUtil.unregisterEvent(PropsKeys.LOGIN_EVENTS_POST, postJAASAction);

      JAASHelper.setInstance(jaasHelper);
    }
  }
Example #9
0
  public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVStorage webDAVStorage = new DLWebDAVStorageImpl();

    webDAVStorage.setToken("document_library");

    WebDAVUtil.addStorage(webDAVStorage);

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {
      mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
      Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
      headers = new HashMap<String, String>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
      throw new Exception();
    } catch (Exception e) {
      StackTraceElement[] stackTraceElements = e.getStackTrace();

      for (StackTraceElement stackTraceElement : stackTraceElements) {
        String methodName = stackTraceElement.getMethodName();

        if (methodName.equals("setUp")
            || methodName.equals("tearDown")
            || methodName.startsWith("test")) {

          String testName =
              StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

          testName =
              StringUtil.replace(testName, new String[] {"WebDAV", "Test"}, new String[] {"", ""});

          headers.put(
              "X-Litmus",
              testName
                  + ": ("
                  + stackTraceElement.getMethodName()
                  + ":"
                  + stackTraceElement.getLineNumber()
                  + ")");

          break;
        }
      }
    }

    if (data != null) {
      mockHttpServletRequest.setContent(data);

      String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

      if (contentType != null) {
        mockHttpServletRequest.setContentType(contentType);
      } else {
        mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
      }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      mockHttpServletRequest.addHeader(key, value);
    }

    try {
      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

      webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

      int statusCode = mockHttpServletResponse.getStatus();
      byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

      Map<String, String> responseHeaders = new HashMap<String, String>();

      for (String name : mockHttpServletResponse.getHeaderNames()) {
        responseHeaders.put(name, (String) mockHttpServletResponse.getHeader(name));
      }

      return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }