Related to issue 38365: added automated test
authorAlejandro <alekosmp86@gmail.com>
Fri, 13 Jul 2018 11:29:38 -0400
changeset 7552 3df81719a438
parent 7551 07d3d1308c5f
child 7553 c1af4e7e6a77
child 7574 145ff9670dbd
Related to issue 38365: added automated test
src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/layaway/I38365_VerifyOpenLayawayWithoutPermissionForThatTicket.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/layaway/I38365_VerifyOpenLayawayWithoutPermissionForThatTicket.java	Fri Jul 13 11:29:38 2018 -0400
@@ -0,0 +1,199 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo Public License
+ * Version 1.0 (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 S.L.U.
+ * All portions are Copyright (C) 2018 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author alekosmp86@gmail.com
+ *
+ */
+
+package org.openbravo.test.mobile.retail.pack.selenium.tests.layaway;
+
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.core.utils.OBConstants;
+import org.openbravo.test.mobile.core.utils.OBUtils;
+import org.openbravo.test.mobile.core.utils.OBWait;
+import org.openbravo.test.mobile.retail.mobilecore.database.DatabaseHelperInsertUpdateOrDelete;
+import org.openbravo.test.mobile.retail.mobilecore.javascript.BackboneHelper;
+import org.openbravo.test.mobile.retail.mobilecore.javascript.MobileCoreAPI;
+import org.openbravo.test.mobile.retail.mobilecore.javascript.SynchronizationMonitor;
+import org.openbravo.test.mobile.retail.mobilecore.selenium.TestIdConstructor;
+import org.openbravo.test.mobile.retail.mobilecore.selenium.TestIdCore;
+import org.openbravo.test.mobile.retail.mobilecore.utils.ApplicationWindow;
+import org.openbravo.test.mobile.retail.mobilecore.utils.RetailUtils;
+import org.openbravo.test.mobile.retail.pack.selenium.TestIdPack;
+import org.openbravo.test.mobile.retail.pack.selenium.terminals.WebPOSTerminalHelper;
+
+public class I38365_VerifyOpenLayawayWithoutPermissionForThatTicket extends WebPOSTerminalHelper {
+  final static String PRODUCT_NAME = "Avalanche transceiver";
+  final static String PREFERENCE_ID = RetailUtils.getRandomUUID();
+  final static String testUserName = "testuser";
+  final static String testUserPassword = "openbravo";
+  final static String TestUserId = "049899F8BE0C45E39D10952ABD7A8778";
+
+  @Test
+  public void test() {
+    // logout and login using the Test User
+    simpleLogOut();
+    simpleLogIn(testUserName, testUserPassword);
+
+    // create a layaway
+    String layawayDocNo = BackboneHelper.getDocumentNo();
+    tap(TestIdCore.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_MENU_LAYAWAYTHISRECEIPT);
+    verify(TestIdPack.LABEL_RECEIPT_TYPE, "To be laid away");
+
+    // add a product and save the layaway
+    addProduct(PRODUCT_NAME);
+    verify(TestIdPack.LABEL_RECEIPT_TOTAL_GROSS, "150.50");
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_LAYAWAY);
+    SynchronizationMonitor.waitUntilDataIsInTheServer(true);
+
+    // create a receipt
+    addProduct(PRODUCT_NAME);
+    String receiptDocNo = BackboneHelper.getDocumentNo();
+    verify(TestIdPack.LABEL_RECEIPT_TOTAL_GROSS, "150.50");
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    SynchronizationMonitor.waitUntilDataIsInTheServer(true);
+
+    // try open the created layaway
+    openTicket(layawayDocNo);
+
+    // verify the user has no privileges to open the layaway (due to the preference)
+    isVisible(TestIdPack.LABEL_POPUP_TITLE, true);
+    verify(TestIdPack.LABEL_POPUP_TITLE, "Error");
+    tap(TestIdPack.BUTTON_POPUP_CONFIRMATION_OK);
+    // the layaway was not opened
+    isVisible(TestIdPack.EMPTY_RECEIPT_TEXT, true);
+
+    // try open a receipt, verify there are no errors
+    openTicket(receiptDocNo);
+    isVisible(TestIdPack.BUTTON_RECEIPT_ROW1, true);
+    verify(TestIdPack.LABEL_RECEIPT_TOTAL_GROSS, "150.50");
+    verify(TestIdPack.LABEL_RECEIPT_TYPE, "PAID");
+    tap(TestIdPack.BUTTON_DELETERECEIPT);
+
+    // logout and login using the default user
+    simpleLogOut();
+    simpleLogIn("vallblanca", "openbravo");
+  }
+
+  public void beforeWithReload() {
+    activateTestUser(true);
+    setPreferenceValue(PREFERENCE_ID, "OBPOS_retail.layaways", true);
+  }
+
+  public void afterWithReload() {
+    activateTestUser(false);
+    setPreferenceValue(PREFERENCE_ID, "OBPOS_retail.layaways", false);
+  }
+
+  private void addProduct(String productName) {
+    tap(TestIdPack.BUTTON_SEARCH);
+    write(TestIdPack.FIELD_SEARCH_TEXT, productName);
+    TestIdPack.FIELD_SEARCH_TEXT.enyoNode().executeExtension(".waterfall('onchange')");
+    tap(TestIdPack.BUTTON_SEARCH_EXECUTE);
+    tap(TestIdPack.BUTTON_SEARCHPRODUCT_FIRSTROW);
+  }
+
+  private void openTicket(String docNo) {
+    tap(TestIdCore.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_MENU_OPENRECEIPT);
+    write(TestIdPack.FIELD_OPEN_RECEIPT_FILTER_TXT, docNo);
+    TestIdPack.FIELD_OPEN_RECEIPT_FILTER_TXT.enyoNode().executeExtension(".waterfall('onchange')");
+    tap(TestIdPack.BUTTON_OPEN_RECEIPT_SEARCH);
+    tap(TestIdPack.BUTTON_OPEN_RECEIPT_ROW1);
+  }
+
+  private void activateTestUser(boolean isActive) {
+    String sql = String.format("update ad_user set isactive = '%s' where ad_user_id = '%s'",
+        isActive == true ? "Y" : "N", TestUserId);
+
+    new DatabaseHelperInsertUpdateOrDelete().execute(sql, 1);
+  }
+
+  private void setPreferenceValue(String preferenceId, String preferenceName, boolean value) {
+    String sql;
+    String clientId = BackboneHelper.getTerminalValue("client");
+    if (value) {
+      sql = String
+          .format(
+              "INSERT INTO ad_preference(ad_preference_id, ad_client_id, ad_org_id, isactive, createdby, updatedby, value, property, ispropertylist, selected) "
+                  + "VALUES ('%s', '%s', '0', to_char('Y'),'100', '100', 'N', '%s', to_char('Y'), to_char('Y'))",
+              preferenceId, clientId, preferenceName);
+    } else {
+      sql = String.format("DELETE FROM ad_preference where ad_preference_id = '%s'", preferenceId);
+    }
+    new DatabaseHelperInsertUpdateOrDelete().execute(sql, 1);
+  }
+
+  private void simpleLogOut() {
+    logger.info("Logging out");
+    SynchronizationMonitor.waitUntilDataIsInTheServer();
+
+    new OBWait() {
+      @Override
+      protected boolean until(final int currentIteration) {
+        final ArrayList<TestIdConstructor> array = new ArrayList<TestIdConstructor>();
+        array.add(TestIdCore.LABEL_TERMINALAUTH_TITLE);
+        array.add(TestIdCore.LABEL_LOGIN_OPENBRAVOMOBILE);
+        array.add(TestIdCore.BUTTON_MENU);
+        final TestIdConstructor firstFoundTestId = firstOf(array);
+        if (firstFoundTestId == TestIdCore.BUTTON_MENU) {
+          tap(TestIdCore.BUTTON_MENU);
+          tap(TestIdCore.BUTTON_MENU_ONLINE);
+          tap(TestIdCore.BUTTON_ENDSESSION);
+          tap(TestIdCore.BUTTON_LOGOUT);
+        } else if (firstFoundTestId == TestIdCore.LABEL_LOGIN_OPENBRAVOMOBILE
+            || firstFoundTestId == TestIdCore.LABEL_TERMINALAUTH_TITLE) {
+          return true;
+        }
+        return false;
+      }
+
+      @Override
+      protected void timeout() {
+        fail("Failed to finish the logOut process");
+      }
+    };
+
+    postLogOutActions();
+  }
+
+  private void simpleLogIn(String user, String pwd) {
+    OBUtils.pause(OBConstants.WAIT_AFTERLOGOUT);
+    if (!MobileCoreAPI.isSessionActive()) {
+      preLogInActions();
+      logger.debug(String.format("Login with username '%s' and password '%s'", user, pwd));
+      TestIdCore.LABEL_LOGIN_OPENBRAVOMOBILE.enyoNode().waitUntilIsReady();
+      OBUtils.pause(OBConstants.WAIT_AFTERWEBPOSLOGO);
+      verifyLoginPage();
+      write(TestIdCore.FIELD_LOGIN_USERNAME, user);
+      write(TestIdCore.FIELD_LOGIN_PASSWORD, pwd);
+      OBUtils.pause(OBConstants.WAIT_FORUITOUPDATE);
+      tap(TestIdCore.BUTTON_LOGIN);
+    }
+    postLogInActions();
+    waitUntilCertainWindowIsReached(ApplicationWindow.POINTOFSALE);
+  }
+}