PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 202 lines | 156 code | 29 blank | 17 comment | 6 complexity | 8b0e10a38d2d20f1939d6e0806d2dd07 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.Common.Validation;
  30. using BaoJianSoft.Platform;
  31. using BaoJianSoft.Platform.Initialization;
  32. using BaoJianSoft.Platform.Linq;
  33. using BaoJianSoft.RapidWeb;
  34. using BaoJianSoft.RapidWeb.Controls;
  35. using BaoJianSoft.RapidWeb.DynamicPages;
  36. using NUnit.Framework;
  37. namespace BaoJianSoft.Tests.Platform
  38. {
  39. [TestFixture]
  40. public class RoleApiTests
  41. {
  42. private List<Guid> createdRoleIds = new List<Guid>();
  43. private List<Guid> createdUserIds = new List<Guid>();
  44. private List<Guid> createdOrganizationTypeIds = new List<Guid>();
  45. [TearDown]
  46. public void TearDown()
  47. {
  48. using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
  49. {
  50. foreach (Guid createdUserId in createdUserIds)
  51. {
  52. ctx.UsersInRoles.Delete(uir => uir.UserId == createdUserId);
  53. ctx.Memberships.Delete(m => m.UserId == createdUserId);
  54. ctx.Users.Delete(u => u.UserId == createdUserId);
  55. }
  56. foreach (Guid createdRoleId in createdRoleIds)
  57. {
  58. ctx.RolesInOrganizationTypes.Delete(x => x.RoleId == createdRoleId);
  59. ctx.Roles.Delete(r => r.RoleId == createdRoleId);
  60. }
  61. foreach (Guid createdOrganizationTypeId in createdOrganizationTypeIds)
  62. {
  63. ctx.OrganizationTypes.Delete(orgType => orgType.OrganizationTypeId == createdOrganizationTypeId);
  64. }
  65. ctx.SubmitChanges();
  66. }
  67. createdRoleIds.Clear();
  68. createdUserIds.Clear();
  69. createdOrganizationTypeIds.Clear();
  70. }
  71. [Test, Description("Basic role CRUD test")]
  72. public void BasicCRUDTest()
  73. {
  74. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  75. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  76. organizationApi.Save(department);
  77. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  78. OrganizationTypeObject customer = new OrganizationTypeObject { Name = "customer", Domain = "Customer", Description = "customer-desc" };
  79. organizationApi.Save(customer);
  80. createdOrganizationTypeIds.Add(customer.OrganizationTypeId);
  81. IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();
  82. RoleObject powerAdministrators = new RoleObject { RoleName = "powerAdministrators", Description = "powerAdministrators-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId }, Predefined = true };
  83. RoleObject business = new RoleObject { RoleName = "business", Description = "business-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
  84. RoleObject customers = new RoleObject { RoleName = "customers", Description = "customers-desc", OrganizationTypeIds = new Collection<Guid> { customer.OrganizationTypeId } };
  85. roleApi.Save(powerAdministrators);
  86. roleApi.Save(business);
  87. roleApi.Save(customers);
  88. createdRoleIds.AddRange(new Guid[] { powerAdministrators.RoleId, business.RoleId, customers.RoleId });
  89. powerAdministrators = roleApi.Get(powerAdministrators.RoleId);
  90. Assert.AreEqual("powerAdministrators", powerAdministrators.RoleName);
  91. Assert.AreEqual("powerAdministrators-desc", powerAdministrators.Description);
  92. Assert.IsTrue(powerAdministrators.Predefined);
  93. Assert.AreEqual(1, powerAdministrators.OrganizationTypeIds.Count);
  94. business = roleApi.Get(business.RoleId);
  95. Assert.AreEqual("business", business.RoleName);
  96. Assert.AreEqual("business-desc", business.Description);
  97. Assert.IsFalse(business.Predefined);
  98. Assert.AreEqual(1, business.OrganizationTypeIds.Count);
  99. powerAdministrators.RoleName = "admins";
  100. powerAdministrators.Description = "admins-desc";
  101. roleApi.Save(powerAdministrators);
  102. powerAdministrators = roleApi.Get(powerAdministrators.RoleId);
  103. Assert.AreEqual("admins", powerAdministrators.RoleName);
  104. Assert.AreEqual("admins-desc", powerAdministrators.Description);
  105. Assert.IsTrue(powerAdministrators.Predefined);
  106. Assert.AreEqual(1, powerAdministrators.OrganizationTypeIds.Count);
  107. Assert.AreEqual(4, roleApi.FindAll().Count());
  108. Assert.AreEqual(2, roleApi.FindByOrganizationTypes(new Guid[] { department.OrganizationTypeId }).Count());
  109. Assert.AreEqual(3, roleApi.FindByOrganizationTypes(new Guid[] { department.OrganizationTypeId, customer.OrganizationTypeId }).Count());
  110. roleApi.Delete(business.RoleId);
  111. business = roleApi.Get(business.RoleId);
  112. Assert.IsNull(business);
  113. Assert.AreEqual(3, roleApi.FindAll().Count());
  114. Assert.AreEqual(1, roleApi.FindByOrganizationTypes(new Guid[] { department.OrganizationTypeId }).Count());
  115. }
  116. [Test, Description("Update role to a existed role name test"), ExpectedException(typeof(ValidationException))]
  117. public void UpdateRoleWithDuplicateNameTest()
  118. {
  119. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  120. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  121. organizationApi.Save(department);
  122. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  123. IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();
  124. RoleObject administrators = new RoleObject { RoleName = "administrators", Description = "administrators-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
  125. RoleObject business = new RoleObject { RoleName = "business", Description = "business-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
  126. roleApi.Save(administrators);
  127. roleApi.Save(business);
  128. createdRoleIds.AddRange(new Guid[] { administrators.RoleId, business.RoleId });
  129. business.RoleName = "administrators";
  130. roleApi.Save(business);
  131. }
  132. [Test, Description("Roles and users relationship test")]
  133. public void UsersAndRolesTest()
  134. {
  135. IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
  136. IMembershipApi membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
  137. IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  138. IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();
  139. UserObject eunge = new UserObject
  140. {
  141. OrganizationId = platformConfiguration.Organization.OrganizationId,
  142. UserName = "eunge",
  143. DisplayName = "Eunge",
  144. Email = "eunge.liu@gmail.com",
  145. Comment = "The author of BaoJianSoft.",
  146. IsApproved = true
  147. };
  148. membershipApi.Save(eunge, "password1", null);
  149. createdUserIds.Add(eunge.UserId);
  150. OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
  151. organizationApi.Save(department);
  152. createdOrganizationTypeIds.Add(department.OrganizationTypeId);
  153. RoleObject powerAdministrators = new RoleObject { RoleName = "powerAdministrators", Description = "powerAdministrators-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
  154. RoleObject business = new RoleObject { RoleName = "business", Description = "business-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
  155. roleApi.Save(powerAdministrators);
  156. roleApi.Save(business);
  157. createdRoleIds.AddRange(new Guid[] { powerAdministrators.RoleId, business.RoleId });
  158. roleApi.SetUserToRoles(eunge.UserId, new Guid[] { powerAdministrators.RoleId });
  159. Assert.AreEqual(1, roleApi.FindByUserId(eunge.UserId).Count());
  160. Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, powerAdministrators.RoleId));
  161. Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, "powerAdministrators"));
  162. roleApi.SetUserToRoles(eunge.UserId, new Guid[] { powerAdministrators.RoleId, business.RoleId });
  163. Assert.AreEqual(2, roleApi.FindByUserId(eunge.UserId).Count());
  164. Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, powerAdministrators.RoleId));
  165. Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, "powerAdministrators"));
  166. Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, business.RoleId));
  167. Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, "business"));
  168. }
  169. }
  170. }