Example usage for org.springframework.mock.web MockHttpServletRequest setRemoteUser

List of usage examples for org.springframework.mock.web MockHttpServletRequest setRemoteUser

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setRemoteUser.

Prototype

public void setRemoteUser(@Nullable String remoteUser) 

Source Link

Usage

From source file:com.mtgi.analytics.servlet.SpringSessionContextTest.java

@Test
public void testAuthenticated() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteUser("testUser");
    ServletRequestAttributes atts = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(atts);

    assertEquals("context inherits user from request attributes", "testUser", inst.getContextUserId());
    assertEquals("context inherits session from request attributes", request.getSession().getId(),
            inst.getContextSessionId());
}

From source file:org.jasig.cas.adaptors.trusted.web.flow.PrincipalFromRequestRemoteUserNonInteractiveCredentialsActionTests.java

public void testRemoteUserExists() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteUser("test");

    final MockRequestContext context = new MockRequestContext();
    context.setExternalContext(//from  w  w w  .  ja v  a 2s.  com
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));

    assertEquals("success", this.action.execute(context).getId());
}

From source file:alpha.portal.webapp.controller.CardAssignFormControllerTest.java

/**
 * Test on submit.//from  w w w  .j a  v  a  2 s  .c  om
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testOnSubmit() throws Exception {
    final String cardId = "440e4816-e01b-74d4-a716-449955440092";
    final String caseId = "550e4713-e22b-11d4-a716-446655440000";

    final MockHttpServletRequest request = this.newPost("/cardassignform");
    request.setRemoteUser("admin");
    request.setParameter("card", cardId);
    request.setParameter("case", caseId);
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setParameter("cancel", "");
    this.form.onSubmit(request, response);

    response = new MockHttpServletResponse();
    request.removeParameter("cancel");
    request.setParameter("user", "-1");
    this.form.onSubmit(request, response);

    boolean found = false;
    for (final ContributorRequest r : this.contrReqManager.getAll()) {
        if (r.getAcceptingUser().getId().equals(-1L)
                && r.getAlphaCard().getAlphaCardIdentifier().getCardId().equals(cardId)
                && r.getRequestingUser().getId().equals(-2L)) {
            found = true;
            break;
        }
    }
    Assert.assertTrue(found);
}

From source file:alpha.portal.webapp.controller.CardAssignFormControllerTest.java

/**
 * Test show form./*w  ww .  j  a va 2s .  c  o  m*/
 */
@Test
public void testShowForm() {
    final MockHttpServletRequest request = this.newGet("/cardassignform");
    request.setRemoteUser("admin");
    request.addParameter("card", "");
    request.addParameter("case", "");

    Model m = new ExtendedModelMap();
    this.form.showForm(request, m);
    Assert.assertFalse(m.containsAttribute("users"));

    request.setParameter("card", "440e4816-e01b-74d4-a716-449955440092");
    m = new ExtendedModelMap();
    this.form.showForm(request, m);
    Assert.assertFalse(m.containsAttribute("users"));

    request.setParameter("case", "atjaerhe");
    m = new ExtendedModelMap();
    this.form.showForm(request, m);
    Assert.assertFalse(m.containsAttribute("users"));

    request.setParameter("case", "550e4713-e22b-11d4-a716-446655440000");
    m = new ExtendedModelMap();
    this.form.showForm(request, m);

    final List<UserExtension> l = new LinkedList<UserExtension>();
    l.add(this.userExtensionManager.get(-5L));
    l.add(this.userExtensionManager.get(-4L));
    l.add(this.userExtensionManager.get(-2L));

    // FIXME the sample-data.xml change concerning the contributorrole
    // values has broken this
    // ModelAndViewAssert.assertModelAttributeValue(new ModelAndView("",
    // m.asMap()), "users", l);
}

From source file:alpha.portal.webapp.controller.CardFormControllerTest.java

/**
 * Test assign card.//from www  . j  a va 2s .c o m
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testAssignCard() throws Exception {
    final MockHttpServletRequest request = this.newPost("/cardform");
    request.setRemoteUser("admin");
    final String view = this.c.assignCard(this.alphaCard, null, request);
    Assert.assertEquals("redirect:/caseform?caseId=" + CardFormControllerTest.CASE_ID + "&activeCardId="
            + CardFormControllerTest.CARD_ID, view);
}

From source file:fi.okm.mpass.shibboleth.authn.impl.ShibbolethSpAuthnServletTest.java

@Test
public void testWithRemoteUser() throws Exception {
    final MockHttpServletRequest servletRequest = initServletRequest();
    final String username = "mockUser";
    servletRequest.setRemoteUser(username);
    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    servlet.doGet(servletRequest, servletResponse);
    Assert.assertEquals(servletResponse.getRedirectedUrl(), flowExecutionUrl);
    assertExternalContext(null, null, username);
}

From source file:alpha.portal.webapp.controller.CardFormControllerTest.java

/**
 * Test save card./*from  ww w .  j a  v  a  2s .c o m*/
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testSaveCard() throws Exception {
    final MockHttpServletRequest request = this.newPost("/cardform");
    request.setRemoteUser("admin");
    final BindingResult errors = new DataBinder(this.alphaCard).getBindingResult();
    final String view = this.c.saveCard(this.alphaCard, errors, request, new MockHttpServletResponse());
    Assert.assertEquals("redirect:/caseform?activeCardId=" + CardFormControllerTest.CARD_ID + "&caseId="
            + CardFormControllerTest.CASE_ID, view);

    final MockHttpServletRequest request2 = this.newPost("/cardform");
    request2.setRemoteUser("admin");
    final AlphaCardIdentifier identifier = new AlphaCardIdentifier(CardFormControllerTest.CASE_ID);
    final AlphaCard alphaCard2 = new AlphaCard();
    alphaCard2.setAlphaCardIdentifier(identifier);

    this.c.saveCard(alphaCard2, null, request2, new MockHttpServletResponse());
    // unable to verify cardId since the controller doesn't return the saved
    // card to us :/
}

From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java

/**
 * Test on cancel./*from   w  w  w . jav a2s . c om*/
 */
@Test
public void testOnCancel() {
    final String caseId = "550e4713-e22b-11d4-a716-446655440000";
    final String cardId = "440e4816-e01b-74d4-a716-449955440092";
    final String fileName = "doesnotcompute.file";
    final String mimeType = "text/plain";
    final byte[] content = "roflcopter".getBytes();

    final MockHttpServletRequest request = this.newGet("/cardfileupload");
    request.setRemoteUser("admin");
    request.addParameter("case", caseId);
    request.addParameter("card", cardId);
    final FileUpload fileUpload = this.ctrl.showForm(request);
    fileUpload.setFile(content);

    final MockMultipartHttpServletRequest upload = new MockMultipartHttpServletRequest();
    upload.setRemoteUser("admin");
    final MockMultipartFile file = new MockMultipartFile("file", fileName, mimeType, content);
    upload.addFile(file);
    upload.addParameter("case", caseId);
    upload.addParameter("card", cardId);
    upload.addParameter("cancel", "Abbrechen");

    final BindingResult errors = new DataBinder(fileUpload).getBindingResult();
    String result = "";
    try {
        result = this.ctrl.onSubmit(fileUpload, errors, upload);
    } catch (final IOException e) {
        Assert.fail("Should not fail on fail upload");
    }
    Assert.assertFalse(errors.hasErrors());
    Assert.assertNull(upload.getSession().getAttribute("successMessages"));

    Assert.assertEquals("redirect:/caseform?caseId=" + caseId + "&activeCardId=" + cardId, result);
}

From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java

/**
 * Test zero file./*from   www.  j av a2 s.  c  o  m*/
 */
@Test
public void testZeroFile() {
    final String caseId = "550e4713-e22b-11d4-a716-446655440000";
    final String cardId = "440e4816-e01b-74d4-a716-449955440092";
    final String fileName = "doesnotcompute.file";
    final String mimeType = "text/plain";
    final byte[] content = "".getBytes();

    final MockHttpServletRequest request = this.newGet("/cardfileupload");
    request.setRemoteUser("admin");
    request.addParameter("case", caseId);
    request.addParameter("card", cardId);
    final FileUpload fileUpload = this.ctrl.showForm(request);
    fileUpload.setFile(content);

    final MockMultipartHttpServletRequest upload = new MockMultipartHttpServletRequest();
    upload.setRemoteUser("user");
    final MockMultipartFile file = new MockMultipartFile("file", fileName, mimeType, content);
    upload.addFile(file);
    upload.addParameter("case", caseId);
    upload.addParameter("card", cardId);

    final BindingResult errors = new DataBinder(fileUpload).getBindingResult();
    String result = "";
    try {
        result = this.ctrl.onSubmit(fileUpload, errors, upload);
    } catch (final IOException e) {
        Assert.fail("Should not fail on fail upload");
    }
    Assert.assertTrue(errors.hasErrors());
    final List<ObjectError> errorList = errors.getAllErrors();
    Assert.assertEquals(1, errorList.size());
    Assert.assertEquals("errors.required", errorList.get(0).getCode());
    Assert.assertNull(upload.getSession().getAttribute("successMessages"));

    Assert.assertEquals("redirect:/cardfileupload?card=" + cardId + "&case=" + caseId, result);
}

From source file:alpha.portal.webapp.controller.CaseFormControllerTest.java

/**
 * Test new./*  ww w . j a v a2  s.c o m*/
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testNew() throws Exception {
    final MockHttpServletRequest request = this.newGet("/caseform");
    request.setRemoteUser("admin");

    final HttpServletResponse response = new MockHttpServletResponse();
    final ModelAndView mv = this.form.showForm(this.filters, request, response);
    Assert.assertNotNull(mv);
    Assert.assertEquals("caseform", mv.getViewName());
    Assert.assertEquals(new AlphaCase(), mv.getModel().get("case"));
}