diff --git a/Rhino.Security.Tests/DatabaseFixtureSqlServer.cs b/Rhino.Security.Tests/DatabaseFixtureSqlServer.cs new file mode 100644 index 0000000..5d5926c --- /dev/null +++ b/Rhino.Security.Tests/DatabaseFixtureSqlServer.cs @@ -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(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(); + permissionService = ServiceLocator.Current.GetInstance(); + permissionsBuilderService = ServiceLocator.Current.GetInstance(); + authorizationRepository = ServiceLocator.Current.GetInstance(); + + authorizationRepository.CreateUsersGroup("Administrators"); + authorizationRepository.CreateEntitiesGroup("Important Accounts"); + authorizationRepository.CreateOperation("/Account/Edit"); + + + authorizationRepository.AssociateUserWith(user, "Administrators"); + authorizationRepository.AssociateEntityWith(account, "Important Accounts"); + } + } +} \ No newline at end of file diff --git a/Rhino.Security.Tests/DeleteEntityEventListenerFixture_SqlServerProblem.cs b/Rhino.Security.Tests/DeleteEntityEventListenerFixture_SqlServerProblem.cs new file mode 100644 index 0000000..a6b8e7b --- /dev/null +++ b/Rhino.Security.Tests/DeleteEntityEventListenerFixture_SqlServerProblem.cs @@ -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, californiaGroup); + session.Flush(); + + permissionsBuilderService + .Allow(deleteAccountOperation) + .For(group) + .On(californiaGroup) + .Level(10) + .Save(); + + session.Flush(); + + bool isAllowed = authorizationService.IsAllowed(user, account, deleteAccountOperation.Name); + + session.Delete(account); + session.Flush(); + + var accountFetch = session.CreateCriteria() + .Add(Restrictions.Eq("Name", "Bob")) + .UniqueResult(); + + Assert.Null(accountFetch); + } + + } +} diff --git a/Rhino.Security.Tests/Rhino.Security.Tests-vs2008.csproj b/Rhino.Security.Tests/Rhino.Security.Tests-vs2008.csproj index 0ca58de..f03776d 100644 --- a/Rhino.Security.Tests/Rhino.Security.Tests-vs2008.csproj +++ b/Rhino.Security.Tests/Rhino.Security.Tests-vs2008.csproj @@ -89,6 +89,8 @@ + + diff --git a/Rhino.Security/Rhino.Security-vs2008.csproj b/Rhino.Security/Rhino.Security-vs2008.csproj index 8f1a76e..3c426e5 100644 --- a/Rhino.Security/Rhino.Security-vs2008.csproj +++ b/Rhino.Security/Rhino.Security-vs2008.csproj @@ -112,7 +112,6 @@ -