use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class ExistingMessageHandlerTest method existing.
@Test
public void existing() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
OperationContext octx = new OperationContext(mbox);
Message msg = mbox.addMessage(octx, new ParsedMessage("From: sender@zimbra.com\nTo: test@zimbra.com\nSubject: test".getBytes(), false), new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setFlags(Flag.BITMASK_PRIORITY), new DeliveryContext());
Folder f = mbox.createFolder(null, "test", new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
ExistingMessageHandler handler = new ExistingMessageHandler(octx, mbox, msg.getId(), (int) msg.getSize());
ItemId newMsgItemId = handler.fileInto("test", new ArrayList<ActionFlag>(), new String[0]);
Message newMsg = mbox.getMessageById(octx, newMsgItemId.getId());
Assert.assertEquals(msg.getFolderId(), Integer.parseInt(newMsg.getUnderlyingData().getPrevFolders().split(":")[1]));
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class FacebookTestTest method test.
@Test
public void test() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
account.setMailSieveScript("if facebook { tag \"facebook\"; }");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
// notifications
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: \"Facebook\" <notification+mhw1-ivm@facebookmail.com>\n".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals("facebook", ArrayUtil.getFirstElement(msg.getTags()));
// deals
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: \"Facebook Deals\" <deals+mhw1-ivm@facebookmail.com>\n".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertTrue(msg.getTags().length == 0);
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class EnvelopeTest method testNumericNegativeValueCount.
@Test
public void testNumericNegativeValueCount() {
String filterScript = "require [\"envelope\", \"tag\", \"relational\"];\n" + "if envelope :all :count \"lt\" :comparator \"i;ascii-numeric\" \"to\" \"-1\" {\n" + " tag \"To\";\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);
account.setMail("test@zimbra.com");
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(null, ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class ErejectTest method testThatSenderRcdUnDeliveredEmail.
/*
* applyRulesToIncomingMessage() should throw an exception to cancel the message delivery.
* No message is delivered.
*
* The following error will be logged:
* ERROR - Evaluation failed. Reason: 'ereject' action refuses delivery of a message. Sieve rule evaluation is cancelled
*/
@Test
public void testThatSenderRcdUnDeliveredEmail() {
Account acct1 = null;
Mailbox mbox1 = null;
Account acct2 = null;
Mailbox mbox2 = null;
try {
acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
} catch (DeliveryServiceException e) {
if (e.getCause() instanceof ErejectException) {
try {
List<Integer> items = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE);
Assert.assertEquals(null, items);
} catch (Exception ex) {
ex.printStackTrace();
fail("No exception should be thrown: " + ex.getMessage());
}
} else {
fail("No exception other than DeliveryServiceException/ErejectException should be thrown: " + e.getMessage());
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class FileIntoCopyTest method testCopyFileIntoPattern3Test.
/*
* fileinto :copy foo; if header :contains "Subject" "Test" { discard; }
*
* if message has "Subject: Test" ==> it should be stored in "foo"
*/
@Test
public void testCopyFileIntoPattern3Test() {
try {
String filterScriptPattern1 = "require [\"copy\", \"fileinto\"];\n" + "fileinto :copy \"foo\";" + "if header :contains \"Subject\" \"Test\" {\n" + "discard; }";
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: Test\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 not be stored in inbox
Assert.assertNull(mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE));
} catch (Exception e) {
e.printStackTrace();
fail("No exception should be thrown");
}
}
Aggregations