/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
- /****************************************************************************************************
- Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
- Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ****************************************************************************************************/
-
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Data.Linq;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Transactions;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Xml;
- using System.Xml.Schema;
- using BaoJianSoft.Common;
- using BaoJianSoft.Common.Data;
- using BaoJianSoft.Common.Validation;
- using BaoJianSoft.Platform;
- using BaoJianSoft.Platform.Initialization;
- using BaoJianSoft.Platform.Linq;
- using BaoJianSoft.RapidWeb;
- using BaoJianSoft.RapidWeb.Controls;
- using BaoJianSoft.RapidWeb.DynamicPages;
- using NUnit.Framework;
-
- namespace BaoJianSoft.Tests.Platform
- {
- [TestFixture]
- public class RoleApiTests
- {
- private List<Guid> createdRoleIds = new List<Guid>();
- private List<Guid> createdUserIds = new List<Guid>();
- private List<Guid> createdOrganizationTypeIds = new List<Guid>();
-
- [TearDown]
- public void TearDown()
- {
- using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
- {
- foreach (Guid createdUserId in createdUserIds)
- {
- ctx.UsersInRoles.Delete(uir => uir.UserId == createdUserId);
- ctx.Memberships.Delete(m => m.UserId == createdUserId);
- ctx.Users.Delete(u => u.UserId == createdUserId);
- }
-
- foreach (Guid createdRoleId in createdRoleIds)
- {
- ctx.RolesInOrganizationTypes.Delete(x => x.RoleId == createdRoleId);
- ctx.Roles.Delete(r => r.RoleId == createdRoleId);
- }
-
- foreach (Guid createdOrganizationTypeId in createdOrganizationTypeIds)
- {
- ctx.OrganizationTypes.Delete(orgType => orgType.OrganizationTypeId == createdOrganizationTypeId);
- }
-
- ctx.SubmitChanges();
- }
-
- createdRoleIds.Clear();
- createdUserIds.Clear();
- createdOrganizationTypeIds.Clear();
- }
-
- [Test, Description("Basic role CRUD test")]
- public void BasicCRUDTest()
- {
- IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
- OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
- organizationApi.Save(department);
- createdOrganizationTypeIds.Add(department.OrganizationTypeId);
-
- OrganizationTypeObject customer = new OrganizationTypeObject { Name = "customer", Domain = "Customer", Description = "customer-desc" };
- organizationApi.Save(customer);
- createdOrganizationTypeIds.Add(customer.OrganizationTypeId);
-
- IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();
- RoleObject powerAdministrators = new RoleObject { RoleName = "powerAdministrators", Description = "powerAdministrators-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId }, Predefined = true };
- RoleObject business = new RoleObject { RoleName = "business", Description = "business-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
- RoleObject customers = new RoleObject { RoleName = "customers", Description = "customers-desc", OrganizationTypeIds = new Collection<Guid> { customer.OrganizationTypeId } };
- roleApi.Save(powerAdministrators);
- roleApi.Save(business);
- roleApi.Save(customers);
-
- createdRoleIds.AddRange(new Guid[] { powerAdministrators.RoleId, business.RoleId, customers.RoleId });
-
- powerAdministrators = roleApi.Get(powerAdministrators.RoleId);
- Assert.AreEqual("powerAdministrators", powerAdministrators.RoleName);
- Assert.AreEqual("powerAdministrators-desc", powerAdministrators.Description);
- Assert.IsTrue(powerAdministrators.Predefined);
- Assert.AreEqual(1, powerAdministrators.OrganizationTypeIds.Count);
-
- business = roleApi.Get(business.RoleId);
- Assert.AreEqual("business", business.RoleName);
- Assert.AreEqual("business-desc", business.Description);
- Assert.IsFalse(business.Predefined);
- Assert.AreEqual(1, business.OrganizationTypeIds.Count);
-
- powerAdministrators.RoleName = "admins";
- powerAdministrators.Description = "admins-desc";
- roleApi.Save(powerAdministrators);
-
- powerAdministrators = roleApi.Get(powerAdministrators.RoleId);
- Assert.AreEqual("admins", powerAdministrators.RoleName);
- Assert.AreEqual("admins-desc", powerAdministrators.Description);
- Assert.IsTrue(powerAdministrators.Predefined);
- Assert.AreEqual(1, powerAdministrators.OrganizationTypeIds.Count);
-
- Assert.AreEqual(4, roleApi.FindAll().Count());
- Assert.AreEqual(2, roleApi.FindByOrganizationTypes(new Guid[] { department.OrganizationTypeId }).Count());
- Assert.AreEqual(3, roleApi.FindByOrganizationTypes(new Guid[] { department.OrganizationTypeId, customer.OrganizationTypeId }).Count());
-
- roleApi.Delete(business.RoleId);
- business = roleApi.Get(business.RoleId);
- Assert.IsNull(business);
-
- Assert.AreEqual(3, roleApi.FindAll().Count());
- Assert.AreEqual(1, roleApi.FindByOrganizationTypes(new Guid[] { department.OrganizationTypeId }).Count());
- }
-
- [Test, Description("Update role to a existed role name test"), ExpectedException(typeof(ValidationException))]
- public void UpdateRoleWithDuplicateNameTest()
- {
- IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
- OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
- organizationApi.Save(department);
- createdOrganizationTypeIds.Add(department.OrganizationTypeId);
-
- IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();
- RoleObject administrators = new RoleObject { RoleName = "administrators", Description = "administrators-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
- RoleObject business = new RoleObject { RoleName = "business", Description = "business-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
- roleApi.Save(administrators);
- roleApi.Save(business);
- createdRoleIds.AddRange(new Guid[] { administrators.RoleId, business.RoleId });
-
- business.RoleName = "administrators";
- roleApi.Save(business);
- }
-
- [Test, Description("Roles and users relationship test")]
- public void UsersAndRolesTest()
- {
- IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
- IMembershipApi membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
- IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
- IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();
-
- UserObject eunge = new UserObject
- {
- OrganizationId = platformConfiguration.Organization.OrganizationId,
- UserName = "eunge",
- DisplayName = "Eunge",
- Email = "eunge.liu@gmail.com",
- Comment = "The author of BaoJianSoft.",
- IsApproved = true
- };
-
- membershipApi.Save(eunge, "password1", null);
- createdUserIds.Add(eunge.UserId);
-
- OrganizationTypeObject department = new OrganizationTypeObject { Name = "department", Domain = "Inc", Description = "department-desc" };
- organizationApi.Save(department);
- createdOrganizationTypeIds.Add(department.OrganizationTypeId);
-
- RoleObject powerAdministrators = new RoleObject { RoleName = "powerAdministrators", Description = "powerAdministrators-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
- RoleObject business = new RoleObject { RoleName = "business", Description = "business-desc", OrganizationTypeIds = new Collection<Guid> { department.OrganizationTypeId } };
- roleApi.Save(powerAdministrators);
- roleApi.Save(business);
- createdRoleIds.AddRange(new Guid[] { powerAdministrators.RoleId, business.RoleId });
-
- roleApi.SetUserToRoles(eunge.UserId, new Guid[] { powerAdministrators.RoleId });
- Assert.AreEqual(1, roleApi.FindByUserId(eunge.UserId).Count());
- Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, powerAdministrators.RoleId));
- Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, "powerAdministrators"));
-
- roleApi.SetUserToRoles(eunge.UserId, new Guid[] { powerAdministrators.RoleId, business.RoleId });
- Assert.AreEqual(2, roleApi.FindByUserId(eunge.UserId).Count());
- Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, powerAdministrators.RoleId));
- Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, "powerAdministrators"));
- Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, business.RoleId));
- Assert.IsTrue(roleApi.IsUserInRole(eunge.UserId, "business"));
- }
- }
- }
-