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.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;
26 public class TestSEAdminPageUserDomainSearch extends ClientTest {
28 public TestSEAdminPageUserDomainSearch() throws IOException {
29 grant(email, Group.SUPPORTER);
30 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
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, user, domainName);
40 URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
41 uc.addRequestProperty("Cookie", cookie);
42 String csrf = getCSRF(uc, 0);
44 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
45 uc.addRequestProperty("Cookie", cookie);
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"));
51 assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
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, user, domainName);
61 URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
62 uc.addRequestProperty("Cookie", cookie);
63 String csrf = getCSRF(uc, 0);
65 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
66 uc.addRequestProperty("Cookie", cookie);
68 OutputStream os = uc.getOutputStream();
69 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
70 + "process&domain=#" + d.getId()).getBytes("UTF-8"));
72 assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
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);
81 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
82 uc.addRequestProperty("Cookie", cookie);
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"));
88 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
92 public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
93 int id = (int) (Math.random() * 10000);
95 boolean found = false;
97 while (Domain.getById(id) != null && count < 20) {
99 id = (int) (Math.random() * 10000);
101 } catch (Exception e) {
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"));
115 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));