]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java
add: defense-in-depth mechanism to prevent unauthorized adding of groups
[gigi.git] / tests / org / cacert / gigi / pages / admin / TestSEAdminPageUserDomainSearch.java
1 package org.cacert.gigi.pages.admin;
2
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
5
6 import java.io.IOException;
7 import java.io.UnsupportedEncodingException;
8 import java.net.MalformedURLException;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Domain;
14 import org.cacert.gigi.dbObjects.Group;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.pages.admin.support.FindUserByDomainPage;
17 import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage;
18 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
19 import org.cacert.gigi.testUtils.ClientTest;
20 import org.cacert.gigi.testUtils.IOUtils;
21 import org.cacert.gigi.util.ServerConstants;
22 import org.junit.Test;
23
24 public class TestSEAdminPageUserDomainSearch extends ClientTest {
25
26     private Domain d;
27
28     private String domainName;
29
30     private String unique;
31
32     private int tid;
33
34     public TestSEAdminPageUserDomainSearch() throws IOException, GigiApiException {
35         grant(u, Group.SUPPORTER);
36         cookie = login(email, TEST_PASSWORD);
37         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
38
39         String mail = createUniqueName() + "@example.com";
40         tid = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
41         User user = User.getById(tid);
42         unique = createUniqueName();
43         domainName = unique + "pattern.org";
44         this.d = new Domain(user, user, domainName);
45     }
46
47     @Test
48     public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
49         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(domainName, "UTF-8"));
50
51         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + tid + "/", uc.getHeaderField("Location"));
52     }
53
54     @Test
55     public void testDomainSearchById() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
56         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + d.getId());
57         assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + tid + "/", uc.getHeaderField("Location"));
58     }
59
60     @Test
61     public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
62         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8"));
63         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
64     }
65
66     @Test
67     public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
68         int id = (int) (Math.random() * 10000);
69         int count = 0;
70         boolean found = false;
71         try {
72             while (Domain.getById(id) != null && count < 20) {
73                 count++;
74                 id = (int) (Math.random() * 10000);
75             }
76         } catch (Exception e) {
77             found = true;
78         }
79         assumeTrue(found);
80         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + id);
81         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
82     }
83 }