]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/BusinessTest.java
upd: cleanup SQL statements to make them statically verifiable.
[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.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;
30
31 public abstract class BusinessTest extends ConfiguredTest {
32
33     public static class InVMEmail extends EmailProvider implements MailReceiver {
34
35         private static InVMEmail instance;
36
37         LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
38
39         public InVMEmail(Properties p) {
40             instance = this;
41         }
42
43         @Override
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) {
46
47                 @Override
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());
51                     assertTrue(m.find());
52                     String type = m.group(1);
53                     try {
54                         if (type.equals("domain")) {
55                             Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
56                         } else {
57                             EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
58                         }
59                     } catch (GigiApiException e) {
60                         throw new Error(e);
61                     }
62                 }
63             };
64             mails.add(tm);
65         }
66
67         public static InVMEmail getInstance() {
68             return instance;
69         }
70
71         @Override
72         public void clearMails() {
73             mails.clear();
74         }
75
76         @Override
77         public TestMail receive() {
78             try {
79                 return mails.poll(30, TimeUnit.SECONDS);
80             } catch (InterruptedException e) {
81                 throw new Error(e);
82             }
83         }
84
85         @Override
86         public void setApproveRegex(Pattern compiled) {
87             throw new Error("Currently unimplemented");
88         }
89
90         @Override
91         public void setEmailCheckError(String string) {
92             throw new Error("Currently unimplemented");
93         }
94
95         @Override
96         public TestMail poll() {
97             throw new Error("Currently unimplemented");
98         }
99
100     }
101
102     @BeforeClass
103     public static void purgeDBBeforeTest() throws SQLException, IOException {
104         purgeOnlyDB();
105     }
106
107     @BeforeClass
108     public static void initMail() {
109         Properties p = new Properties();
110         p.setProperty("emailProvider", InVMEmail.class.getName());
111         EmailProvider.initSystem(p, null, null);
112         try {
113             new PingerDaemon(KeyStore.getInstance("JKS")).start();
114         } catch (KeyStoreException e) {
115             throw new Error(e);
116         }
117     }
118
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);
123
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();
127         return u;
128     }
129
130     public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
131         User u = createUser(f, l, mail, pw);
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 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);
144
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));
147         return u;
148     }
149
150     public static int createAssuranceUser(String f, String l, String mail, String pw) throws GigiApiException {
151         int u = createVerifiedUser(f, l, mail, pw);
152         makeAssurer(u);
153         return u;
154     }
155
156     @Override
157     public MailReceiver getMailReceiver() {
158         return InVMEmail.getInstance();
159     }
160
161     private User supporter;
162
163     public User getSupporter() throws GigiApiException, IOException {
164         if (supporter != null) {
165             return supporter;
166         }
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());
172             ps.execute();
173         }
174         supporter.refreshGroups();
175         return supporter;
176     }
177 }