1 package org.cacert.gigi.pages.admin;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.io.UnsupportedEncodingException;
9 import java.net.MalformedURLException;
11 import java.net.URLConnection;
12 import java.net.URLEncoder;
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;
24 public class TestSEAdminPageUserDomainSearch extends ClientTest {
26 public TestSEAdminPageUserDomainSearch() throws IOException {
27 grant(email, Group.SUPPORTER);
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);
38 URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
39 uc.addRequestProperty("Cookie", cookie);
40 String csrf = getCSRF(uc, 0);
42 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
43 uc.addRequestProperty("Cookie", cookie);
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"));
49 assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
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);
60 URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
61 uc.addRequestProperty("Cookie", cookie);
62 String csrf = getCSRF(uc, 0);
64 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
65 uc.addRequestProperty("Cookie", cookie);
67 OutputStream os = uc.getOutputStream();
68 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
69 + "process&domain=#" + d.getId()).getBytes("UTF-8"));
71 assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
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);
80 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
81 uc.addRequestProperty("Cookie", cookie);
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"));
87 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
91 public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
92 int id = (int) (Math.random() * 10000);
94 boolean found = false;
96 while (Domain.getById(id) != null && count < 20) {
98 id = (int) (Math.random() * 10000);
100 } catch (Exception e) {
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"));
114 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));