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.cacert.gigi.util.ServerConstants;
23 import org.junit.Test;
25 public class TestSEAdminPageUserDomainSearch extends ClientTest {
27 public TestSEAdminPageUserDomainSearch() throws IOException {
28 grant(email, Group.SUPPORTER);
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);
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://" + ServerConstants.getWwwHostNamePortSecure() + 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);
59 URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
60 uc.addRequestProperty("Cookie", cookie);
61 String csrf = getCSRF(uc, 0);
63 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
64 uc.addRequestProperty("Cookie", cookie);
66 OutputStream os = uc.getOutputStream();
67 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
68 + "process&domain=#" + d.getId()).getBytes("UTF-8"));
70 assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
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);
79 uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection();
80 uc.addRequestProperty("Cookie", cookie);
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"));
86 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
90 public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
91 int id = (int) (Math.random() * 10000);
93 boolean found = false;
95 while (Domain.getById(id) != null && count < 20) {
97 id = (int) (Math.random() * 10000);
99 } catch (Exception e) {
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"));
113 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));