@Test
  public void request_content_image_that_binary_is_on_main_version() throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    // setup: draft version of content
    ContentEntity content = fixture.findContentByKey(contentKey);

    BinaryDataEntity binaryDataOfMainVersion = content.getMainVersion().getBinaryData("source");

    // exercise & verify
    String imageRequestPath =
        "_image/" + contentKey + "/binary/" + binaryDataOfMainVersion.getKey() + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() > 0);
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  @Test
  public void testArgumentsMatching() throws Exception {
    MockHttpServletRequest mockHttpServletRequest = createHttpRequest("/foo/hello-world");

    try {
      lookupJSONWebServiceAction(mockHttpServletRequest);

      Assert.fail();
    } catch (RuntimeException re) {
    }

    mockHttpServletRequest =
        createHttpRequest("/foo/hello-world/user-id/173/world-name/Forbidden Planet");

    JSONWebServiceAction jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    Assert.assertEquals("Welcome 173 to Forbidden Planet", jsonWebServiceAction.invoke());

    mockHttpServletRequest = createHttpRequest("/foo/hello-world");

    mockHttpServletRequest.setParameter("userId", "371");
    mockHttpServletRequest.setParameter("worldName", "Impossible Planet");

    jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    Assert.assertEquals("Welcome 371 to Impossible Planet", jsonWebServiceAction.invoke());

    mockHttpServletRequest = createHttpRequest("/foo/hello-world/user-id/173");

    mockHttpServletRequest.setParameter("worldName", "Impossible Planet");

    jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    Assert.assertEquals("Welcome 173 to Impossible Planet", jsonWebServiceAction.invoke());
  }
示例#3
0
  @Test
  public void request_content_image_with_no_access() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    fixture.createAndStoreUserAndUserGroup(
        "user-with-no-access", "testuser fullname", UserType.NORMAL, "testuserstore");
    loginUserInPortal(fixture.findUserByName("user-with-no-access").getKey());

    String imageRequestPath = "_image/" + contentKey + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");
    try {
      imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
      fail("Expected exception");
    } catch (Exception e) {
      assertTrue(e instanceof ImageRequestException);
      ImageRequestException imageRequestException = (ImageRequestException) e;
      assertTrue(
          imageRequestException
              .getMessage()
              .contains("Resource '/_image/" + contentKey + ".jpg' not found"));
    }
  }
  @Test
  public void testCamelCaseNormalizedParameters() throws Exception {
    MockHttpServletRequest mockHttpServletRequest =
        createHttpRequest("/foo/camel/good-name/goodboy/bad-name/badboy");

    JSONWebServiceAction jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    String result = (String) jsonWebServiceAction.invoke();

    Assert.assertEquals("goodboy*badboy", result);

    mockHttpServletRequest = createHttpRequest("/foo/camel");

    mockHttpServletRequest.setParameter("goodName", "goodboy");
    mockHttpServletRequest.setParameter("badNAME", "badboy");

    jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    result = (String) jsonWebServiceAction.invoke();

    Assert.assertEquals("goodboy*badboy", result);

    mockHttpServletRequest.removeAllParameters();
    mockHttpServletRequest.setParameter("goodName", "goodboy");
    mockHttpServletRequest.setParameter("badName", "badboy");

    jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    result = (String) jsonWebServiceAction.invoke();

    Assert.assertEquals("goodboy*badboy", result);
  }
示例#5
0
  @Test
  public void request_content_image_that_is_deleted() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);
    ContentEntity content = fixture.findContentByKey(contentKey);
    content.setDeleted(true);

    String imageRequestPath = "_image/" + contentKey + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");
    try {
      imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
      fail("Expected exception");
    } catch (Exception e) {
      assertTrue(e instanceof ImageRequestException);
      ImageRequestException imageRequestException = (ImageRequestException) e;
      assertTrue(
          imageRequestException
              .getMessage()
              .contains("Resource '/_image/" + contentKey + ".jpg' not found"));
    }
  }
  @Test
  public void excludeの指定がincludeのアスタリスクの指定より優先される() throws Exception {

    req.setParameter("key1", "value1");
    req.setParameter("key2", "value2");
    req.setParameter("key3", "value3");
    req.setParameter("key4", "value4");
    ViewIdDefinitionTag tag = new ViewIdDefinitionTag();
    tag.setPageContext(context);
    tag.setId("VID#0001");
    tag.setLabel("画面A");
    tag.setUrl("/foo/bar/baz.do");
    tag.setPankuzu(true);
    tag.setScope("session");
    tag.setExclude("key2,key3");
    tag.setInclude("*");
    tag.doStartTag();
    tag.doEndTag();
    assertThat(tag.getId(), is(nullValue()));
    assertThat(tag.getLabel(), is(nullValue()));
    assertThat(tag.getUrl(), is(nullValue()));
    assertThat(tag.getScope(), is("session"));
    ViewId id = ViewId.current(req);
    assertThat(id.getId(), is("VID#0001"));
    assertThat(id.getLabel(), is("画面A"));
    assertThat(id.getUrl(), is("/foo/bar/baz.do"));
    assertThat(id.getParams().size(), is(2));
    assertThat(id.getParams().get("key1")[0], is("value1"));
    assertThat(id.getParams().get("key4")[0], is("value4"));
  }
  @Test
  public void verifyOK() throws Exception {
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    ((OAuth20WrapperController) oauth20WrapperController)
        .getServicesManager()
        .save(getRegisteredService(REDIRECT_URI, CLIENT_SECRET));

    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal p = org.jasig.cas.authentication.TestUtils.getPrincipal(ID, map);
    final TicketGrantingTicketImpl impl =
        new TicketGrantingTicketImpl(
            TGT_ID,
            org.jasig.cas.authentication.TestUtils.getAuthentication(p),
            new NeverExpiresExpirationPolicy());

    ((OAuth20WrapperController) oauth20WrapperController)
        .getTicketRegistry()
        .addTicket(
            new ServiceTicketImpl(
                CODE,
                impl,
                org.jasig.cas.authentication.TestUtils.getService(),
                false,
                new ExpirationPolicy() {
                  private static final long serialVersionUID = -7321055962209199811L;

                  @Override
                  public boolean isExpired(final TicketState ticketState) {
                    return false;
                  }
                }));

    oauth20WrapperController.handleRequest(mockRequest, mockResponse);

    ((OAuth20WrapperController) oauth20WrapperController).getTicketRegistry().deleteTicket(CODE);

    assertEquals("text/plain", mockResponse.getContentType());
    assertEquals(200, mockResponse.getStatus());
    final String body = mockResponse.getContentAsString();

    assertTrue(
        body.startsWith(
            OAuthConstants.ACCESS_TOKEN + '=' + TGT_ID + '&' + OAuthConstants.EXPIRES + '='));
    // delta = 2 seconds
    final int delta = 2;
    final int timeLeft =
        Integer.parseInt(StringUtils.substringAfter(body, '&' + OAuthConstants.EXPIRES + '='));
    assertTrue(timeLeft >= TIMEOUT - 10 - delta);
  }
 @Test
 public void shouldCheckOperatePermissionForStageOperationRequest() throws Exception {
   request.setParameter("pipelineName", "cruise");
   request.setParameter("stageName", "dev");
   request.setMethod("post");
   assumeUserHasOperatePermissionForStage();
   assertThat(permissionInterceptor.preHandle(request, response, null), is(true));
 }
示例#9
0
  @Test
  @Ignore
  public void exception_thrown_for_request_to_content_image_that_binary_is_not_on_main_version()
      throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    // setup: draft version of content
    ContentEntity content = fixture.findContentByKey(contentKey);
    ContentVersionEntity draftVersion = createDraftVersion("Arn.JPG");
    draftVersion.setContentDataXml(content.getMainVersion().getContentDataAsXmlString());
    content.setDraftVersion(draftVersion);
    content.addVersion(draftVersion);
    fixture.save(draftVersion);

    BinaryDataEntity binaryDataForDraftVersion = factory.createBinaryData("Arn.JPG", bytes.length);
    binaryDataForDraftVersion.setBlobKey(
        content.getMainVersion().getBinaryData("source").getBlobKey());
    fixture.save(binaryDataForDraftVersion);

    ContentBinaryDataEntity contentBinaryDataForDraftVersion =
        factory.createContentBinaryData("source", binaryDataForDraftVersion, draftVersion);
    draftVersion.addContentBinaryData(contentBinaryDataForDraftVersion);
    fixture.save(contentBinaryDataForDraftVersion);

    fixture.flushAndClearHibernateSesssion();

    // exercise & verify
    String imageRequestPath =
        "_image/" + contentKey + "/binary/" + binaryDataForDraftVersion.getKey() + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_version", draftVersion.getKey().toString());
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    try {
      imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
      fail("Expected exception");
    } catch (Exception e) {
      assertTrue(e instanceof ImageRequestException);
      ImageRequestException imageRequestException = (ImageRequestException) e;
      assertTrue(
          imageRequestException
              .getMessage()
              .contains(
                  "Resource '/_image/"
                      + contentKey
                      + "/binary/"
                      + binaryDataForDraftVersion.getKey()
                      + ".jpg' not found"));
    }
  }
  @Test
  public void request_content_image_that_does_not_exist() throws Exception {
    String imageRequestPath = "_image/123.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
 @Test
 public void verifyNoClientSecret() throws Exception {
   final MockHttpServletRequest mockRequest =
       new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
   mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
   mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
   mockRequest.setParameter(OAuthConstants.CODE, CODE);
   final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
   oauth20WrapperController.handleRequest(mockRequest, mockResponse);
   assertEquals(400, mockResponse.getStatus());
   assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
 }
  @Test
  public void testTypeConversion2() throws Exception {
    MockHttpServletRequest mockHttpServletRequest = createHttpRequest("/foo/hey");

    mockHttpServletRequest.setParameter("calendar", "1330419334285");
    mockHttpServletRequest.setParameter("userIds", "[1,2,3]");
    mockHttpServletRequest.setParameter("locales", "[en,fr]");
    mockHttpServletRequest.setParameter("ids", "[173,-7,007]");

    JSONWebServiceAction jsonWebServiceAction = lookupJSONWebServiceAction(mockHttpServletRequest);

    Assert.assertEquals("2012, 1/3, en/2, 173/3", jsonWebServiceAction.invoke());
  }
  @Test
  public void verifyExpiredServiceTicket() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    ((OAuth20WrapperController) oauth20WrapperController)
        .getServicesManager()
        .save(getRegisteredService(REDIRECT_URI, CLIENT_SECRET));

    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal p = org.jasig.cas.authentication.TestUtils.getPrincipal(ID, map);
    final TicketGrantingTicketImpl impl =
        new TicketGrantingTicketImpl(
            TGT_ID,
            org.jasig.cas.authentication.TestUtils.getAuthentication(p),
            new NeverExpiresExpirationPolicy());

    ((OAuth20WrapperController) oauth20WrapperController)
        .getTicketRegistry()
        .addTicket(
            new ServiceTicketImpl(
                "ST1",
                impl,
                org.jasig.cas.authentication.TestUtils.getService(),
                false,
                new ExpirationPolicy() {
                  private static final long serialVersionUID = -7321055962209199811L;

                  @Override
                  public boolean isExpired(final TicketState ticketState) {
                    return true;
                  }
                }));

    oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(400, mockResponse.getStatus());
    assertEquals("error=" + OAuthConstants.INVALID_GRANT, mockResponse.getContentAsString());
  }
  @Test
  public void verifyExpiredAccessToken() throws Exception {
    final Principal principal =
        org.jasig.cas.authentication.TestUtils.getPrincipal(ID, new HashMap<String, Object>());
    final Authentication authentication = new OAuthAuthentication(ZonedDateTime.now(), principal);
    final DefaultAccessTokenFactory expiringAccessTokenFactory = new DefaultAccessTokenFactory();
    expiringAccessTokenFactory.setExpirationPolicy(
        new ExpirationPolicy() {
          @Override
          public boolean isExpired(final TicketState ticketState) {
            return true;
          }
        });
    final AccessTokenImpl accessToken =
        (AccessTokenImpl) expiringAccessTokenFactory.create(TestUtils.getService(), authentication);
    oAuth20ProfileController.getTicketRegistry().addTicket(accessToken);

    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getId());
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    oAuth20ProfileController.handleRequest(mockRequest, mockResponse);
    assertEquals(200, mockResponse.getStatus());
    assertEquals(
        "{\"error\":\"" + OAuthConstants.EXPIRED_ACCESS_TOKEN + "\"}",
        mockResponse.getContentAsString());
  }
示例#15
0
 @Test
 public void request_content_image_that_does_not_exist() throws Exception {
   String imageRequestPath = "_image/123" + ".jpg";
   setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
   httpServletRequest.setParameter("_background", "0xffffff");
   httpServletRequest.setParameter("_quality", "100");
   try {
     imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
     fail("Expected exception");
   } catch (Exception e) {
     assertTrue(e instanceof ImageRequestException);
     ImageRequestException imageRequestException = (ImageRequestException) e;
     assertTrue(
         imageRequestException.getMessage().contains("Resource '/_image/123.jpg' not found"));
   }
 }
示例#16
0
  @Test
  public void editDocumentWithParams2() throws DocumentNotFoundException {

    final DocumentReference documentReference =
        newDocumentReference("hello.txt")
            .withDocumentClass("test")
            .withIndex("name", "Wangler")
            .build();

    when(documentService.findDocumentReference(1L)).thenReturn(documentReference);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter("firstname", "Silvio");
    final ModelAndView modelAndView = controller.editDocument(1L, request);

    assertThat(modelAndView.getViewName(), is("import.successful"));
    assertThat(modelAndView.getModel().size(), is(1));
    assertThat(modelAndView.getModel().containsKey("doc"), is(true));
    final DocumentReference doc = (DocumentReference) modelAndView.getModel().get("doc");
    assertThat(doc, is(documentReference));
    assertThat(
        doc.getIndices().get(new TranslatableKey("name")).getValue().toString(), is("Wangler"));

    InOrder order = inOrder(documentService);

    order.verify(documentService).findDocumentReference(1L);
    order.verify(documentService, never()).updateIndices(documentReference);
    order.verifyNoMoreInteractions();
  }
 @Test
 public void shouldNotCheckOperatePermissionForEditingConfigurationRequest() throws Exception {
   request.setParameter("pipelineName", "cruise");
   request.setRequestURI("/admin/restful/configuration");
   request.setMethod("post");
   assertThat(permissionInterceptor.preHandle(request, response, null), is(true));
 }
  @Test
  public void includeに指定したパラメータのみがViewIdにパラメータとして設定される() throws Exception {

    req.setParameter("key1", "value1");
    req.setParameter("key2", "value2");
    ViewIdDefinitionTag tag = new ViewIdDefinitionTag();
    tag.setPageContext(context);
    tag.setId("VID#0001");
    tag.setLabel("画面A");
    tag.setUrl("/foo/bar/baz.do");
    tag.setPankuzu(true);
    tag.setScope("session");
    tag.setInclude("key1");
    tag.doStartTag();
    tag.doEndTag();
    assertThat(tag.getId(), is(nullValue()));
    assertThat(tag.getLabel(), is(nullValue()));
    assertThat(tag.getUrl(), is(nullValue()));
    assertThat(tag.getScope(), is("session"));
    ViewId id = ViewId.current(req);
    assertThat(id.getId(), is("VID#0001"));
    assertThat(id.getLabel(), is("画面A"));
    assertThat(id.getUrl(), is("/foo/bar/baz.do"));
    assertThat(id.getParams().get("key1")[0], is("value1"));
    assertThat(id.getParams().get("key2"), is(nullValue()));
  }
  @Test
  public void request_content_image_with_label_small() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    String imageRequestPath = "_image/" + contentKey.toString() + "/label/small";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");
    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals("image/png", httpServletResponse.getContentType());
    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
  }
  @Test
  public void request_content_image_with_label_that_does_not_exist() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    String imageRequestPath = "_image/" + contentKey + "/label/nonexistinglabel.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
 @Test
 public void shouldCheckOperatePermissionOnFirstStageForForcePipelineRequest() throws Exception {
   request.setParameter("pipelineName", "cruise");
   request.setRequestURI("/admin/force");
   request.setMethod("post");
   assumeUserHasOperatePermissionForFirstStage();
   assertThat(permissionInterceptor.preHandle(request, response, null), is(true));
 }
  @Test
  public void request_user_image() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    UserEntity testUser = fixture.findUserByName("testuser");
    testUser.setPhoto(bytes);

    fixture.flushAndClearHibernateSesssion();

    String imageRequestPath = "_image/user/" + testUser.getKey() + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");
    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  /** Zuul DebugFilter can be triggered by "deug" parameter. */
  @Test
  public void shouldFilter_on_default_swagger_url_with_param() {

    MockHttpServletRequest request = new MockHttpServletRequest("GET", DEFAULT_URL);
    request.setParameter("debug", "true");
    RequestContext.getCurrentContext().setRequest(request);

    assertTrue(filter.shouldFilter());
  }
  @Test
  public void request_content_image_that_is_deleted() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);
    ContentEntity content = fixture.findContentByKey(contentKey);
    content.setDeleted(true);

    String imageRequestPath = "_image/" + contentKey + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
示例#25
0
  @Test
  public void
      exception_thrown_for_request_to_content_image_that_is_not_online_when_is_not_related_to_content_in_preview()
          throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 13, 0, 0, 0), null);

    // setup: preview
    Mockito.when(previewService.isInPreview()).thenReturn(true);

    ContentEntity contentPreviewed = new ContentEntity();

    ContentData contentData = Mockito.mock(ContentData.class);
    Mockito.when(contentData.resolveRelatedContentKeys())
        .thenReturn(Sets.newHashSet(new ContentKey(666)));
    ContentVersionEntity versionPreviewed = Mockito.mock(ContentVersionEntity.class);
    Mockito.when(versionPreviewed.getContentData()).thenReturn(contentData);
    versionPreviewed.setContentData(contentData);
    ContentAndVersion contentAndVersionPreviewed =
        new ContentAndVersion(contentPreviewed, versionPreviewed);
    ContentPreviewContext contentPreviewContext =
        new ContentPreviewContext(contentAndVersionPreviewed);
    PreviewContext previewContext = new PreviewContext(contentPreviewContext);

    Mockito.when(previewService.getPreviewContext()).thenReturn(previewContext);

    // exercise & verify
    String imageRequestPath = "_image/" + contentKey;
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    try {
      imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
      fail("Expected exception");
    } catch (Exception e) {
      assertTrue(e instanceof ImageRequestException);
      assertTrue(e.getMessage().contains("Resource '/_image/" + contentKey + "' not found"));
    }
  }
  @Test
  public void verifyNoServiceTicket() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    ((OAuth20WrapperController) oauth20WrapperController)
        .getServicesManager()
        .save(getRegisteredService(REDIRECT_URI, CLIENT_SECRET));

    oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(400, mockResponse.getStatus());
    assertEquals("error=" + OAuthConstants.INVALID_GRANT, mockResponse.getContentAsString());
  }
 @Test
 public void shouldCheckViewPermissionForGetRequestIfPipelineNamePresent() throws Exception {
   request.setParameter("pipelineName", "cruise");
   request.setMethod("get");
   assumeUserHasViewPermission();
   assertThat(
       "shouldCheckViewPermissionIfPipelineNamePresent",
       permissionInterceptor.preHandle(request, response, null),
       is(true));
 }
  @Test
  public void response_ok_for_request_to_content_image_that_binary_is_not_on_main_version()
      throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    // setup: draft version of content
    ContentEntity content = fixture.findContentByKey(contentKey);
    ContentVersionEntity draftVersion = createDraftVersion("Arn.JPG");
    draftVersion.setContentDataXml(content.getMainVersion().getContentDataAsXmlString());
    content.setDraftVersion(draftVersion);
    content.addVersion(draftVersion);
    fixture.save(draftVersion);

    BinaryDataEntity binaryDataForDraftVersion = factory.createBinaryData("Arn.JPG", bytes.length);
    binaryDataForDraftVersion.setBlobKey(
        content.getMainVersion().getBinaryData("source").getBlobKey());
    fixture.save(binaryDataForDraftVersion);

    ContentBinaryDataEntity contentBinaryDataForDraftVersion =
        factory.createContentBinaryData("source", binaryDataForDraftVersion, draftVersion);
    draftVersion.addContentBinaryData(contentBinaryDataForDraftVersion);
    fixture.save(contentBinaryDataForDraftVersion);

    fixture.flushAndClearHibernateSesssion();

    // exercise & verify
    String imageRequestPath = "_image/" + contentKey + "/label/source.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_version", draftVersion.getKey().toString());
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() > 0);
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
 @Test
 public void getRequestNotAllowed() throws Exception {
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.setMethod("GET");
   request.setParameter("foo", "bar");
   MockHttpServletResponse response = new MockHttpServletResponse();
   postOnlyAdapter.handleRequest(request, response);
   assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getStatus());
   Message<?> message = requests.receive(0);
   assertNull(message);
 }
 private ServletRequest prepareServletRequest(
     Object target, NativeWebRequest request, MethodParameter parameter) {
   String modelPrefixName = parameter.getParameterAnnotation(FormModel.class).value();
   HttpServletRequest nativeRequest = (HttpServletRequest) request.getNativeRequest();
   MultipartRequest multipartRequest =
       WebUtils.getNativeRequest(nativeRequest, MultipartRequest.class);
   MockHttpServletRequest mockRequest = null;
   if (multipartRequest != null) {
     MockMultipartHttpServletRequest mockMultipartRequest = new MockMultipartHttpServletRequest();
     for (MultipartFile file : multipartRequest.getFileMap().values()) {
       mockMultipartRequest.addFile(
           new MultipartFileWrapper(getNewParameterName(file.getName(), modelPrefixName), file));
     }
     mockRequest = mockMultipartRequest;
   } else {
     mockRequest = new MockHttpServletRequest();
   }
   for (Entry<String, String> entry : getUriTemplateVariables(request).entrySet()) {
     String parameterName = entry.getKey();
     String value = entry.getValue();
     if (isFormModelAttribute(parameterName, modelPrefixName)) {
       mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value);
     }
   }
   for (Object parameterEntry : nativeRequest.getParameterMap().entrySet()) {
     Entry<String, String[]> entry = (Entry<String, String[]>) parameterEntry;
     String parameterName = entry.getKey();
     String[] value = entry.getValue();
     if (isFormModelAttribute(parameterName, modelPrefixName)) {
       mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value);
     }
   }
   return mockRequest;
 }