use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class RuleManagerAdminFilterTest method requireText.
// Verification for the ZCS-272
/* @Test
public void deleteHeaderInAdminBefore() throws Exception {
String adminBefore = "require [\"editheader\",\"log\"];\n"
+ "deleteheader :matches \"X-Test-Header\" \"Ran*\";\n";
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
account.setAdminSieveScriptBefore(adminBefore);
String rawTest = "From: sender@zimbra.com\n"
+ "To: test1@zimbra.com\n"
+ "Subject: Test\n"
+ "X-Test-Header: Random\n"
+ "\n"
+ "Hello World";
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox),
mbox, new ParsedMessage(rawTest.getBytes(), false), 0, account.getName(),
new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox.getMessageById(null, itemId);
boolean headerDeleted = true;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements();) {
Header temp = enumeration.nextElement();
if ("X-Test-Header".equals(temp.getName())) {
headerDeleted = false;
break;
}
}
Assert.assertTrue(headerDeleted);
}
// Verification for the ZCS-272
@Test
public void deleteHeaderInUser() throws Exception {
String endUser = "require [\"editheader\",\"log\"];\n"
+ "deleteheader :matches \"X-Test-Header\" \"Ran*\";\n";
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
account.setMailSieveScript(endUser);
String rawTest = "From: sender@zimbra.com\n"
+ "To: test1@zimbra.com\n"
+ "Subject: Test\n"
+ "X-Test-Header: Random\n"
+ "\n"
+ "Hello World";
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox),
mbox, new ParsedMessage(rawTest.getBytes(), false), 0, account.getName(),
new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox.getMessageById(null, itemId);
boolean headerDeleted = true;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements();) {
Header temp = enumeration.nextElement();
if ("X-Test-Header".equals(temp.getName())) {
headerDeleted = false;
break;
}
}
Assert.assertTrue(headerDeleted);
}
// Verification for the ZCS-272
@Test
public void deleteHeaderInAdminAfter() throws Exception {
String adminAfter = "require [\"editheader\",\"log\"];\n"
+ "deleteheader :matches \"X-Test-Header\" \"Ran*\";\n";
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
account.setAdminSieveScriptAfter(adminAfter);
String rawTest = "From: sender@zimbra.com\n"
+ "To: test1@zimbra.com\n"
+ "Subject: Test\n"
+ "X-Test-Header: Random\n"
+ "\n"
+ "Hello World";
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox),
mbox, new ParsedMessage(rawTest.getBytes(), false), 0, account.getName(),
new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox.getMessageById(null, itemId);
boolean headerDeleted = true;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements();) {
Header temp = enumeration.nextElement();
if ("X-Test-Header".equals(temp.getName())) {
headerDeleted = false;
break;
}
}
Assert.assertTrue(headerDeleted);
}
*/
/* Verification for the ZCS-611
*/
@Test
public void requireText() throws Exception {
String adminAfter = "require [\"log\", \"fileinto\"];\n" + "require \"tag\";\n" + "if header :contains [\"Subject\"] \"require abc def\" {\n" + " tag \"--require--\";" + " tag \"123require789\";\n" + "}";
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
account.setAdminSieveScriptAfter(adminAfter);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("Subject: require abc def\n".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox.getMessageById(null, itemId);
String[] tags = message.getTags();
Assert.assertTrue(tags != null);
Assert.assertEquals(2, tags.length);
Assert.assertEquals("--require--", tags[0]);
Assert.assertEquals("123require789", tags[1]);
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class RuleManagerTest method tagAndFileinto.
@Test
public void tagAndFileinto() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.setMailSieveScript("if socialcast { tag \"priority\"; fileinto \"socialcast\"; }");
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: test".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("socialcast", mbox.getFolderById(null, msg.getFolderId()).getName());
Assert.assertEquals("priority", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
account.setMailSieveScript("if socialcast { tag \"priority\"; }\n" + "if header :contains [\"Subject\"] [\"Zimbra\"] { fileinto \"zimbra\"; }");
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: Zimbra".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.assertEquals("zimbra", mbox.getFolderById(null, msg.getFolderId()).getName());
Assert.assertEquals("priority", ArrayUtil.getFirstElement(msg.getTags()));
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class RuleManagerTest method tagAndTag.
@Test
public void tagAndTag() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.setMailSieveScript("if socialcast { tag \"priority\"; tag \"socialcast\"; }");
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: test".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.assertArrayEquals(new String[] { "priority", "socialcast" }, msg.getTags());
RuleManager.clearCachedRules(account);
account.setMailSieveScript("if socialcast { tag \"priority\"; }\n" + "if header :contains [\"Subject\"] [\"Zimbra\"] { tag \"zimbra\"; }");
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: Zimbra".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.assertArrayEquals(new String[] { "priority", "zimbra" }, msg.getTags());
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class SetVariableTest method testSetVarNameWithDigits.
@Test
public void testSetVarNameWithDigits() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "require [\"variables\"];\n" + "set \"var2\" \"hello\"\n;" + "set \"var_2\" \"hellovar_2\"\n;" + "set \"_var2\" \"hello_var2\"\n;" + "set \"_var2_ad\" \"hello_var2_ad\"\n;" + "set \"_var2_\" \"hello_var2_\"\n;" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var2}\";\n" + " tag \"${var_2}\";\n" + " tag \"${_var2}\";\n" + " tag \"${_var2_ad}\";\n" + " tag \"${_var2_}\";\n" + "}\n";
account.setMailSieveScript(filterScript);
String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World.";
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.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("hello", msg.getTags()[0]);
Assert.assertEquals("hellovar_2", msg.getTags()[1]);
Assert.assertEquals("hello_var2", msg.getTags()[2]);
Assert.assertEquals("hello_var2_ad", msg.getTags()[3]);
Assert.assertEquals("hello_var2_", msg.getTags()[4]);
} catch (Exception e) {
e.printStackTrace();
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class SetVariableTest method testWildCardGreedyMatch.
@Test
public void testWildCardGreedyMatch() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "if header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"sample*test\" { " + "tag \"${1}\";}";
account.setMailSieveScript(filterScript);
String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: sample abc test 123 test ABC test";
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.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("abc test 123 test ABC", msg.getTags()[0]);
} catch (Exception e) {
e.printStackTrace();
fail("No exception should be thrown");
}
}
Aggregations