]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/testUtils/BusinessTest.java
upd: enforce that test cases receive all mails explicitly
[gigi.git] / tests / club / wpia / gigi / testUtils / BusinessTest.java
1 package club.wpia.gigi.testUtils;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.security.KeyStore;
7 import java.security.KeyStoreException;
8 import java.sql.SQLException;
9 import java.util.Calendar;
10 import java.util.Locale;
11 import java.util.Properties;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.TimeUnit;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import org.junit.BeforeClass;
18
19 import club.wpia.gigi.GigiApiException;
20 import club.wpia.gigi.database.GigiPreparedStatement;
21 import club.wpia.gigi.dbObjects.Domain;
22 import club.wpia.gigi.dbObjects.EmailAddress;
23 import club.wpia.gigi.dbObjects.Group;
24 import club.wpia.gigi.dbObjects.NamePart;
25 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
26 import club.wpia.gigi.dbObjects.User;
27 import club.wpia.gigi.email.EmailProvider;
28 import club.wpia.gigi.ping.PingerDaemon;
29 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
30 import club.wpia.gigi.util.DayDate;
31
32 public abstract class BusinessTest extends ConfiguredTest {
33
34     public static class InVMEmail extends EmailProvider implements MailReceiver {
35
36         private static InVMEmail instance;
37
38         LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
39
40         public InVMEmail(Properties p) {
41             instance = this;
42         }
43
44         @Override
45         public void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
46             TestMail tm = new TestEmailReceiver.TestMail(to, subject, message, replyto) {
47
48                 @Override
49                 public void verify() throws IOException {
50                     Pattern p = Pattern.compile("type=(email|domain)&id=([0-9]+)&hash=([a-zA-Z0-9]*)");
51                     Matcher m = p.matcher(extractLink());
52                     assertTrue(m.find());
53                     String type = m.group(1);
54                     try {
55                         if (type.equals("domain")) {
56                             Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
57                         } else {
58                             EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
59                         }
60                     } catch (GigiApiException e) {
61                         throw new Error(e);
62                     }
63                 }
64             };
65             mails.add(tm);
66         }
67
68         public static InVMEmail getInstance() {
69             return instance;
70         }
71
72         @Override
73         public void assertEmpty() {
74             mails.clear();
75         }
76
77         @Override
78         public TestMail receive(String to) {
79             TestMail poll;
80             try {
81                 poll = mails.poll(30, TimeUnit.SECONDS);
82             } catch (InterruptedException e) {
83                 throw new Error(e);
84             }
85             if (poll == null) {
86                 throw new AssertionError("Mail receiving timed out");
87             }
88             if (to != null) {
89                 assertEquals(to, poll.getTo());
90             }
91             return poll;
92         }
93
94         @Override
95         public void setApproveRegex(Pattern compiled) {
96             throw new Error("Currently unimplemented");
97         }
98
99         @Override
100         public void setEmailCheckError(String string) {
101             throw new Error("Currently unimplemented");
102         }
103
104         @Override
105         public TestMail poll(String to) {
106             throw new Error("Currently unimplemented");
107         }
108
109     }
110
111     @BeforeClass
112     public static void purgeDBBeforeTest() throws SQLException, IOException {
113         purgeOnlyDB();
114     }
115
116     @BeforeClass
117     public static void initMail() {
118         Properties p = new Properties();
119         p.setProperty("emailProvider", InVMEmail.class.getName());
120         EmailProvider.initSystem(p, null, null);
121         try {
122             new PingerDaemon(KeyStore.getInstance("JKS")).start();
123         } catch (KeyStoreException e) {
124             throw new Error(e);
125         }
126     }
127
128     public static User createVerifiedUser() throws GigiApiException, IOException {
129         Calendar c = Calendar.getInstance();
130         c.set(1950, 1, 1, 0, 0, 0);
131         c.set(Calendar.MILLISECOND, 0);
132
133         User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
134                 new NamePart(NamePartType.FIRST_NAME, "a"), new NamePart(NamePartType.FIRST_NAME, "m"), new NamePart(NamePartType.LAST_NAME, "c"));
135         InVMEmail.getInstance().mails.poll().verify();
136         return u;
137     }
138
139     public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
140         User u = createUser(f, l, mail, pw);
141         try {
142             InVMEmail.getInstance().mails.poll().verify();
143         } catch (IOException e) {
144             throw new Error(e);
145         }
146         return u.getId();
147     }
148
149     public static User createUser(String f, String l, String mail, String pw) throws GigiApiException {
150         Calendar c = Calendar.getInstance();
151         c.set(1950, 1, 1, 0, 0, 0);
152         c.set(Calendar.MILLISECOND, 0);
153
154         User u = new User(mail, pw, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
155                 new NamePart(NamePartType.FIRST_NAME, f), new NamePart(NamePartType.LAST_NAME, l));
156         return u;
157     }
158
159     public static int createVerificationUser(String f, String l, String mail, String pw) throws GigiApiException {
160         int u = createVerifiedUser(f, l, mail, pw);
161         makeAgent(u);
162         return u;
163     }
164
165     @Override
166     public MailReceiver getMailReceiver() {
167         return InVMEmail.getInstance();
168     }
169
170     private User supporter;
171
172     public User getSupporter() throws GigiApiException, IOException {
173         if (supporter != null) {
174             return supporter;
175         }
176         supporter = createVerifiedUser();
177         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
178             ps.setInt(1, supporter.getId());
179             ps.setString(2, Group.SUPPORTER.getDBName());
180             ps.setInt(3, supporter.getId());
181             ps.execute();
182         }
183         supporter.refreshGroups();
184         return supporter;
185     }
186 }