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.NamePart;
21 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
22 import org.cacert.gigi.dbObjects.User;
23 import org.cacert.gigi.email.EmailProvider;
24 import org.cacert.gigi.ping.PingerDaemon;
25 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
26 import org.cacert.gigi.util.DayDate;
27 import org.junit.BeforeClass;
29 public abstract class BusinessTest extends ConfiguredTest {
31 public static class InVMEmail extends EmailProvider implements MailReceiver {
33 private static InVMEmail instance;
35 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
37 public InVMEmail(Properties p) {
42 public void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
43 TestMail tm = new TestEmailReceiver.TestMail(to, subject, message, replyto) {
46 public void verify() throws IOException {
47 Pattern p = Pattern.compile("type=(email|domain)&id=([0-9]+)&hash=([a-zA-Z0-9]*)");
48 Matcher m = p.matcher(extractLink());
50 String type = m.group(1);
52 if (type.equals("domain")) {
53 Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
55 EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
57 } catch (GigiApiException e) {
65 public static InVMEmail getInstance() {
70 public void clearMails() {
75 public TestMail receive() {
77 return mails.poll(30, TimeUnit.SECONDS);
78 } catch (InterruptedException e) {
84 public void setApproveRegex(Pattern compiled) {
85 throw new Error("Currently unimplemented");
89 public void setEmailCheckError(String string) {
90 throw new Error("Currently unimplemented");
94 public TestMail poll() {
95 throw new Error("Currently unimplemented");
101 public static void purgeDBBeforeTest() throws SQLException, IOException {
106 public static void initMail() {
107 Properties p = new Properties();
108 p.setProperty("emailProvider", InVMEmail.class.getName());
109 EmailProvider.initSystem(p, null, null);
111 new PingerDaemon(KeyStore.getInstance("JKS")).start();
112 } catch (KeyStoreException e) {
117 public static User createVerifiedUser() throws GigiApiException, IOException {
118 Calendar c = Calendar.getInstance();
119 c.set(1950, 1, 1, 0, 0, 0);
120 c.set(Calendar.MILLISECOND, 0);
122 User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, //
123 new NamePart(NamePartType.FIRST_NAME, "a"), new NamePart(NamePartType.FIRST_NAME, "m"), new NamePart(NamePartType.LAST_NAME, "c"));
124 InVMEmail.getInstance().mails.poll().verify();
128 public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
129 User u = createUser(f, l, mail, pw);
131 InVMEmail.getInstance().mails.poll().verify();
132 } catch (IOException e) {
138 public static User createUser(String f, String l, String mail, String pw) throws GigiApiException {
139 Calendar c = Calendar.getInstance();
140 c.set(1950, 1, 1, 0, 0, 0);
141 c.set(Calendar.MILLISECOND, 0);
143 User u = new User(mail, pw, new DayDate(c.getTimeInMillis()), Locale.ENGLISH,//
144 new NamePart(NamePartType.FIRST_NAME, f), new NamePart(NamePartType.LAST_NAME, l));
148 public static int createAssuranceUser(String f, String l, String mail, String pw) throws GigiApiException {
149 int u = createVerifiedUser(f, l, mail, pw);
155 public MailReceiver getMailReceiver() {
156 return InVMEmail.getInstance();