Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I had to remove that line cause, apparently, it was trying to delete my entity before the children. #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Rhino.Security.Tests/DatabaseFixtureSqlServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using Microsoft.Practices.ServiceLocation;
using NHibernate;
using NHibernate.ByteCode.Castle;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Tool.hbm2ddl;
using Rhino.Security.Interfaces;
using Xunit;
using Environment = NHibernate.Cfg.Environment;

namespace Rhino.Security.Tests
{

public abstract class DatabaseFixtureSqlServer : IDisposable
{
protected Account account;
protected IAuthorizationRepository authorizationRepository;
protected IAuthorizationService authorizationService;
protected IPermissionsBuilderService permissionsBuilderService;
protected IPermissionsService permissionService;
protected User user;

protected ISession session;
protected readonly ISessionFactory factory;

protected DatabaseFixtureSqlServer()
{
BeforeSetup();

SillyContainer.SessionProvider = (() => session);
var sillyContainer = new SillyContainer();
ServiceLocator.SetLocatorProvider(() => sillyContainer);

Assert.NotNull(typeof(System.Data.SqlClient.SqlConnection));

var cfg = new Configuration()
.SetProperty(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName)
.SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, ConnectionString)
.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.UseSecondLevelCache, "true")
.SetProperty(Environment.UseQueryCache, "true")
.SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName)
.AddAssembly(typeof(User).Assembly);

Security.Configure<User>(cfg, SecurityTableStructure.Prefix);

factory = cfg.BuildSessionFactory();

session = factory.OpenSession();

new SchemaExport(cfg).Execute(false, true, false, session.Connection, null);

session.BeginTransaction();

SetupEntities();

session.Flush();
}

protected virtual void BeforeSetup()
{

}

public virtual string ConnectionString
{
get { return "Data Source=localhost; Initial Catalog=test; Trusted_Connection=true;"; }
}

public void Dispose()
{
if (session.Transaction.IsActive)
session.Transaction.Rollback();
session.Dispose();
}

private void SetupEntities()
{
user = new User { Name = "Ayende" };
account = new Account { Name = "south sand" };

session.Save(user);
session.Save(account);

authorizationService = ServiceLocator.Current.GetInstance<IAuthorizationService>();
permissionService = ServiceLocator.Current.GetInstance<IPermissionsService>();
permissionsBuilderService = ServiceLocator.Current.GetInstance<IPermissionsBuilderService>();
authorizationRepository = ServiceLocator.Current.GetInstance<IAuthorizationRepository>();

authorizationRepository.CreateUsersGroup("Administrators");
authorizationRepository.CreateEntitiesGroup("Important Accounts");
authorizationRepository.CreateOperation("/Account/Edit");


authorizationRepository.AssociateUserWith(user, "Administrators");
authorizationRepository.AssociateEntityWith(account, "Important Accounts");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace Rhino.Security.Tests
{
using System;
using NHibernate;
using NHibernate.Criterion;
using Rhino.Security.Model;
using Xunit;

public class DeleteEntityEventListenerFixture_SqlServerProblem : DatabaseFixtureSqlServer
{
[Fact]
public void DoesDeletingEntityRemoveEntityReferences()
{
var user = new User() { Name = "Alberto" };
session.Save(user);
session.Flush();

var group = new UsersGroup() { Name = "Guests" };
session.Save(group);
session.Flush();

authorizationRepository.AssociateUserWith(user, group);
session.Flush();

var deleteAccountOperation = authorizationRepository.CreateOperation("/Account/Delete");
session.Flush();

var californiaGroup = authorizationRepository.CreateEntitiesGroup("California");
session.Flush();

var account = new Account() { Name = "Bob" };
session.Save(account);
session.Flush();

authorizationRepository.AssociateEntityWith<Account>(account, californiaGroup);
session.Flush();

permissionsBuilderService
.Allow(deleteAccountOperation)
.For(group)
.On(californiaGroup)
.Level(10)
.Save();

session.Flush();

bool isAllowed = authorizationService.IsAllowed<Account>(user, account, deleteAccountOperation.Name);

session.Delete(account);
session.Flush();

var accountFetch = session.CreateCriteria<Account>()
.Add(Restrictions.Eq("Name", "Bob"))
.UniqueResult<Account>();

Assert.Null(accountFetch);
}

}
}
2 changes: 2 additions & 0 deletions Rhino.Security.Tests/Rhino.Security.Tests-vs2008.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
<Compile Include="AuthorizationServiceWithSecondLevelCacheFixture.cs" />
<Compile Include="AuthorizationService_Explainations_Fixture.cs" />
<Compile Include="AuthorizationService_Queries_Fixture.cs" />
<Compile Include="DatabaseFixtureSqlServer.cs" />
<Compile Include="DeleteEntityEventListenerFixture_SqlServerProblem.cs" />
<Compile Include="PermissionsServiceFixture.cs" />
<Compile Include="DatabaseFixture.cs" />
<Compile Include="DeleteEntityEventListenerFixture.cs" />
Expand Down
1 change: 0 additions & 1 deletion Rhino.Security/Rhino.Security-vs2008.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
<Compile Include="IUser.cs" />
<Compile Include="Model\Operation.cs" />
<Compile Include="Model\Permission.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
Expand Down