|
/****************************************************************************************************
Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
Author: Tim, Legal Name: Long Yi, Email: tim.yi@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.Linq;
using BaoJianSoft.Common;
using BaoJianSoft.Common.Data;
using BaoJianSoft.Platform;
using BaoJianSoft.Platform.Linq;
using NBehave.Narrator.Framework;
using NBehave.Spec.NUnit;
using NUnit.Framework;
namespace BaoJianSoft.Tests.BehaveTest.Stories.Permission
{
[TestFixture, Theme]
public class PermissionStory : SetupFixture
{
private IPermissionApi _PermissionApi;
private Story _story;
private List<Guid> createdRoleIds = new List<Guid>();
private List<Guid> createdOrganizationTypeIds = new List<Guid>();
private List<Guid> createdUserIds = new List<Guid>();
private IRoleApi roleApi;
private IMembershipApi _membershipApi;
private RoleObject business;
private UserObject _Uobject;
private BehaveMemberShipUtils _utils;
[SetUp]
public void Setup()
{
base.GlobalSetup();
_PermissionApi = SpringContext.Current.GetObject<IPermissionApi>();
roleApi = SpringContext.Current.GetObject<IRoleApi>();
}
public PermissionStory()
{
Console.WriteLine("=================Setup===================");
base.GlobalSetup();
_PermissionApi = SpringContext.Current.GetObject<IPermissionApi>();
roleApi = SpringContext.Current.GetObject<IRoleApi>();
_membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
_utils = new BehaveMemberShipUtils();
Console.WriteLine("============Ending Setup===================");
}
[TearDown]
public void CleanUp()
{
Console.WriteLine("============Clean Up====================");
using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
{
foreach (Guid createdUserId in createdUserIds)
{
ctx.Permissions.Delete(p => p.UserId == createdUserId);
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.Permissions.Delete(p => p.RoleId == createdRoleId);
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();
}
Console.WriteLine("========Ending Clean Up====================");
}
[Story, Test]
public void SaveRolePermissionStory()
{
_story = new Story("Save Role Permission");
_story.AsA("User")
.IWant("to be able to create a Role")
.SoThat("I can assign permission to this Role");
_story.WithScenario(
"create a Role By IRoleApi including Create the Same Name;Same Domain; Same Description; Empty Name; ")
.Given("Create several new roles", () =>
{
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);
business = new RoleObject
{
RoleName = "business",
Description = "business-desc",
OrganizationTypeIds =
new Collection<Guid> { department.OrganizationTypeId }
};
})
.When("I save this member", () =>
{
//roleApi.Save(powerAdministrators);
roleApi.Save(business);
//createdRoleIds.Add(powerAdministrators.RoleId);
createdRoleIds.Add(business.RoleId);
})
.And("Create Permission value and assign to Role", () =>
{
_PermissionApi.SetRolePermissions(
business.RoleId,
new string[] { "p1", "p2" });
})
.Then("I get assign permission from role", () =>
{
_PermissionApi.FindRolePermissions(business.RoleId).
Count().ShouldEqual(2);
})
.WithScenario("I can overwrite the permission of specific role")
.Given("an existing role id")
.And("Save new permission to the role", () =>
{
_PermissionApi.SetRolePermissions(business.RoleId,
new string[] { "t1", "t2" });
})
.When("Get the permission from this role")
.Then("I won't get P1,P2 any more", () =>
{
var temp =
_PermissionApi.FindRolePermissions(business.RoleId);
temp.Contains(new PermissionObject("t1")).ShouldBeTrue();
temp.Contains(new PermissionObject("t2")).ShouldBeTrue();
}
)
;
this.CleanUp();
}
[Story, Test]
public void SaveToExistConfigPermission()
{
_story = new Story("Save Permission to config User");
_story.AsA("User")
.IWant("to be able to get config permission")
.SoThat("I can assign new permission to this User");
IEnumerable<PermissionConfig> config;
_story.WithScenario("Assign new permission")
.Given("Get the configured permission", () =>
{
UserObject _object = _membershipApi.Get("admin");
config = _PermissionApi.FindPermissionConfig(_object.UserId);
})
.When("")
.Then("I get can the value of the config", () =>
{
});
}
[Story, Test]
public void SaveToUserPermissionStory()
{
_story = new Story("Save Permission to User");
_story.AsA("User")
.IWant("to be able to get config permission")
.SoThat("I can assign new permission to this Role");
_story.WithScenario("Create User and assign Permission")
.Given("Create User", () =>
{
_Uobject = _utils.CreateUserObject("Eunge Liu", true);
})
.When("Save this object", () =>
{
_membershipApi.Save(_Uobject, "Hello123", "Hello123");
createdUserIds.Add(_Uobject.UserId);
})
.Then("I can assign permission on it", () =>
{
_PermissionApi.SetUserPermissions(_Uobject.UserId, new string[] { "p1", "p2" });
_PermissionApi.FindUserPermissions(_Uobject.UserId,true).Contains(new PermissionObject("p1")).ShouldBeTrue();
_PermissionApi.FindUserPermissions(_Uobject.UserId, true).Contains(new PermissionObject("p2")).ShouldBeTrue();
})
;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've worked as a software architect and developer based on Microsoft .NET framework and web technology since 2002, having rich experience on SaaS, multiple-tier web system and website development. I've devoted into open source project development since 2003, and have created 3 main projects including DataQuicker (ORM), ExcelQuicker (Excel Report Generator), and RapidWebDev (Enterprise-level CMS)
I worked in BenQ (8 mo), Bleum (4 mo), Newegg (1 mo), Microsoft (3 yr) and Autodesk (5 yr) before 2012. Now I own a startup company in Shanghai China to deliver the exceptional results to global internet users by leveraging the power of Internet and the local excellence.