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.database.GigiPreparedStatement;
19 import org.cacert.gigi.dbObjects.Domain;
20 import org.cacert.gigi.dbObjects.EmailAddress;
21 import org.cacert.gigi.dbObjects.Group;
22 import org.cacert.gigi.dbObjects.NamePart;
23 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
24 import org.cacert.gigi.dbObjects.User;
25 import org.cacert.gigi.email.EmailProvider;
26 import org.cacert.gigi.ping.PingerDaemon;
27 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
28 import org.cacert.gigi.util.DayDate;
29 import org.junit.BeforeClass;
31 public abstract class BusinessTest extends ConfiguredTest {
33 public static class InVMEmail extends EmailProvider implements MailReceiver {
35 private static InVMEmail instance;
37 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
39 public InVMEmail(Properties p) {
44 public void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
45 TestMail tm = new TestEmailReceiver.TestMail(to, subject, message, replyto) {
48 public void verify() throws IOException {
49 Pattern p = Pattern.compile("type=(email|domain)&id=([0-9]+)&hash=([a-zA-Z0-9]*)");
50 Matcher m = p.matcher(extractLink());
52 String type = m.group(1);
54 if (type.equals("domain")) {
55 Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
57 EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
59 } catch (GigiApiException e) {
67 public static InVMEmail getInstance() {
72 public void clearMails() {
77 public TestMail receive() {
79 return mails.poll(30, TimeUnit.SECONDS);
80 } catch (InterruptedException e) {
86 public void setApproveRegex(Pattern compiled) {
87 throw new Error("Currently unimplemented");
91 public void setEmailCheckError(String string) {
92 throw new Error("Currently unimplemented");
96 public TestMail poll() {
97 throw new Error("Currently unimplemented");
103 public static void purgeDBBeforeTest() throws SQLException, IOException {
108 public static void initMail() {
109 Properties p = new Properties();
110 p.setProperty("emailProvider", InVMEmail.class.getName());
111 EmailProvider.initSystem(p, null, null);
113 new PingerDaemon(KeyStore.getInstance("JKS")).start();
114 } catch (KeyStoreException e) {
119 public static User createVerifiedUser() throws GigiApiException, IOException {
120 Calendar c = Calendar.getInstance();
121 c.set(1950, 1, 1, 0, 0, 0);
122 c.set(Calendar.MILLISECOND, 0);
124 User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
125 new NamePart(NamePartType.FIRST_NAME, "a"), new NamePart(NamePartType.FIRST_NAME, "m"), new NamePart(NamePartType.LAST_NAME, "c"));
126 InVMEmail.getInstance().mails.poll().verify();
130 public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
131 User u = createUser(f, l, mail, pw);
133 InVMEmail.getInstance().mails.poll().verify();
134 } catch (IOException e) {
140 public static User createUser(String f, String l, String mail, String pw) throws GigiApiException {
141 Calendar c = Calendar.getInstance();
142 c.set(1950, 1, 1, 0, 0, 0);
143 c.set(Calendar.MILLISECOND, 0);
145 User u = new User(mail, pw, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
146 new NamePart(NamePartType.FIRST_NAME, f), new NamePart(NamePartType.LAST_NAME, l));
150 public static int createAssuranceUser(String f, String l, String mail, String pw) throws GigiApiException {
151 int u = createVerifiedUser(f, l, mail, pw);
157 public MailReceiver getMailReceiver() {
158 return InVMEmail.getInstance();
161 private User supporter;
163 public User getSupporter() throws GigiApiException, IOException {
164 if (supporter != null) {
167 supporter = createVerifiedUser();
168 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
169 ps.setInt(1, supporter.getId());
170 ps.setString(2, Group.SUPPORTER.getDBName());
171 ps.setInt(3, supporter.getId());
174 supporter.refreshGroups();