1 package club.wpia.gigi.testUtils;
3 import static org.junit.Assert.*;
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;
17 import org.junit.BeforeClass;
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;
32 public abstract class BusinessTest extends ConfiguredTest {
34 public static class InVMEmail extends EmailProvider implements MailReceiver {
36 private static InVMEmail instance;
38 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
40 public InVMEmail(Properties p) {
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) {
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());
53 String type = m.group(1);
55 if (type.equals("domain")) {
56 Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
58 EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
60 } catch (GigiApiException e) {
68 public static InVMEmail getInstance() {
73 public void assertEmpty() {
78 public TestMail receive(String to) {
81 poll = mails.poll(30, TimeUnit.SECONDS);
82 } catch (InterruptedException e) {
86 throw new AssertionError("Mail receiving timed out");
89 assertEquals(to, poll.getTo());
95 public void setApproveRegex(Pattern compiled) {
96 throw new Error("Currently unimplemented");
100 public void setEmailCheckError(String string) {
101 throw new Error("Currently unimplemented");
105 public TestMail poll(String to) {
106 throw new Error("Currently unimplemented");
112 public static void purgeDBBeforeTest() throws SQLException, IOException {
117 public static void initMail() {
118 Properties p = new Properties();
119 p.setProperty("emailProvider", InVMEmail.class.getName());
120 EmailProvider.initSystem(p, null, null);
122 new PingerDaemon(KeyStore.getInstance("JKS")).start();
123 } catch (KeyStoreException e) {
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);
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();
139 public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
140 User u = createUser(f, l, mail, pw);
142 InVMEmail.getInstance().mails.poll().verify();
143 } catch (IOException e) {
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);
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));
159 public static int createVerificationUser(String f, String l, String mail, String pw) throws GigiApiException {
160 int u = createVerifiedUser(f, l, mail, pw);
166 public MailReceiver getMailReceiver() {
167 return InVMEmail.getInstance();
170 private User supporter;
172 public User getSupporter() throws GigiApiException, IOException {
173 if (supporter != null) {
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());
183 supporter.refreshGroups();