PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.31/src/Tests/Platform/SiteMapApiTests.cs

#
C# | 144 lines | 103 code | 21 blank | 20 comment | 0 complexity | 494d4b5ef3ed3f171bb26511d72e7993 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. /****************************************************************************************************
  2. Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
  3. Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ****************************************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.Data.Linq;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Transactions;
  23. using System.Web.UI;
  24. using System.Web.UI.WebControls;
  25. using System.Xml;
  26. using System.Xml.Schema;
  27. using BaoJianSoft.Common;
  28. using BaoJianSoft.Common.Data;
  29. using BaoJianSoft.Platform;
  30. using BaoJianSoft.Platform.Initialization;
  31. using BaoJianSoft.Platform.Linq;
  32. using BaoJianSoft.RapidWeb;
  33. using BaoJianSoft.RapidWeb.Controls;
  34. using BaoJianSoft.RapidWeb.DynamicPages;
  35. using NUnit.Framework;
  36. using Rhino.Mocks;
  37. using BaoJianSoft.Common.Globalization;
  38. namespace BaoJianSoft.Tests.Platform
  39. {
  40. [TestFixture]
  41. public class SiteMapApiTests
  42. {
  43. private MockRepository mockRepository;
  44. private static IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  45. private static IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
  46. private static IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
  47. private List<Guid> createdRoleIds = new List<Guid>();
  48. private List<Guid> createdUserIds = new List<Guid>();
  49. private List<Guid> createdOrganizationTypeIds = new List<Guid>();
  50. [SetUp]
  51. public void StartUp()
  52. {
  53. this.mockRepository = new MockRepository();
  54. }
  55. [TearDown]
  56. public void TearDown()
  57. {
  58. this.mockRepository.VerifyAll();
  59. string sessionKey = "FindSiteMapConfig_" + authenticationContext.User.UserId.ToString("N");
  60. authenticationContext.Session[sessionKey] = null;
  61. }
  62. [Test, Description("Test cases that get site map for administrators.")]
  63. public void SiteMapForAdministrators()
  64. {
  65. string siteMapFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../TestData/siteMap.config");
  66. IRoleApi roleApi = this.mockRepository.StrictMock<IRoleApi>();
  67. Expect.Call(roleApi.IsUserInRole(Guid.Empty, Guid.Empty)).IgnoreArguments().Return(true);
  68. SiteMapApi siteMapApi = new SiteMapApi(authenticationContext, roleApi, organizationApi, null, platformConfiguration, siteMapFilePath);
  69. this.mockRepository.ReplayAll();
  70. IEnumerable<SiteMapItemConfig> siteMapItems = siteMapApi.FindSiteMapConfig(authenticationContext.User.UserId);
  71. Assert.AreEqual(2, siteMapItems.Count());
  72. SiteMapItemConfig accountSiteMapItem = siteMapItems.First();
  73. string accountText = GlobalizationKit.ReplaceGlobalizationVariables("$Resources.SiteMap.Account, BaoJianSoft.Web$");
  74. Assert.AreEqual(accountText, accountSiteMapItem.Text);
  75. }
  76. [Test, Description("Test cases that get site map for non-administrators users having all permissions.")]
  77. public void SiteMapForNonAdministratorsWithAllPermissions()
  78. {
  79. string siteMapFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../TestData/siteMap.config");
  80. IRoleApi roleApi = this.mockRepository.StrictMock<IRoleApi>();
  81. Expect.Call(roleApi.IsUserInRole(Guid.Empty, Guid.Empty)).IgnoreArguments().Return(false);
  82. IPermissionApi permissionApi = this.mockRepository.StrictMock<IPermissionApi>();
  83. // there are 12 sitemap elements which have the attribute "Value".
  84. Expect.Call(permissionApi.HasPermission(Guid.Empty, null))
  85. .IgnoreArguments()
  86. .Return(true)
  87. .Repeat.Times(12);
  88. SiteMapApi siteMapApi = new SiteMapApi(authenticationContext, roleApi, organizationApi, permissionApi, platformConfiguration, siteMapFilePath);
  89. this.mockRepository.ReplayAll();
  90. IEnumerable<SiteMapItemConfig> siteMapItems = siteMapApi.FindSiteMapConfig(authenticationContext.User.UserId);
  91. Assert.AreEqual(2, siteMapItems.Count());
  92. }
  93. [Test, Description("Test cases that get site map for non-administrators users having partial permissions.")]
  94. public void SiteMapForNonAdministratorsWithPartialPermissions()
  95. {
  96. string siteMapFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../TestData/siteMap.config");
  97. IRoleApi roleApi = this.mockRepository.StrictMock<IRoleApi>();
  98. Expect.Call(roleApi.IsUserInRole(Guid.Empty, Guid.Empty)).IgnoreArguments().Return(false);
  99. IPermissionApi permissionApi = this.mockRepository.StrictMock<IPermissionApi>();
  100. Guid userId = authenticationContext.User.UserId;
  101. // there are 4 sitemap elements which have the attribute "Value" equals to "EveryOne".
  102. Expect.Call(permissionApi.HasPermission(userId, "EveryOne"))
  103. .Return(true)
  104. .Repeat.Times(4);
  105. // there are 8 sitemap elements which have the attribute "Value" not equals to "EveryOne".
  106. Expect.Call(permissionApi.HasPermission(userId, "OrganizationTypeManagement")).Return(false);
  107. Expect.Call(permissionApi.HasPermission(userId, "IncManagement")).Return(false);
  108. Expect.Call(permissionApi.HasPermission(userId, "CustomerManagement")).Return(false);
  109. Expect.Call(permissionApi.HasPermission(userId, "Inc.RoleManagement")).Return(false);
  110. Expect.Call(permissionApi.HasPermission(userId, "Customer.RoleManagement")).Return(false);
  111. Expect.Call(permissionApi.HasPermission(userId, "Inc.UserManagement")).Return(false);
  112. Expect.Call(permissionApi.HasPermission(userId, "Customer.UserManagement")).Return(false);
  113. Expect.Call(permissionApi.HasPermission(userId, "AreaManagement")).Return(false);
  114. SiteMapApi siteMapApi = new SiteMapApi(authenticationContext, roleApi, organizationApi, permissionApi, platformConfiguration, siteMapFilePath);
  115. this.mockRepository.ReplayAll();
  116. IEnumerable<SiteMapItemConfig> siteMapItems = siteMapApi.FindSiteMapConfig(userId);
  117. Assert.AreEqual(1, siteMapItems.Count());
  118. }
  119. }
  120. }