]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/testUtils/ClientTest.java
add: ensure that for support actions certificate login is used
[gigi.git] / tests / club / wpia / gigi / testUtils / ClientTest.java
1 package club.wpia.gigi.testUtils;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.HttpURLConnection;
6 import java.net.URL;
7 import java.net.URLConnection;
8 import java.net.URLEncoder;
9 import java.security.GeneralSecurityException;
10
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.dbObjects.User;
13
14 /**
15  * Superclass for testsuites in a scenario where there is an registered member,
16  * who is already logged on.
17  */
18 public abstract class ClientTest extends ManagedTest {
19
20     /**
21      * Email of the member.
22      */
23     protected String email = createUniqueName() + "@example.org";
24
25     /**
26      * Id of the member
27      */
28     protected int id = createVerifiedUser("a", "b", email, TEST_PASSWORD);
29
30     /**
31      * {@link User} object of the member
32      */
33     protected User u = User.getById(id);
34
35     /**
36      * Session cookie of the member.
37      */
38     protected String cookie;
39
40     public ClientTest() {
41         try {
42             cookie = login(email, TEST_PASSWORD);
43         } catch (IOException e) {
44             throw new Error(e);
45         }
46     }
47
48     public HttpURLConnection post(String path, String query) throws IOException {
49         return post(path, query, 0);
50     }
51
52     public HttpURLConnection post(String path, String query, int formIndex) throws IOException {
53         String server = getServerName();
54         if (loginCertificate != null) {
55             server = getSecureServerName();
56         }
57         URLConnection uc = new URL("https://" + server + path).openConnection();
58         authenticate((HttpURLConnection) uc);
59         String csrf = getCSRF(uc, formIndex);
60
61         uc = new URL("https://" + server + path).openConnection();
62         authenticate((HttpURLConnection) uc);
63         uc.setDoOutput(true);
64         OutputStream os = uc.getOutputStream();
65         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
66                 + query//
67         ).getBytes("UTF-8"));
68         os.flush();
69         return (HttpURLConnection) uc;
70     }
71
72     public HttpURLConnection get(String path) throws IOException {
73         String server = getServerName();
74         if (loginCertificate != null) {
75             server = getSecureServerName();
76         }
77         URLConnection uc = new URL("https://" + server + path).openConnection();
78         authenticate((HttpURLConnection) uc);
79         return (HttpURLConnection) uc;
80     }
81
82     protected void authenticate(HttpURLConnection uc) throws IOException {
83         uc.addRequestProperty("Cookie", cookie);
84         if (loginCertificate != null) {
85             try {
86                 authenticateClientCert(loginPrivateKey, loginCertificate.cert(), uc);
87             } catch (GeneralSecurityException | GigiApiException e) {
88                 throw new IOException(e);
89             }
90         }
91     }
92
93 }