fix: move switch to organisation context to separate page
authorINOPIAE <m.maengel@inopiae.de>
Tue, 30 Jan 2018 06:00:03 +0000 (07:00 +0100)
committerINOPIAE <m.maengel@inopiae.de>
Tue, 6 Feb 2018 04:25:55 +0000 (05:25 +0100)
fixes issue #125

Change-Id: Id70d645e720cc43a0b28fc5c7355ba3492495d59

src/club/wpia/gigi/Gigi.java
src/club/wpia/gigi/pages/account/MyDetails.java
src/club/wpia/gigi/pages/account/MyOrganisationsForm.templ [deleted file]
src/club/wpia/gigi/pages/orga/CreateOrgPage.java
src/club/wpia/gigi/pages/orga/MyOrganisationsForm.java [moved from src/club/wpia/gigi/pages/account/MyOrganisationsForm.java with 94% similarity]
src/club/wpia/gigi/pages/orga/MyOrganisationsForm.templ [new file with mode: 0644]
src/club/wpia/gigi/pages/orga/SwitchOrganisation.java [new file with mode: 0644]
tests/club/wpia/gigi/pages/orga/TestOrgSwitch.java [new file with mode: 0644]

index 6d8996c..035f1a6 100644 (file)
@@ -71,6 +71,7 @@ import club.wpia.gigi.pages.main.CertStatusRequestPage;
 import club.wpia.gigi.pages.main.KeyCompromisePage;
 import club.wpia.gigi.pages.main.RegisterPage;
 import club.wpia.gigi.pages.orga.CreateOrgPage;
+import club.wpia.gigi.pages.orga.SwitchOrganisation;
 import club.wpia.gigi.pages.orga.ViewOrgPage;
 import club.wpia.gigi.pages.statistics.StatisticsRoles;
 import club.wpia.gigi.pages.wot.Points;
@@ -164,6 +165,7 @@ public final class Gigi extends HttpServlet {
             putPage(TTPAdminPage.PATH + "/*", new TTPAdminPage(), admMenu);
             putPage(CreateOrgPage.DEFAULT_PATH, new CreateOrgPage(), orgAdm);
             putPage(ViewOrgPage.DEFAULT_PATH + "/*", new ViewOrgPage(), orgAdm);
+            putPage(SwitchOrganisation.PATH, new SwitchOrganisation(), orgAdm);
 
             Menu support = createMenu("Support Console");
             putPage(SupportEnterTicketPage.PATH, new SupportEnterTicketPage(), support);
index 12d127f..b4a52d4 100644 (file)
@@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletResponse;
 import club.wpia.gigi.output.template.Form;
 import club.wpia.gigi.pages.LoginPage;
 import club.wpia.gigi.pages.Page;
+import club.wpia.gigi.pages.orga.MyOrganisationsForm;
 
 public class MyDetails extends Page {
 
diff --git a/src/club/wpia/gigi/pages/account/MyOrganisationsForm.templ b/src/club/wpia/gigi/pages/account/MyOrganisationsForm.templ
deleted file mode 100644 (file)
index 96d7bbe..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<input type='hidden' name='orgaForm' value='orga'/>
-<h2><?=_My Organisations?></h2>
-<table class="table">
-<? foreach($orgas) { ?>
-<tr><td><?=$orgName?></td><td><?=$orgID?></td><td><input class="btn btn-info" type='submit' value='<?=_switch to this organisation?>' name='org:<?=$orgID?>'/></td></tr>
-<? } ?>
-</table>
-<? if($personal) { ?>
-<input class="btn btn-primary" type='submit' value='<?=_switch back to personal use?>' name='org-leave'/>
-<? } ?>
index 4eab3d6..98076fe 100644 (file)
@@ -1,7 +1,6 @@
 package club.wpia.gigi.pages.orga;
 
 import java.io.IOException;
-import java.util.HashMap;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -27,6 +26,6 @@ public class CreateOrgPage extends ManagedFormPage {
 
     @Override
     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-        new CreateOrgForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
+        new CreateOrgForm(req).output(resp.getWriter(), getLanguage(req), getDefaultVars(req));
     }
 }
@@ -1,4 +1,4 @@
-package club.wpia.gigi.pages.account;
+package club.wpia.gigi.pages.orga;
 
 import java.io.PrintWriter;
 import java.util.Enumeration;
@@ -33,7 +33,7 @@ public class MyOrganisationsForm extends Form {
     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
         if (req.getParameter("org-leave") != null) {
             req.getSession().setAttribute(Gigi.AUTH_CONTEXT, new AuthorizationContext(target.getActor(), target.getActor()));
-            return new RedirectResult(MyDetails.PATH);
+            return new RedirectResult(SwitchOrganisation.PATH);
         }
         Enumeration<String> i = req.getParameterNames();
         int orgId = -1;
@@ -52,7 +52,7 @@ public class MyOrganisationsForm extends Form {
             if (org.getId() == orgId) {
 
                 req.getSession().setAttribute(Gigi.AUTH_CONTEXT, new AuthorizationContext(org, target.getActor()));
-                return new RedirectResult(MyDetails.PATH);
+                return new RedirectResult(SwitchOrganisation.PATH);
             }
         }
         throw new PermamentFormException(new GigiApiException("Context switch failed."));
diff --git a/src/club/wpia/gigi/pages/orga/MyOrganisationsForm.templ b/src/club/wpia/gigi/pages/orga/MyOrganisationsForm.templ
new file mode 100644 (file)
index 0000000..5c63f04
--- /dev/null
@@ -0,0 +1,9 @@
+<h2><?=_My Organisations?></h2>
+<? if($personal) { ?>
+<button class="btn btn-primary" type='submit' value='personal' name='org-leave'/><?=_Switch back to personal context?></button>
+<? } ?>
+<table class="table">
+<? foreach($orgas) { ?>
+<tr><td><?=$orgName?></td><td><?=$orgID?></td><td><button class="btn btn-info" type='submit' value='y' name='org:<?=$orgID?>'/><?=_Switch to this organisation?></button></td></tr>
+<? } ?>
+</table>
diff --git a/src/club/wpia/gigi/pages/orga/SwitchOrganisation.java b/src/club/wpia/gigi/pages/orga/SwitchOrganisation.java
new file mode 100644 (file)
index 0000000..16dfe6e
--- /dev/null
@@ -0,0 +1,29 @@
+package club.wpia.gigi.pages.orga;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import club.wpia.gigi.pages.ManagedFormPage;
+import club.wpia.gigi.util.AuthorizationContext;
+
+public class SwitchOrganisation extends ManagedFormPage {
+
+    public static final String PATH = "/orga/switch-orga";
+
+    public SwitchOrganisation() {
+        super("Switch to Organisation", MyOrganisationsForm.class);
+    }
+
+    @Override
+    public boolean isPermitted(AuthorizationContext ac) {
+        return ac != null && ac.getActor().getOrganisations().size() != 0;
+    }
+
+    @Override
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+        new MyOrganisationsForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
+    }
+}
diff --git a/tests/club/wpia/gigi/pages/orga/TestOrgSwitch.java b/tests/club/wpia/gigi/pages/orga/TestOrgSwitch.java
new file mode 100644 (file)
index 0000000..94586e3
--- /dev/null
@@ -0,0 +1,121 @@
+package club.wpia.gigi.pages.orga;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.sql.SQLException;
+
+import org.junit.After;
+import org.junit.Test;
+
+import club.wpia.gigi.GigiApiException;
+import club.wpia.gigi.dbObjects.Organisation;
+import club.wpia.gigi.dbObjects.User;
+import club.wpia.gigi.testUtils.IOUtils;
+import club.wpia.gigi.testUtils.OrgTest;
+
+public class TestOrgSwitch extends OrgTest {
+
+    private User u2;
+
+    private Organisation org1 = createUniqueOrg();
+
+    private Organisation org2 = createUniqueOrg();
+
+    public TestOrgSwitch() throws IOException, GigiApiException {
+
+        assertEquals(403, get(SwitchOrganisation.PATH).getResponseCode());
+
+        String email = createUniqueName() + "@testdom.com";
+        u2 = User.getById(createVerificationUser("testworker", "testname", email, TEST_PASSWORD));
+        assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + org1.getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
+        assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + org2.getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
+
+        // login with new user u2
+        cookie = login(email, TEST_PASSWORD);
+    }
+
+    @After
+    public void purgeDbAfterTest() throws SQLException, IOException {
+        purgeDatabase();
+    }
+
+    @Test
+    public void testSwitchToOrg() throws IOException, GigiApiException {
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
+
+        String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
+        assertThat(res, containsString("Logged in as " + org1.getName() + " (on behalf of " + u2.getPreferredName()));
+
+    }
+
+    @Test
+    public void testSwitchToNonOrg() throws IOException, GigiApiException {
+
+        String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
+        assertThat(res, containsString("Context switch failed"));
+
+    }
+
+    @Test
+    public void testSwitchToPersonal() throws IOException, GigiApiException {
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
+
+        String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
+        assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
+
+        res = IOUtils.readURL(get(SwitchOrganisation.PATH));
+        assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
+
+    }
+
+    @Test
+    public void testSwitchOrgToOrg() throws IOException, GigiApiException {
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org2.getId() + "=y", 0));
+
+        String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
+        assertThat(res, containsString("Logged in as " + org2.getName() + " (on behalf of " + u2.getPreferredName()));
+
+    }
+
+    @Test
+    public void testSwitchOrgToSameOrg() throws IOException, GigiApiException {
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
+
+        String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
+        assertThat(res, containsString("Logged in as " + org1.getName() + " (on behalf of " + u2.getPreferredName()));
+
+    }
+
+    @Test
+    public void testSwitchOrgToNonOrg() throws IOException, GigiApiException {
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
+        String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
+        assertThat(res, containsString("Context switch failed"));
+
+    }
+
+    @Test
+    public void testSwitchOrgToPersonal() throws IOException, GigiApiException {
+
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
+        assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
+
+        String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
+        assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
+
+    }
+
+}