use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class TestFilter method testMultipleMultilineText.
/**
* Tests fix for bug 57890 (https://issues.apache.org/jira/browse/JSIEVE-75).
*/
@Test
public void testMultipleMultilineText() throws Exception {
if (mAvailableRFCCompliantNotify) {
fail("Unable to test because 'zimbraSieveNotifyActionRFCCompliant' is set to TRUE");
return;
}
List<ZFilterRule> rules;
List<ZFilterCondition> conditions;
List<ZFilterAction> actions;
rules = new ArrayList<ZFilterRule>();
conditions = new ArrayList<ZFilterCondition>();
actions = new ArrayList<ZFilterAction>();
conditions.add(new ZFilterCondition.ZTrueCondition());
String notifyMsg1 = "From: ${FROM}\nMessage: ${BODY}\n";
actions.add(new ZFilterAction.ZNotifyAction("abc@xyz.com", "${SUBJECT}", notifyMsg1));
String notifyMsg2 = "you've got mail";
actions.add(new ZFilterAction.ZNotifyAction("abc@xyz.com", "subject", notifyMsg2));
rules.add(new ZFilterRule("testMultipleMultilineText 1", true, false, conditions, actions));
conditions = new ArrayList<ZFilterCondition>();
actions = new ArrayList<ZFilterAction>();
conditions.add(new ZFilterCondition.ZTrueCondition());
String replyMsg = "Replying to:\n${BODY}";
actions.add(new ZFilterAction.ZReplyAction(replyMsg));
rules.add(new ZFilterRule("testMultipleMultilineText 2", true, false, conditions, actions));
saveIncomingRules(mMbox, new ZFilterRules(rules));
rules = mMbox.getIncomingFilterRules().getRules();
assertEquals("There should be 2 rules", rules.size(), 2);
assertEquals("Rule 1 should have 2 actions", rules.get(0).getActions().size(), 2);
assertEquals(((ZFilterAction.ZNotifyAction) rules.get(0).getActions().get(0)).getBodyTemplate(), notifyMsg1);
assertEquals(((ZFilterAction.ZNotifyAction) rules.get(0).getActions().get(1)).getBodyTemplate(), notifyMsg2);
assertEquals("Rule 2 should have 1 action", rules.get(1).getActions().size(), 1);
assertEquals(((ZFilterAction.ZReplyAction) rules.get(1).getActions().get(0)).getBodyTemplate(), replyMsg);
}
use of com.zimbra.client.ZFilterRule 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()));
}
use of com.zimbra.client.ZFilterRule 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());
}
use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class TestFilter method testManyAsterisks.
/**
* Make sure we disallow more than four asterisks in a :matches condition (bug 35983).
*/
@Test
public void testManyAsterisks() throws Exception {
List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
ZFilterCondition condition = new ZHeaderCondition("from", HeaderOp.MATCHES, "*****address@yahoo.com");
ZFilterAction action = new ZKeepAction();
conditions.add(condition);
actions.add(action);
rules.add(new ZFilterRule("test many asterisks", true, false, conditions, actions));
ZFilterRules zRules = new ZFilterRules(rules);
try {
mMbox.saveIncomingFilterRules(zRules);
fail("Saving filter rules with quotes should not have succeeded");
} catch (SoapFaultException e) {
assertTrue("Unexpected exception: " + e, e.getMessage().contains("four asterisks"));
}
}
use of com.zimbra.client.ZFilterRule 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()));
}
Aggregations