]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java
FIX: Correct hostname and port for SE testcases
[gigi.git] / tests / org / cacert / gigi / pages / admin / TestSEAdminPageUserDomainSearch.java
1 package org.cacert.gigi.pages.admin;
2
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
5
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.io.UnsupportedEncodingException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.net.URLConnection;
12 import java.net.URLEncoder;
13
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.dbObjects.Domain;
16 import org.cacert.gigi.dbObjects.Group;
17 import org.cacert.gigi.dbObjects.User;
18 import org.cacert.gigi.pages.admin.support.FindDomainPage;
19 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
20 import org.cacert.gigi.testUtils.ClientTest;
21 import org.cacert.gigi.testUtils.IOUtils;
22 import org.cacert.gigi.util.ServerConstants;
23 import org.junit.Test;
24
25 public class TestSEAdminPageUserDomainSearch extends ClientTest {
26
27     public TestSEAdminPageUserDomainSearch() throws IOException {
28         grant(email, Group.SUPPORTER);
29     }
30
31     @Test
32     public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
33         String mail = createUniqueName() + "@example.com";
34         int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
35         User user = User.getById(id);
36         String domainName = createUniqueName() + ".org";
37         Domain d = new Domain(user, domainName);
38         d.insert();
39         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
40         uc.addRequestProperty("Cookie", cookie);
41         String csrf = getCSRF(uc, 0);
42
43         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
44         uc.addRequestProperty("Cookie", cookie);
45         uc.setDoOutput(true);
46         OutputStream os = uc.getOutputStream();
47         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
48                 + "process&domain=" + URLEncoder.encode(domainName, "UTF-8")).getBytes("UTF-8"));
49         os.flush();
50         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
51     }
52
53     @Test
54     public void testDomainSearchById() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
55         String mail = createUniqueName() + "@example.com";
56         int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
57         User user = User.getById(id);
58         String domainName = createUniqueName() + ".org";
59         Domain d = new Domain(user, domainName);
60         d.insert();
61         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
62         uc.addRequestProperty("Cookie", cookie);
63         String csrf = getCSRF(uc, 0);
64
65         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
66         uc.addRequestProperty("Cookie", cookie);
67         uc.setDoOutput(true);
68         OutputStream os = uc.getOutputStream();
69         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
70                 + "process&domain=#" + d.getId()).getBytes("UTF-8"));
71         os.flush();
72         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
73     }
74
75     @Test
76     public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
77         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
78         uc.addRequestProperty("Cookie", cookie);
79         String csrf = getCSRF(uc, 0);
80
81         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
82         uc.addRequestProperty("Cookie", cookie);
83         uc.setDoOutput(true);
84         OutputStream os = uc.getOutputStream();
85         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
86                 + "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8")).getBytes("UTF-8"));
87         os.flush();
88         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
89     }
90
91     @Test
92     public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
93         int id = (int) (Math.random() * 10000);
94         int count = 0;
95         boolean found = false;
96         try {
97             while (Domain.getById(id) != null && count < 20) {
98                 count++;
99                 id = (int) (Math.random() * 10000);
100             }
101         } catch (Exception e) {
102             found = true;
103         }
104         assumeTrue(found);
105         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
106         uc.addRequestProperty("Cookie", cookie);
107         String csrf = getCSRF(uc, 0);
108         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
109         uc.addRequestProperty("Cookie", cookie);
110         uc.setDoOutput(true);
111         OutputStream os = uc.getOutputStream();
112         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
113                 + "process&domain=#" + id).getBytes("UTF-8"));
114         os.flush();
115         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
116     }
117 }