@Test(expected = InvalidModelException.class) public void testCreateS3AttachmentTokenBadMD5() throws NumberFormatException, DatastoreException, NotFoundException, UnauthorizedException, InvalidModelException { S3AttachmentToken startToken = new S3AttachmentToken(); startToken.setFileName("SomeFile.jpg"); String almostMd5 = "79054025255fb1a26e4bc422aef"; startToken.setMd5(almostMd5); Long tokenId = new Long(456); String entityId = "132"; String userId = "007"; String expectedPath = entityId + "/" + tokenId.toString(); String expectePreSigneUrl = "I am a presigned url! whooot!"; when(mocIdGenerator.generateNewId()).thenReturn(tokenId); Credentials mockCreds = Mockito.mock(Credentials.class); when(mockUuserManager.getUserInfo(userId)).thenReturn(mockUser); when(mockPermissionsManager.hasAccess(entityId, ACCESS_TYPE.UPDATE, mockUser)).thenReturn(true); when(mocKLocationHelper.createFederationTokenForS3(userId, HttpMethod.PUT, expectedPath)) .thenReturn(mockCreds); when(mocKLocationHelper.presignS3PUTUrl(mockCreds, expectedPath, almostMd5, "image/jpeg")) .thenReturn(expectePreSigneUrl); // Make the actual call S3AttachmentToken endToken = manager.createS3AttachmentToken(userId, entityId, startToken); assertNotNull(endToken); assertEquals(expectePreSigneUrl, endToken.getPresignedUrl()); }
@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); }