From 5710a89b52699e27afeff28ec4dcf07f2a3bf9e0 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 22 Apr 2022 09:35:26 +0200 Subject: [PATCH] working on tests --- pom.xml | 2 +- .../srsoftware/widerhall/data/ListMember.java | 25 ++- .../widerhall/data/MailingList.java | 27 ++-- .../de/srsoftware/widerhall/web/Rest.java | 2 +- .../widerhall/data/ListMemberTest.java | 144 ++++++++++++++++++ 5 files changed, 178 insertions(+), 22 deletions(-) create mode 100644 src/test/java/de/srsoftware/widerhall/data/ListMemberTest.java diff --git a/pom.xml b/pom.xml index b4e7d47..f1c8a40 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.2.12 + 0.2.13 diff --git a/src/main/java/de/srsoftware/widerhall/data/ListMember.java b/src/main/java/de/srsoftware/widerhall/data/ListMember.java index 61807ea..78886b8 100644 --- a/src/main/java/de/srsoftware/widerhall/data/ListMember.java +++ b/src/main/java/de/srsoftware/widerhall/data/ListMember.java @@ -22,6 +22,7 @@ public class ListMember { public static final int STATE_OWNER = 1; public static final int STATE_SUBSCRIBER = 2; public static final int STATE_AWAITING_CONFIRMATION = 4; + public static final int STATE_MODERATOR = 8; private static final Logger LOG = LoggerFactory.getLogger(ListMember.class); private static final String LIST_EMAIL = "list_email"; private static final String USER_EMAIL = "user_email"; @@ -135,11 +136,23 @@ public class ListMember { return (state & testState) > 0; } + public boolean isAwaiting(){ + return hasState(STATE_AWAITING_CONFIRMATION); + } + + public boolean isModerator() { + return hasState(STATE_OWNER|STATE_MODERATOR); + } public boolean isOwner(){ return hasState(STATE_OWNER); } + public boolean isSubscriber(){ + return hasState(STATE_SUBSCRIBER|STATE_MODERATOR|STATE_OWNER); + } + + /** * return a set of list emails of MailingLists owned by the given user * @param user @@ -197,7 +210,7 @@ public class ListMember { return Map.of( EMAIL,user.email(), NAME,user.name(), - STATE,ListMember.stateText(state) + STATE,stateText() ); } @@ -219,14 +232,14 @@ public class ListMember { /** * convert state flag to readable text - * @param state * @return */ - public static String stateText(int state) { + public String stateText() { var words = new ArrayList(); - if ((state & STATE_OWNER) > 0) words.add("owner"); - if ((state & STATE_SUBSCRIBER) > 0) words.add("subscriber"); - if ((state & STATE_AWAITING_CONFIRMATION) > 0) words.add("awaiting confirmation"); + if (isAwaiting()) words.add("awaiting confirmation"); + if (isModerator()) words.add("moderator"); + if (isOwner()) words.add("owner"); + if (isSubscriber()) words.add("subscriber"); return String.join(", ",words); } diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index bc2323e..b4ed0d4 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -1,11 +1,9 @@ package de.srsoftware.widerhall.data; import de.srsoftware.widerhall.Configuration; -import de.srsoftware.widerhall.Util; import de.srsoftware.widerhall.mail.ImapClient; import de.srsoftware.widerhall.mail.MessageHandler; import de.srsoftware.widerhall.mail.SmtpClient; -import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,17 +39,18 @@ public class MailingList implements MessageHandler { private static final String SMTP_PASS = "smtp_pass"; public static final String TABLE_NAME = "Lists"; private static final int STATE_PENDING = 0; - private static final int STATE_ENABLED = 1; - private static final int STATE_PUBLIC = 2; - public static final int STATE_FORWARD_FROM = 4; - public static final int STATE_FORWARD_ATTACHED = 8; - public static final int STATE_HIDE_RECEIVERS = 16; - public static final int STATE_REPLY_TO_LIST = 32; - public static final int STATE_OPEN = 64; - public static final int STATE_PUBLIC_ARCHIVE = 128; + private static final int STATE_ENABLED = 1; // do we process incoming messages? + private static final int STATE_PUBLIC = 2; // can guests see this ML? + public static final int STATE_FORWARD_FROM = 4; // set original sender as FROM when forwarding? + public static final int STATE_FORWARD_ATTACHED = 8; // forward messages as attachment? + public static final int STATE_HIDE_RECEIVERS = 16; // send using BCC receivers + public static final int STATE_REPLY_TO_LIST = 32; // set REPLY TO field to list address? + public static final int STATE_OPEN_FOR_GUESTS = 64; // allow anyone to send via this list? + public static final int STATE_PUBLIC_ARCHIVE = 128; // save received messages in archive? + public static final int STATE_OPEN_FOR_MODS = 256; // allow mods to send via this list? private static final int VISIBLE = 1; private static final int HIDDEN = 0; - private static final int DEFAULT_STATE = STATE_PENDING|STATE_HIDE_RECEIVERS|STATE_PUBLIC_ARCHIVE; + private static final int DEFAULT_STATE = STATE_PENDING|STATE_HIDE_RECEIVERS|STATE_PUBLIC_ARCHIVE|STATE_OPEN_FOR_MODS; private static final String RETAINED_FOLDER = "retained"; private final String name; private final String email; @@ -335,7 +334,7 @@ public class MailingList implements MessageHandler { Address from = message.getFrom()[0]; if (from instanceof InternetAddress internetAddress){ var senderEmail = ((InternetAddress) from).getAddress(); - if (!hasState(STATE_OPEN) && !this.hashMember(senderEmail)){ + if (!hasState(STATE_OPEN_FOR_GUESTS) && !this.hashMember(senderEmail)){ retainMessage(message); sentRetentionNotification(senderEmail); return; @@ -346,7 +345,7 @@ public class MailingList implements MessageHandler { } public MailingList open(boolean open) throws SQLException { - return setFlag(STATE_OPEN,open); + return setFlag(STATE_OPEN_FOR_GUESTS,open); } /** @@ -491,7 +490,7 @@ public class MailingList implements MessageHandler { if (hasState(STATE_FORWARD_ATTACHED)) map.put("forward_attached",HIDDEN); if (hasState(STATE_HIDE_RECEIVERS)) map.put("hide_receivers",HIDDEN); if (hasState(STATE_REPLY_TO_LIST)) map.put("reply_to_list",HIDDEN); - if (hasState(STATE_OPEN)) map.put("open",VISIBLE); + if (hasState(STATE_OPEN_FOR_GUESTS)) map.put("open",VISIBLE); if (hasState(STATE_PUBLIC_ARCHIVE)) map.put("archive",VISIBLE); return map; } diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index f1ff180..30c81ce 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -236,7 +236,7 @@ public class Rest extends HttpServlet { if (list.hasState(MailingList.STATE_FORWARD_ATTACHED)) map.put("forward_attached",true); if (list.hasState(MailingList.STATE_HIDE_RECEIVERS)) map.put("hide_receivers",true); if (list.hasState(MailingList.STATE_REPLY_TO_LIST)) map.put("reply_to_list",true); - if (list.hasState(MailingList.STATE_OPEN)) map.put("open",true); + if (list.hasState(MailingList.STATE_OPEN_FOR_GUESTS)) map.put("open",true); if (list.hasState(MailingList.STATE_PUBLIC_ARCHIVE)) map.put("archive",true); return map; } diff --git a/src/test/java/de/srsoftware/widerhall/data/ListMemberTest.java b/src/test/java/de/srsoftware/widerhall/data/ListMemberTest.java new file mode 100644 index 0000000..43f0361 --- /dev/null +++ b/src/test/java/de/srsoftware/widerhall/data/ListMemberTest.java @@ -0,0 +1,144 @@ +package de.srsoftware.widerhall.data; + +import junit.framework.TestCase; + +import java.util.Map; + +import static de.srsoftware.widerhall.Constants.*; + +public class ListMemberTest extends TestCase { + + /*public void testConfirm() { + NEEDS MOCK + }*/ + + /*public void testCreate() { + NEEDS MOCK + }*/ + + /*public void testCreateTable() { + NEEDS MOCK + }*/ + + /*public void testFrom() { + NEEDS MOCK + }*/ + + public void testHasState() { + int state = 32 | 8 | 2; + ListMember lm = new ListMember(null,null,state,null); + assertFalse(lm.hasState(1)); + assertTrue(lm.hasState(2)); + assertFalse(lm.hasState(4)); + assertTrue(lm.hasState(8)); + assertFalse(lm.hasState(16)); + assertTrue(lm.hasState(32)); + + assertTrue(lm.hasState(1|2)); + assertFalse(lm.hasState(1|4)); + assertTrue(lm.hasState(1|8)); + assertFalse(lm.hasState(1|16)); + assertTrue(lm.hasState(1|32)); + + assertTrue(lm.hasState(2|4)); + assertTrue(lm.hasState(2|8)); + assertTrue(lm.hasState(2|16)); + assertTrue(lm.hasState(2|32)); + + assertTrue(lm.hasState(4|8)); + assertFalse(lm.hasState(4|16)); + assertTrue(lm.hasState(4|32)); + + assertTrue(lm.hasState(8|16)); + assertTrue(lm.hasState(8|32)); + + assertTrue(lm.hasState(16|32)); + + assertTrue(lm.hasState(1|2|4)); + assertTrue(lm.hasState(1|2|8)); + assertTrue(lm.hasState(1|2|16)); + assertTrue(lm.hasState(1|2|32)); + assertTrue(lm.hasState(1|4|8)); + assertFalse(lm.hasState(1|4|16)); + assertTrue(lm.hasState(1|4|32)); + assertTrue(lm.hasState(1|8|16)); + assertTrue(lm.hasState(1|8|32)); + assertTrue(lm.hasState(1|16|32)); + + assertTrue(lm.hasState(2|4|8)); + assertTrue(lm.hasState(2|4|16)); + assertTrue(lm.hasState(2|4|32)); + assertTrue(lm.hasState(2|8|16)); + assertTrue(lm.hasState(2|8|32)); + assertTrue(lm.hasState(2|16|32)); + + assertTrue(lm.hasState(4|8|16)); + assertTrue(lm.hasState(4|8|32)); + assertTrue(lm.hasState(4|16|32)); + assertTrue(lm.hasState(8|16|32)); + + } + + public void testIsOwner() { + var guest = new ListMember(null,null,ListMember.STATE_SUBSCRIBER,null); + var owner = new ListMember(null,null,ListMember.STATE_OWNER,null); + var mod = new ListMember(null,null,ListMember.STATE_MODERATOR,null); + assertFalse(guest.isOwner()); + assertTrue(owner.isOwner()); + assertFalse(mod.isOwner()); + } + + public void testIsModerator() { + var guest = new ListMember(null,null,ListMember.STATE_SUBSCRIBER,null); + var owner = new ListMember(null,null,ListMember.STATE_OWNER,null); + var mod = new ListMember(null,null,ListMember.STATE_MODERATOR,null); + assertFalse(guest.isModerator()); + assertTrue(owner.isModerator()); + assertTrue(mod.isModerator()); + } + + /*public void testListsOwnedBy() { + NEEDS MOCK + }*/ + + /*public void testLoad() { + NEEDS MOCK + }*/ + + /*public void testOf() { + NEEDS MOCK + }*/ + + public void testSafeMap() { + var user = new User("email","name","salt","hash",0); + var lm = new ListMember(null,user,ListMember.STATE_AWAITING_CONFIRMATION,"token"); + assertEquals(Map.of(EMAIL,"email",NAME,"name",STATE,"awaiting confirmation"),lm.safeMap()); + } + + public void testStateText() { + var guest = new ListMember(null,null,ListMember.STATE_SUBSCRIBER,null); + var owner = new ListMember(null,null,ListMember.STATE_OWNER,null); + var mod = new ListMember(null,null,ListMember.STATE_MODERATOR,null); + var await = new ListMember(null,null,ListMember.STATE_AWAITING_CONFIRMATION,null); + + assertEquals("subscriber",guest.stateText()); + assertEquals("moderator, owner, subscriber",owner.stateText()); + assertEquals("moderator, subscriber",mod.stateText()); + assertEquals("awaiting confirmation",await.stateText()); + } + + public void testToken() { + var lm = new ListMember(null,null,0,"bam"); + assertEquals("bam",lm.token()); + } + + /*public void testUnsubscribe() { + NEEDS MOCK + }*/ + + public void testUser() { + var user = new User("email","name","salt","hash",0); + var lm = new ListMember(null,user,ListMember.STATE_AWAITING_CONFIRMATION,"token"); + assertEquals(user,lm.user()); + } +} \ No newline at end of file