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