]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java
UPD: minor consistency cleanups
[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         new Domain(user, domainName);
38         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
39         uc.addRequestProperty("Cookie", cookie);
40         String csrf = getCSRF(uc, 0);
41
42         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
43         uc.addRequestProperty("Cookie", cookie);
44         uc.setDoOutput(true);
45         OutputStream os = uc.getOutputStream();
46         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
47                 + "process&domain=" + URLEncoder.encode(domainName, "UTF-8")).getBytes("UTF-8"));
48         os.flush();
49         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
50     }
51
52     @Test
53     public void testDomainSearchById() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
54         String mail = createUniqueName() + "@example.com";
55         int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
56         User user = User.getById(id);
57         String domainName = createUniqueName() + ".org";
58         Domain d = new Domain(user, domainName);
59         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
60         uc.addRequestProperty("Cookie", cookie);
61         String csrf = getCSRF(uc, 0);
62
63         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
64         uc.addRequestProperty("Cookie", cookie);
65         uc.setDoOutput(true);
66         OutputStream os = uc.getOutputStream();
67         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
68                 + "process&domain=#" + d.getId()).getBytes("UTF-8"));
69         os.flush();
70         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
71     }
72
73     @Test
74     public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
75         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
76         uc.addRequestProperty("Cookie", cookie);
77         String csrf = getCSRF(uc, 0);
78
79         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
80         uc.addRequestProperty("Cookie", cookie);
81         uc.setDoOutput(true);
82         OutputStream os = uc.getOutputStream();
83         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
84                 + "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8")).getBytes("UTF-8"));
85         os.flush();
86         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
87     }
88
89     @Test
90     public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
91         int id = (int) (Math.random() * 10000);
92         int count = 0;
93         boolean found = false;
94         try {
95             while (Domain.getById(id) != null && count < 20) {
96                 count++;
97                 id = (int) (Math.random() * 10000);
98             }
99         } catch (Exception e) {
100             found = true;
101         }
102         assumeTrue(found);
103         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
104         uc.addRequestProperty("Cookie", cookie);
105         String csrf = getCSRF(uc, 0);
106         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
107         uc.addRequestProperty("Cookie", cookie);
108         uc.setDoOutput(true);
109         OutputStream os = uc.getOutputStream();
110         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
111                 + "process&domain=#" + id).getBytes("UTF-8"));
112         os.flush();
113         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
114     }
115 }