Search in sources :

Example 26 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilter method testRedirect.

/**
     * Tests the redirect filter action and confirms that the X-ZimbraForwarded
     * header is set on the redirected message.
     */
@Test
public void testRedirect() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "testRedirect", redirect to user2
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testRedirect"));
    actions.add(new ZRedirectAction(TestUtil.getAddress(REMOTE_USER_NAME)));
    rules.add(new ZFilterRule("testRedirect", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message.  Set the From header to something bogus to make
    // sure we're not rewriting it
    String from = "joebob@mycompany.com";
    String subject = NAME_PREFIX + " testRedirect 1";
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    // Confirm that user1 did not receive it.
    List<ZMessage> messages = TestUtil.search(mMbox, "subject:\"" + subject + "\"");
    assertEquals(0, messages.size());
    // Confirm that user2 received it, and make sure X-ZimbraForwarded is set.
    ZMailbox remoteMbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
    ZMessage msg = TestUtil.waitForMessage(remoteMbox, "in:inbox subject:\"" + subject + "\"");
    byte[] content = TestUtil.getContent(remoteMbox, msg.getId()).getBytes();
    MimeMessage mimeMsg = new MimeMessage(new ByteArrayInputStream(content));
    assertEquals(user1.getName(), mimeMsg.getHeader(FilterUtil.HEADER_FORWARDED));
    assertEquals(from, mimeMsg.getHeader("From"));
    // Check zimbraMailRedirectSetEnvelopeSender=FALSE.
    int port = 6025;
    DummySmtpServer smtp = startSmtpServer(port);
    localServer.setSmtpPort(port);
    localServer.setMailRedirectSetEnvelopeSender(false);
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    assertEquals(from, smtp.getMailFrom());
    // Check zimbraMailRedirectSetEnvelopeSender=TRUE.
    smtp = startSmtpServer(port);
    localServer.setMailRedirectSetEnvelopeSender(true);
    subject = NAME_PREFIX + " testRedirect 2";
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    String userAddress = Strings.nullToEmpty(TestUtil.getAddress(USER_NAME)).toLowerCase();
    assertEquals("testRedirect 2 mail from", userAddress, smtp.getMailFrom());
    // Check empty envelope sender.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 3";
    String msgContent = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    String[] recipients = new String[] { USER_NAME };
    TestUtil.addMessageLmtp(recipients, null, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
    // Check Auto-Submitted=yes.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 4";
    msgContent = "Auto-Submitted: yes\r\n" + TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
    // Check Auto-Submitted=no.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 5";
    msgContent = "Auto-Submitted: no\r\n" + TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertEquals("testRedirect 5 mail from", userAddress, smtp.getMailFrom());
    // Check Content-Type=multipart/report.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 6";
    msgContent = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    msgContent = msgContent.replace("text/plain", "multipart/report");
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) ZMailbox(com.zimbra.client.ZMailbox) MimeMessage(com.zimbra.common.mime.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 27 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilter method testHeaderMatches.

/**
     * Tests matching a header against a wildcard expression.  See bug 21701.
     */
@Test
public void testHeaderMatches() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // If subject matches the first for characters + *, mark as flagged.
    String pattern = NAME_PREFIX.substring(0, 4) + "*";
    conditions.add(new ZHeaderCondition("subject", HeaderOp.MATCHES, pattern));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testHeaderMatches", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message and confirm it is flagged
    String address = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " testHeaderMatches";
    TestUtil.addMessageLmtp(subject, address, address);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:testHeaderMatches");
    assertTrue("Message was not flagged", msg.isFlagged());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 28 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilter method testMatchMultipleFilters.

/**
     * Confirms that all actions are performed when a message matches multiple
     * filter rules.
     */
public void testMatchMultipleFilters() throws Exception {
    String sender = TestUtil.getAddress("multiplefilters");
    String recipient = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " This goes into folder1 and folder2";
    TestUtil.addMessageLmtp(subject, recipient, sender);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:" + FOLDER1_PATH + " " + subject);
    TestUtil.verifyTag(mMbox, msg, TAG1_NAME);
    TestUtil.verifyTag(mMbox, msg, TAG2_NAME);
    TestUtil.verifyFlag(mMbox, msg, Flag.flagged);
    msg = TestUtil.getMessage(mMbox, "in:" + FOLDER2_PATH + " " + subject);
    TestUtil.verifyTag(mMbox, msg, TAG1_NAME);
    TestUtil.verifyTag(mMbox, msg, TAG2_NAME);
    TestUtil.verifyFlag(mMbox, msg, Flag.flagged);
}
Also used : ZMessage(com.zimbra.client.ZMessage)

Example 29 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilter method testToOrCc.

public void testToOrCc() throws Exception {
    // Create filter rules
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("to,cc", HeaderOp.CONTAINS, "checkthis.com"));
    actions.add(new ZTagAction(mTag1.getName()));
    rules.add(new ZFilterRule("testToOrCc", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    // Deliver message 1 and make sure that tag 1 was applied
    String subject = NAME_PREFIX + " testToOrCc 1";
    String content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withCcRecipient("cc@checkthis.com").create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    Set<String> tagIds = new HashSet<String>();
    assertNotNull(msg.getTagIds());
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(mTag1.getId()));
    // Deliver message 2 and make sure that tag 1 was applied
    subject = NAME_PREFIX + " testToOrCc 2";
    content = new MessageBuilder().withSubject(subject).withToRecipient("to@checkthis.com").withCcRecipient(USER_NAME).create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    tagIds = new HashSet<String>();
    assertNotNull(msg.getTagIds());
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(mTag1.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) HashSet(java.util.HashSet)

Example 30 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilter method testMimeHeader.

public void testMimeHeader() throws Exception {
    // Create a text/plain message with a text/html attachment.
    String subject = NAME_PREFIX + " testMimeHeader";
    String attachmentData = "<html><body>I'm so attached to you.</body></html>";
    String content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withAttachment(attachmentData, "attachment.html", "text/html").create();
    // Create filter rules.
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZMimeHeaderCondition("Content-Type", HeaderOp.CONTAINS, "text/plain"));
    actions.add(new ZTagAction(mTag1.getName()));
    rules.add(new ZFilterRule("testMarkRead 1", true, false, conditions, actions));
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZMimeHeaderCondition("Content-Type", HeaderOp.CONTAINS, "text/html"));
    actions.add(new ZTagAction(mTag2.getName()));
    rules.add(new ZFilterRule("testMarkRead 2", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    // Deliver message and make sure that tag 2 was applied, but not tag 1.
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    Set<String> tagIds = new HashSet<String>();
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertEquals(2, tagIds.size());
    assertTrue(tagIds.contains(mTag1.getId()));
    assertTrue(tagIds.contains(mTag2.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMimeHeaderCondition(com.zimbra.client.ZFilterCondition.ZMimeHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) HashSet(java.util.HashSet)

Aggregations

ZMessage (com.zimbra.client.ZMessage)101 Test (org.junit.Test)63 ZMailbox (com.zimbra.client.ZMailbox)59 ArrayList (java.util.ArrayList)43 ZFilterRule (com.zimbra.client.ZFilterRule)29 ZFilterAction (com.zimbra.client.ZFilterAction)28 ZFilterCondition (com.zimbra.client.ZFilterCondition)28 ZFilterRules (com.zimbra.client.ZFilterRules)28 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)12 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)12 ZEmailAddress (com.zimbra.client.ZEmailAddress)11 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)10 ZFolder (com.zimbra.client.ZFolder)10 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)8 Account (com.zimbra.cs.account.Account)8 MessagePart (com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart)7 Date (java.util.Date)7 ZGetMessageParams (com.zimbra.client.ZGetMessageParams)5 ZMimePart (com.zimbra.client.ZMessage.ZMimePart)5 ZTag (com.zimbra.client.ZTag)5