public void TestBrokenClientPermissions() { TLogging.Log("Running test 'TestBrokenClientPermissions'..." + Environment.NewLine); // Give the user access at the client end to open the screen TLstTasks.Init(UserInfo.GUserInfo.UserID, TruePermission); // Check that the user is 'demo' Assert.AreEqual("demo", UserInfo.GUserInfo.UserID.ToLower(), "Test should be run with DEMO user"); // get the node that opens the screen TFrmMaintainUsers XPathExpression expr = FNavigator.Compile("//*[@ActionOpenScreen='TFrmMaintainUsers']"); XPathNodeIterator iterator = FNavigator.Select(expr); iterator.MoveNext(); if (iterator.Current is IHasXmlNode) { XmlNode ActionNode = ((IHasXmlNode)iterator.Current).GetNode(); TLogging.Log(ActionNode.Name + " " + ActionNode.Attributes["ActionOpenScreen"].Value); // Check that the node does not give permission to user 'demo' Assert.AreEqual(false, TFrmMainWindowNew.HasAccessPermission(ActionNode, UserInfo.GUserInfo.UserID, false), "user DEMO should not have permissions for TFrmMaintainUsers"); string errorResult = TLstTasks.ExecuteAction(ActionNode, null); // The server should have rejected us Assert.AreNotEqual(String.Empty, errorResult, "Demo was able to open the screen!"); Assert.IsTrue(errorResult.Equals( "No access for user DEMO to Module SYSMAN."), "Expected the fail reason to be module access permission"); } }
public void TestPermissionsSystemManager() { TLogging.Log("Running test 'TestPermissionsSystemManager'..." + Environment.NewLine); Assert.AreEqual("demo", UserInfo.GUserInfo.UserID.ToLower(), "Test should be run with DEMO user"); //change demo's permissions in the xml back to normal TLstTasks.Init(UserInfo.GUserInfo.UserID, FalsePermission); // get the node that opens the screen TFrmMaintainUsers XPathExpression expr = FNavigator.Compile("//*[@ActionOpenScreen='TFrmMaintainUsers']"); XPathNodeIterator iterator = FNavigator.Select(expr); iterator.MoveNext(); if (iterator.Current is IHasXmlNode) { XmlNode ActionNode = ((IHasXmlNode)iterator.Current).GetNode(); TLogging.Log(ActionNode.Name + " " + ActionNode.Attributes["ActionOpenScreen"].Value); Assert.AreEqual(false, TFrmMainWindowNew.HasAccessPermission(ActionNode, UserInfo.GUserInfo.UserID, false), "user DEMO should not have permissions for TFrmMaintainUsers"); // open the screen. should return an error message Assert.AreEqual(Catalog.GetString("Sorry, you don't have enough permissions to do this"), TLstTasks.ExecuteAction(ActionNode, null)); } }
private string ExecuteAction(XmlNode ActionNode, out bool GotServerAccessDeniedException) { string ReturnValue = string.Empty; Exception TheException = null; GotServerAccessDeniedException = false; string actionClick = TYml2Xml.GetAttribute(ActionNode, "ActionClick"); string actionOpenScreen = TYml2Xml.GetAttribute(ActionNode, "ActionOpenScreen"); string className = string.Empty; if (actionClick.Contains(".")) { className = actionClick.Substring(0, actionClick.IndexOf(".")); } else if (actionOpenScreen.Length > 0) { className = actionOpenScreen; } TLogging.Log("Opening " + className); try { // Execute the action by calling the TLstTasks static method // If the call goes through to the server we may get an exception. // It really ought to be one of our Ict.Common.Exceptions.EAccessDeniedExceptions // but Remoting turns that into a TargetInvocationException (sadly). Microsoft may change this in the future. ReturnValue = TLstTasks.ExecuteAction(ActionNode, null); GotServerAccessDeniedException = false; } catch (System.Reflection.TargetInvocationException ex) { // Get the InnerException, if we can TheException = (ex.InnerException == null) ? ex : ex.InnerException; } catch (Exception ex) { // This would include a real Ict.Common.Exceptions.EAccessDeniedException if we ever got one TheException = ex; } if (TheException != null) { if (TheException is ESecurityAccessDeniedException) { GotServerAccessDeniedException = true; } ReturnValue = TheException.Message; } return(ReturnValue); }
/// <summary> /// For development and testing purposes this Method can either execute actions that /// are set up by the program 'PetraMultiStart' (indicated by 'RunAutoTests=true' on /// the command line) OR open a screen with parameters that /// come either from the .config file or Command Line (indicated by 'TestAction="xxx"'). /// The 'Test Action' will not be run if the Control Key is pressed. /// </summary> /// <remarks> /// sample action: TestAction="Namespace=Ict.Petra.Client.MPartner.Gui,ActionOpenScreen=TFrmPartnerEdit2,PartnerKey=0043005002,InitiallySelectedTabPage=petpDetails" ///</remarks> private void RunTestAction() { string DisconnectTimeFromCommandLine = TAppSettingsManager.GetValue("DisconnectTime"); #if TODORemoting if (TAppSettingsManager.GetBoolean("RunAutoTests", false) == true) { // We need to manually 'fix up' the value of DisconnectTime that we get from .NET when we request // the commandline parameters as an array because .NET removes quotation marks in two places where // they were present on the command line. Those two quotation marks need to be there as the call to // TVariant.DecodeFromString() will not succeed if they aren't there in their proper places! DisconnectTimeFromCommandLine = DisconnectTimeFromCommandLine.Substring( 0, DisconnectTimeFromCommandLine.IndexOf(':') + 1) + "\"" + DisconnectTimeFromCommandLine.Substring( DisconnectTimeFromCommandLine.IndexOf(':') + 1) + "\""; TestRunner = new PetraClient_AutomatedAppTest.TAutomatedAppTest( TAppSettingsManager.GetValue("AutoTestConfigFile"), TAppSettingsManager.GetValue("AutoTestParameters"), TVariant.DecodeFromString(DisconnectTimeFromCommandLine).ToDate(), TConnectionManagementBase.GConnectionManagement.ClientName); TestRunner.TestForm = this; TestRunner.ClientID = TConnectionManagementBase.GConnectionManagement.ClientID; TestRunner.Start(this); } else #endif if (System.Windows.Forms.Form.ModifierKeys != Keys.Control) { string testAction = TAppSettingsManager.GetValue("TestAction", false); if (testAction != TAppSettingsManager.UNDEFINEDVALUE) { XmlDocument temp = new XmlDocument(); XmlNode testActionNode = temp.CreateElement("testAction"); temp.AppendChild(testActionNode); testAction = testAction.Trim(new char[] { '"' }); while (testAction.Length > 0) { string[] pair = StringHelper.GetNextCSV(ref testAction, ",").Split(new char[] { '=' }); XmlAttribute attr = temp.CreateAttribute(pair[0]); attr.Value = pair[1]; testActionNode.Attributes.Append(attr); } TLstTasks.ExecuteAction(testActionNode, null); } } }
/// <summary> /// verify that all windows open either without error or with proper exception handling /// </summary> private void TestOpenAllWindows() { TLogging.Log(String.Format("Running test 'TestOpenAllWindows' with Culture '{0}'...", Thread.CurrentThread.CurrentCulture.ToString()) + Environment.NewLine); // get all nodes that have an attribute ActionOpenScreen XPathExpression expr = FNavigator.Compile("//*[@ActionOpenScreen]"); XPathNodeIterator iterator = FNavigator.Select(expr); //create counter variables to keep track of number of failures (might do with lists soon...for modules(?)) int NoSysManPermissionCount = 0; int NoOtherPermissionCount = 0; int BadFailures = 0; int TotalWindowsOpened = 0; List <String> notOpened = new List <String>(); List <String> sysManPermissions = new List <String>(); List <String> otherPermissions = new List <String>(); List <String> workingWindows = new List <String>(); while (iterator.MoveNext()) { if (iterator.Current is IHasXmlNode) { XmlNode ActionNode = ((IHasXmlNode)iterator.Current).GetNode(); string className = ActionNode.Attributes["ActionOpenScreen"].Value; if (className == "TFrmBankStatementImport") { // skip this one because it pops up an additional dialog that requires user input continue; } // look at the permissions module the window came from string Module = TYml2Xml.GetAttributeRecursive(ActionNode, "PermissionsRequired"); TLstTasks.CurrentLedger = TFrmMainWindowNew.CurrentLedger; // Try to open each screen and log the screens that cannot open try { Assert.AreEqual(String.Empty, TLstTasks.ExecuteAction(ActionNode, null)); if (TLstTasks.LastOpenedScreen != null) { TLstTasks.LastOpenedScreen.Close(); } TotalWindowsOpened++; string WindowAndModule = ActionNode.Name + Environment.NewLine + " Permission Required: " + Module; workingWindows.Add(WindowAndModule); //make sure the user had the permissions to open the windows that it opened if (!UserInfo.GUserInfo.IsInModule(Module) && !Module.Contains("")) { BadFailures++; workingWindows.Add("User did not have permission to access " + Module); } } catch (AssertionException e) { TLogging.Log("Window can't be opened: " + ActionNode.Name + " " + ActionNode.Attributes["ActionOpenScreen"].Value + Environment.NewLine + e.ToString()); // if the failure is a permission failure, just log it but don't fail the test if (Catalog.GetString("Sorry, you don't have enough permissions to do this") == TLstTasks.ExecuteAction(ActionNode, null)) { // make sure user didn't have the necessary permissions to open that window // true means user should have been able to open without error if (UserInfo.GUserInfo.IsInModule(Module)) { BadFailures++; string whyFailed = "User should have been able to open " + ActionNode.Name + " with his " + Module + " permission..."; notOpened.Add(whyFailed); } else { string permissions = TYml2Xml.GetAttributeRecursive(ActionNode, "PermissionsRequired"); string WindowAndModule = ActionNode.Name + Environment.NewLine + " Permission Required: " + permissions; if (permissions.Contains("SYSMAN")) { NoSysManPermissionCount++; sysManPermissions.Add(WindowAndModule); } else { NoOtherPermissionCount++; otherPermissions.Add(WindowAndModule); } } } else { BadFailures++; string WindowAndModule = ActionNode.Name + Environment.NewLine + " Permission Required: " + TYml2Xml.GetAttributeRecursive(ActionNode, "PermissionsRequired"); notOpened.Add(WindowAndModule); } } // if the exception has to due with a ledger that the user doesn't have permission to access, // make it a permission exception. Else, fail the test. catch (Exception e) { TLogging.Log("Window can't be opened: " + ActionNode.Name + " " + ActionNode.Attributes["ActionOpenScreen"].Value + Environment.NewLine + e.ToString()); string ledgerNumber = TXMLParser.GetAttributeRecursive(ActionNode, "LedgerNumber", true); string ledger = "LEDGER00" + ledgerNumber; if ((ledgerNumber != String.Empty) && !UserInfo.GUserInfo.IsInModule(ledger)) { NoOtherPermissionCount++; string WindowAndModule = ActionNode.Name + Environment.NewLine + " Permission Required: " + TYml2Xml.GetAttributeRecursive(ActionNode, "PermissionsRequired") + Environment.NewLine + " " + ledger; otherPermissions.Add(WindowAndModule); } else { BadFailures++; string WindowAndModule = ActionNode.Name + Environment.NewLine + " Permission Required: " + TYml2Xml.GetAttributeRecursive(ActionNode, "PermissionsRequired"); if (ledgerNumber != String.Empty) { WindowAndModule += (Environment.NewLine + " " + ledger); } notOpened.Add(WindowAndModule); } } } } //Give stats about where failures were //feel free to change any formatting, I'm not in love with it right now string notOpenedString = string.Join(Environment.NewLine + " ", notOpened.ToArray()); string sysManPermissionsString = string.Join(Environment.NewLine + " ", sysManPermissions.ToArray()); string otherPermissionsString = string.Join(Environment.NewLine + " ", otherPermissions.ToArray()); string workingWindowsString = string.Join(Environment.NewLine + " ", workingWindows.ToArray()); //print the permissions the user should have TLogging.Log(Environment.NewLine + Environment.NewLine + "User Permissions: " + Environment.NewLine + UserInfo.GUserInfo.GetPermissions()); TLogging.Log(Environment.NewLine + Environment.NewLine + "Statistics: " + Environment.NewLine + "Number of windows opened: " + TotalWindowsOpened + Environment.NewLine + " " + workingWindowsString + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "SYSMAN Permission Exceptions: " + sysManPermissions.Count + Environment.NewLine + " " + sysManPermissionsString + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Other Permission Exceptions: " + otherPermissions.Count + Environment.NewLine + " " + otherPermissionsString + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Windows that should be opened but couldn't: " + notOpened.Count + Environment.NewLine + " " + notOpenedString + Environment.NewLine); //Now the loop is finished so fail if there were exceptions Assert.GreaterOrEqual(TotalWindowsOpened, 190, "Expected to open at least 190 windows"); Assert.GreaterOrEqual(sysManPermissions.Count, 3, "Expected to fail to open at least 3 windows requiring SYSMAN permissions"); Assert.AreEqual(notOpened.Count, 0, "Failed to open at least one window for unexplained reasons"); Assert.AreEqual(otherPermissions.Count, 0, "Unexpected failure to open some windows due to a permissions error, when we should have had sufficient permission"); if (BadFailures > 0) { Assert.Fail(String.Format("General failure to successfully open {0} window(s). Maybe there was an exception??", BadFailures)); } }