Search in sources :

Example 11 with ZFilterAction

use of com.zimbra.client.ZFilterAction 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)

Example 12 with ZFilterAction

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

the class TestFilter method testNotifyWithDiscard.

@Test
public void testNotifyWithDiscard() 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
    actions.add(new ZFilterAction.ZNotifyAction(TestUtil.getAddress(REMOTE_USER_NAME), "${SUBJECT}", "From: ${FROM}, Message: ${BODY}"));
    // add discard action
    actions.add(new ZDiscardAction());
    rules.add(new ZFilterRule("testNotifyWithDiscard", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testNotifyWithDiscard";
    String body = "Hi, How r u?";
    String msg = new MessageBuilder().withFrom(REMOTE_USER_NAME).withSubject(subject).withBody(body).create();
    // send msg to user1
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, REMOTE_USER_NAME, msg);
    // check msg not filed into user1's mailbox
    List<ZMessage> msgs = TestUtil.search(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("original message should not have been filed", msgs.isEmpty());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZDiscardAction(com.zimbra.client.ZFilterAction.ZDiscardAction) 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 13 with ZFilterAction

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

the class TestFilter method testCaseSensitiveComparison.

public void testCaseSensitiveComparison() throws Exception {
    // Create case sensitive header and body filter rules
    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, true, "CHECK THIS"));
    conditions.add(new ZBodyCondition(BodyOp.CONTAINS, true, "CHECK THIS"));
    actions.add(new ZTagAction(mTag1.getName()));
    rules.add(new ZFilterRule("testCaseSensitiveComparison", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    // Deliver message having lower case subject substring and make sure that tag1 was not applied
    String subject = NAME_PREFIX + " testCaseSensitiveComparison1 check this";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("message should not have been tagged", msg.getTagIds() == null || msg.getTagIds().isEmpty());
    // Deliver message having upper case subject substring and make sure that tag1 was applied
    subject = NAME_PREFIX + " testCaseSensitiveComparison2 CHECK THIS";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertNotNull("message should have been tagged", msg.getTagIds());
    Set<String> tagIds = new HashSet<String>();
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertTrue("message should have been tagged with tag1", tagIds.contains(mTag1.getId()));
    // Deliver message having lower case body content substring and make sure that tag1 was not applied
    subject = NAME_PREFIX + " testCaseSensitiveComparison3";
    String content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withBody("Hi check this").create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("message should not have been tagged", msg.getTagIds() == null || msg.getTagIds().isEmpty());
    // Deliver message having upper case body content substring and make sure that tag1 was applied
    subject = NAME_PREFIX + " testCaseSensitiveComparison4";
    content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withBody("Hi CHECK THIS").create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertNotNull("message should have been tagged", msg.getTagIds());
    tagIds = new HashSet<String>();
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertTrue("message should have been tagged with tag1", 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) ZBodyCondition(com.zimbra.client.ZFilterCondition.ZBodyCondition) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) HashSet(java.util.HashSet)

Example 14 with ZFilterAction

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

the class TestFilter method testCurrentTimeTest.

@Test
public void testCurrentTimeTest() throws Exception {
    TimeZone userTz = Util.getAccountTimeZone(user1);
    Calendar calendar = Calendar.getInstance(userTz);
    // Add 5 mins to current time
    calendar.add(Calendar.MINUTE, 5);
    // format time in HHmm
    SimpleDateFormat format = new SimpleDateFormat("HHmm");
    format.setTimeZone(userTz);
    String timeStr = format.format(calendar.getTime());
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    // Condition true if msg received after timeStr
    conditions.add(new ZFilterCondition.ZCurrentTimeCondition(DateOp.AFTER, timeStr));
    actions.add(new ZTagAction(mTag1.getName()));
    rules.add(new ZFilterRule("testCurrentTimeTest after", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testCurrentTimeTest1";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("message should not have been tagged", msg.getTagIds() == null || msg.getTagIds().isEmpty());
    rules = new ArrayList<ZFilterRule>();
    conditions = new ArrayList<ZFilterCondition>();
    // Condition true if msg received before timeStr
    conditions.add(new ZFilterCondition.ZCurrentTimeCondition(DateOp.BEFORE, timeStr));
    rules.add(new ZFilterRule("testCurrentTimeTest before", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    subject = NAME_PREFIX + " testCurrentTimeTest2";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertNotNull("message should have been tagged", msg.getTagIds());
    Set<String> tagIds = new HashSet<String>();
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertTrue("message should have been tagged with tag1", tagIds.contains(mTag1.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) Calendar(java.util.Calendar) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) TimeZone(java.util.TimeZone) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) SimpleDateFormat(java.text.SimpleDateFormat) ZFilterRules(com.zimbra.client.ZFilterRules) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with ZFilterAction

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

the class TestFilter method testSpam.

/**
     * Confirms that spam filtering takes a higher precedence than
     * custom user filtering (bug 23886).
     */
public void testSpam() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String sender = TestUtil.getAddress(USER_NAME);
    String[] recipients = new String[] { sender };
    String message = TestUtil.getTestMessage(NAME_PREFIX + " testSpam", USER_NAME, USER_NAME, null);
    Config config = Provisioning.getInstance().getConfig();
    message = config.getAttr(Provisioning.A_zimbraSpamHeader) + ": " + config.getAttr(Provisioning.A_zimbraSpamHeaderValue) + "\r\n" + message;
    // Make sure spam message doesn't already exist
    assertEquals(0, TestUtil.search(mbox, "in:junk subject:testSpam").size());
    // Deliver spam message without matching rule, make sure it gets delivered
    // to the junk folder, and delete.
    TestUtil.addMessageLmtp(recipients, sender, message);
    ZMessage msg = TestUtil.getMessage(mbox, "in:junk subject:testSpam");
    mbox.deleteMessage(msg.getId());
    // Add matching filter rule: if subject contains "testSpam", file into folder1
    ZFilterRules rules = getTestIncomingRules();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testSpam"));
    actions.add(new ZFileIntoAction(FOLDER1_PATH));
    rules.getRules().add(new ZFilterRule("testBug5455", true, false, conditions, actions));
    saveIncomingRules(mMbox, rules);
    // Set "apply user rules" attribute to TRUE and make sure the message gets filed into folder1
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraSpamApplyUserFilters, LdapConstants.LDAP_TRUE);
    TestUtil.addMessageLmtp(recipients, sender, message);
    msg = TestUtil.waitForMessage(mbox, "in:" + FOLDER1_PATH + " subject:testSpam");
    mbox.deleteMessage(msg.getId());
    // Set "apply user rules" attribute to FALSE and make sure the message gets filed into junk
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraSpamApplyUserFilters, LdapConstants.LDAP_FALSE);
    TestUtil.addMessageLmtp(recipients, sender, message);
    TestUtil.waitForMessage(mbox, "in:junk subject:testSpam");
}
Also used : ZMessage(com.zimbra.client.ZMessage) Config(com.zimbra.cs.account.Config) 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) ZMailbox(com.zimbra.client.ZMailbox) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZFilterRules(com.zimbra.client.ZFilterRules)

Aggregations

ZFilterAction (com.zimbra.client.ZFilterAction)37 ZFilterCondition (com.zimbra.client.ZFilterCondition)37 ZFilterRule (com.zimbra.client.ZFilterRule)37 ZFilterRules (com.zimbra.client.ZFilterRules)37 ArrayList (java.util.ArrayList)35 ZMessage (com.zimbra.client.ZMessage)28 Test (org.junit.Test)23 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)20 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)13 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)10 ZFileIntoAction (com.zimbra.client.ZFilterAction.ZFileIntoAction)9 ZMailbox (com.zimbra.client.ZMailbox)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 ZKeepAction (com.zimbra.client.ZFilterAction.ZKeepAction)2 ZRFCCompliantNotifyAction (com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction)2 ZFolder (com.zimbra.client.ZFolder)2