コード例 #1
0
  @Test
  public void testcreateS3ProfileToken()
      throws JSONObjectAdapterException, ServletException, IOException, DatastoreException {
    S3AttachmentToken startToken = new S3AttachmentToken();
    startToken.setFileName("someImage.jpg");
    startToken.setMd5(TEST_MD5);

    String fileName = "images/squarish.png";
    URL toUpUrl = S3TokenControllerTest.class.getClassLoader().getResource(fileName);
    assertNotNull("Failed to find: " + fileName + " on the classpath", toUpUrl);
    File toUpload = new File(toUpUrl.getFile());
    // Create the token

    S3AttachmentToken resultToken =
        testHelper.createS3AttachmentToken(
            TestUserDAO.TEST_USER_NAME,
            ServiceConstants.AttachmentType.USER_PROFILE,
            testUser.getId(),
            startToken);
    System.out.println(resultToken);
    assertNotNull(resultToken);
    assertNotNull(resultToken.getTokenId());
    assertNotNull(resultToken.getPresignedUrl());

    // Upload it
    String path =
        S3TokenManagerImpl.createAttachmentPathNoSlash(testUser.getId(), resultToken.getTokenId());
    s3Utility.uploadToS3(toUpload, path);

    // Make sure we can get a signed download URL for this attachment.
    long now = System.currentTimeMillis();
    long oneMinuteFromNow = now + (60 * 1000);
    PresignedUrl url =
        testHelper.getUserProfileAttachmentUrl(
            TestUserDAO.TEST_USER_NAME, testUser.getId(), resultToken.getTokenId());
    System.out.println(url);
    assertNotNull(url);
    assertNotNull(url.getPresignedUrl());
    URL urlReal = new URL(url.getPresignedUrl());
    // Check that it expires quickly (not as important when it's the public user profile picture)
    String[] split = urlReal.getQuery().split("&");
    assertTrue(split.length > 1);
    String[] expiresSplit = split[0].split("=");
    assertEquals("Expires", expiresSplit[0]);
    // It should expire within a minute max
    Long expirsInt = Long.parseLong(expiresSplit[1]);
    long expiresMil = expirsInt.longValue() * 1000l;
    System.out.println("Now: " + new Date(now));
    System.out.println("Expires: " + new Date(expiresMil));
    assertTrue("This URL should expire in under a minute!", expiresMil < oneMinuteFromNow);
    assertTrue("This URL should expire after now!", now <= expiresMil);

    // Delete the file
    s3Utility.deleteFromS3(path);
  }
コード例 #2
0
 @Test
 public void testGetAttachmentUrl() throws Exception {
   Long tokenId = new Long(456);
   String entityId = "132";
   String expectedPath =
       S3TokenManagerImpl.createAttachmentPathSlash(entityId, tokenId.toString());
   String expectePreSigneUrl = "I am a presigned url! whooot!";
   when(mockUuserManager.getUserInfo(userId)).thenReturn(mockUser);
   when(mockPermissionsManager.hasAccess(entityId, ACCESS_TYPE.READ, mockUser)).thenReturn(true);
   when(mocKLocationHelper.presignS3GETUrlShortLived(userId, expectedPath))
       .thenReturn(expectePreSigneUrl);
   when(mockS3Utilitiy.doesExist(any(String.class))).thenReturn(true);
   // Make the actual call
   when(mockUuserManager.getUserInfo(userId)).thenReturn(mockUser);
   PresignedUrl url = manager.getAttachmentUrl(userId, entityId, tokenId.toString());
   assertNotNull(url);
   assertEquals(expectePreSigneUrl, url.getPresignedUrl());
   assertEquals(URLStatus.READ_FOR_DOWNLOAD, url.getStatus());
 }