]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/BusinessTest.java
c014e2b3a22c427ab811ec9c9304eb360da5993a
[gigi.git] / tests / org / cacert / gigi / testUtils / BusinessTest.java
1 package org.cacert.gigi.testUtils;
2
3 import static org.junit.Assert.*;
4
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;
16
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;
27
28 public abstract class BusinessTest extends ConfiguredTest {
29
30     public static class InVMEmail extends EmailProvider implements MailReceiver {
31
32         private static InVMEmail instance;
33
34         LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
35
36         public InVMEmail(Properties p) {
37             instance = this;
38         }
39
40         @Override
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) {
43
44                 @Override
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());
48                     assertTrue(m.find());
49                     String type = m.group(1);
50                     try {
51                         if (type.equals("domain")) {
52                             Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
53                         } else {
54                             EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
55                         }
56                     } catch (GigiApiException e) {
57                         throw new Error(e);
58                     }
59                 }
60             };
61             mails.add(tm);
62         }
63
64         public static InVMEmail getInstance() {
65             return instance;
66         }
67
68         @Override
69         public void clearMails() {
70             mails.clear();
71         }
72
73         @Override
74         public TestMail receive() {
75             try {
76                 return mails.poll(30, TimeUnit.SECONDS);
77             } catch (InterruptedException e) {
78                 throw new Error(e);
79             }
80         }
81
82         @Override
83         public void setApproveRegex(Pattern compiled) {
84             throw new Error("Currently unimplemented");
85         }
86
87         @Override
88         public void setEmailCheckError(String string) {
89             throw new Error("Currently unimplemented");
90         }
91
92         @Override
93         public TestMail poll() {
94             throw new Error("Currently unimplemented");
95         }
96
97     }
98
99     @BeforeClass
100     public static void purgeDBBeforeTest() throws SQLException, IOException {
101         purgeOnlyDB();
102     }
103
104     @BeforeClass
105     public static void initMail() {
106         Properties p = new Properties();
107         p.setProperty("emailProvider", InVMEmail.class.getName());
108         EmailProvider.initSystem(p, null, null);
109         try {
110             new PingerDaemon(KeyStore.getInstance("JKS")).start();
111         } catch (KeyStoreException e) {
112             throw new Error(e);
113         }
114     }
115
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);
120
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();
123         return u;
124     }
125
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);
130
131         User u = new User(mail, pw, new Name(f, l, "", ""), new DayDate(c.getTimeInMillis()), Locale.ENGLISH);
132         try {
133             InVMEmail.getInstance().mails.poll().verify();
134         } catch (IOException e) {
135             throw new Error(e);
136         }
137         return u.getId();
138     }
139
140     public static int createAssuranceUser(String f, String l, String mail, String pw) throws GigiApiException {
141         int u = createVerifiedUser(f, l, mail, pw);
142         makeAssurer(u);
143         return u;
144     }
145
146     @Override
147     public MailReceiver getMailReceiver() {
148         return InVMEmail.getInstance();
149     }
150 }