Search in sources :

Example 16 with ZFilterRules

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

the class TestFilter method saveOutgoingRules.

/**
     * Saves the given outgoing filter rules.  Then gets them from the server and confirms that
     * the element tree matches.
     */
private void saveOutgoingRules(ZMailbox mbox, ZFilterRules rules) throws Exception {
    mbox.saveOutgoingFilterRules(rules);
    ZFilterRules result = mbox.getOutgoingFilterRules(true);
    TestUtil.assertEquals(rules.dump(), result.dump());
}
Also used : ZFilterRules(com.zimbra.client.ZFilterRules)

Example 17 with ZFilterRules

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

Example 18 with ZFilterRules

use of com.zimbra.client.ZFilterRules 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 19 with ZFilterRules

use of com.zimbra.client.ZFilterRules 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 20 with ZFilterRules

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

Aggregations

ZFilterRules (com.zimbra.client.ZFilterRules)48 ZFilterAction (com.zimbra.client.ZFilterAction)37 ZFilterCondition (com.zimbra.client.ZFilterCondition)37 ZFilterRule (com.zimbra.client.ZFilterRule)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