]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java
add: ensure that for support actions certificate login is used
[gigi.git] / tests / club / wpia / gigi / pages / admin / TestSEAdminPageUserDomainSearch.java
1 package club.wpia.gigi.pages.admin;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
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;
12
13 import org.hamcrest.CoreMatchers;
14 import org.junit.Assume;
15 import org.junit.Test;
16
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;
31
32 public class TestSEAdminPageUserDomainSearch extends SEClientTest {
33
34     private Domain d;
35
36     private String domainName;
37
38     private String unique;
39
40     private int tid;
41
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);
49     }
50
51     @Test
52     public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
53         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(domainName, "UTF-8"));
54
55         assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.SECURE) + SupportUserDetailsPage.PATH + tid + "/", uc.getHeaderField("Location"));
56     }
57
58     @Test
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.SECURE) + SupportUserDetailsPage.PATH + tid + "/", uc.getHeaderField("Location"));
62     }
63
64     @Test
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)));
68     }
69
70     @Test
71     public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
72         int id = (int) (Math.random() * 10000);
73         int count = 0;
74         while (Domain.getById(id) != null && count < 20) {
75             count++;
76             id = (int) (Math.random() * 10000);
77         }
78         Assume.assumeThat(Domain.getById(id), CoreMatchers.nullValue());
79         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + id);
80         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
81     }
82
83     @Test
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);
90
91         // test
92         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(dom, "UTF-8"));
93
94         assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.SECURE) + SupportOrgDomainPage.PATH + d.getId(), uc.getHeaderField("Location"));
95
96         String s = IOUtils.readURL(get(cookie, SupportOrgDomainPage.PATH + d.getId()));
97         assertThat(s, containsString(dom));
98         assertThat(s, containsString(o1.getName()));
99
100         // test malformated id
101         HttpURLConnection uc1 = get(SupportOrgDomainPage.PATH + d.getId() + "a");
102         assertEquals(400, uc1.getResponseCode());
103
104         // test non existing id
105         uc1 = get(SupportOrgDomainPage.PATH + "5000");
106         assertEquals(400, uc1.getResponseCode());
107
108     }
109
110     @Test
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)));
114     }
115
116 }