]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java
ADD: More search test
[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.junit.Test;
23
24 public class TestSEAdminPageUserDomainSearch extends ClientTest {
25
26     public TestSEAdminPageUserDomainSearch() throws IOException {
27         grant(email, Group.SUPPORTER);
28     }
29
30     @Test
31     public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
32         String mail = createUniqueName() + "@example.com";
33         int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
34         User user = User.getById(id);
35         String domainName = createUniqueName() + ".org";
36         Domain d = new Domain(user, domainName);
37         d.insert();
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://" + getServerName() + 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         d.insert();
60         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
61         uc.addRequestProperty("Cookie", cookie);
62         String csrf = getCSRF(uc, 0);
63
64         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
65         uc.addRequestProperty("Cookie", cookie);
66         uc.setDoOutput(true);
67         OutputStream os = uc.getOutputStream();
68         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
69                 + "process&domain=#" + d.getId()).getBytes("UTF-8"));
70         os.flush();
71         assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
72     }
73
74     @Test
75     public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
76         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
77         uc.addRequestProperty("Cookie", cookie);
78         String csrf = getCSRF(uc, 0);
79
80         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
81         uc.addRequestProperty("Cookie", cookie);
82         uc.setDoOutput(true);
83         OutputStream os = uc.getOutputStream();
84         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
85                 + "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8")).getBytes("UTF-8"));
86         os.flush();
87         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
88     }
89
90     @Test
91     public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
92         int id = (int) (Math.random() * 10000);
93         int count = 0;
94         boolean found = false;
95         try {
96             while (Domain.getById(id) != null && count < 20) {
97                 count++;
98                 id = (int) (Math.random() * 10000);
99             }
100         } catch (Exception e) {
101             found = true;
102         }
103         assumeTrue(found);
104         URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
105         uc.addRequestProperty("Cookie", cookie);
106         String csrf = getCSRF(uc, 0);
107         uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
108         uc.addRequestProperty("Cookie", cookie);
109         uc.setDoOutput(true);
110         OutputStream os = uc.getOutputStream();
111         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
112                 + "process&domain=#" + id).getBytes("UTF-8"));
113         os.flush();
114         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
115     }
116 }