]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ManagedTest.java
Test login with email verification.
[gigi.git] / tests / org / cacert / gigi / testUtils / ManagedTest.java
1 package org.cacert.gigi.testUtils;
2
3 import static org.junit.Assert.assertNotEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.BufferedReader;
7 import java.io.DataOutputStream;
8 import java.io.FileInputStream;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.UnsupportedEncodingException;
12 import java.net.HttpURLConnection;
13 import java.net.InetSocketAddress;
14 import java.net.URL;
15 import java.net.URLEncoder;
16 import java.nio.file.Files;
17 import java.nio.file.Paths;
18 import java.util.Properties;
19
20 import org.cacert.gigi.DevelLauncher;
21 import org.cacert.gigi.IOUtils;
22 import org.cacert.gigi.InitTruststore;
23 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27
28 public class ManagedTest {
29         private final String registerService = "/register";
30
31         private static TestEmailReciever ter;
32         private static Process gigi;
33         private static String url = "localhost:4443";
34
35         public static String getServerName() {
36                 return url;
37         }
38         static Properties testProps = new Properties();
39         static {
40                 InitTruststore.run();
41                 HttpURLConnection.setFollowRedirects(false);
42         }
43
44         @BeforeClass
45         public static void connectToServer() {
46                 try {
47                         testProps.load(new FileInputStream("config/test.properties"));
48                         String type = testProps.getProperty("type");
49                         if (type.equals("local")) {
50                                 url = testProps.getProperty("server");
51                                 String[] parts = testProps.getProperty("mail").split(":", 2);
52                                 ter = new TestEmailReciever(new InetSocketAddress(parts[0],
53                                                 Integer.parseInt(parts[1])));
54                                 return;
55                         }
56                         url = "localhost:" + testProps.getProperty("serverPort");
57                         gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
58                         DataOutputStream toGigi = new DataOutputStream(
59                                         gigi.getOutputStream());
60                         System.out.println("... starting server");
61                         Properties mainProps = new Properties();
62                         mainProps.load(new FileInputStream("config/gigi.properties"));
63                         mainProps.setProperty("host", "127.0.0.1");
64                         mainProps.setProperty("port", testProps.getProperty("serverPort"));
65                         mainProps.setProperty("emailProvider",
66                                         "org.cacert.gigi.email.TestEmailProvider");
67                         mainProps.setProperty("emailProvider.port", "8473");
68
69                         byte[] cacerts = Files
70                                         .readAllBytes(Paths.get("config/cacerts.jks"));
71                         byte[] keystore = Files.readAllBytes(Paths
72                                         .get("config/keystore.pkcs12"));
73
74                         DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
75                                         "changeit".getBytes(), mainProps, cacerts, keystore);
76                         toGigi.flush();
77                         // TODO wait for ready
78                         try {
79                                 Thread.sleep(3000);
80                         } catch (InterruptedException e) {
81                                 e.printStackTrace();
82                         }
83                         final BufferedReader br = new BufferedReader(new InputStreamReader(
84                                         gigi.getErrorStream()));
85                         String line;
86                         while ((line = br.readLine()) != null
87                                         && !line.contains("Server:main: Started")) {
88                                 System.err.println(line);
89                         }
90                         new Thread() {
91                                 @Override
92                                 public void run() {
93                                         String line;
94                                         try {
95                                                 while ((line = br.readLine()) != null) {
96                                                         System.err.println(line);
97                                                 }
98                                         } catch (IOException e) {
99                                                 e.printStackTrace();
100                                         }
101                                 }
102                         }.start();
103                         System.err.println(line);
104                         if (line == null) {
105                                 throw new Error("Server startup failed");
106                         }
107                         ter = new TestEmailReciever(
108                                         new InetSocketAddress("localhost", 8473));
109                 } catch (IOException e) {
110                         throw new Error(e);
111                 }
112
113         }
114         @AfterClass
115         public static void tearDownServer() {
116                 String type = testProps.getProperty("type");
117                 if (type.equals("local")) {
118                         return;
119                 }
120                 gigi.destroy();
121         }
122
123         @After
124         public void removeMails() {
125                 ter.reset();
126         }
127
128         public TestMail waitForMail() {
129                 try {
130                         return ter.recieve();
131                 } catch (InterruptedException e) {
132                         throw new Error(e);
133                 }
134         }
135         public static TestEmailReciever getMailReciever() {
136                 return ter;
137         }
138         public String runRegister(String param) throws IOException {
139                 HttpURLConnection uc = (HttpURLConnection) new URL("https://"
140                                 + getServerName() + registerService).openConnection();
141                 uc.setDoOutput(true);
142                 uc.getOutputStream().write(param.getBytes());
143                 String d = IOUtils.readURL(uc);
144                 return d;
145         }
146         public String fetchStartErrorMessage(String query) throws IOException {
147                 String d = runRegister(query);
148                 String formFail = "<div class='formError'>";
149                 int idx = d.indexOf(formFail);
150                 assertNotEquals(-1, idx);
151                 String startError = d.substring(idx + formFail.length(), idx + 100)
152                                 .trim();
153                 return startError;
154         }
155
156         public void registerUser(String firstName, String lastName, String email,
157                         String password) {
158                 try {
159                         String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
160                                         + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
161                                         + "&email=" + URLEncoder.encode(email, "UTF-8")
162                                         + "&pword1=" + URLEncoder.encode(password, "UTF-8")
163                                         + "&pword2=" + URLEncoder.encode(password, "UTF-8")
164                                         + "&day=1&month=1&year=1910&cca_agree=1";
165                         String data = fetchStartErrorMessage(query);
166                         assertTrue(data, data.startsWith("</div>"));
167                 } catch (UnsupportedEncodingException e) {
168                         throw new Error(e);
169                 } catch (IOException e) {
170                         throw new Error(e);
171                 }
172         }
173         public void createVerifiedUser(String firstName, String lastName,
174                         String email, String password) {
175                 registerUser(firstName, lastName, email, password);
176                 try {
177                         TestMail tm = ter.recieve();
178                         String verifyLink = tm.extractLink();
179                         String[] parts = verifyLink.split("\\?");
180                         URL u = new URL("https://" + getServerName() + "/verify?"
181                                         + parts[1]);
182                         u.openStream().close();;
183                 } catch (InterruptedException e) {
184                         throw new Error(e);
185                 } catch (IOException e) {
186                         throw new Error(e);
187                 }
188         }
189 }