Author: btellier
Date: Thu Dec 17 15:02:01 2015
New Revision: 1720587
URL: http://svn.apache.org/viewvc?rev=1720587&view=rev
Log:
JAMES-1618 LISTSSCRIPT should be MPT tested
Added:
james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ListScriptsTest.java
james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/listscripts.test
Modified:
james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailet.java
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/transcode/MessageToCoreToMessage.java
james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
Added:
james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ListScriptsTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ListScriptsTest.java?rev=1720587&view=auto
==============================================================================
---
james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ListScriptsTest.java
(added)
+++
james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ListScriptsTest.java
Thu Dec 17 15:02:01 2015
@@ -0,0 +1,47 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mpt.testsuite;
+
+import com.google.inject.Inject;
+import org.apache.james.mpt.host.ManageSieveHostSystem;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Locale;
+
+public class ListScriptsTest extends ManageSieveMPTTest {
+
+ @Inject
+ private static ManageSieveHostSystem hostSystem;
+
+ public ListScriptsTest() throws Exception {
+ super(hostSystem);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Test
+ public void listScriptsShouldWork() throws Exception {
+ scriptTest("listscripts", Locale.US);
+ }
+}
Added:
james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/listscripts.test
URL:
http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/listscripts.test?rev=1720587&view=auto
==============================================================================
---
james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/listscripts.test
(added)
+++
james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/listscripts.test
Thu Dec 17 15:02:01 2015
@@ -0,0 +1,83 @@
+################################################################
+# Licensed to the Apache Software Foundation (ASF) under one #
+# or more contributor license agreements. See the NOTICE file #
+# distributed with this work for additional information #
+# regarding copyright ownership. The ASF licenses this file #
+# to you under the Apache License, Version 2.0 (the #
+# "License"); you may not use this file except in compliance #
+# with the License. You may obtain a copy of the License at #
+# #
+# http://www.apache.org/licenses/LICENSE-2.0 #
+# #
+# Unless required by applicable law or agreed to in writing, #
+# software distributed under the License is distributed on an #
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY #
+# KIND, either express or implied. See the License for the #
+# specific language governing permissions and limitations #
+# under the License. #
+################################################################
+
+C: LISTSCRIPTS
+S: NO
+
+C: AUTHENTICATE "PLAIN"
+S: \+ ""
+C: user password
+S: OK authentication successfull
+
+C: LISTSCRIPTS
+S: OK
+
+C: PUTSCRIPT "mysievescript" {110+}
+C: require ["fileinto"];
+C:
+C: if envelope :contains "to" "tmartin+sent" {
+C: fileinto "INBOX.sent";
+C: }
+S: OK
+
+C: PUTSCRIPT "toto1" {110+}
+C: require ["fileinto"];
+C:
+C: if envelope :contains "to" "tmartin+sent" {
+C: fileinto "INBOX.sent";
+C: }
+S: OK
+
+C: PUTSCRIPT "toto2" {110+}
+C: require ["fileinto"];
+C:
+C: if envelope :contains "to" "tmartin+sent" {
+C: fileinto "INBOX.sent";
+C: }
+S: OK
+
+C: LISTSCRIPTS
+SUB {
+S: "toto2"
+S: "toto1"
+S: "mysievescript"
+}
+S: OK
+
+C: SETACTIVE "toto1"
+S: OK
+
+C: LISTSCRIPTS
+SUB {
+S: "toto2"
+S: "toto1" ACTIVE
+S: "mysievescript"
+}
+S: OK
+
+C: SETACTIVE "toto2"
+S: OK
+
+C: LISTSCRIPTS
+SUB {
+S: "toto2" ACTIVE
+S: "toto1"
+S: "mysievescript"
+}
+S: OK
Modified:
james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java?rev=1720587&r1=1720586&r2=1720587&view=diff
==============================================================================
---
james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java
(original)
+++
james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java
Thu Dec 17 15:02:01 2015
@@ -25,6 +25,7 @@ import org.apache.james.mpt.testsuite.Ch
import org.apache.james.mpt.testsuite.DeleteScriptTest;
import org.apache.james.mpt.testsuite.GetScriptTest;
import org.apache.james.mpt.testsuite.HaveSpaceTest;
+import org.apache.james.mpt.testsuite.ListScriptsTest;
import org.apache.james.mpt.testsuite.LogoutTest;
import org.apache.james.mpt.testsuite.NoopTest;
import org.apache.james.mpt.testsuite.PutScriptTest;
@@ -52,7 +53,8 @@ import org.junit.runners.Suite;
GetScriptTest.class,
DeleteScriptTest.class,
RenameScriptTest.class,
- CheckScriptTest.class
+ CheckScriptTest.class,
+ ListScriptsTest.class
})
public class ManageSieveFileTest {
}
Modified:
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailet.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailet.java?rev=1720587&r1=1720586&r2=1720587&view=diff
==============================================================================
---
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailet.java
(original)
+++
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailet.java
Thu Dec 17 15:02:01 2015
@@ -30,15 +30,19 @@ import javax.inject.Inject;
import javax.mail.MessagingException;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Lists;
+import org.apache.james.managesieve.api.Session;
import org.apache.james.managesieve.api.SieveParser;
import org.apache.james.managesieve.core.CoreProcessor;
import org.apache.james.managesieve.transcode.LineToCore;
import org.apache.james.managesieve.transcode.LineToCoreToLine;
+import org.apache.james.managesieve.transcode.ManageSieveProcessor;
import org.apache.james.managesieve.util.SettableSession;
import org.apache.james.sieverepository.api.SieveRepository;
import
org.apache.james.transport.mailets.managesieve.transcode.MessageToCoreToMessage;
import org.apache.james.user.api.UsersRepository;
import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
import org.apache.mailet.MailetContext;
import org.apache.mailet.base.GenericMailet;
@@ -109,10 +113,10 @@ public class ManageSieveMailet extends G
setHelpURL(getInitParameter("helpURL"));
cache = getInitParameter("cache", true);
- transcoder = new MessageToCoreToMessage(
+ transcoder = new MessageToCoreToMessage(new ManageSieveProcessor(
new LineToCoreToLine(
new LineToCore(
- new CoreProcessor(sieveRepository, usersRepository,
sieveParser))),
+ new CoreProcessor(sieveRepository, usersRepository,
sieveParser)))),
this);
}
@@ -130,9 +134,13 @@ public class ManageSieveMailet extends G
// Update the Session for the current mail and execute
SettableSession session = new SettableSession();
-
session.setAuthentication(mail.getAttribute(SMTP_AUTH_USER_ATTRIBUTE_NAME) !=
null);
+ if (mail.getAttribute(SMTP_AUTH_USER_ATTRIBUTE_NAME) != null) {
+ session.setState(Session.State.AUTHENTICATED);
+ } else {
+ session.setState(Session.State.UNAUTHENTICATED);
+ }
session.setUser(mail.getSender().getLocalPart() + '@' +
(mail.getSender().getDomain() == null ? "localhost" :
mail.getSender().getDomain()));
- getMailetContext().sendMail(transcoder.execute(session,
mail.getMessage()));
+ getMailetContext().sendMail(mail.getRecipients().iterator().next(),
Lists.newArrayList(mail.getSender()),transcoder.execute(session,
mail.getMessage()));
mail.setState(Mail.GHOST);
// And tidy up
Modified:
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/transcode/MessageToCoreToMessage.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/transcode/MessageToCoreToMessage.java?rev=1720587&r1=1720586&r2=1720587&view=diff
==============================================================================
---
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/transcode/MessageToCoreToMessage.java
(original)
+++
james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/managesieve/transcode/MessageToCoreToMessage.java
Thu Dec 17 15:02:01 2015
@@ -20,23 +20,19 @@
package org.apache.james.transport.mailets.managesieve.transcode;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Scanner;
+import org.apache.james.managesieve.api.ManageSieveException;
+import org.apache.james.managesieve.api.Session;
+import org.apache.james.managesieve.transcode.ManageSieveProcessor;
+import org.apache.james.sieverepository.api.exception.SieveRepositoryException;
-import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
-
-import org.apache.james.managesieve.api.Session;
-import org.apache.james.managesieve.transcode.LineToCoreToLine;
-import org.apache.james.managesieve.util.ParserUtils;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Scanner;
public class MessageToCoreToMessage {
@@ -44,10 +40,6 @@ public class MessageToCoreToMessage {
String getHelp() throws MessagingException;
}
- private interface Executable {
- MimeMultipart execute(Session session, String operands, MimeMessage
message) throws MessagingException;
- }
-
protected static String getScript(MimeMessage message) throws IOException,
MessagingException {
String result = null;
if (message.getContentType().startsWith("multipart/")) {
@@ -91,19 +83,6 @@ public class MessageToCoreToMessage {
return result;
}
- protected static MimeBodyPart toPart(String name, String content) throws
MessagingException, IOException {
- MimeBodyPart scriptPart = new MimeBodyPart();
- scriptPart.setDataHandler(
- new DataHandler(
- new ByteArrayDataSource(
- content,
- "application/sieve; charset=UTF-8")
- ));
- scriptPart.setDisposition(MimeBodyPart.ATTACHMENT);
- scriptPart.setFileName(name);
- return scriptPart;
- }
-
protected static MimeBodyPart toPart(String message) throws
MessagingException {
MimeBodyPart part = new MimeBodyPart();
part.setText(message);
@@ -111,32 +90,17 @@ public class MessageToCoreToMessage {
return part;
}
- private final Map<String, Executable> commands;
- private final LineToCoreToLine adapter;
+ private final ManageSieveProcessor manageSieveProcessor;
private final HelpProvider helpProvider;
- public MessageToCoreToMessage(LineToCoreToLine adapter, HelpProvider
helpProvider) {
- this.commands = computeCommands();
- this.adapter = adapter;
+ public MessageToCoreToMessage(ManageSieveProcessor manageSieveProcessor,
HelpProvider helpProvider) {
+ this.manageSieveProcessor = manageSieveProcessor;
this.helpProvider = helpProvider;
}
public MimeMessage execute(Session session, MimeMessage message) throws
MessagingException {
- // Extract the command and operands from the subject
- String subject = null == message.getSubject() ? "" :
message.getSubject();
- String[] args = subject.split(" ", 2);
- // If there are no arguments, reply with help
- String command = 0 == args.length ? "HELP" : args[0].toUpperCase();
- Executable executable;
- // If the command isn't supported, reply with help
- if (null == (executable = commands.get(command))) {
- executable = commands.get("HELP");
- }
- // Execute the resultant command...
- MimeMultipart content = executable.execute(session, args.length > 1 ?
args[1] : "", message);
- // ...and wrap it in a MimeMessage
MimeMessage reply = (MimeMessage) message.reply(false);
- reply.setContent(content);
+ reply.setContent(computeMultiPartResponse(session, message));
if (null == message.getAllRecipients() || 0 >=
message.getAllRecipients().length) {
throw new MessagingException("Message has no recipients");
} else {
@@ -146,220 +110,43 @@ public class MessageToCoreToMessage {
reply.saveChanges();
return reply;
}
-
- protected Map<String, Executable> computeCommands() {
- Map<String, Executable> commands = new HashMap<String, Executable>();
- commands.put("HELP", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return help();
- }
- });
- commands.put("CAPABILITY", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return capability(session, operands);
- }
- });
- commands.put("CHECKSCRIPT", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return checkScript(session, operands, message);
- }
- });
- commands.put("DELETESCRIPT", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message)
- throws MessagingException {
- return deleteScript(session, operands);
- }
- });
- commands.put("GETSCRIPT", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return getScript(session, operands);
- }
- });
- commands.put("HAVESPACE", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return haveSpace(session, operands);
- }
- });
- commands.put("LISTSCRIPTS", new Executable() {
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return listScripts(session, operands);
- }
- });
- commands.put("PUTSCRIPT", new Executable() {
-
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return putScript(session, operands, message);
- }
- });
- commands.put("RENAMESCRIPT", new Executable() {
-
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return renameScript(session, operands);
- }
- });
- commands.put("SETACTIVE", new Executable() {
-
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return setActive(session, operands);
- }
- });
- commands.put("GETACTIVE", new Executable() {
-
- public MimeMultipart execute(Session session, String operands,
MimeMessage message) throws MessagingException {
- return getActive(session, operands);
- }
- });
-
- return commands;
- }
-
- protected MimeMultipart help() throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(helpProvider.getHelp()));
- return multipart;
- }
-
- protected MimeMultipart capability(Session session, String operands)
throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(adapter.capability(session, operands)));
- return multipart;
- }
-
- protected MimeMultipart checkScript(Session session, String operands,
MimeMessage message) throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- String result;
- Scanner scanner = new Scanner(operands).useDelimiter("\\A");
- if (scanner.hasNext()) {
- result = "NO \"Too many arguments: " + scanner.next() + "\"";
- } else {
- try {
- String content = getScript(message);
- result = adapter.checkScript(session, content);
- } catch (MessagingException ex) {
- result = "NO \"" + ex.getMessage() + "\"";
- } catch (IOException ex) {
- result = "NO \"Failed to read script part\"";
- }
- }
- multipart.addBodyPart(toPart(result));
- return multipart;
- }
-
- protected MimeMultipart deleteScript(Session session, String operands)
throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(adapter.deleteScript(session, operands)));
- return multipart;
- }
- protected MimeMultipart getScript(Session session, String operands) throws
MessagingException {
- String result = adapter.getScript(session, operands);
- // Everything but the last line is the script
- // The last line is the response
- String response;
- String script = null;
- int endOfScript = result.lastIndexOf("\r\n");
- if (endOfScript > 0) {
- script = result.substring(0, endOfScript);
- response = result.substring(endOfScript + "\r\n".length());
- } else {
- response = result;
+ private MimeMultipart computeMultiPartResponse(Session session,
MimeMessage message) throws MessagingException {
+ // Extract the command and operands from the subject
+ String subject = null == message.getSubject() ? "" :
message.getSubject();
+ if (subject.startsWith("HELP")) {
+ return help();
}
-
+ String result = computeStringResult(session, message, subject);
MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(response));
- if (null != script) {
- try {
- multipart.addBodyPart(toPart(ParserUtils.unquote(ParserUtils
- .getScriptName(operands)), script));
- } catch (IOException ex) {
- throw new MessagingException("Failed to add script part", ex);
- }
- }
- return multipart;
- }
-
- protected MimeMultipart haveSpace(Session session, String operands) throws
MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(adapter.haveSpace(session, operands)));
- return multipart;
- }
-
- protected MimeMultipart listScripts(Session session, String operands)
throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(adapter.listScripts(session, operands)));
- return multipart;
- }
-
- protected MimeMultipart putScript(Session session, String operands,
MimeMessage message) throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- String result;
- String scriptName = ParserUtils.getScriptName(operands);
- if (null == scriptName || scriptName.isEmpty()) {
- result = "NO \"Missing argument: script name\"";
- } else {
- Scanner scanner = new
Scanner(operands.substring(scriptName.length()).trim())
- .useDelimiter("\\A");
- if (scanner.hasNext()) {
- result = "NO \"Too many arguments: " + scanner.next() + "\"";
- } else {
- StringBuilder builder = new StringBuilder(scriptName);
- String content = null;
- try {
- content = getScript(message);
- } catch (MessagingException ex) {
- result = "NO \"" + ex.getMessage() + "\"";
- } catch (IOException ex) {
- result = "NO \"Failed to read script part\"";
- }
- if (null != content) {
- builder
- .append(' ')
- .append(content);
- }
- result = adapter.putScript(session, builder.toString().trim());
- }
- }
multipart.addBodyPart(toPart(result));
return multipart;
}
- protected MimeMultipart renameScript(Session session, String operands)
throws MessagingException {
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(adapter.renameScript(session, operands)));
- return multipart;
- }
-
- protected MimeMultipart setActive(Session session, String operands) throws
MessagingException {
+ protected MimeMultipart help() throws MessagingException {
MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(adapter.setActive(session, operands)));
+ multipart.addBodyPart(toPart(helpProvider.getHelp()));
return multipart;
}
- protected MimeMultipart getActive(Session session, String operands) throws
MessagingException {
- String result = adapter.getActive(session, operands);
- adapter.getActive(session, operands);
- // Everything but the last line is the script
- // The last line is the response
- String response;
- String script = null;
- int endOfScript = result.lastIndexOf("\r\n");
- if (endOfScript > 0) {
- script = result.substring(0, endOfScript);
- response = result.substring(endOfScript + "\r\n".length());
- } else {
- response = result;
+ private String computeStringResult(Session session, MimeMessage message,
String subject) {
+ try {
+ return manageSieveProcessor.handleRequest(session,subject + "\r\n"
+ retrieveAttachedScript(message));
+ } catch (ManageSieveException e) {
+ return "NO Manage sieve exception : " + e.getMessage();
+ } catch (SieveRepositoryException e) {
+ return "NO SieveRepository exception : " + e.getMessage();
}
+ }
- MimeMultipart multipart = new MimeMultipart();
- multipart.addBodyPart(toPart(response));
- if (null != script) {
- try {
- multipart.addBodyPart(toPart("active", script));
- } catch (IOException ex) {
- throw new MessagingException("Failed to add script part", ex);
- }
+ private String retrieveAttachedScript(MimeMessage message) {
+ try {
+ return getScript(message);
+ } catch (IOException e) {
+ return "";
+ } catch (MessagingException e) {
+ return "";
}
- return multipart;
}
}
Modified:
james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java?rev=1720587&r1=1720586&r2=1720587&view=diff
==============================================================================
---
james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
(original)
+++
james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
Thu Dec 17 15:02:01 2015
@@ -20,27 +20,12 @@
package org.apache.james.transport.mailets.managesieve;
-
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.collect.Lists;
-import javax.activation.DataHandler;
-import javax.mail.Address;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMessage.RecipientType;
-import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.util.List;
-import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.james.managesieve.api.SieveParser;
import org.apache.james.managesieve.api.SyntaxException;
@@ -49,13 +34,25 @@ import org.apache.james.sieverepository.
import org.apache.james.sieverepository.api.exception.ScriptNotFoundException;
import org.apache.james.user.api.UsersRepository;
import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMailetConfig;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMessage.RecipientType;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.util.ByteArrayDataSource;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Properties;
public class ManageSieveMailetTestCase {
@@ -70,462 +67,396 @@ public class ManageSieveMailetTestCase {
private ManageSieveMailet mailet;
private SieveRepository sieveRepository;
private SieveParser sieveParser;
+ private UsersRepository usersRepository;
+ private FakeMailContext fakeMailContext;
@Before
public void setUp() throws Exception {
- mailet = new ManageSieveMailet();
sieveRepository = mock(SieveRepository.class);
sieveParser = mock(SieveParser.class);
- UsersRepository usersRepository = mock(UsersRepository.class);
- mailet.setSieveParser(sieveParser);
- mailet.setSieveRepository(sieveRepository);
- mailet.setUsersRepository(usersRepository);
- FakeMailetConfig config = new FakeMailetConfig("ManageSieve mailet",
new FakeMailContext());
- config.setProperty("helpURL", "file:./src/test/resources/help.txt");
- mailet.init(config);
- when(usersRepository.contains(USER)).thenAnswer(new Answer<Boolean>() {
- public Boolean answer(InvocationOnMock invocationOnMock) throws
Throwable {
- return true;
- }
- });
+ usersRepository = mock(UsersRepository.class);
+ initializeMailet();
+ when(usersRepository.contains(USER)).thenReturn(true);
}
@Test
- public final void testCapabilityUnauthorised() throws MessagingException,
IOException {
- MimeMessage message = prepareMimeMessage("CAPABILITY", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- when(sieveParser.getExtensions()).thenAnswer(new
Answer<List<String>>() {
- public List<String> answer(InvocationOnMock invocationOnMock)
throws Throwable {
- return Lists.newArrayList("a", "b", "c");
- }
- });
- mailet.service(mail);
- ensureResponseContains("Re: CAPABILITY", message.getSender(), "SIEVE a
b c",
- "GETACTIVE ",
- "IMPLEMENTATION Apache ManageSieve v1.0",
- "VERSION 1.0",
+ public final void testCapabilityUnauthorised() throws Exception {
+ MimeMessage message = prepareMimeMessage("CAPABILITY");
+ Mail mail = createUnauthenticatedMail(message);
+ when(sieveParser.getExtensions()).thenReturn(Lists.newArrayList("a",
"b", "c"));
+ initializeMailet();
+ mailet.service(mail);
+ ensureResponseContains("Re: CAPABILITY", "\"SIEVE\" \"a b c\"",
+ "\"IMPLEMENTATION\" \"Apache ManageSieve v1.0\"",
+ "\"VERSION\" \"1.0\"",
+ "\"STARTTLS\"",
+ "\"SASL\" \"PLAIN\"",
"OK");
}
@Test
- public final void testCapability() throws MessagingException, IOException {
- MimeMessage message = prepareMimeMessage("CAPABILITY", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- when(sieveParser.getExtensions()).thenAnswer(new
Answer<List<String>>() {
- public List<String> answer(InvocationOnMock invocationOnMock)
throws Throwable {
- return Lists.newArrayList("a", "b", "c");
- }
- });
+ public final void testCapability() throws Exception {
+ MimeMessage message = prepareMimeMessage("CAPABILITY");
+ Mail mail = createUnauthenticatedMail(message);
+ when(sieveParser.getExtensions()).thenReturn(Lists.newArrayList("a",
"b", "c"));
+ initializeMailet();
mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
"test");
mailet.service(mail);
- ensureResponseContains("Re: CAPABILITY", message.getSender(), "SIEVE a
b c",
- "GETACTIVE ",
- "IMPLEMENTATION Apache ManageSieve v1.0",
- "OWNER test@localhost",
- "VERSION 1.0",
+ ensureResponseContains("Re: CAPABILITY", "\"SIEVE\" \"a b c\"",
+ "\"IMPLEMENTATION\" \"Apache ManageSieve v1.0\"",
+ "\"OWNER\" \"test@localhost\"",
+ "\"VERSION\" \"1.0\"",
+ "\"STARTTLS\"",
+ "\"SASL\" \"PLAIN\"",
"OK");
}
@Test
- public final void testCapabilityExtraArguments() throws
MessagingException, IOException {
- MimeMessage message = prepareMimeMessage("CAPABILITY", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ public final void testCapabilityExtraArguments() throws Exception {
+ MimeMessage message = prepareMimeMessage("CAPABILITY");
+ Mail mail = createUnauthenticatedMail(message);
message.setSubject("CAPABILITY extra");
message.saveChanges();
mailet.service(mail);
- ensureResponse("Re: CAPABILITY extra", message.getSender(), "NO \"Too
many arguments: extra\"");
+ ensureResponse("Re: CAPABILITY extra", "NO \"Too many arguments:
extra\"");
}
@Test
- public final void testPutScriptUnauthorised() throws Exception {
- MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ public final void testPutScriptinvalidLiteral() throws Exception {
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO");
+ ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\"", "NO \"Missing
argument: script size\"");
}
@Test
public final void testPutScript() throws Exception {
- when(sieveParser.parse(SCRIPT_CONTENT)).thenAnswer(new
Answer<List<String>>() {
- public List<String> answer(InvocationOnMock invocationOnMock)
throws Throwable {
- return Lists.newArrayList("warning1", "warning2");
- }
- });
- MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+
when(sieveParser.parse(SCRIPT_CONTENT)).thenReturn(Lists.newArrayList("warning1",
"warning2"));
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\" {100+}");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "OK (WARNINGS) \"warning1\" \"warning2\"");
+ ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\" {100+}", "OK
(WARNINGS) \"warning1\" \"warning2\"");
+ }
+
+ @Test
+ public final void testPutScriptInvalidLiteral() throws Exception {
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\" extra");
+ Mail mail = createUnauthenticatedMail(message);
+ mailet.service(mail);
+ ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\" extra", "NO
\"extra is an invalid size literal : it should be at least 4 char looking like
{_+}\"");
}
@Test
public final void testPutScriptExtraArgs() throws Exception {
- MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\" extra", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"PUTSCRIPT \"" + SCRIPT_NAME + "\" {10+} extra");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\" extra",
message.getSender(), "NO \"Too many arguments: extra\"");
+ ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\" {10+} extra",
"NO \"Extra arguments not supported\"");
}
@Test
public final void testPutScriptSyntaxError() throws Exception {
doThrow(new SyntaxException("error
message")).when(sieveParser).parse(SYNTAX_EXCEPTION);
- MimeMessage message = prepareMessageWithAttachment(SYNTAX_EXCEPTION,
"PUTSCRIPT \"" + SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMessageWithAttachment(SYNTAX_EXCEPTION,
"PUTSCRIPT \"" + SCRIPT_NAME + "\" {10+}");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO \"Syntax Error: error message\"");
+ ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\" {10+}", "NO
\"Syntax Error: error message\"");
}
@Test
public final void testPutScriptNoScript() throws Exception {
- MimeMessage message = prepareMimeMessage("PUTSCRIPT \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("PUTSCRIPT \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO \"Missing argument: script content\"");
+ ensureResponse("Re: PUTSCRIPT \"" + SCRIPT_NAME + "\"", "NO \"Missing
argument: script size\"");
}
@Test
public final void testPutScriptNoScriptName() throws Exception {
- MimeMessage message = prepareMimeMessage("PUTSCRIPT", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("PUTSCRIPT");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: PUTSCRIPT", message.getSender(), "NO \"Missing
argument: script name\"");
+ ensureResponse("Re: PUTSCRIPT", "NO \"Missing argument: script
name\"");
}
@Test
public final void testGetScriptNonAuthorized() throws Exception {
- MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO");
+ ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\"", "NO");
}
@Test
public final void testGetScript() throws Exception {
- when(sieveRepository.getScript(USER, SCRIPT_NAME)).thenAnswer(new
Answer<String>() {
- public String answer(InvocationOnMock invocationOnMock) throws
Throwable {
- return SCRIPT_CONTENT;
- }
- });
- MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ when(sieveRepository.getScript(USER, SCRIPT_NAME)).thenReturn(new
ByteArrayInputStream(SCRIPT_CONTENT.getBytes()));
+ MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createUnauthenticatedMail(message);
mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
USER);
mailet.service(mail);
- ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "OK", SCRIPT_CONTENT);
+ ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\"", "{13}\r\n" +
SCRIPT_CONTENT + "\r\nOK");
}
@Test
public final void testGetScriptExtraArgs() throws Exception {
- MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\" extra", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\" extra");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\" extra",
message.getSender(), "NO \"Too many arguments: extra\"");
+ ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\" extra", "NO
\"Too many arguments: extra\"");
}
@Test
public final void testGetScriptNoScript() throws Exception {
doThrow(new
ScriptNotFoundException()).when(sieveRepository).getScript(USER, SCRIPT_NAME);
- MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("GETSCRIPT \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createUnauthenticatedMail(message);
mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
USER);
mailet.service(mail);
- ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO (NONEXISTENT) \"There is no script by that name\"");
+ ensureResponse("Re: GETSCRIPT \"" + SCRIPT_NAME + "\"", "NO
(NONEXISTENT) \"There is no script by that name\"");
}
@Test
public final void testGetScriptNoScriptName() throws Exception {
String scriptContent = "line1\r\nline2";
sieveRepository.putScript(USER, SCRIPT_NAME, scriptContent);
- MimeMessage message = prepareMimeMessage("GETSCRIPT", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("GETSCRIPT");
+ Mail mail = createUnauthenticatedMail(message);
mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
USER);
mailet.service(mail);
- ensureResponse("Re: GETSCRIPT", message.getSender(), "NO \"Missing
argument: script name\"");
+ ensureResponse("Re: GETSCRIPT", "NO \"Missing argument: script
name\"");
}
@Test
public final void testCheckScriptUnauthorised() throws Exception {
- MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"CHECKSCRIPT", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"CHECKSCRIPT {10+}");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: CHECKSCRIPT", message.getSender(), "NO");
+ ensureResponse("Re: CHECKSCRIPT {10+}", "NO");
}
@Test
public final void testCheckScript() throws Exception {
- when(sieveParser.parse(SCRIPT_CONTENT)).thenAnswer(new
Answer<List<String>>() {
- public List<String> answer(InvocationOnMock invocationOnMock)
throws Throwable {
- return Lists.newArrayList("warning1", "warning2");
- }
- });
- MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"CHECKSCRIPT", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+
when(sieveParser.parse(SCRIPT_CONTENT)).thenReturn(Lists.newArrayList("warning1",
"warning2"));
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"CHECKSCRIPT {100+}");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: CHECKSCRIPT", message.getSender(), "OK (WARNINGS)
\"warning1\" \"warning2\"");
+ ensureResponse("Re: CHECKSCRIPT {100+}", "OK (WARNINGS) \"warning1\"
\"warning2\"");
}
@Test
public final void testCheckScriptExtraArgs() throws Exception {
- MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"CHECKSCRIPT extra", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMessageWithAttachment(SCRIPT_CONTENT,
"CHECKSCRIPT {10+} extra");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: CHECKSCRIPT extra", message.getSender(), "NO \"Too
many arguments: extra\"");
+ ensureResponse("Re: CHECKSCRIPT {10+} extra", "NO \"Extra arguments
not supported\"");
}
@Test
public final void testCheckScriptSyntaxError() throws Exception {
doThrow(new SyntaxException("error
message")).when(sieveParser).parse(SYNTAX_EXCEPTION);
- MimeMessage message = prepareMessageWithAttachment(SYNTAX_EXCEPTION,
"CHECKSCRIPT", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMessageWithAttachment(SYNTAX_EXCEPTION,
"CHECKSCRIPT {10+}");
+ Mail mail = createAuthentificatedMail(message);
+ mailet.service(mail);
+ ensureResponse("Re: CHECKSCRIPT {10+}", "NO \"Syntax Error: error
message\"");
+ }
+
+ @Test
+ public final void testCheckScriptNoSize() throws Exception {
+ MimeMessage message = prepareMimeMessage("CHECKSCRIPT");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: CHECKSCRIPT", message.getSender(), "NO \"Syntax
Error: error message\"");
+ ensureResponse("Re: CHECKSCRIPT", "NO \" is an invalid size literal :
it should be at least 4 char looking like {_+}\"");
}
@Test
public final void testCheckScriptNoScript() throws Exception {
- MimeMessage message = prepareMimeMessage("CHECKSCRIPT", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMimeMessage("CHECKSCRIPT {10+}");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: CHECKSCRIPT", message.getSender(), "NO \"Script
part not found in this message\"");
+ ensureResponse("Re: CHECKSCRIPT {10+}", "NO \"Missing argument: script
content\"");
}
@Test
public final void testDeleteScriptUnauthenticated() throws Exception {
- MimeMessage message = prepareMimeMessage("DELETESCRIPT \"" +
SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("DELETESCRIPT \"" +
SCRIPT_NAME + "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: DELETESCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO");
+ ensureResponse("Re: DELETESCRIPT \"" + SCRIPT_NAME + "\"", "NO");
}
@Test
public final void testDeleteScript() throws Exception {
- MimeMessage message = prepareMimeMessage("DELETESCRIPT \"" +
SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMimeMessage("DELETESCRIPT \"" +
SCRIPT_NAME + "\"");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: DELETESCRIPT \"" + SCRIPT_NAME + "\"",
message.getSender(), "OK");
+ ensureResponse("Re: DELETESCRIPT \"" + SCRIPT_NAME + "\"", "OK");
}
@Test
public final void testDeleteScriptExtraArgs() throws Exception {
- MimeMessage message = prepareMimeMessage("DELETESCRIPT \"" +
SCRIPT_NAME + "\" extra", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("DELETESCRIPT \"" +
SCRIPT_NAME + "\" extra");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: DELETESCRIPT \"" + SCRIPT_NAME + "\" extra",
message.getSender(), "NO \"Too many arguments: extra\"");
+ ensureResponse("Re: DELETESCRIPT \"" + SCRIPT_NAME + "\" extra", "NO
\"Too many arguments: extra\"");
}
@Test
public final void testDeleteScriptNoScriptName() throws Exception {
- MimeMessage message = prepareMimeMessage("DELETESCRIPT", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("DELETESCRIPT");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: DELETESCRIPT", message.getSender(), "NO \"Missing
argument: script name\"");
+ ensureResponse("Re: DELETESCRIPT", "NO \"Missing argument: script
name\"");
}
@Test
public final void testHaveSpaceUnauthenticated() throws Exception {
- MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" 1", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" 1");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" 1",
message.getSender(), "NO");
+ ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" 1", "NO");
}
@Test
public final void testHaveSpace() throws Exception {
- MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" 1", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" 1");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" 1",
message.getSender(), "OK");
+ ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" 1", "OK");
}
@Test
public final void testHaveSpaceExtraArgs() throws Exception {
- MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" 1 extra", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" 1 extra");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" 1 extra",
message.getSender(), "NO \"Too many arguments: extra\"");
+ ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" 1 extra", "NO
\"Too many arguments: extra\"");
}
@Test
public final void testHaveSpaceNoScriptName() throws Exception {
- MimeMessage message = prepareMimeMessage("HAVESPACE", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("HAVESPACE");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: HAVESPACE", message.getSender(), "NO \"Missing
argument: script name\"");
+ ensureResponse("Re: HAVESPACE", "NO \"Missing argument: script
name\"");
}
@Test
public final void testHaveSpaceNoScriptSize() throws Exception {
- MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO \"Missing argument: script size\"");
+ ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\"", "NO \"Missing
argument: script size\"");
}
@Test
public final void testHaveSpaceInvalidScriptSize() throws Exception {
- MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" X", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("HAVESPACE \"" + SCRIPT_NAME
+ "\" X");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" X",
message.getSender(), "NO \"Invalid argument: script size\"");
+ ensureResponse("Re: HAVESPACE \"" + SCRIPT_NAME + "\" X", "NO
\"Invalid argument: script size\"");
}
@Test
public final void testListScriptsUnauthorised() throws Exception {
- MimeMessage message = prepareMimeMessage("LISTSCRIPTS", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("LISTSCRIPTS");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: LISTSCRIPTS", message.getSender(), "NO");
+ ensureResponse("Re: LISTSCRIPTS", "NO");
}
@Test
public final void testListScripts() throws Exception {
- when(sieveRepository.listScripts(USER)).thenAnswer(new
Answer<List<ScriptSummary>>() {
- @Override
- public List<ScriptSummary> answer(InvocationOnMock
invocationOnMock) throws Throwable {
- return Lists.newArrayList(new ScriptSummary("scriptName2",
true), new ScriptSummary("scriptName1", false));
- }
- });
- MimeMessage message = prepareMimeMessage("LISTSCRIPTS", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+
when(sieveRepository.listScripts(USER)).thenReturn(Lists.newArrayList(new
ScriptSummary("scriptName2", true), new ScriptSummary("scriptName1", false)));
+ MimeMessage message = prepareMimeMessage("LISTSCRIPTS");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: LISTSCRIPTS", message.getSender(),
"\"scriptName2\" ACTIVE\r\n\"scriptName1\"\r\nOK");
+ ensureResponse("Re: LISTSCRIPTS", "\"scriptName2\"
ACTIVE\r\n\"scriptName1\"\r\nOK");
}
@Test
public final void testListScriptsExtraArgs() throws Exception {
- MimeMessage message = prepareMimeMessage("LISTSCRIPTS extra", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMimeMessage("LISTSCRIPTS extra");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: LISTSCRIPTS extra", message.getSender(), "NO \"Too
many arguments: extra\"");
+ ensureResponse("Re: LISTSCRIPTS extra", "NO \"Too many arguments:
extra\"");
}
@Test
public final void testRenameScriptsUnauthorised() throws Exception {
sieveRepository.putScript(USER, OLD_SCRIPT_NAME, NEW_SCRIPT_NAME);
- MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\" \"" + NEW_SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\" \"" + NEW_SCRIPT_NAME + "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\" \"" +
NEW_SCRIPT_NAME + "\"", message.getSender(), "NO");
+ ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\" \"" +
NEW_SCRIPT_NAME + "\"", "NO");
}
@Test
public final void testRenameScripts() throws Exception {
sieveRepository.putScript(USER, OLD_SCRIPT_NAME, NEW_SCRIPT_NAME);
- MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\" \"" + NEW_SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\" \"" + NEW_SCRIPT_NAME + "\"");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\" \"" +
NEW_SCRIPT_NAME + "\"", message.getSender(), "OK");
+ ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\" \"" +
NEW_SCRIPT_NAME + "\"", "OK");
}
@Test
public final void testRenameScriptsExtraArgs() throws Exception {
sieveRepository.putScript(USER, OLD_SCRIPT_NAME, NEW_SCRIPT_NAME);
- MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\" \"" + NEW_SCRIPT_NAME + "\" extra", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\" \"" + NEW_SCRIPT_NAME + "\" extra");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\" \"" +
NEW_SCRIPT_NAME + "\" extra", message.getSender(), "NO \"Too many arguments:
extra\"");
+ ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\" \"" +
NEW_SCRIPT_NAME + "\" extra", "NO \"Too many arguments: extra\"");
}
@Test
public final void testRenameScriptsNoScriptName() throws Exception {
sieveRepository.putScript(USER, OLD_SCRIPT_NAME, NEW_SCRIPT_NAME);
- MimeMessage message = prepareMimeMessage("RENAMESCRIPT", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("RENAMESCRIPT");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: RENAMESCRIPT", message.getSender(), "NO \"Missing
argument: old script name\"");
+ ensureResponse("Re: RENAMESCRIPT", "NO \"Missing argument: old script
name\"");
}
@Test
public final void testRenameScriptsNoNewScriptName() throws Exception {
sieveRepository.putScript(USER, OLD_SCRIPT_NAME, NEW_SCRIPT_NAME);
- MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("RENAMESCRIPT \"" +
OLD_SCRIPT_NAME + "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\"",
message.getSender(), "NO \"Missing argument: new script name\"");
+ ensureResponse("Re: RENAMESCRIPT \"" + OLD_SCRIPT_NAME + "\"", "NO
\"Missing argument: new script name\"");
}
@Test
public final void testSetActiveUnauthorised() throws Exception {
- MimeMessage message = prepareMimeMessage("SETACTIVE \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("SETACTIVE \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: SETACTIVE \"" + SCRIPT_NAME + "\"",
message.getSender(), "NO");
+ ensureResponse("Re: SETACTIVE \"" + SCRIPT_NAME + "\"", "NO");
}
@Test
public final void testSetActive() throws Exception {
- MimeMessage message = prepareMimeMessage("SETACTIVE \"" + SCRIPT_NAME
+ "\"", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
- mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ MimeMessage message = prepareMimeMessage("SETACTIVE \"" + SCRIPT_NAME
+ "\"");
+ Mail mail = createAuthentificatedMail(message);
mailet.service(mail);
- ensureResponse("Re: SETACTIVE \"" + SCRIPT_NAME + "\"",
message.getSender(), "OK");
+ ensureResponse("Re: SETACTIVE \"" + SCRIPT_NAME + "\"", "OK");
}
@Test
public final void testSetActiveExtraArgs() throws Exception {
- MimeMessage message = prepareMimeMessage("SETACTIVE \"" + SCRIPT_NAME
+ "\" extra", USER, SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("SETACTIVE \"" + SCRIPT_NAME
+ "\" extra");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: SETACTIVE \"" + SCRIPT_NAME + "\" extra",
message.getSender(), "NO \"Too many arguments: extra\"");
+ ensureResponse("Re: SETACTIVE \"" + SCRIPT_NAME + "\" extra", "NO
\"Too many arguments: extra\"");
}
@Test
public final void testSetActiveNoScriptName() throws Exception {
- MimeMessage message = prepareMimeMessage("SETACTIVE", USER,
SIEVE_LOCALHOST);
- Mail mail = new FakeMail();
- mail.setMessage(message);
+ MimeMessage message = prepareMimeMessage("SETACTIVE");
+ Mail mail = createUnauthenticatedMail(message);
mailet.service(mail);
- ensureResponse("Re: SETACTIVE", message.getSender(), "NO \"Missing
argument: script name\"");
+ ensureResponse("Re: SETACTIVE", "NO \"Missing argument: script
name\"");
}
@Test
@@ -549,7 +480,7 @@ public class ManageSieveMailetTestCase {
mail.setMessage(message);
mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
USER);
mailet.service(mail);
- ensureResponse("Re: GETACTIVE", message.getSender(), "OK",
SCRIPT_CONTENT);
+ ensureResponse("Re: GETACTIVE", message.getSender(), SCRIPT_CONTENT +
"\r\n" + "OK");
}
@Test
@@ -572,17 +503,42 @@ public class ManageSieveMailetTestCase {
ensureResponse("Re: GETACTIVE", message.getSender(), "NO");
}
- private MimeMessage prepareMimeMessage(String subject, String sender,
String recipient) throws MessagingException {
+ private void initializeMailet() throws MessagingException {
+ mailet = new ManageSieveMailet();
+ mailet.setSieveParser(sieveParser);
+ mailet.setSieveRepository(sieveRepository);
+ mailet.setUsersRepository(usersRepository);
+ fakeMailContext = new FakeMailContext();
+ FakeMailetConfig config = new FakeMailetConfig("ManageSieve mailet",
fakeMailContext);
+ config.setProperty("helpURL", "file:./src/test/resources/help.txt");
+ mailet.init(config);
+ }
+
+ private Mail createUnauthenticatedMail(MimeMessage message) throws
Exception {
+ FakeMail mail = new FakeMail();
+ mail.setMessage(message);
+ mail.setSender(new MailAddress(USER));
+ mail.setRecipients(Lists.newArrayList(new
MailAddress(SIEVE_LOCALHOST)));
+ return mail;
+ }
+
+ private Mail createAuthentificatedMail(MimeMessage message) throws
Exception {
+ Mail mail = createUnauthenticatedMail(message);
+ mail.setAttribute(ManageSieveMailet.SMTP_AUTH_USER_ATTRIBUTE_NAME,
message.getSender().toString());
+ return mail;
+ }
+
+ private MimeMessage prepareMimeMessage(String subject) throws
MessagingException {
MimeMessage message = new MimeMessage(Session.getDefaultInstance(new
Properties()));
message.setSubject(subject);
- message.setSender(new InternetAddress(sender));
- message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
+ message.setSender(new InternetAddress(USER));
+ message.setRecipient(RecipientType.TO, new
InternetAddress(SIEVE_LOCALHOST));
message.saveChanges();
return message;
}
- private MimeMessage prepareMessageWithAttachment(String scriptContent,
String subject, String recipient, String sender) throws MessagingException,
IOException {
- MimeMessage message = prepareMimeMessage(subject, recipient, sender);
+ private MimeMessage prepareMessageWithAttachment(String scriptContent,
String subject) throws MessagingException, IOException {
+ MimeMessage message = prepareMimeMessage(subject);
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart scriptPart = new MimeBodyPart();
scriptPart.setDataHandler(
@@ -604,10 +560,8 @@ public class ManageSieveMailetTestCase {
return message;
}
- private void ensureResponse(String subject, Address recipient, String...
contents) throws MessagingException, IOException {
- MimeMessage result = ((FakeMailContext)
mailet.getMailetContext()).getSentMessage();
- assertThat(result.getSubject()).isEqualTo(subject);
-
assertThat(result.getRecipients(RecipientType.TO)).containsOnly(recipient);
+ private void ensureResponse(String subject, String... contents) throws
MessagingException, IOException {
+ MimeMessage result = verifyHeaders(subject);
MimeMultipart multipart = (MimeMultipart) result.getContent();
assertThat(multipart.getCount()).isEqualTo(contents.length);
for(int i = 0; i < contents.length; i++) {
@@ -619,11 +573,17 @@ public class ManageSieveMailetTestCase {
}
}
- private void ensureResponseContains(String subject, Address recipient,
String... contents) throws MessagingException, IOException {
- MimeMessage result = ((FakeMailContext)
mailet.getMailetContext()).getSentMessage();
- assertThat(result.getSubject()).isEqualTo(subject);
-
assertThat(result.getRecipients(RecipientType.TO)).containsOnly(recipient);
+ private void ensureResponseContains(String subject, String... contents)
throws MessagingException, IOException {
+ MimeMessage result = verifyHeaders(subject);
MimeMultipart multipart = (MimeMultipart) result.getContent();
assertThat(((String)
multipart.getBodyPart(0).getContent()).split("\r\n")).containsOnly(contents);
}
+
+ private MimeMessage verifyHeaders(String subject) throws
MessagingException {
+ assertThat(fakeMailContext.getSentMails()).containsOnly(new
FakeMailContext.SentMail(new MailAddress(SIEVE_LOCALHOST),
Lists.newArrayList(new MailAddress(USER)), null));
+ MimeMessage result = fakeMailContext.getSentMails().get(0).getMsg();
+ assertThat(result.getSubject()).isEqualTo(subject);
+ assertThat(result.getRecipients(RecipientType.TO)).containsOnly(new
InternetAddress(USER));
+ return result;
+ }
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]