]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java
81d727bcd882d5e52126a3d1033dd7196db9cd6a
[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.SupportEnterTicketPage;
20 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
21 import org.cacert.gigi.testUtils.ClientTest;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.util.ServerConstants;
24 import org.junit.Test;
25
26 public class TestSEAdminPageUserDomainSearch extends ClientTest {
27
28     public TestSEAdminPageUserDomainSearch() throws IOException {
29         grant(email, Group.SUPPORTER);
30         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
31     }
32
33     @Test
34     public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
35         String mail = createUniqueName() + "@example.com";
36         int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
37         User user = User.getById(id);
38         String domainName = createUniqueName() + ".org";
39         new Domain(user, domainName);
40         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
41         uc.addRequestProperty("Cookie", cookie);
42         String csrf = getCSRF(uc, 0);
43
44         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
45         uc.addRequestProperty("Cookie", cookie);
46         uc.setDoOutput(true);
47         OutputStream os = uc.getOutputStream();
48         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
49                 + "process&domain=" + URLEncoder.encode(domainName, "UTF-8")).getBytes("UTF-8"));
50         os.flush();
51         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
52     }
53
54     @Test
55     public void testDomainSearchById() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
56         String mail = createUniqueName() + "@example.com";
57         int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
58         User user = User.getById(id);
59         String domainName = createUniqueName() + ".org";
60         Domain d = new Domain(user, domainName);
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 }