use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class ModifyFilterRulesTest method testBug71036_NestedIfMultiRulesWithMultiConditions.
@Test
public void testBug71036_NestedIfMultiRulesWithMultiConditions() throws Exception {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Element request = new Element.XMLElement(MailConstants.MODIFY_FILTER_RULES_REQUEST);
Element rules = request.addElement(MailConstants.E_FILTER_RULES);
Element rule = rules.addElement(MailConstants.E_FILTER_RULE);
rule.addAttribute(MailConstants.A_ACTIVE, true);
rule.addAttribute(MailConstants.A_NAME, "Test1");
Element filterTests = rule.addElement(MailConstants.E_FILTER_TESTS);
filterTests.addAttribute(MailConstants.A_CONDITION, "anyof");
Element headerTest = filterTests.addElement(MailConstants.E_HEADER_TEST);
headerTest.addAttribute(MailConstants.A_HEADER, "Subject");
headerTest.addAttribute(MailConstants.A_STRING_COMPARISON, "contains");
headerTest.addAttribute(MailConstants.A_VALUE, "important");
Element nestedRule = rule.addElement(MailConstants.E_NESTED_RULE);
Element childFilterTests = nestedRule.addElement(MailConstants.E_FILTER_TESTS);
childFilterTests.addAttribute(MailConstants.A_CONDITION, "allof");
Element childheaderTest = childFilterTests.addElement(MailConstants.E_HEADER_TEST);
childheaderTest.addAttribute(MailConstants.A_HEADER, "Subject");
childheaderTest.addAttribute(MailConstants.A_STRING_COMPARISON, "is");
childheaderTest.addAttribute(MailConstants.A_VALUE, "confifential");
Element childheaderTest2 = childFilterTests.addElement(MailConstants.E_HEADER_TEST);
childheaderTest2.addAttribute(MailConstants.A_HEADER, "Subject");
childheaderTest2.addAttribute(MailConstants.A_STRING_COMPARISON, "contains");
childheaderTest2.addAttribute(MailConstants.A_VALUE, "secret");
Element filteraction = nestedRule.addElement(MailConstants.E_FILTER_ACTIONS);
Element actionInto = filteraction.addElement(MailConstants.E_ACTION_FILE_INTO);
actionInto.addAttribute(MailConstants.A_FOLDER_PATH, "Junk");
filteraction.addElement(MailConstants.E_ACTION_STOP);
Element rule2 = rules.addElement(MailConstants.E_FILTER_RULE);
rule2.addAttribute(MailConstants.A_ACTIVE, true);
rule2.addAttribute(MailConstants.A_NAME, "Test2");
//Element filteraction2 = rule2.addElement(MailConstants.E_FILTER_ACTIONS);
//Element actionInto2 = filteraction2.addElement(MailConstants.E_ACTION_FILE_INTO);
//actionInto2.addAttribute(MailConstants.A_FOLDER_PATH, "Trush");
//filteraction2.addElement(MailConstants.E_ACTION_STOP);
Element filterTests2 = rule2.addElement(MailConstants.E_FILTER_TESTS);
filterTests2.addAttribute(MailConstants.A_CONDITION, "allof");
Element headerTest21 = filterTests2.addElement(MailConstants.E_HEADER_TEST);
headerTest21.addAttribute(MailConstants.A_HEADER, "subject");
headerTest21.addAttribute(MailConstants.A_STRING_COMPARISON, "contains");
headerTest21.addAttribute(MailConstants.A_VALUE, "important");
Element headerTest22 = filterTests2.addElement(MailConstants.E_HEADER_TEST);
headerTest22.addAttribute(MailConstants.A_HEADER, "from");
headerTest22.addAttribute(MailConstants.A_STRING_COMPARISON, "contains");
headerTest22.addAttribute(MailConstants.A_VALUE, "solutions");
Element nestedRule2 = rule2.addElement(MailConstants.E_NESTED_RULE);
Element childFilterTests2 = nestedRule2.addElement(MailConstants.E_FILTER_TESTS);
childFilterTests2.addAttribute(MailConstants.A_CONDITION, "anyof");
Element childheaderTest21 = childFilterTests2.addElement(MailConstants.E_HEADER_TEST);
childheaderTest21.addAttribute(MailConstants.A_HEADER, "subject");
childheaderTest21.addAttribute(MailConstants.A_STRING_COMPARISON, "is");
childheaderTest21.addAttribute(MailConstants.A_VALUE, "confifential");
Element childheaderTest22 = childFilterTests2.addElement(MailConstants.E_HEADER_TEST);
childheaderTest22.addAttribute(MailConstants.A_HEADER, "Cc");
childheaderTest22.addAttribute(MailConstants.A_STRING_COMPARISON, "contains");
childheaderTest22.addAttribute(MailConstants.A_VALUE, "test");
Element nfilteraction2 = nestedRule2.addElement(MailConstants.E_FILTER_ACTIONS);
Element nactionInto2 = nfilteraction2.addElement(MailConstants.E_ACTION_FILE_INTO);
nactionInto2.addAttribute(MailConstants.A_FOLDER_PATH, "Trash");
nfilteraction2.addElement(MailConstants.E_ACTION_STOP);
try {
new ModifyFilterRules().handle(request, ServiceTestUtil.getRequestContext(acct));
} catch (ServiceException e) {
fail("This test is expected not to throw exception. ");
}
String expectedScript = "require [\"fileinto\", \"reject\", \"tag\", \"flag\", \"variables\", \"log\", \"enotify\"];\n\n";
expectedScript += "# Test1\n";
expectedScript += "if anyof (header :contains [\"Subject\"] \"important\") {\n";
expectedScript += " if allof (header :is [\"Subject\"] \"confifential\",\n";
expectedScript += " header :contains [\"Subject\"] \"secret\") {\n";
expectedScript += " fileinto \"Junk\";\n";
expectedScript += " stop;\n";
expectedScript += " }\n";
expectedScript += "}\n";
expectedScript += "\n";
expectedScript += "# Test2\n";
expectedScript += "if allof (header :contains [\"subject\"] \"important\",\n";
expectedScript += " header :contains [\"from\"] \"solutions\") {\n";
expectedScript += " if anyof (header :is [\"subject\"] \"confifential\",\n";
expectedScript += " header :contains [\"Cc\"] \"test\") {\n";
expectedScript += " fileinto \"Trash\";\n";
expectedScript += " stop;\n";
expectedScript += " }\n";
expectedScript += "}\n";
//ZimbraLog.filter.info(acct.getMailSieveScript());
//ZimbraLog.filter.info(expectedScript);
assertEquals(expectedScript, acct.getMailSieveScript());
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class TagActionTest method permissions.
@Test
public void permissions() throws Exception {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
Tag tag1 = mbox.createTag(null, name1, (byte) 0);
Element request = new Element.XMLElement(MailConstants.TAG_ACTION_REQUEST);
Element action = request.addElement(MailConstants.E_ACTION);
action.addAttribute(MailConstants.A_OPERATION, ItemAction.OP_COLOR).addAttribute(MailConstants.A_COLOR, 4);
action.addAttribute(MailConstants.A_TAG_NAMES, TagUtil.encodeTags(name1));
try {
new TagAction().handle(request, ServiceTestUtil.getRequestContext(acct2, acct));
Assert.fail("colored another user's tags without permissions");
} catch (ServiceException e) {
Assert.assertEquals("expected error code: " + ServiceException.PERM_DENIED, ServiceException.PERM_DENIED, e.getCode());
}
action.addAttribute(MailConstants.A_TAG_NAMES, (String) null).addAttribute(MailConstants.A_ID, tag1.getId());
try {
new TagAction().handle(request, ServiceTestUtil.getRequestContext(acct2, acct));
Assert.fail("colored another user's tags without permissions");
} catch (ServiceException e) {
Assert.assertEquals("expected error code: " + ServiceException.PERM_DENIED, ServiceException.PERM_DENIED, e.getCode());
}
action.addAttribute(MailConstants.A_TAG_NAMES, TagUtil.encodeTags(name2));
try {
new TagAction().handle(request, ServiceTestUtil.getRequestContext(acct2, acct));
Assert.fail("colored another user's tags without permissions");
} catch (ServiceException e) {
Assert.assertEquals("expected error code: " + ServiceException.PERM_DENIED, ServiceException.PERM_DENIED, e.getCode());
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class TagActionTest method delete.
@Test
public void delete() throws Exception {
Account acct = Provisioning.getInstance().getAccountByName("test@zimbra.com");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
// create the tag
Element request = new Element.XMLElement(MailConstants.CREATE_TAG_REQUEST);
request.addUniqueElement(MailConstants.E_TAG).addAttribute(MailConstants.A_COLOR, 4).addAttribute(MailConstants.A_NAME, "test");
Element response = new CreateTag().handle(request, ServiceTestUtil.getRequestContext(acct));
int tagId = response.getElement(MailConstants.E_TAG).getAttributeInt(MailConstants.A_ID);
try {
mbox.getTagById(null, tagId);
} catch (ServiceException e) {
Assert.fail("tag not created: " + e);
}
// delete the tag
request = new Element.XMLElement(MailConstants.TAG_ACTION_REQUEST);
request.addUniqueElement(MailConstants.E_ACTION).addAttribute(MailConstants.A_OPERATION, ItemAction.OP_HARD_DELETE).addAttribute(MailConstants.A_ID, tagId);
new TagAction().handle(request, ServiceTestUtil.getRequestContext(acct));
try {
mbox.getTagById(null, tagId);
Assert.fail("tag not deleted");
} catch (NoSuchItemException e) {
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class HttpStoreManagerTest method fail.
@Test
public void fail() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
int count = countMailItems(mbox);
MockHttpStore.setFail();
try {
mbox.addMessage(null, MailboxTestUtil.generateMessage("test"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
Assert.fail("expected exception not thrown");
} catch (ServiceException expected) {
}
Assert.assertEquals(count, countMailItems(mbox));
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class CsrfUtilTest method testIsValidCsrfTokenForAccountWithMultipleTokens.
@Test
public final void testIsValidCsrfTokenForAccountWithMultipleTokens() {
try {
Account acct = Provisioning.getInstance().getAccountByName("test@zimbra.com");
AuthToken authToken = new ZimbraAuthToken(acct);
String csrfToken1 = CsrfUtil.generateCsrfToken(acct.getId(), AUTH_TOKEN_EXPR, CSRFTOKEN_SALT, authToken);
boolean validToken = CsrfUtil.isValidCsrfToken(csrfToken1, authToken);
assertTrue(validToken);
} catch (ServiceException e) {
fail("Should not throw exception.");
}
}
Aggregations