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.User;
26 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
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 clearMails() {
78 public TestMail receive() {
80 return mails.poll(30, TimeUnit.SECONDS);
81 } catch (InterruptedException e) {
87 public void setApproveRegex(Pattern compiled) {
88 throw new Error("Currently unimplemented");
92 public void setEmailCheckError(String string) {
93 throw new Error("Currently unimplemented");
97 public TestMail poll() {
98 throw new Error("Currently unimplemented");
104 public static void purgeDBBeforeTest() throws SQLException, IOException {
109 public static void initMail() {
110 Properties p = new Properties();
111 p.setProperty("emailProvider", InVMEmail.class.getName());
112 EmailProvider.initSystem(p, null, null);
114 new PingerDaemon(KeyStore.getInstance("JKS")).start();
115 } catch (KeyStoreException e) {
120 public static User createVerifiedUser() throws GigiApiException, IOException {
121 Calendar c = Calendar.getInstance();
122 c.set(1950, 1, 1, 0, 0, 0);
123 c.set(Calendar.MILLISECOND, 0);
125 User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
126 new NamePart(NamePartType.FIRST_NAME, "a"), new NamePart(NamePartType.FIRST_NAME, "m"), new NamePart(NamePartType.LAST_NAME, "c"));
127 InVMEmail.getInstance().mails.poll().verify();
131 public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
132 User u = createUser(f, l, mail, pw);
134 InVMEmail.getInstance().mails.poll().verify();
135 } catch (IOException e) {
141 public static User createUser(String f, String l, String mail, String pw) throws GigiApiException {
142 Calendar c = Calendar.getInstance();
143 c.set(1950, 1, 1, 0, 0, 0);
144 c.set(Calendar.MILLISECOND, 0);
146 User u = new User(mail, pw, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
147 new NamePart(NamePartType.FIRST_NAME, f), new NamePart(NamePartType.LAST_NAME, l));
151 public static int createVerificationUser(String f, String l, String mail, String pw) throws GigiApiException {
152 int u = createVerifiedUser(f, l, mail, pw);
158 public MailReceiver getMailReceiver() {
159 return InVMEmail.getInstance();
162 private User supporter;
164 public User getSupporter() throws GigiApiException, IOException {
165 if (supporter != null) {
168 supporter = createVerifiedUser();
169 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
170 ps.setInt(1, supporter.getId());
171 ps.setString(2, Group.SUPPORTER.getDBName());
172 ps.setInt(3, supporter.getId());
175 supporter.refreshGroups();