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.cacert.gigi.util.ServerConstants;
20 import org.junit.Test;
22 public class TestSEAdminPageUserMailSearch extends ClientTest {
24 public TestSEAdminPageUserMailSearch() throws IOException {
25 grant(email, Group.SUPPORTER);
29 public void testFulltextMailSearch() throws MalformedURLException, UnsupportedEncodingException, IOException {
30 String mail = createUniqueName() + "@example.com";
31 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
32 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
33 uc.addRequestProperty("Cookie", cookie);
34 String csrf = getCSRF(uc, 0);
36 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
37 uc.addRequestProperty("Cookie", cookie);
39 OutputStream os = uc.getOutputStream();
40 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
41 + "process&email=" + URLEncoder.encode(mail, "UTF-8")).getBytes("UTF-8"));
43 assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
47 public void testWildcardMailSearchSingle() throws MalformedURLException, UnsupportedEncodingException, IOException {
48 String mail = createUniqueName() + "@example.tld";
49 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
50 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
51 uc.addRequestProperty("Cookie", cookie);
52 String csrf = getCSRF(uc, 0);
54 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
55 uc.addRequestProperty("Cookie", cookie);
57 OutputStream os = uc.getOutputStream();
58 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
59 + "process&email=" + URLEncoder.encode("%@example.tld", "UTF-8")).getBytes("UTF-8"));
61 assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location"));
65 public void testWildcardMailSearchMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
66 String mail = createUniqueName() + "@example.org";
67 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
68 String mail2 = createUniqueName() + "@example.org";
69 int id2 = createVerifiedUser("Först", "Secönd", mail2, TEST_PASSWORD);
70 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
71 uc.addRequestProperty("Cookie", cookie);
72 String csrf = getCSRF(uc, 0);
74 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
75 uc.addRequestProperty("Cookie", cookie);
77 OutputStream os = uc.getOutputStream();
78 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
79 + "process&email=" + URLEncoder.encode("%@example.org", "UTF-8")).getBytes("UTF-8"));
81 String res = IOUtils.readURL(uc);
82 assertThat(res, containsString(SupportUserDetailsPage.PATH + id));
83 assertThat(res, containsString(SupportUserDetailsPage.PATH + id2));
87 public void testWildcardMailSearchSingleChar() throws MalformedURLException, UnsupportedEncodingException, IOException {
88 String mail = createUniqueName() + "@example.org";
89 int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD);
90 String mail2 = createUniqueName() + "@example.org";
91 int id2 = createVerifiedUser("Först", "Secönd", mail2, TEST_PASSWORD);
92 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
93 uc.addRequestProperty("Cookie", cookie);
94 String csrf = getCSRF(uc, 0);
96 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
97 uc.addRequestProperty("Cookie", cookie);
99 OutputStream os = uc.getOutputStream();
100 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
101 + "process&email=" + URLEncoder.encode("%@_xample.org", "UTF-8")).getBytes("UTF-8"));
103 String res = IOUtils.readURL(uc);
104 assertThat(res, containsString(SupportUserDetailsPage.PATH + id));
105 assertThat(res, containsString(SupportUserDetailsPage.PATH + id2));
109 public void testWildcardMailSearchNoRes() throws MalformedURLException, UnsupportedEncodingException, IOException {
110 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
111 uc.addRequestProperty("Cookie", cookie);
112 String csrf = getCSRF(uc, 0);
114 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
115 uc.addRequestProperty("Cookie", cookie);
116 uc.setDoOutput(true);
117 OutputStream os = uc.getOutputStream();
118 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
119 + "process&email=" + URLEncoder.encode("%@_humpfelkumpf.org", "UTF-8")).getBytes("UTF-8"));
121 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
125 public void testFulltextMailSearchNoRes() throws MalformedURLException, UnsupportedEncodingException, IOException {
126 URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
127 uc.addRequestProperty("Cookie", cookie);
128 String csrf = getCSRF(uc, 0);
130 uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection();
131 uc.addRequestProperty("Cookie", cookie);
132 uc.setDoOutput(true);
133 OutputStream os = uc.getOutputStream();
134 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
135 + "process&email=" + URLEncoder.encode(createUniqueName() + "@example.org", "UTF-8")).getBytes("UTF-8"));
137 assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));