]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/testUtils/BusinessTest.java
upd: terminology in code
[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.User;
26 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
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             try {
80                 return mails.poll(30, TimeUnit.SECONDS);
81             } catch (InterruptedException e) {
82                 throw new Error(e);
83             }
84         }
85
86         @Override
87         public void setApproveRegex(Pattern compiled) {
88             throw new Error("Currently unimplemented");
89         }
90
91         @Override
92         public void setEmailCheckError(String string) {
93             throw new Error("Currently unimplemented");
94         }
95
96         @Override
97         public TestMail poll() {
98             throw new Error("Currently unimplemented");
99         }
100
101     }
102
103     @BeforeClass
104     public static void purgeDBBeforeTest() throws SQLException, IOException {
105         purgeOnlyDB();
106     }
107
108     @BeforeClass
109     public static void initMail() {
110         Properties p = new Properties();
111         p.setProperty("emailProvider", InVMEmail.class.getName());
112         EmailProvider.initSystem(p, null, null);
113         try {
114             new PingerDaemon(KeyStore.getInstance("JKS")).start();
115         } catch (KeyStoreException e) {
116             throw new Error(e);
117         }
118     }
119
120     public static User createVerifiedUser() throws GigiApiException, IOException {
121         Calendar c = Calendar.getInstance();
122         c.set(1950, 1, 1, 0, 0, 0);
123         c.set(Calendar.MILLISECOND, 0);
124
125         User u = new User(createUniqueName() + "@email.com", TEST_PASSWORD, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
126                 new NamePart(NamePartType.FIRST_NAME, "a"), new NamePart(NamePartType.FIRST_NAME, "m"), new NamePart(NamePartType.LAST_NAME, "c"));
127         InVMEmail.getInstance().mails.poll().verify();
128         return u;
129     }
130
131     public static int createVerifiedUser(String f, String l, String mail, String pw) throws GigiApiException {
132         User u = createUser(f, l, mail, pw);
133         try {
134             InVMEmail.getInstance().mails.poll().verify();
135         } catch (IOException e) {
136             throw new Error(e);
137         }
138         return u.getId();
139     }
140
141     public static User createUser(String f, String l, String mail, String pw) throws GigiApiException {
142         Calendar c = Calendar.getInstance();
143         c.set(1950, 1, 1, 0, 0, 0);
144         c.set(Calendar.MILLISECOND, 0);
145
146         User u = new User(mail, pw, new DayDate(c.getTimeInMillis()), Locale.ENGLISH, null, //
147                 new NamePart(NamePartType.FIRST_NAME, f), new NamePart(NamePartType.LAST_NAME, l));
148         return u;
149     }
150
151     public static int createVerificationUser(String f, String l, String mail, String pw) throws GigiApiException {
152         int u = createVerifiedUser(f, l, mail, pw);
153         makeAgent(u);
154         return u;
155     }
156
157     @Override
158     public MailReceiver getMailReceiver() {
159         return InVMEmail.getInstance();
160     }
161
162     private User supporter;
163
164     public User getSupporter() throws GigiApiException, IOException {
165         if (supporter != null) {
166             return supporter;
167         }
168         supporter = createVerifiedUser();
169         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
170             ps.setInt(1, supporter.getId());
171             ps.setString(2, Group.SUPPORTER.getDBName());
172             ps.setInt(3, supporter.getId());
173             ps.execute();
174         }
175         supporter.refreshGroups();
176         return supporter;
177     }
178 }