[roleInheritance] Add test cases for table access
authorCarlos Aristu <carlos.aristu@openbravo.com>
Wed, 30 Sep 2015 10:19:00 +0200
changeset 27781 a24894d931bd
parent 27780 0eca808004b9
child 27782 1e5a95337267
[roleInheritance] Add test cases for table access
Also, it fixes some problems when removing accesses on the tests
src-test/src/org/openbravo/test/role/AccessPropagationNotInheritedTest.java
src-test/src/org/openbravo/test/role/AccessPropagationTest.java
src-test/src/org/openbravo/test/role/DeletedAccessPropagation.java
src-test/src/org/openbravo/test/role/HorizontalInheritanceTest.java
src-test/src/org/openbravo/test/role/RecalculatePermissionsTest.java
src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java
src-test/src/org/openbravo/test/role/VerticalInheritanceTest.java
--- a/src-test/src/org/openbravo/test/role/AccessPropagationNotInheritedTest.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/AccessPropagationNotInheritedTest.java	Wed Sep 30 10:19:00 2015 +0200
@@ -55,12 +55,13 @@
   private final List<String> WIDGETS = Arrays.asList("Best Sellers");
   private final List<String> VIEWS = Arrays.asList("OBUIAPP_AlertManagement");
   private final List<String> PROCESSES = Arrays.asList("Create Purchase Order Lines");
+  private final List<String> TABLES = Arrays.asList("C_Order");
   private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo");
   private final List<String> PREFERENCES = Arrays.asList("AllowAttachment");
 
   @SuppressWarnings("unchecked")
   private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
-      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, TABLES, ALERTS, PREFERENCES);
   private static int testCounter = 0;
 
   /** defines the values the parameter will take. */
@@ -99,6 +100,10 @@
       RoleInheritanceTestUtils.addAccess(parameter, role, accesses.get(0));
       RoleInheritanceTestUtils.addAccess(parameter, template, accesses.get(0));
 
+      OBDal.getInstance().commitAndClose();
+      role = OBDal.getInstance().get(Role.class, roleId);
+      template = OBDal.getInstance().get(Role.class, templateId);
+
       String[] expected = { accesses.get(0), "" };
       String[] result = RoleInheritanceTestUtils.getOrderedAccessNames(parameter, role);
       assertThat("New access has not affected non inherited access", result, equalTo(expected));
@@ -128,8 +133,14 @@
       assertThat("Updated access has not affected non inherited access", result2,
           equalTo(expected2));
 
+      // NOTE: At this point, if we remove the access of "role", it will not inherit automatically
+      // the access from the template because is not possible to handle the removal and the insert
+      // of the record at the same time inside the event handler. So, in this case the user should
+      // call the "Recalculate Process" after removing the non inherited access.
+
       RoleInheritanceTestUtils.removeAccesses(parameter, template);
       RoleInheritanceTestUtils.removeAccesses(parameter, role);
+      OBDal.getInstance().flush();
       testCounter++;
 
     } finally {
--- a/src-test/src/org/openbravo/test/role/AccessPropagationTest.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/AccessPropagationTest.java	Wed Sep 30 10:19:00 2015 +0200
@@ -58,13 +58,14 @@
       "OBUIAPP_RegistrationView");
   private final List<String> PROCESSES = Arrays.asList("Create Purchase Order Lines",
       "Grant Portal Access");
+  private final List<String> TABLES = Arrays.asList("AD_User", "C_Order");
   private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo",
       "CUSTOMER WITHOUT ACCOUNTING");
   private final List<String> PREFERENCES = Arrays.asList("AllowAttachment", "AllowDelete");
 
   @SuppressWarnings("unchecked")
   private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
-      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, TABLES, ALERTS, PREFERENCES);
   private static int testCounter = 0;
 
   /** defines the values the parameter will take. */
@@ -103,6 +104,10 @@
       RoleInheritanceTestUtils.addAccess(parameter, template, accesses.get(0));
       RoleInheritanceTestUtils.addAccess(parameter, role, accesses.get(1));
 
+      OBDal.getInstance().commitAndClose();
+      role = OBDal.getInstance().get(Role.class, roleId);
+      template = OBDal.getInstance().get(Role.class, templateId);
+
       String[] expected = { accesses.get(0), templateId, accesses.get(1), "" };
       String[] result = RoleInheritanceTestUtils.getOrderedAccessNames(parameter, role);
       assertThat("New access has been propagated", result, equalTo(expected));
@@ -144,6 +149,7 @@
 
       RoleInheritanceTestUtils.removeAccesses(parameter, template);
       RoleInheritanceTestUtils.removeAccesses(parameter, role);
+      OBDal.getInstance().flush();
       testCounter++;
 
     } finally {
--- a/src-test/src/org/openbravo/test/role/DeletedAccessPropagation.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/DeletedAccessPropagation.java	Wed Sep 30 10:19:00 2015 +0200
@@ -58,13 +58,14 @@
       "OBUIAPP_RegistrationView");
   private final List<String> PROCESSES = Arrays.asList("Create Purchase Order Lines",
       "Grant Portal Access");
+  private final List<String> TABLES = Arrays.asList("AD_User", "C_Order");
   private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo",
       "CUSTOMER WITHOUT ACCOUNTING");
   private final List<String> PREFERENCES = Arrays.asList("AllowAttachment", "AllowDelete");
 
   @SuppressWarnings("unchecked")
   private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
-      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, TABLES, ALERTS, PREFERENCES);
   private static int testCounter = 0;
 
   /** defines the values the parameter will take. */
--- a/src-test/src/org/openbravo/test/role/HorizontalInheritanceTest.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/HorizontalInheritanceTest.java	Wed Sep 30 10:19:00 2015 +0200
@@ -65,6 +65,8 @@
   private final List<String> PROCESSES = Arrays
       .asList("Create Purchase Order Lines", "Grant Portal Access", "Manage Variants",
           "Modify Payment In Plan", "Process Cost Adjustment");
+  private final List<String> TABLES = Arrays.asList("A_Amortization", "AD_User", "C_Order",
+      "FIN_Payment", "M_Warehouse");
   private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo",
       "CUSTOMER WITHOUT ACCOUNTING", "Process Execution Failed", "Updates Available",
       "Wrong Purchase Order Payment Plan");
@@ -73,7 +75,7 @@
 
   @SuppressWarnings("unchecked")
   private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
-      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, TABLES, ALERTS, PREFERENCES);
   private static int testCounter = 0;
 
   /** defines the values the parameter will take. */
@@ -182,6 +184,8 @@
       RoleInheritanceTestUtils.removeAccesses(parameter, template1);
       RoleInheritanceTestUtils.removeAccesses(parameter, template2);
       RoleInheritanceTestUtils.removeAccesses(parameter, template3);
+      RoleInheritanceTestUtils.removeAccesses(parameter, inheritedRole);
+      OBDal.getInstance().flush();
 
       testCounter++;
 
--- a/src-test/src/org/openbravo/test/role/RecalculatePermissionsTest.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/RecalculatePermissionsTest.java	Wed Sep 30 10:19:00 2015 +0200
@@ -31,9 +31,9 @@
 
 public class RecalculatePermissionsTest extends OBBaseTest {
   // This test case is intended to simulate the "Recalculate Permissions" process
-  // We make use a extension of the OBBaseTest to avoid the execution of the related event handlers
-  // This way, we can simulate the process of adding of a permission without using DAL, for example,
-  // like when using the Grant Access process which is based on a DB stored procedure
+  // We make use of a extension of the OBBaseTest to avoid the execution of the related event
+  // handlers. This way, we can simulate the process of adding a permission without using DAL, like
+  // for example, when using the "Grant Access" process which uses xsql to insert data.
 
   @Test
   public void testRolePermissionRecalculate() {
--- a/src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java	Wed Sep 30 10:19:00 2015 +0200
@@ -37,9 +37,11 @@
 import org.openbravo.model.ad.access.RoleInheritance;
 import org.openbravo.model.ad.access.RoleOrganization;
 import org.openbravo.model.ad.access.TabAccess;
+import org.openbravo.model.ad.access.TableAccess;
 import org.openbravo.model.ad.access.WindowAccess;
 import org.openbravo.model.ad.alert.AlertRecipient;
 import org.openbravo.model.ad.alert.AlertRule;
+import org.openbravo.model.ad.datamodel.Table;
 import org.openbravo.model.ad.domain.Preference;
 import org.openbravo.model.ad.system.Client;
 import org.openbravo.model.ad.ui.Field;
@@ -53,7 +55,7 @@
   public final static String CLIENT_ID = "23C59575B9CF467C9620760EB255B389";
   public final static String ASTERISK_ORG_ID = "0";
   public final static List<String> ACCESS_NAMES = Arrays.asList("ORGANIZATION", "WINDOW", "TAB",
-      "FIELD", "REPORT", "FORM", "WIDGET", "VIEW", "PROCESS", "ALERT", "PREFERENCE");
+      "FIELD", "REPORT", "FORM", "WIDGET", "VIEW", "PROCESS", "TABLE", "ALERT", "PREFERENCE");
 
   public static Role createRole(String name, String clientId, String organizationId,
       String userLevel, boolean isManual, boolean isTemplate) {
@@ -120,6 +122,8 @@
       addViewImplementationAccess(role, accessName);
     } else if ("PROCESS".equals(type)) {
       addProcessDefinitionAccess(role, accessName);
+    } else if ("TABLE".equals(type)) {
+      addTableAccess(role, accessName, true);
     } else if ("ALERT".equals(type)) {
       addAlertRecipient(role, accessName);
     } else if ("PREFERENCE".equals(type)) {
@@ -148,6 +152,8 @@
       removeViewImplementationAccesses(role);
     } else if ("PROCESS".equals(type)) {
       removeProcessDefinitionAccesses(role);
+    } else if ("TABLE".equals(type)) {
+      removeTableAccesses(role);
     } else if ("ALERT".equals(type)) {
       removeAlertRecipients(role);
     } else if ("PREFERENCE".equals(type)) {
@@ -178,6 +184,8 @@
       updateViewImplementationAccess(role, accessName, isActive);
     } else if ("PROCESS".equals(type)) {
       updateProcessDefinitionAccess(role, accessName, isActive);
+    } else if ("TABLE".equals(type)) {
+      updateTableAccess(role, accessName, editedValue, isActive);
     } else if ("ALERT".equals(type)) {
       updateAlertRecipientAccess(role, accessName, editedValue, isActive);
     } else if ("PREFERENCE".equals(type)) {
@@ -206,6 +214,8 @@
       return getViewImplementationAccessInfo(role, accessName);
     } else if ("PROCESS".equals(type)) {
       return getProcessDefinitonAccessInfo(role, accessName);
+    } else if ("TABLE".equals(type)) {
+      return getTableAccessInfo(role, accessName);
     } else if ("ALERT".equals(type)) {
       return getAlertRecipientAccessInfo(role, accessName);
     } else if ("PREFERENCE".equals(type)) {
@@ -253,9 +263,9 @@
     obCriteria.add(Restrictions.eq(RoleOrganization.PROPERTY_ROLE, role));
     obCriteria.setFilterOnActive(false);
     for (RoleOrganization ro : obCriteria.list()) {
+      role.getADRoleOrganizationList().remove(ro);
       OBDal.getInstance().remove(ro);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getOrgAccessInfo(Role role, String orgName) {
@@ -309,9 +319,9 @@
     obCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
     obCriteria.setFilterOnActive(false);
     for (WindowAccess wa : obCriteria.list()) {
+      role.getADWindowAccessList().remove(wa);
       OBDal.getInstance().remove(wa);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getWindowAccessInfo(Role role, String windowName) {
@@ -414,7 +424,6 @@
       wa.getADTabAccessList().remove(ta);
       OBDal.getInstance().remove(ta);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getTabAccessInfo(Role role, String windowName, String tabName) {
@@ -563,7 +572,6 @@
       ta.getADFieldAccessList().remove(fa);
       OBDal.getInstance().remove(fa);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getFieldAccessInfo(Role role, String windowName, String tabName,
@@ -627,9 +635,9 @@
         .add(Restrictions.eq(org.openbravo.model.ad.access.ProcessAccess.PROPERTY_ROLE, role));
     obCriteria.setFilterOnActive(false);
     for (org.openbravo.model.ad.access.ProcessAccess pa : obCriteria.list()) {
+      role.getADProcessAccessList().remove(pa);
       OBDal.getInstance().remove(pa);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getReportAndProcessAccessInfo(Role role, String reportName) {
@@ -678,9 +686,9 @@
     obCriteria.add(Restrictions.eq(FormAccess.PROPERTY_ROLE, role));
     obCriteria.setFilterOnActive(false);
     for (FormAccess fa : obCriteria.list()) {
+      role.getADFormAccessList().remove(fa);
       OBDal.getInstance().remove(fa);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getFormAccessInfo(Role role, String formName) {
@@ -732,9 +740,9 @@
     obCriteria.add(Restrictions.eq(WidgetClassAccess.PROPERTY_ROLE, role));
     obCriteria.setFilterOnActive(false);
     for (WidgetClassAccess wa : obCriteria.list()) {
+      role.getOBKMOWidgetClassAccessList().remove(wa);
       OBDal.getInstance().remove(wa);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getWidgetAccessInfo(Role role, String widgetTitle) {
@@ -787,9 +795,9 @@
     obCriteria.add(Restrictions.eq(ViewRoleAccess.PROPERTY_ROLE, role));
     obCriteria.setFilterOnActive(false);
     for (ViewRoleAccess va : obCriteria.list()) {
+      role.getObuiappViewRoleAccessList().remove(va);
       OBDal.getInstance().remove(va);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getViewImplementationAccessInfo(Role role, String viewName) {
@@ -849,9 +857,9 @@
         role));
     obCriteria.setFilterOnActive(false);
     for (org.openbravo.client.application.ProcessAccess pa : obCriteria.list()) {
+      role.getOBUIAPPProcessAccessList().remove(pa);
       OBDal.getInstance().remove(pa);
     }
-    OBDal.getInstance().flush();
   }
 
   private static String[] getProcessDefinitonAccessInfo(Role role, String processName) {
@@ -867,6 +875,61 @@
     return result;
   }
 
+  private static void addTableAccess(Role role, String tableName, boolean isReadOnly) {
+    final TableAccess tableAccess = OBProvider.getInstance().get(TableAccess.class);
+    final OBCriteria<Table> obCriteria = OBDal.getInstance().createCriteria(Table.class);
+    obCriteria.add(Restrictions.eq(Table.PROPERTY_DBTABLENAME, tableName));
+    obCriteria.setMaxResults(1);
+    tableAccess.setClient(role.getClient());
+    tableAccess.setOrganization(role.getOrganization());
+    tableAccess.setRole(role);
+    tableAccess.setTable((Table) obCriteria.uniqueResult());
+    tableAccess.setReadOnly(isReadOnly);
+    OBDal.getInstance().save(tableAccess);
+    OBDal.getInstance().flush();
+    OBDal.getInstance().refresh(role);
+  }
+
+  private static void updateTableAccess(Role role, String tableName, boolean isReadOnly,
+      boolean isActive) {
+    final OBCriteria<Table> tableCriteria = OBDal.getInstance().createCriteria(Table.class);
+    tableCriteria.add(Restrictions.eq(Table.PROPERTY_DBTABLENAME, tableName));
+    tableCriteria.setMaxResults(1);
+    final OBCriteria<TableAccess> tableAccessCriteria = OBDal.getInstance().createCriteria(
+        TableAccess.class);
+    tableAccessCriteria.add(Restrictions.eq(TableAccess.PROPERTY_ROLE, role));
+    tableAccessCriteria.add(Restrictions.eq(TableAccess.PROPERTY_TABLE,
+        (Table) tableCriteria.uniqueResult()));
+    tableAccessCriteria.setMaxResults(1);
+    TableAccess ta = (TableAccess) tableAccessCriteria.uniqueResult();
+    ta.setReadOnly(isReadOnly);
+    ta.setActive(isActive);
+  }
+
+  private static String[] getTableAccessInfo(Role role, String tableName) {
+    String result[] = new String[3];
+    for (TableAccess ta : role.getADTableAccessList()) {
+      if (tableName.equals(ta.getTable().getDBTableName())) {
+        result[0] = ta.isReadOnly().toString();
+        result[1] = ta.isActive().toString();
+        result[2] = ta.getInheritedFrom() != null ? ta.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
+  private static void removeTableAccesses(Role role) {
+    final OBCriteria<TableAccess> obCriteria = OBDal.getInstance()
+        .createCriteria(TableAccess.class);
+    obCriteria.add(Restrictions.eq(TableAccess.PROPERTY_ROLE, role));
+    obCriteria.setFilterOnActive(false);
+    for (TableAccess ta : obCriteria.list()) {
+      role.getADTableAccessList().remove(ta);
+      OBDal.getInstance().remove(ta);
+    }
+  }
+
   private static void addAlertRecipient(Role role, String alertName) {
     final AlertRecipient alertRecipient = OBProvider.getInstance().get(AlertRecipient.class);
     final OBCriteria<AlertRule> obCriteria = OBDal.getInstance().createCriteria(AlertRule.class);
@@ -990,6 +1053,8 @@
       return getViewsFromViewAccesses(role);
     } else if ("PROCESS".equals(type)) {
       return getProcessFromProcessAccesses(role);
+    } else if ("TABLE".equals(type)) {
+      return getTablesFromTableAccesses(role);
     } else if ("ALERT".equals(type)) {
       return getAlertRulesFromAlertRecipients(role);
     } else if ("PREFERENCE".equals(type)) {
@@ -1193,6 +1258,23 @@
     return result;
   }
 
+  private static String[] getTablesFromTableAccesses(Role role) {
+    final OBCriteria<TableAccess> obCriteria = OBDal.getInstance()
+        .createCriteria(TableAccess.class);
+    obCriteria.add(Restrictions.eq(TableAccess.PROPERTY_ROLE, role));
+    obCriteria.addOrderBy(TableAccess.PROPERTY_TABLE + "." + Table.PROPERTY_DBTABLENAME, true);
+    List<TableAccess> list = obCriteria.list();
+    String[] result = new String[list.size() * 2];
+    int i = 0;
+    for (TableAccess ta : list) {
+      result[i] = ta.getTable().getDBTableName();
+      result[i + 1] = ta.getInheritedFrom() != null ? (String) DalUtil.getId(ta.getInheritedFrom())
+          : "";
+      i += 2;
+    }
+    return result;
+  }
+
   private static String[] getAlertRulesFromAlertRecipients(Role role) {
     final OBCriteria<AlertRecipient> obCriteria = OBDal.getInstance().createCriteria(
         AlertRecipient.class);
--- a/src-test/src/org/openbravo/test/role/VerticalInheritanceTest.java	Tue Sep 29 17:03:02 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/VerticalInheritanceTest.java	Wed Sep 30 10:19:00 2015 +0200
@@ -58,13 +58,14 @@
       "OBUIAPP_RegistrationView");
   private final List<String> PROCESSES = Arrays.asList("Create Purchase Order Lines",
       "Grant Portal Access");
+  private final List<String> TABLES = Arrays.asList("AD_User", "C_Order");
   private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo",
       "CUSTOMER WITHOUT ACCOUNTING");
   private final List<String> PREFERENCES = Arrays.asList("AllowAttachment", "AllowDelete");
 
   @SuppressWarnings("unchecked")
   private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
-      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, TABLES, ALERTS, PREFERENCES);
   private static int testCounter = 0;
 
   /** defines the values the parameter will take. */
@@ -125,6 +126,8 @@
 
       RoleInheritanceTestUtils.removeAccesses(parameter, roleA);
       RoleInheritanceTestUtils.removeAccesses(parameter, roleB);
+      RoleInheritanceTestUtils.removeAccesses(parameter, roleC);
+      OBDal.getInstance().flush();
       testCounter++;
 
     } finally {