1 package org.cacert.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.cacert.gigi.GigiApiException;
18 import org.cacert.gigi.dbObjects.Domain;
19 import org.cacert.gigi.dbObjects.EmailAddress;
20 import org.cacert.gigi.dbObjects.Name;
21 import org.cacert.gigi.dbObjects.User;
22 import org.cacert.gigi.email.EmailProvider;
23 import org.cacert.gigi.ping.PingerDaemon;
24 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
25 import org.cacert.gigi.util.DayDate;
26 import org.junit.BeforeClass;
28 public abstract class BusinessTest extends ConfiguredTest {
30 public static class InVMEmail extends EmailProvider implements MailReceiver {
32 private static InVMEmail instance;
34 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
36 public InVMEmail(Properties p) {
41 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
42 TestMail tm = new TestEmailReceiver.TestMail(to, subject, message, fromname, replyto) {
45 public void verify() throws IOException {
46 Pattern p = Pattern.compile("type=(email|domain)&id=([0-9]+)&hash=([a-zA-Z0-9]*)");
47 Matcher m = p.matcher(extractLink());
49 String type = m.group(1);
51 if (type.equals("domain")) {
52 Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
54 EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
56 } catch (GigiApiException e) {
64 public static InVMEmail getInstance() {
69 public void clearMails() {
74 public TestMail receive() {
76 return mails.poll(30, TimeUnit.SECONDS);
77 } catch (InterruptedException e) {
83 public void setApproveRegex(Pattern compiled) {
84 throw new Error("Currently unimplemented");
88 public void setEmailCheckError(String string) {
89 throw new Error("Currently unimplemented");
93 public TestMail poll() {
94 throw new Error("Currently unimplemented");
100 public static void purgeDBBeforeTest() throws SQLException, IOException {
105 public static void initMail() {
106 Properties p = new Properties();
107 p.setProperty("emailProvider", InVMEmail.class.getName());
108 EmailProvider.initSystem(p, null, null);
110 new PingerDaemon(KeyStore.getInstance("JKS")).start();
111 } catch (KeyStoreException e) {
116 public static User createVerifiedUser() throws GigiApiException, IOException {
117 Calendar c = Calendar.getInstance();
118 c.set(1950, 1, 1, 0, 0, 0);
119 c.set(Calendar.MILLISECOND, 0);
121 User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new Name("a", "m", "c", ""), new DayDate(c.getTimeInMillis()), Locale.ENGLISH);
122 InVMEmail.getInstance().mails.poll().verify();
126 public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
127 Calendar c = Calendar.getInstance();
128 c.set(1950, 1, 1, 0, 0, 0);
129 c.set(Calendar.MILLISECOND, 0);
131 User u = new User(mail, pw, new Name(f, l, "", ""), new DayDate(c.getTimeInMillis()), Locale.ENGLISH);
133 InVMEmail.getInstance().mails.poll().verify();
134 } catch (IOException e) {
140 public static int createAssuranceUser(String f, String l, String mail, String pw) throws GigiApiException {
141 int u = createVerifiedUser(f, l, mail, pw);
147 public MailReceiver getMailReciever() {
148 return InVMEmail.getInstance();