1 package club.wpia.gigi.pages.admin;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.io.UnsupportedEncodingException;
8 import java.net.HttpURLConnection;
9 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
13 import org.hamcrest.CoreMatchers;
14 import org.junit.Assume;
15 import org.junit.Test;
17 import club.wpia.gigi.GigiApiException;
18 import club.wpia.gigi.dbObjects.Country;
19 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
20 import club.wpia.gigi.dbObjects.Domain;
21 import club.wpia.gigi.dbObjects.Group;
22 import club.wpia.gigi.dbObjects.Organisation;
23 import club.wpia.gigi.dbObjects.User;
24 import club.wpia.gigi.pages.admin.support.FindUserByDomainPage;
25 import club.wpia.gigi.pages.admin.support.SupportOrgDomainPage;
26 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
27 import club.wpia.gigi.testUtils.IOUtils;
28 import club.wpia.gigi.testUtils.SEClientTest;
29 import club.wpia.gigi.util.ServerConstants;
30 import club.wpia.gigi.util.ServerConstants.Host;
32 public class TestSEAdminPageUserDomainSearch extends SEClientTest {
36 private String domainName;
38 private String unique;
42 public TestSEAdminPageUserDomainSearch() throws IOException, GigiApiException {
43 String mail = createUniqueName() + "@example.com";
44 tid = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
45 User user = User.getById(tid);
46 unique = createUniqueName();
47 domainName = unique + "pattern.org";
48 this.d = new Domain(user, user, domainName);
52 public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
53 URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(domainName, "UTF-8"));
55 assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + SupportUserDetailsPage.PATH + tid + "/", uc.getHeaderField("Location"));
59 public void testDomainSearchById() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
60 URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + d.getId());
61 assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + SupportUserDetailsPage.PATH + tid + "/", uc.getHeaderField("Location"));
65 public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
66 URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8"));
67 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
71 public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
72 int id = (int) (Math.random() * 10000);
74 while (Domain.getById(id) != null && count < 20) {
76 id = (int) (Math.random() * 10000);
78 Assume.assumeThat(Domain.getById(id), CoreMatchers.nullValue());
79 URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + id);
80 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
84 public void testOrgDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
85 // generate organisation with domain
86 u.grantGroup(getSupporter(), Group.ORG_AGENT);
87 Organisation o1 = new Organisation(createUniqueName(), Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u);
88 String dom = createUniqueName() + ".de";
89 Domain d = new Domain(u, o1, dom);
92 URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(dom, "UTF-8"));
94 assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + SupportOrgDomainPage.PATH + d.getId(), uc.getHeaderField("Location"));
96 String s = IOUtils.readURL(get(cookie, SupportOrgDomainPage.PATH + d.getId()));
97 assertThat(s, containsString(dom));
98 assertThat(s, containsString(o1.getName()));
100 // test malformated id
101 HttpURLConnection uc1 = get(SupportOrgDomainPage.PATH + d.getId() + "a");
102 assertEquals(400, uc1.getResponseCode());
104 // test non existing id
105 uc1 = get(SupportOrgDomainPage.PATH + "5000");
106 assertEquals(400, uc1.getResponseCode());
111 public void testDomainSearchByMalformatedId() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
112 URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + d.getId() + "a");
113 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));