details:   https://code.openbravo.com/erp/devel/pi/rev/e7913c7e9ed0
changeset: 33009:e7913c7e9ed0
user:      Inigo Sanchez <inigo.sanchez <at> openbravo.com>
date:      Thu Nov 16 12:32:13 2017 +0100
summary:   Related with issue 37289: Added tests and some improvements in test 
class

diffstat:

 src-test/src/org/openbravo/test/security/RoleListForTheCurrentUser.java |  132 
----
 src-test/src/org/openbravo/test/security/UserInfoSessionDataTest.java   |  273 
++++++++++
 2 files changed, 273 insertions(+), 132 deletions(-)

diffs (truncated from 415 to 300 lines):

diff -r 73796483a567 -r e7913c7e9ed0 
src-test/src/org/openbravo/test/security/RoleListForTheCurrentUser.java
--- a/src-test/src/org/openbravo/test/security/RoleListForTheCurrentUser.java   
Thu Nov 16 09:53:06 2017 +0100
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,132 +0,0 @@
-/*
- *************************************************************************
- * The contents of this file are subject to the Openbravo  Public  License
- * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
- * Version 1.1  with a permitted attribution clause; you may not  use this
- * file except in compliance with the License. You  may  obtain  a copy of
- * the License at http://www.openbravo.com/legal/license.html 
- * Software distributed under the License  is  distributed  on  an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
- * License for the specific  language  governing  rights  and  limitations
- * under the License. 
- * The Original Code is Openbravo ERP. 
- * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2017 Openbravo SLU 
- * All Rights Reserved. 
- * Contributor(s):  ______________________________________.
- ************************************************************************
- */
-package org.openbravo.test.security;
-
-import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.assertThat;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.codehaus.jettison.json.JSONArray;
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
-import org.junit.Test;
-import org.openbravo.dal.core.OBContext;
-import org.openbravo.dal.service.OBDal;
-import org.openbravo.model.ad.access.UserRoles;
-import org.openbravo.test.datasource.BaseDataSourceTestDal;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * This test ensures that a deactivated role in the User Roles subtab is not 
appeared in the User
- * profile widget.
- */
-public class RoleListForTheCurrentUser extends BaseDataSourceTestDal {
-  private static Logger log = 
LoggerFactory.getLogger(RoleListForTheCurrentUser.class);
-
-  private static final String USER_INFO_START = "OB.User.userInfo=";
-  private static final String USER_INFO_FINISH = "};";
-
-  private static final String ROLE_START = ",role:{value";
-  private static final String ROLE_FINISH = "],roles";
-
-  // Role: F&B US, Inc. - Employee
-  private static final String TESTED_ROLE_ID = 
"19AE26382A674FE8946D2B8070D10122";
-  // User Role: Openbravo User - F&B US, Inc. - Employee Role
-  private static final String USER_ROLE_TEST = 
"3B960D8A87CA4F77907DF2B7F9A77366";
-
-  @Test
-  public void deactivatedRoleNotShowInUserProfile() throws Exception {
-    try {
-      String response = doSessionDynamicRequest();
-      JSONArray rolesInfo = getRoles(response);
-      assertThat("Active role is available for the user.", 
isRoleInUserProfileWidget(rolesInfo),
-          equalTo(true));
-
-      setActiveUserRole(false);
-
-      String responseDeactivatedUserRole = doSessionDynamicRequest();
-      rolesInfo = getRoles(responseDeactivatedUserRole);
-      assertThat("Deactivated role is not available for the user.",
-          isRoleInUserProfileWidget(rolesInfo), equalTo(false));
-    } finally {
-      setActiveUserRole(true);
-    }
-  }
-
-  private String doSessionDynamicRequest() throws Exception {
-    Map<String, String> params = new HashMap<String, String>();
-    return 
doRequest("/org.openbravo.client.kernel/OBCLKER_Kernel/SessionDynamic", params, 
200,
-        "POST");
-  }
-
-  /**
-   * Remove the data of the response that is not necessary for the test. Then 
retrieve the roles
-   * into a JSONArray.
-   * 
-   * @param resp
-   *          original response
-   * @return JSONArray with the roles
-   */
-  private JSONArray getRoles(String resp) {
-    String userInfoResp = getUserInfo(resp);
-    userInfoResp = userInfoResp.substring(userInfoResp.indexOf(ROLE_START) + 
6);
-    userInfoResp = userInfoResp.substring(0, userInfoResp.indexOf(ROLE_FINISH) 
+ 1) + "}";
-    JSONArray resultRoles = null;
-    try {
-      resultRoles = new JSONObject(userInfoResp).getJSONArray("valueMap");
-    } catch (JSONException e) {
-      log.error("Could not transform the response in a JSONArray.", e);
-    }
-    return resultRoles;
-  }
-
-  private String getUserInfo(String resp) {
-    String strResponse = resp.substring(resp.indexOf(USER_INFO_START) + 
USER_INFO_START.length());
-    strResponse = strResponse.substring(0, 
strResponse.indexOf(USER_INFO_FINISH) + 1);
-    return strResponse;
-  }
-
-  private boolean isRoleInUserProfileWidget(JSONArray resultRoles) {
-    try {
-      for (int i = 0; i < resultRoles.length(); i++) {
-        JSONObject role = resultRoles.getJSONObject(i);
-        if (TESTED_ROLE_ID.equals(role.getString("id"))) {
-          return true;
-        }
-      }
-    } catch (JSONException e) {
-      log.error("Could not retrieve the role information.", e);
-    }
-    return false;
-  }
-
-  private void setActiveUserRole(boolean isActive) {
-    OBContext.setAdminMode();
-    try {
-      UserRoles userRole = OBDal.getInstance().get(UserRoles.class, 
USER_ROLE_TEST);
-      userRole.setActive(isActive);
-      OBDal.getInstance().commitAndClose();
-    } finally {
-      OBContext.restorePreviousMode();
-    }
-  }
-}
\ No newline at end of file
diff -r 73796483a567 -r e7913c7e9ed0 
src-test/src/org/openbravo/test/security/UserInfoSessionDataTest.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/security/UserInfoSessionDataTest.java     
Thu Nov 16 12:32:13 2017 +0100
@@ -0,0 +1,273 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2017 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.security;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.hibernate.criterion.Restrictions;
+import org.junit.Test;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.Role;
+import org.openbravo.model.ad.access.RoleOrganization;
+import org.openbravo.model.ad.access.UserRoles;
+import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.model.common.enterprise.Warehouse;
+import org.openbravo.test.datasource.BaseDataSourceTestDal;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * These tests ensure that the information that is displayed in the user 
profile widget is updated
+ * properly when roles, organization accesses or warehouses are 
activated/deactivated.
+ */
+public class UserInfoSessionDataTest extends BaseDataSourceTestDal {
+  private static Logger log = 
LoggerFactory.getLogger(UserInfoSessionDataTest.class);
+
+  private static final String USER_INFO_START = "OB.User.userInfo=";
+  private static final String USER_INFO_FINISH = "};";
+
+  // Role: F&B US, Inc. - Employee
+  private static final String US_EMPLOYEE_ROLE_ID = 
"19AE26382A674FE8946D2B8070D10122";
+  // User Role for Openbravo User - F&B US, Inc. - Employee Role
+  private static final String US_EMPLOYEE_USER_ROLE_ID = 
"3B960D8A87CA4F77907DF2B7F9A77366";
+  // Organization for Role: F&B US East Coast
+  private static final String US_EASTCOAST_ORG_ID = 
"7BABA5FF80494CAFA54DEBD22EC46F01";
+  // Warehouse for Role: F&B US East Coast
+  private static final String US_EASTCOAST_WAREHOUSE_ID = 
"9CF98A18BC754B99998E421F91C5FE12";
+
+  @Test
+  public void deactivatedRoleNotShowInUserProfile() throws Exception {
+    try {
+      String response = doSessionDynamicRequest();
+      JSONArray rolesInfo = getRoles(response);
+      assertThat("Active role is available for the user.",
+          isIdInUserProfileWidget(rolesInfo, US_EMPLOYEE_ROLE_ID), 
equalTo(true));
+
+      setActiveUserRole(false);
+
+      String responseDeactivatedUserRole = doSessionDynamicRequest();
+      rolesInfo = getRoles(responseDeactivatedUserRole);
+      assertThat("Deactivated role is not available for the user.",
+          isIdInUserProfileWidget(rolesInfo, US_EMPLOYEE_ROLE_ID), 
equalTo(false));
+    } finally {
+      setActiveUserRole(true);
+    }
+  }
+
+  @Test
+  public void deactivatedOrganizationNotShowInUserProfile() throws Exception {
+    try {
+      String response = doSessionDynamicRequest();
+      JSONArray organizationRole = 
getOrganizationsRole(getRolesInfo(response), US_EMPLOYEE_ROLE_ID);
+      assertThat("Active organization is available for the user.",
+          isIdInUserProfileWidget(organizationRole, US_EASTCOAST_ORG_ID), 
equalTo(true));
+
+      setActiveOrganizationRole(false);
+
+      String responseDeactivatedOrg = doSessionDynamicRequest();
+      organizationRole = 
getOrganizationsRole(getRolesInfo(responseDeactivatedOrg),
+          US_EMPLOYEE_ROLE_ID);
+      assertThat("Deactivated organization is not available for the user.",
+          isIdInUserProfileWidget(organizationRole, US_EASTCOAST_ORG_ID), 
equalTo(false));
+    } finally {
+      setActiveOrganizationRole(true);
+    }
+  }
+
+  @Test
+  public void deactivatedWarehouseNotShowInUserProfile() throws Exception {
+    try {
+      String response = doSessionDynamicRequest();
+      JSONArray warehousesOrgRole = 
getWarehousesOrgRole(getRolesInfo(response),
+          US_EMPLOYEE_ROLE_ID);
+      JSONArray warehousesRoles = getWarehousesRole(warehousesOrgRole, 
US_EASTCOAST_ORG_ID);
+      assertThat("Active warehouse is available for the user.",
+          isIdInUserProfileWidget(warehousesRoles, US_EASTCOAST_WAREHOUSE_ID), 
equalTo(true));
+
+      setActiveWarehouse(false);
+
+      String responseDeactivatedWarehouse = doSessionDynamicRequest();
+      warehousesOrgRole = 
getWarehousesOrgRole(getRolesInfo(responseDeactivatedWarehouse),
+          US_EMPLOYEE_ROLE_ID);
+      warehousesRoles = getWarehousesRole(warehousesOrgRole, 
US_EASTCOAST_ORG_ID);
+
+      assertThat("Deactivated warehouse is not available for the user.",
+          isIdInUserProfileWidget(warehousesRoles, US_EASTCOAST_WAREHOUSE_ID), 
equalTo(false));
+    } finally {
+      setActiveWarehouse(true);
+    }
+  }
+
+  private String doSessionDynamicRequest() throws Exception {
+    Map<String, String> params = new HashMap<String, String>();
+    return 
doRequest("/org.openbravo.client.kernel/OBCLKER_Kernel/SessionDynamic", params, 
200,
+        "POST");
+  }
+
+  /**
+   * Remove the data of the response that is not necessary for the test. Then 
retrieve the roles
+   * into a JSONArray.
+   * 
+   * @param resp
+   *          original response
+   * @return JSONArray with the roles
+   */
+  private JSONArray getRoles(String resp) {
+    JSONObject userInfoResp = getUserInfo(resp);
+    JSONArray resultRoles = null;
+    try {
+      resultRoles = 
userInfoResp.getJSONObject("role").getJSONArray("valueMap");
+    } catch (JSONException e) {
+      log.error("Could not transform the response in a JSONArray.", e);
+    }
+    return resultRoles;
+  }
+
+  private JSONArray getRolesInfo(String resp) {
+    JSONObject userInfoResp = getUserInfo(resp);
+    JSONArray resultOrgRoles = null;
+    try {
+      resultOrgRoles = 
userInfoResp.getJSONObject("role").getJSONArray("roles");
+    } catch (JSONException e) {
+      log.error("Could not transform the response in a JSONArray.", e);
+    }
+    return resultOrgRoles;

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to