public static String getSystemResourceAsString(String filename) { return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); }
public static SharedByteArrayInputStream getSystemResourceAsSharedStream(String filename) { return new SharedByteArrayInputStream(getSystemResourceAsByteArray(filename)); } }
@Test public void spamEmailShouldBeWellConvertedToJsonWithApacheTika() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( textExtractor, ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/nonTextual.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo( ClassLoaderUtils.getSystemResourceAsString("eml/nonTextual.json", StandardCharsets.UTF_8)); }
@Test public void emailWithNoMailboxIdShouldThrow() { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mailWithNoMailboxId = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, null); mailWithNoMailboxId.setModSeq(MOD_SEQ); mailWithNoMailboxId.setUid(UID); assertThatThrownBy(() -> messageToElasticSearchJson.convertToJson(mailWithNoMailboxId, ImmutableList.of(MAILBOX_SESSION.getUser()))) .isInstanceOf(NullPointerException.class); }
@Test public void spamEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/spamMail.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/spamMail.json")); }
@Test public void pgpSignedEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage pgpSignedMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/pgpSignedMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); pgpSignedMail.setModSeq(MOD_SEQ); pgpSignedMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(pgpSignedMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/pgpSignedMail.json")); }
public static String getSystemResourceAsString(String filename) { return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); }
public static SharedByteArrayInputStream getSystemResourceAsSharedStream(String filename) { return new SharedByteArrayInputStream(getSystemResourceAsByteArray(filename)); } }
@Test public void htmlEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage htmlMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/htmlMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("social", "pocket-money").build(), propertyBuilder, MAILBOX_ID); htmlMail.setModSeq(MOD_SEQ); htmlMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(htmlMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/htmlMail.json")); }
public static String getSystemResourceAsString(String filename) { return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); }
public static SharedByteArrayInputStream getSystemResourceAsSharedStream(String filename) { return new SharedByteArrayInputStream(getSystemResourceAsByteArray(filename)); } }
@Test public void emailWithNoInternalDateShouldUseNowDate() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test public void retrieveMessageShouldReturnTheMessageWhenMailExists() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)).thenReturn(Stream.of(repository)); FakeMail mail = FakeMail.builder() .name(NAME_1.asString()) .fileName("mail.eml") .build(); repository.store(mail); Optional<MimeMessage> mimeMessage = testee.retrieveMessage(FIRST_REPOSITORY_PATH, NAME_1); assertThat(mimeMessage).isNotEmpty(); String eml = IOUtils.toString(new MimeMessageInputStream(mimeMessage.get()), StandardCharsets.UTF_8); String expectedContent = ClassLoaderUtils.getSystemResourceAsString("mail.eml"); assertThat(eml).isEqualToNormalizingNewlines(expectedContent); } }
@Test public void forPayloadShouldCalculateDifferentHashesWhenCraftedSha1Collision() throws Exception { byte[] payload1 = ClassLoaderUtils.getSystemResourceAsByteArray("shattered-1.pdf"); byte[] payload2 = ClassLoaderUtils.getSystemResourceAsByteArray("shattered-2.pdf"); BlobId blobId1 = BLOB_ID_FACTORY.forPayload(payload1); BlobId blobId2 = BLOB_ID_FACTORY.forPayload(payload2); assertThat(blobId1).isNotEqualTo(blobId2); } }
@Test public void recursiveEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage recursiveMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); recursiveMail.setModSeq(MOD_SEQ); recursiveMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(recursiveMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test public void quotaRatioShouldBeWellConvertedToJson() throws IOException { String user = "user@domain.org"; QuotaUsageUpdatedEvent event = new QuotaUsageUpdatedEvent( new MockMailboxSession(user), QuotaRoot.quotaRoot("any", Optional.of(Domain.of("domain.org"))), QuotaFixture.Counts._52_PERCENT, QuotaFixture.Sizes._55_PERCENT, Instant.now()); QuotaRatioToElasticSearchJson quotaRatioToElasticSearchJson = new QuotaRatioToElasticSearchJson(); String convertToJson = quotaRatioToElasticSearchJson.convertToJson(user, event); assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("quotaRatio.json")); }
@Test public void setMessagesShouldCreateMessageWhenSendingMessageWithNonIndexableAttachment() throws Exception { Attachment nonIndexableAttachment = Attachment.builder() .bytes(ClassLoaderUtils.getSystemResourceAsByteArray("attachment/nonIndexableAttachment.html")) .type("text/html") .build();
@Test public void emailWithAttachmentsShouldConvertAttachmentsWhenIndexAttachmentsIsTrue() throws IOException { // Given MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); // When MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(MAILBOX_SESSION.getUser())); // Then assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test public void quotaRatioShouldBeWellConvertedToJsonWhenNoDomain() throws IOException { String user = "user"; QuotaUsageUpdatedEvent event = new QuotaUsageUpdatedEvent( new MockMailboxSession(user), QuotaRoot.quotaRoot("any", Optional.empty()), QuotaFixture.Counts._52_PERCENT, QuotaFixture.Sizes._55_PERCENT, Instant.now()); QuotaRatioToElasticSearchJson quotaRatioToElasticSearchJson = new QuotaRatioToElasticSearchJson(); String convertToJson = quotaRatioToElasticSearchJson.convertToJson(user, event); assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("quotaRatioNoDomain.json")); } }
@Test public void getMessageListShouldExcludeMessagesWhenAttachmentFilterDoesntMatch() throws Exception { mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, ALICE, "mailbox"); byte[] attachmentContent = ClassLoaderUtils.getSystemResourceAsByteArray("eml/attachment.pdf"); Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(BodyPartBuilder.create() .setBody(attachmentContent, "application/pdf") .setContentDisposition("attachment")) .addBodyPart(BodyPartBuilder.create() .setBody("The message has a PDF attachment.", "plain", StandardCharsets.UTF_8)) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); mailboxProbe.appendMessage(ALICE, MailboxPath.forUser(ALICE, "mailbox"), new ByteArrayInputStream(DefaultMessageWriter.asBytes(message)), new Date(), false, new Flags()); await(); given() .header("Authorization", aliceAccessToken.serialize()) .body("[[\"getMessageList\", {\"filter\":{\"attachments\":\"no apple inside\"}}, \"#0\"]]") .when() .post("/jmap") .then() .statusCode(200) .body(ARGUMENTS + ".messageIds", empty()); }