use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class EnvelopeTest method testHeaderNameWithLeadingSpace.
@Test
public void testHeaderNameWithLeadingSpace() {
String filterScript = "require \"envelope\";\n" + "if envelope :matches \" TO\" \"*@zimbra.com\" {\n" + " tag \"t1\";\n" + "}\n";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals(0, msg.getTags().length);
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class EnvelopeTest method testInvalidHeaderName.
@Test
public void testInvalidHeaderName() {
String filterScript = "require \"envelope\";\n" + "if anyof envelope :matches \"to123\" \"t1@zimbra.com\" {\n" + " fileinto \"Junk\";\n" + "}\n";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<t1@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
account.setMail("xyz@zimbra.com");
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, msg.getFolderId());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class EnvelopeTest method testAllDomainLocalIs.
@Test
public void testAllDomainLocalIs() {
String filterScript = "require [\"envelope\", \"tag\"];\n" + "if envelope :domain :is \"to\" \"zimbra.com\" {\n" + " tag \"is-domain\";\n" + "}\n" + "if envelope :localpart :is \"to\" \"xyz\" {\n" + " tag \"is-local\";\n" + "}\n" + "if envelope :all :is \"to\" \"xyz@zimbra.com\" {" + " tag \"is-all\";\n" + "}";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<t1@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
account.setMail("xyz@zimbra.com");
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
String[] tags = msg.getTags();
Assert.assertTrue(tags != null);
Assert.assertEquals(3, tags.length);
Assert.assertEquals("is-domain", tags[0]);
Assert.assertEquals("is-local", tags[1]);
Assert.assertEquals("is-all", tags[2]);
} catch (Exception e) {
fail("No exception should be thrown: " + e);
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class EnvelopeTest method testFrom.
@Test
public void testFrom() {
// RFC 5228 5.4. Test envelope example
String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"from\" \"tim@example.com\" {\n" + "discard;\n" + "}";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<tim@example.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(0, ids.size());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class FileIntoCopyTest method testCopyFileIntoPattern2Real.
/*
* fileinto :copy foo; if header :contains "Subject" "Test" { fileinto :copy
* bar; }
*
* if message has "Subject: Real" ==> it should be stored in "foo" and INBOX
*/
@Test
public void testCopyFileIntoPattern2Real() {
try {
String filterScriptPattern1 = "require [\"copy\", \"fileinto\"];\n" + "fileinto :copy \"foo\";" + "if header :contains \"Subject\" \"Test\" {\n" + "fileinto :copy \"bar\"; }";
Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScriptPattern1);
String rawReal = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Real\n" + "\n" + "Hello World";
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(rawReal.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
// message should be stored in foo
Integer item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "foo").getId()).getIds(MailItem.Type.MESSAGE).get(0);
Message msg = mbox.getMessageById(null, item);
Assert.assertEquals("Hello World", msg.getFragment());
// message should be stored in inbox
item = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
msg = mbox.getMessageById(null, item);
Assert.assertEquals("Hello World", msg.getFragment());
} catch (Exception e) {
e.printStackTrace();
fail("No exception should be thrown");
}
}
Aggregations