Search in sources :

Example 16 with ZFilterRule

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

the class TestFilter method testNotifyActionUseOrigHeaders.

@Test
public void testNotifyActionUseOrigHeaders() throws Exception {
    if (mAvailableRFCCompliantNotify) {
        fail("Unable to test because 'zimbraSieveNotifyActionRFCCompliant' is set to TRUE");
        return;
    }
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZFilterCondition.ZTrueCondition());
    // add an action to notify user2
    // copy headers From,To,Cc,Subject from the original message onto the notification message
    actions.add(new ZFilterAction.ZNotifyAction(TestUtil.getAddress(REMOTE_USER_NAME), null, "${BODY}", -1, "From,To,Cc,Subject"));
    rules.add(new ZFilterRule("testNotifyActionUseOrigHeaders", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testNotifyActionUseOrigHeaders";
    String body = "Hi, How r u?";
    String msg = new MessageBuilder().withFrom(REMOTE_USER_NAME).withToRecipient(USER_NAME).withCcRecipient(USER_NAME).withSubject(subject).withBody(body).create();
    // send msg to user1
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, REMOTE_USER_NAME, msg);
    // check notification msg from user1 in user2's mailbox
    ZMessage zMessage = TestUtil.waitForMessage(TestUtil.getZMailbox(REMOTE_USER_NAME), "in:inbox subject:\"" + subject + "\"");
    boolean checkedFrom = false, checkedTo = false, checkedCc = false;
    List<ZEmailAddress> msgAddrs = zMessage.getEmailAddresses();
    for (ZEmailAddress addr : msgAddrs) {
        if ("f".equals(addr.getType())) {
            assertEquals(TestUtil.addDomainIfNecessary(REMOTE_USER_NAME), addr.getAddress());
            if (!checkedFrom) {
                checkedFrom = true;
            } else {
                fail("multiple From addresses");
            }
        }
        if ("t".equals(addr.getType())) {
            assertEquals(TestUtil.addDomainIfNecessary(USER_NAME), addr.getAddress());
            if (!checkedTo) {
                checkedTo = true;
            } else {
                fail("multiple To addresses");
            }
        }
        if ("c".equals(addr.getType())) {
            assertEquals(TestUtil.addDomainIfNecessary(USER_NAME), addr.getAddress());
            if (!checkedCc) {
                checkedCc = true;
            } else {
                fail("multiple Cc addresses");
            }
        }
    }
    assertEquals(subject, zMessage.getSubject());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZEmailAddress(com.zimbra.client.ZEmailAddress) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 17 with ZFilterRule

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

the class TestFilter method testMarkRead.

@Test
public void testMarkRead() throws Exception {
    String folderName = NAME_PREFIX + " testMarkRead";
    // if the subject contains "testMarkRead", file into a folder and mark read
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testMarkRead"));
    actions.add(new ZFileIntoAction(folderName));
    actions.add(new ZMarkAction(MarkOp.READ));
    rules.add(new ZFilterRule("testMarkRead", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    // Deliver message.
    String address = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " testMarkRead";
    TestUtil.addMessageLmtp(subject, address, address);
    // Check folder and unread state.
    ZMessage msg = TestUtil.getMessage(mMbox, "in:\"" + folderName + "\" subject:\"" + subject + "\"");
    String flags = msg.getFlags();
    assertTrue("Unexpected flags: " + flags, flags == null || flags.indexOf(ZMessage.Flag.unread.getFlagChar()) < 0);
}
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) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 18 with ZFilterRule

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

the class TestFilter method testSpecialCharInBody.

/**
     * Tests fix for bug 56019.
     */
@Test
public void testSpecialCharInBody() throws Exception {
    assertTrue("To run this test copy data/unittest/TestFilter-testSpecialCharInBody.msg to /opt/zimbra/unittest", Files.exists(Paths.get("/opt/zimbra/unittest/TestFilter-testSpecialCharInBody.msg")));
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    conditions.add(new ZBodyCondition(BodyOp.CONTAINS, "André"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testSpecialCharInBody", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message and test the flagged state.
    String address = TestUtil.getAddress(USER_NAME);
    // TestFilter-testSpecialCharInBody.msg's body contains base64 encoded content (containing "André")
    String msgContent = new String(ByteUtil.getContent(new File("/opt/zimbra/unittest/TestFilter-testSpecialCharInBody.msg")));
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:testSpecialCharInBody");
    assertTrue("Unexpected message flag state", msg.isFlagged());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZBodyCondition(com.zimbra.client.ZFilterCondition.ZBodyCondition) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) File(java.io.File) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 19 with ZFilterRule

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

the class TestFilter method testReplyAction.

@Test
public void testReplyAction() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZFilterCondition.ZTrueCondition());
    actions.add(new ZFilterAction.ZReplyAction("Hi ${FROM}, Your message was: '${BODY}'. Thanks!"));
    rules.add(new ZFilterRule("testReplyAction", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testReplyAction";
    String body = "Hi, How r u?";
    String msg = new MessageBuilder().withFrom(REMOTE_USER_NAME).withSubject(subject).withBody(body).create();
    // send msg from user2 to user1
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, REMOTE_USER_NAME, msg);
    // check msg got filed into user1's mailbox
    TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // check auto reply from user1 in user2's mailbox
    ZMessage zMessage = TestUtil.waitForMessage(TestUtil.getZMailbox(REMOTE_USER_NAME), "in:inbox subject:\"Re: " + subject + "\"");
    ZMessage.ZMimePart mimeStructure = zMessage.getMimeStructure();
    String bodyContent = mimeStructure.getContent();
    assertTrue("template vars should be replaced", !bodyContent.contains("${FROM}") && !bodyContent.contains("${BODY}"));
    assertTrue(bodyContent.contains(TestUtil.getAddress(REMOTE_USER_NAME)) && bodyContent.contains(body));
    // bug 65369
    assertTrue(!mimeStructure.getContentType().contains("charset") || mimeStructure.getContentType().contains(MimeConstants.P_CHARSET_ASCII));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 20 with ZFilterRule

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

the class TestFilter method testAttachment.

@Test
public void testAttachment() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // If attachment exists, set tag1.  If attachment doesn't exist, set tag2.
    ZTag tag1 = mMbox.createTag(NAME_PREFIX + " testAttachment1", null);
    ZTag tag2 = mMbox.createTag(NAME_PREFIX + " testAttachment2", null);
    conditions.add(new ZAttachmentExistsCondition(true));
    actions.add(new ZTagAction(tag1.getName()));
    rules.add(new ZFilterRule("testAttachment1", true, false, conditions, actions));
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZAttachmentExistsCondition(false));
    actions.add(new ZTagAction(tag2.getName()));
    rules.add(new ZFilterRule("testAttachment2", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message with an attachment.
    String address = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " testAttachment1";
    String msgContent = new MessageBuilder().withSubject(subject).withAttachment("attach this", "attach.txt", "text/plain").create();
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // Check the tag states.
    Set<String> tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tag1.getId()));
    // Add a message with no attachment.
    subject = NAME_PREFIX + " testAttachment2";
    msgContent = new MessageBuilder().withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // Check the tag states.
    tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tag2.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZTag(com.zimbra.client.ZTag) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZAttachmentExistsCondition(com.zimbra.client.ZFilterCondition.ZAttachmentExistsCondition) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Aggregations

ZFilterRule (com.zimbra.client.ZFilterRule)43 ZFilterAction (com.zimbra.client.ZFilterAction)37 ZFilterCondition (com.zimbra.client.ZFilterCondition)37 ZFilterRules (com.zimbra.client.ZFilterRules)37 ArrayList (java.util.ArrayList)35 ZMessage (com.zimbra.client.ZMessage)29 Test (org.junit.Test)26 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)20 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)13 ZMailbox (com.zimbra.client.ZMailbox)12 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)10 ZFileIntoAction (com.zimbra.client.ZFilterAction.ZFileIntoAction)9 HashSet (java.util.HashSet)5 ZBodyCondition (com.zimbra.client.ZFilterCondition.ZBodyCondition)4 ZTag (com.zimbra.client.ZTag)4 ZDiscardAction (com.zimbra.client.ZFilterAction.ZDiscardAction)3 ZRedirectAction (com.zimbra.client.ZFilterAction.ZRedirectAction)3 ZMountpoint (com.zimbra.client.ZMountpoint)3 ZKeepAction (com.zimbra.client.ZFilterAction.ZKeepAction)2 ZRFCCompliantNotifyAction (com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction)2