]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/testUtils/BusinessTest.java
727310db225bc6b0e9b1cd7499e6ab087ddd994b
[gigi.git] / tests / club / wpia / gigi / testUtils / BusinessTest.java
1 package club.wpia.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.junit.BeforeClass;
18
19 import club.wpia.gigi.GigiApiException;
20 import club.wpia.gigi.database.GigiPreparedStatement;
21 import club.wpia.gigi.dbObjects.Domain;
22 import club.wpia.gigi.dbObjects.EmailAddress;
23 import club.wpia.gigi.dbObjects.Group;
24 import club.wpia.gigi.dbObjects.NamePart;
25 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
26 import club.wpia.gigi.dbObjects.User;
27 import club.wpia.gigi.email.EmailProvider;
28 import club.wpia.gigi.ping.PingerDaemon;
29 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
30 import club.wpia.gigi.util.DayDate;
31
32 public abstract class BusinessTest extends ConfiguredTest {
33
34     public static class InVMEmail extends EmailProvider implements MailReceiver {
35
36         private static InVMEmail instance;
37
38         LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<>();
39
40         public InVMEmail(Properties p) {
41             instance = this;
42         }
43
44         @Override
45         public void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
46             TestMail tm = new TestEmailReceiver.TestMail(to, subject, message, replyto) {
47
48                 @Override
49                 public void verify() throws IOException {
50                     Pattern p = Pattern.compile("type=(email|domain)&id=([0-9]+)&hash=([a-zA-Z0-9]*)");
51                     Matcher m = p.matcher(extractLink());
52                     assertTrue(m.find());
53                     String type = m.group(1);
54                     try {
55                         if (type.equals("domain")) {
56                             Domain.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
57                         } else {
58                             EmailAddress.getById(Integer.parseInt(m.group(2))).verify(m.group(3));
59                         }
60                     } catch (GigiApiException e) {
61                         throw new Error(e);
62                     }
63                 }
64             };
65             mails.add(tm);
66         }
67
68         public static InVMEmail getInstance() {
69             return instance;
70         }
71
72         @Override
73         public void clearMails() {
74             mails.clear();
75         }
76
77         @Override
78         public TestMail receive() {
79             TestMail poll;
80             try {
81                 poll = mails.poll(30, TimeUnit.SECONDS);
82             } catch (InterruptedException e) {
83                 throw new Error(e);
84             }
85             if (poll == null) {
86                 throw new AssertionError("Mail receiving timed out");
87             }
88             return poll;
89         }
90
91         @Override
92         public void setApproveRegex(Pattern compiled) {
93             throw new Error("Currently unimplemented");
94         }
95
96         @Override
97         public void setEmailCheckError(String string) {
98             throw new Error("Currently unimplemented");
99         }
100
101         @Override
102         public TestMail poll() {
103             throw new Error("Currently unimplemented");
104         }
105
106     }
107
108     @BeforeClass
109     public static void purgeDBBeforeTest() throws SQLException, IOException {
110         purgeOnlyDB();
111     }
112
113     @BeforeClass
114     public static void initMail() {
115         Properties p = new Properties();
116         p.setProperty("emailProvider", InVMEmail.class.getName());
117         EmailProvider.initSystem(p, null, null);
118         try {
119             new PingerDaemon(KeyStore.getInstance("JKS")).start();
120         } catch (KeyStoreException e) {
121             throw new Error(e);
122         }
123     }
124
125     public static User createVerifiedUser() throws GigiApiException, IOException {
126         Calendar c = Calendar.getInstance();
127         c.set(1950, 1, 1, 0, 0, 0);
128         c.set(Calendar.MILLISECOND, 0);
129
130         User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
131                 new NamePart(NamePartType.FIRST_NAME, "a"), new NamePart(NamePartType.FIRST_NAME, "m"), new NamePart(NamePartType.LAST_NAME, "c"));
132         InVMEmail.getInstance().mails.poll().verify();
133         return u;
134     }
135
136     public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
137         User u = createUser(f, l, mail, pw);
138         try {
139             InVMEmail.getInstance().mails.poll().verify();
140         } catch (IOException e) {
141             throw new Error(e);
142         }
143         return u.getId();
144     }
145
146     public static User createUser(String f, String l, String mail, String pw) throws GigiApiException {
147         Calendar c = Calendar.getInstance();
148         c.set(1950, 1, 1, 0, 0, 0);
149         c.set(Calendar.MILLISECOND, 0);
150
151         User u = new User(mail, pw, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
152                 new NamePart(NamePartType.FIRST_NAME, f), new NamePart(NamePartType.LAST_NAME, l));
153         return u;
154     }
155
156     public static int createVerificationUser(String f, String l, String mail, String pw) throws GigiApiException {
157         int u = createVerifiedUser(f, l, mail, pw);
158         makeAgent(u);
159         return u;
160     }
161
162     @Override
163     public MailReceiver getMailReceiver() {
164         return InVMEmail.getInstance();
165     }
166
167     private User supporter;
168
169     public User getSupporter() throws GigiApiException, IOException {
170         if (supporter != null) {
171             return supporter;
172         }
173         supporter = createVerifiedUser();
174         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
175             ps.setInt(1, supporter.getId());
176             ps.setString(2, Group.SUPPORTER.getDBName());
177             ps.setInt(3, supporter.getId());
178             ps.execute();
179         }
180         supporter.refreshGroups();
181         return supporter;
182     }
183 }