From: Janis Streib Date: Sun, 22 Mar 2015 09:55:58 +0000 (+0100) Subject: ADD: More search test X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=d0d120359b0de6a8ce4349d4ebbdb76d33b99942;hp=9bbfdfd76710c8739b483f499e7744679171bca0 ADD: More search test --- diff --git a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java new file mode 100644 index 00000000..35e7bf74 --- /dev/null +++ b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java @@ -0,0 +1,116 @@ +package org.cacert.gigi.pages.admin; + +import static org.junit.Assert.*; +import static org.junit.Assume.*; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; + +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.Domain; +import org.cacert.gigi.dbObjects.Group; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.pages.admin.support.FindDomainPage; +import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage; +import org.cacert.gigi.testUtils.ClientTest; +import org.cacert.gigi.testUtils.IOUtils; +import org.junit.Test; + +public class TestSEAdminPageUserDomainSearch extends ClientTest { + + public TestSEAdminPageUserDomainSearch() throws IOException { + grant(email, Group.SUPPORTER); + } + + @Test + public void testDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException { + String mail = createUniqueName() + "@example.com"; + int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD); + User user = User.getById(id); + String domainName = createUniqueName() + ".org"; + Domain d = new Domain(user, domainName); + d.insert(); + URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + String csrf = getCSRF(uc, 0); + + uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + uc.setDoOutput(true); + OutputStream os = uc.getOutputStream(); + os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // + + "process&domain=" + URLEncoder.encode(domainName, "UTF-8")).getBytes("UTF-8")); + os.flush(); + assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location")); + } + + @Test + public void testDomainSearchById() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException { + String mail = createUniqueName() + "@example.com"; + int id = createVerifiedUser("Först", "Secönd", mail, TEST_PASSWORD); + User user = User.getById(id); + String domainName = createUniqueName() + ".org"; + Domain d = new Domain(user, domainName); + d.insert(); + URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + String csrf = getCSRF(uc, 0); + + uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + uc.setDoOutput(true); + OutputStream os = uc.getOutputStream(); + os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // + + "process&domain=#" + d.getId()).getBytes("UTF-8")); + os.flush(); + assertEquals("https://" + getServerName() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location")); + } + + @Test + public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException { + URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + String csrf = getCSRF(uc, 0); + + uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + uc.setDoOutput(true); + OutputStream os = uc.getOutputStream(); + os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // + + "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8")).getBytes("UTF-8")); + os.flush(); + assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc))); + } + + @Test + public void testDomainSearchByIdNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException { + int id = (int) (Math.random() * 10000); + int count = 0; + boolean found = false; + try { + while (Domain.getById(id) != null && count < 20) { + count++; + id = (int) (Math.random() * 10000); + } + } catch (Exception e) { + found = true; + } + assumeTrue(found); + URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + String csrf = getCSRF(uc, 0); + uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); + uc.addRequestProperty("Cookie", cookie); + uc.setDoOutput(true); + OutputStream os = uc.getOutputStream(); + os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // + + "process&domain=#" + id).getBytes("UTF-8")); + os.flush(); + assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc))); + } +}