chg: enable support to find organisation domains
authorINOPIAE <m.maengel@inopiae.de>
Mon, 12 Feb 2018 07:35:18 +0000 (08:35 +0100)
committerINOPIAE <m.maengel@inopiae.de>
Fri, 16 Feb 2018 11:21:29 +0000 (12:21 +0100)
fixes issue #46

Change-Id: I5effef053020cfb440d8560c3252399657f33d96

src/club/wpia/gigi/Gigi.java
src/club/wpia/gigi/pages/admin/support/FindUserByDomainForm.java
src/club/wpia/gigi/pages/admin/support/SupportOrgDomainPage.java [new file with mode: 0644]
src/club/wpia/gigi/pages/admin/support/SupportOrgDomainPage.templ [new file with mode: 0644]
tests/club/wpia/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java

index 035f1a6..15a5214 100644 (file)
@@ -64,6 +64,7 @@ import club.wpia.gigi.pages.admin.support.FindCertPage;
 import club.wpia.gigi.pages.admin.support.FindUserByDomainPage;
 import club.wpia.gigi.pages.admin.support.FindUserByEmailPage;
 import club.wpia.gigi.pages.admin.support.SupportEnterTicketPage;
+import club.wpia.gigi.pages.admin.support.SupportOrgDomainPage;
 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
 import club.wpia.gigi.pages.error.AccessDenied;
 import club.wpia.gigi.pages.error.PageNotFound;
@@ -175,6 +176,7 @@ public final class Gigi extends HttpServlet {
 
             Menu account = createMenu("My Account");
             putPage(SupportUserDetailsPage.PATH + "*", new SupportUserDetailsPage(), null);
+            putPage(SupportOrgDomainPage.PATH + "*", new SupportOrgDomainPage(), null);
             putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), account);
             putPage(History.PATH, new History(false), account);
             putPage(FindAgentAccess.PATH, new OneFormPage("Access to Find Agent", FindAgentAccess.class), account);
index 75c8c59..500a3b0 100644 (file)
@@ -59,7 +59,7 @@ public class FindUserByDomainForm extends Form {
         if (res instanceof User) {
             return new RedirectResult(SupportUserDetailsPage.PATH + res.getId() + "/");
         } else if (res instanceof Organisation) {
-            return new RedirectResult("/support/domain/" + res.getId());
+            return new RedirectResult(SupportOrgDomainPage.PATH + d.getId());
         } else {
             throw new PermamentFormException(new GigiApiException("Unknown owner type."));
         }
diff --git a/src/club/wpia/gigi/pages/admin/support/SupportOrgDomainPage.java b/src/club/wpia/gigi/pages/admin/support/SupportOrgDomainPage.java
new file mode 100644 (file)
index 0000000..019f249
--- /dev/null
@@ -0,0 +1,59 @@
+package club.wpia.gigi.pages.admin.support;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import club.wpia.gigi.dbObjects.Domain;
+import club.wpia.gigi.dbObjects.Organisation;
+import club.wpia.gigi.pages.Page;
+import club.wpia.gigi.util.AuthorizationContext;
+
+public class SupportOrgDomainPage extends Page {
+
+    public static final String PATH = "/support/domain/";
+
+    public SupportOrgDomainPage() {
+        super("Support: Organisation Domain");
+    }
+
+    @Override
+    public boolean isPermitted(AuthorizationContext ac) {
+        return ac != null && ac.canSupport();
+    }
+
+    @Override
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+        Domain orgDomain = getDomain(req, resp);
+        if (orgDomain == null) {
+            return;
+        }
+
+        Organisation org = Organisation.getById(orgDomain.getOwner().getId());
+        Map<String, Object> vars = getDefaultVars(req);
+        vars.put("domain", orgDomain.getSuffix());
+        vars.put("organisation", org.getName());
+
+        getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
+    }
+
+    private Domain getDomain(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+        int id = -1;
+        String[] idP = req.getPathInfo().split("/");
+        try {
+            id = Integer.parseInt(idP[idP.length - 1]);
+        } catch (NumberFormatException e) {
+            resp.sendError(400);
+            return null;
+        }
+        final Domain domain = Domain.getById(id);
+        if (domain == null) {
+            resp.sendError(400);
+            return null;
+        }
+        return domain;
+    }
+
+}
diff --git a/src/club/wpia/gigi/pages/admin/support/SupportOrgDomainPage.templ b/src/club/wpia/gigi/pages/admin/support/SupportOrgDomainPage.templ
new file mode 100644 (file)
index 0000000..79f5e34
--- /dev/null
@@ -0,0 +1 @@
+<?=_The domain '${domain}' is linked to the organisation '${organisation}'.?>
index 383d232..d820a1d 100644 (file)
@@ -1,9 +1,11 @@
 package club.wpia.gigi.pages.admin;
 
+import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URLConnection;
 import java.net.URLEncoder;
@@ -13,9 +15,14 @@ import org.junit.Assume;
 import org.junit.Test;
 
 import club.wpia.gigi.GigiApiException;
+import club.wpia.gigi.dbObjects.Country;
+import club.wpia.gigi.dbObjects.Country.CountryCodeType;
 import club.wpia.gigi.dbObjects.Domain;
+import club.wpia.gigi.dbObjects.Group;
+import club.wpia.gigi.dbObjects.Organisation;
 import club.wpia.gigi.dbObjects.User;
 import club.wpia.gigi.pages.admin.support.FindUserByDomainPage;
+import club.wpia.gigi.pages.admin.support.SupportOrgDomainPage;
 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
 import club.wpia.gigi.testUtils.IOUtils;
 import club.wpia.gigi.testUtils.SEClientTest;
@@ -72,4 +79,38 @@ public class TestSEAdminPageUserDomainSearch extends SEClientTest {
         URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + id);
         assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
     }
+
+    @Test
+    public void testOrgDomainSearch() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
+        // generate organisation with domain
+        u.grantGroup(getSupporter(), Group.ORG_AGENT);
+        Organisation o1 = new Organisation(createUniqueName(), Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u);
+        String dom = createUniqueName() + ".de";
+        Domain d = new Domain(u, o1, dom);
+
+        // test
+        URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=" + URLEncoder.encode(dom, "UTF-8"));
+
+        assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + SupportOrgDomainPage.PATH + d.getId(), uc.getHeaderField("Location"));
+
+        String s = IOUtils.readURL(get(cookie, SupportOrgDomainPage.PATH + d.getId()));
+        assertThat(s, containsString(dom));
+        assertThat(s, containsString(o1.getName()));
+
+        // test malformated id
+        HttpURLConnection uc1 = get(SupportOrgDomainPage.PATH + d.getId() + "a");
+        assertEquals(400, uc1.getResponseCode());
+
+        // test non existing id
+        uc1 = get(SupportOrgDomainPage.PATH + "5000");
+        assertEquals(400, uc1.getResponseCode());
+
+    }
+
+    @Test
+    public void testDomainSearchByMalformatedId() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException {
+        URLConnection uc = post(FindUserByDomainPage.PATH, "process&domain=#" + d.getId() + "a");
+        assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc)));
+    }
+
 }