1 package org.cacert.gigi.pages.admin;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
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.dbObjects.Group;
15 import org.cacert.gigi.pages.admin.support.FindUserPage;
16 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
17 import org.cacert.gigi.testUtils.ClientTest;
18 import org.cacert.gigi.testUtils.IOUtils;
19 import org.junit.Test;
21 public class TestSEAdminPageUserMailSearch extends ClientTest {
23 public TestSEAdminPageUserMailSearch() throws IOException {
24 grant(email, Group.SUPPORTER);
28 public void testFulltextMailSearch() throws MalformedURLException, UnsupportedEncodingException, IOException {
29 String mail = createUniqueName() + "@example.com";
30 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
31 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
32 uc.addRequestProperty("Cookie", cookie);
33 String csrf = getCSRF(uc, 0);
35 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
36 uc.addRequestProperty("Cookie", cookie);
38 OutputStream os = uc.getOutputStream();
39 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
40 + "process&email=" + URLEncoder.encode(mail, "UTF-8")).getBytes("UTF-8"));
42 assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
46 public void testWildcardMailSearchSingle() throws MalformedURLException, UnsupportedEncodingException, IOException {
47 String mail = createUniqueName() + "@example.tld";
48 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
49 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
50 uc.addRequestProperty("Cookie", cookie);
51 String csrf = getCSRF(uc, 0);
53 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
54 uc.addRequestProperty("Cookie", cookie);
56 OutputStream os = uc.getOutputStream();
57 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
58 + "process&email=" + URLEncoder.encode("%@example.tld", "UTF-8")).getBytes("UTF-8"));
60 assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
64 public void testWildcardMailSearchMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
65 String mail = createUniqueName() + "@example.org";
66 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
67 String mail2 = createUniqueName() + "@example.org";
68 int id2 = createVerifiedUser("Först", "Secönd", mail2, TEST_PASSWORD);
69 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
70 uc.addRequestProperty("Cookie", cookie);
71 String csrf = getCSRF(uc, 0);
73 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
74 uc.addRequestProperty("Cookie", cookie);
76 OutputStream os = uc.getOutputStream();
77 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
78 + "process&email=" + URLEncoder.encode("%@example.org", "UTF-8")).getBytes("UTF-8"));
80 String res = IOUtils.readURL(uc);
81 assertThat(res, containsString(SupportUserDetailsPage.PATH + id));
82 assertThat(res, containsString(SupportUserDetailsPage.PATH + id2));
86 public void testWildcardMailSearchSingleChar() throws MalformedURLException, UnsupportedEncodingException, IOException {
87 String mail = createUniqueName() + "@example.org";
88 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
89 String mail2 = createUniqueName() + "@example.org";
90 int id2 = createVerifiedUser("Först", "Secönd", mail2, TEST_PASSWORD);
91 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
92 uc.addRequestProperty("Cookie", cookie);
93 String csrf = getCSRF(uc, 0);
95 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
96 uc.addRequestProperty("Cookie", cookie);
98 OutputStream os = uc.getOutputStream();
99 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
100 + "process&email=" + URLEncoder.encode("%@_xample.org", "UTF-8")).getBytes("UTF-8"));
102 String res = IOUtils.readURL(uc);
103 assertThat(res, containsString(SupportUserDetailsPage.PATH + id));
104 assertThat(res, containsString(SupportUserDetailsPage.PATH + id2));
108 public void testWildcardMailSearchNoRes() throws MalformedURLException, UnsupportedEncodingException, IOException {
109 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
110 uc.addRequestProperty("Cookie", cookie);
111 String csrf = getCSRF(uc, 0);
113 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
114 uc.addRequestProperty("Cookie", cookie);
115 uc.setDoOutput(true);
116 OutputStream os = uc.getOutputStream();
117 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
118 + "process&email=" + URLEncoder.encode("%@_humpfelkumpf.org", "UTF-8")).getBytes("UTF-8"));
120 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
124 public void testFulltextMailSearchNoRes() throws MalformedURLException, UnsupportedEncodingException, IOException {
125 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
126 uc.addRequestProperty("Cookie", cookie);
127 String csrf = getCSRF(uc, 0);
129 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
130 uc.addRequestProperty("Cookie", cookie);
131 uc.setDoOutput(true);
132 OutputStream os = uc.getOutputStream();
133 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
134 + "process&email=" + URLEncoder.encode(createUniqueName() + "@example.org", "UTF-8")).getBytes("UTF-8"));
136 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));