public void ExecuteSqlTest()
        {
            using (var scope = TestScope.Create())
            {
                var persistenceTransaction = scope.Resolve <IPersistenceTransaction>();
                var executionContext       = scope.Resolve <Common.ExecutionContext>();

                var principalNamePrefix = "BaseExecuter_";
                var principal1          = new Common.Principal {
                    ID = Guid.NewGuid(), Name = principalNamePrefix + Guid.NewGuid().ToString()
                };
                var principal2 = new Common.Principal {
                    ID = Guid.NewGuid(), Name = principalNamePrefix + Guid.NewGuid().ToString()
                };

                var baseSqlExecuter = new BaseSqlExecuter(persistenceTransaction);

                baseSqlExecuter.ExecuteSqlRaw(@"
                        INSERT INTO Common.Principal (ID, Name) VALUES({0}, {1});
                        INSERT INTO Common.Principal (ID, Name) VALUES(@principal2ID, @principal2Name);
                    ", new object[] { principal1.ID, principal1.Name, new SqlParameter("@principal2ID", principal2.ID),
                                      new SqlParameter("@principal2Name", principal2.Name) });

                var results = executionContext.Repository.Common.Principal.Query(x => x.Name.StartsWith(principalNamePrefix)).ToSimple().ToArray();

                Assert.AreEqual(Report(principal1, principal2), Report(results));
            }
        }
        public void ExecuteReaderTest()
        {
            using (var scope = TestScope.Create())
            {
                var persistenceTransaction = scope.Resolve <IPersistenceTransaction>();
                var executionContext       = scope.Resolve <Common.ExecutionContext>();

                var principalNamePrefix = "BaseExecuter_";
                var principalNameSuffix = "_Test";
                var principal1          = new Common.Principal {
                    ID = Guid.NewGuid(), Name = principalNamePrefix + Guid.NewGuid().ToString()
                };
                var principal2 = new Common.Principal {
                    ID = Guid.NewGuid(), Name = principalNamePrefix + Guid.NewGuid().ToString() + principalNameSuffix
                };
                executionContext.Repository.Common.Principal.Insert(principal1, principal2);

                var baseSqlExecuter = new BaseSqlExecuter(persistenceTransaction);

                var results = new List <Common.Principal>();
                baseSqlExecuter.ExecuteReaderRaw("SELECT ID, Name FROM Common.Principal WHERE Name LIKE {0}+'%' AND Name LIKE '%'+@suffix",
                                                 new object[] { principalNamePrefix, new SqlParameter("@suffix", principalNameSuffix) },
                                                 reader => results.Add(new Common.Principal {
                    ID = reader.GetGuid(0), Name = reader.GetString(1)
                }));

                Assert.AreEqual(Report(principal2), Report(results.ToArray()));
            }
        }
Exemplo n.º 3
0
        public void CheckDatabaseIsOracle()
        {
            // Creating empty Rhetos DI scope just to initialize static utilities that are required for test in this class.
            using (var scope = TestScope.Create())
                Assert.IsNotNull(SqlUtility.ConnectionString);

            TestUtility.CheckDatabaseAvailability("Oracle");
        }
Exemplo n.º 4
0
        public void ParallelRequests()
        {
            string u1Name           = TestName + "U";
            string r1Name           = TestName + "P";
            string commonTestSuffix = Guid.NewGuid().ToString().Replace("-", "");

            IPrincipal u1;

            Common.Role r1;

            using (var scope = TestScope.Create($"{u1Name}-{r1Name}", commonTestSuffix))
            {
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var roles      = scope.Resolve <GenericRepository <Common.Role> >();

                u1 = scope.NewPrincipal(u1Name);
                principals.Insert(u1);

                r1 = scope.NewRole(r1Name);
                roles.Insert(r1);

                scope.CommitAndClose();
            }

            for (int test = 0; test < 5; test++)
            {
                Console.WriteLine("Test: " + test);

                // Test setup: PrincipalHasRole is deleted to make sure it is not up-to-date.
                // PrincipalHasRole will be recomputed when reading PrincipalHasRole.
                using (var scope = TestScope.Create($"{u1Name}-{r1Name}", commonTestSuffix))
                {
                    var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();
                    membership.Delete(membership.Load());
                    Assert.AreEqual(@"", scope.ReportMembership(), "Initial empty membership.");
                    AuthorizationDataCache.ClearCache();

                    scope.CommitAndClose();
                }

                // Recompute membership on authorization with multiple parallel requests:
                Parallel.For(0, 4, thread =>
                {
                    using (var scope = TestScope.Create($"{u1Name}-{r1Name}", commonTestSuffix))
                    {
                        var authorizationProvider = scope.Resolve <CommonAuthorizationProvider>();
                        var userRoles             = authorizationProvider.GetUsersRoles(u1);
                        Assert.AreEqual($@"\{r1Name}", scope.ReportRoles(userRoles), "User's roles should be recomputed.");
                        Assert.AreEqual($@"\{u1Name}-\{r1Name}", scope.ReportMembership(), "Updated role membership");

                        scope.CommitAndClose();
                    }
                });
            }
        }
Exemplo n.º 5
0
        public void TestMock()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                var u1 = scope.NewPrincipal("u1");
                Console.WriteLine(u1.Name);

                var ws = scope.Resolve <IWindowsSecurity>();
                Assert.AreEqual("r1, r12", TestUtility.DumpSorted(ws.GetIdentityMembership(u1.Name), scope.Shorten), "u1 active directory groups");
            }
        }
Exemplo n.º 6
0
        public static void ClassCleanup()
        {
            using (var scope = TestScope.Create("u1-r1"))
            {
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var roles      = scope.Resolve <GenericRepository <Common.Role> >();

                principals.Delete(principals.Load(p => p.Name.Contains(TestName)));
                roles.Delete(roles.Load(r => r.Name.Contains(TestName)));

                scope.CommitAndClose();
            }
        }
        public void InvalidParameterNameTest()
        {
            using (var scope = TestScope.Create())
            {
                var persistenceTransaction = scope.Resolve <IPersistenceTransaction>();
                var executionContext       = scope.Resolve <Common.ExecutionContext>();
                var baseSqlExecuter        = new BaseSqlExecuter(persistenceTransaction);

                var sqlParameter = new SqlParameter("@__p0", "Test");
                TestUtility.ShouldFail <ArgumentException>(
                    () => baseSqlExecuter.ExecuteSqlRaw("INSERT INTO TestEntity.Principal (Name) VALUES(@__p0);", new object[] { sqlParameter }),
                    "parameter name should not start with", "@__p");
            }
        }
Exemplo n.º 8
0
        public void UniqueReferenceCascadeDelete()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var scope = TestScope.Create(builder => builder.ConfigureUseDatabaseNullSemantics(useDatabaseNullSemantics)))
                {
                    var repository = scope.Resolve <Common.DomRepository>();

                    var p1 = new TestBrowse.ParentBase {
                        Name = "p1"
                    };
                    var p2 = new TestBrowse.ParentBase {
                        Name = "p2"
                    };
                    repository.TestBrowse.ParentBase.Insert(p1, p2);

                    var pur1 = new TestBrowse.ParentUniqueReference {
                        ID = p1.ID, Name3 = "pur1"
                    };
                    var pur2 = new TestBrowse.ParentUniqueReference {
                        ID = p2.ID, Name3 = "pur2"
                    };
                    repository.TestBrowse.ParentUniqueReference.Insert(pur1, pur2);

                    var urc1 = new TestBrowse.UniqueReferenceChild {
                        ID = p1.ID, Name4 = "urc1"
                    };
                    var urc2 = new TestBrowse.UniqueReferenceChild {
                        ID = p2.ID, Name4 = "urc2"
                    };
                    repository.TestBrowse.UniqueReferenceChild.Insert(urc1, urc2);

                    Func <string> report = () => TestUtility.DumpSorted(
                        repository.TestBrowse.ParentBase.Query(new[] { p1.ID, p2.ID }).ToList(),
                        item => item.Name + "-" + item.Extension_ParentUniqueReference?.Name3 + "-" + item.Extension_ParentUniqueReference?.Extension_UniqueReferenceChild.Name4);

                    Assert.AreEqual("p1-pur1-urc1, p2-pur2-urc2", report());

                    repository.TestBrowse.ParentUniqueReference.Delete(pur1);
                    Assert.AreEqual("p1--, p2-pur2-urc2", report());

                    repository.TestBrowse.ParentBase.Delete(p1);
                    Assert.AreEqual("p2-pur2-urc2", report());

                    TestUtility.ShouldFail <Rhetos.UserException>(
                        () => repository.TestBrowse.ParentBase.Delete(p2),
                        "It is not allowed to delete a record that is referenced by other records.");
                }
            }
        }
Exemplo n.º 9
0
        public void ComputeOnUpdatePrincipal()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();

                roles.Insert(r1, r2, r25);
                principals.Insert(u1, u2, u3, u5);
                Assert.AreEqual(@"\u1-\r1, \u2-\r2", scope.ReportMembership(), "auto-membership on insert");

                membership.Insert(new[] { // Non-domain users and roles.
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                });
                Assert.AreEqual(@"\u1-\r1, \u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "manual membership for non-domain roles and users");

                // Recompute membership on update principal (domain users only):

                u2      = principals.Load(new[] { u2.ID }).Single(); // Refresh before modification.
                u2.Name = scope.NewName("u2x", domain: false);
                principals.Update(u2);
                Assert.AreEqual(@"\u1-\r1, u2x-\r2, u2x-r25, u5-r25", scope.ReportMembership(), "auto-membership on update ignore non-domain users");
                u2.Name = scope.NewName("u2x");
                principals.Update(u2);
                Assert.AreEqual(@"\u1-\r1, \u2x-r25, u5-r25", scope.ReportMembership(), "auto-membership on update domain users");
                u2.Name = scope.NewName("u2");
                principals.Update(u2);
                Assert.AreEqual(@"\u1-\r1, \u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "auto-membership on update domain users 2");
            }
        }
        public void NullParameterTest()
        {
            using (var scope = TestScope.Create())
            {
                var persistenceTransaction = scope.Resolve <IPersistenceTransaction>();
                var executionContext       = scope.Resolve <Common.ExecutionContext>();

                var principalID     = Guid.NewGuid();
                var baseSqlExecuter = new BaseSqlExecuter(persistenceTransaction);

                baseSqlExecuter.ExecuteSqlRaw("INSERT INTO TestEntity.Principal (ID, Name) VALUES({0}, {1});", new object[] { principalID, null });

                var result = executionContext.Repository.TestEntity.Principal.Query(x => x.ID == principalID).ToSimple().First();

                Assert.IsNull(result.Name);
            }
        }
Exemplo n.º 11
0
        public void ComputeOnUpdateRole()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();

                roles.Insert(r1, r2, r25);
                principals.Insert(u1, u2, u3, u5);
                membership.Insert(new[] { // Non-domain users and roles.
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                });

                // Recompute membership on modified role should remove obsolete memebers:

                Assert.AreEqual(@"\u1-\r1, \u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "initial membership");

                r2.Name = scope.NewName("r2x");
                roles.Update(r2);
                Assert.AreEqual(@"\u1-\r1, \u2-r25, u5-r25", scope.ReportMembership(), "recomputed membership after rename role");

                // New role members will not be added automatically, to avoid performance penalty:
                // (the membership will be added on the principal's authorization check)

                r2.Name = scope.NewName("r2");
                roles.Update(r2);
                // This is not reqested feature, this assert simply describes currently implemented behaviour:
                Assert.AreEqual(@"\u1-\r1, \u2-r25, u5-r25", scope.ReportMembership());
            }
        }
Exemplo n.º 12
0
        public void CommonFilters()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var repository = scope.Resolve <Common.DomRepository>();
                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = repository.Common.PrincipalHasRole;

                roles.Insert(r1, r2, r25);
                principals.Insert(u1, u2, u3, u5);
                membership.Insert(new[] { // Non-domain users and roles.
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                });

                // Common filters:

                var filter1 = new Common.ActiveDirectoryAllUsersParameter();
                Assert.AreEqual(@"\u1-\r1, \u2-\r2",
                                scope.ReportMembership(filter1),
                                "filter ActiveDirectoryAllUsersParameter");

                var filter2 = new[] { u1.Name, u2.Name }.Select(name => new Common.ActiveDirectoryUserParameter {
                    UserName = name
                }).ToArray();
                Assert.AreEqual(@"\u1-\r1, \u2-\r2",
                                scope.ReportMembership(filter2),
                                "filter ActiveDirectoryUserParameter");
            }
        }
Exemplo n.º 13
0
        public void Filters()
        {
            using (var scope = TestScope.Create())
            {
                var repository        = scope.Resolve <Common.DomRepository>();
                var genericRepository = scope.Resolve <GenericRepository <TestBrowse.SF> >();

                //Assert.AreEqual(0, genericRepository.Load(item => item.ID == Guid.Empty).Count(), "Generic repository: Simple loader with filter expression.");
                //Assert.AreEqual(0, repository.TestBrowse.SF.Query().Where(item => item.ID == Guid.Empty).ToSimple().ToList().Count(), "Simple query.");
                //Assert.AreEqual(0, repository.TestBrowse.SF.Load(item => item.ID == Guid.Empty).Count(), "Simple loader with filter expression."); // Same query as above, using different infrastructure

                Guid sourceId = Guid.NewGuid();
                Guid refId    = Guid.NewGuid();
                scope.Resolve <ISqlExecuter>().ExecuteSql(new[]
                {
                    "DELETE FROM TestBrowse.Source;",
                    "DELETE FROM TestBrowse.Other;",
                    "INSERT INTO TestBrowse.Other (ID, Name) SELECT '" + refId + "', 'abc';",
                    "INSERT INTO TestBrowse.Source (ID, RefID) SELECT '" + sourceId + "', '" + refId + "';",
                });

                var ids = new[] { sourceId };
                Assert.AreEqual("abc", repository.TestBrowse.SF.Load(ids).Single().RefName);
                Assert.AreEqual("abc", repository.TestBrowse.SF.Query().Where(item => ids.Contains(item.ID)).Single().RefName);

                {
                    Assert.AreEqual("abc", genericRepository.Load(ids).Single().RefName);

                    var q = genericRepository.Query(ids);
                    Assert.IsTrue(q is IQueryable, q.GetType().FullName);
                    Assert.AreEqual("abc", q.Single().RefName);
                }

                var manyIds = Enumerable.Range(0, 5000).Select(x => Guid.NewGuid()).Concat(ids).ToList();

                {
                    Assert.AreEqual("abc", genericRepository.Load(manyIds).Single().RefName);

                    var q = genericRepository.Query(manyIds);
                    Assert.IsTrue(q is IQueryable, q.GetType().FullName);
                    Assert.AreEqual("abc", q.Single().RefName);
                }
            }
        }
Exemplo n.º 14
0
        public void MultiplePropertiesSameSource()
        {
            using (var scope = TestScope.Create())
            {
                var repository = scope.Resolve <Common.DomRepository>();

                Guid refID = Guid.NewGuid();
                scope.Resolve <ISqlExecuter>().ExecuteSql(new[]
                {
                    "DELETE FROM TestBrowse.Source",
                    "DELETE FROM TestBrowse.Other",
                    "INSERT INTO TestBrowse.Other (ID, Name) VALUES ('" + refID + "', 'abc')",
                    "INSERT INTO TestBrowse.Source (RefID, Code) VALUES ('" + refID + "', '123')",
                });

                Assert.AreEqual("123 123 abc abc", TestUtility.DumpSorted(
                                    repository.TestBrowse.SFMulti.Query(), item => item.Code1 + " " + item.Code2 + " " + item.RefName1 + " " + item.RefName2));
            }
        }
Exemplo n.º 15
0
        public void RoleInheritance()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var repository = scope.Resolve <Common.DomRepository>();
                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();

                roles.Insert(r1, r2, r25);
                principals.Insert(u1, u2, u3, u5);
                membership.Insert(new[] { // Non-domain users and roles.
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                });

                // Modify role inheritance:

                repository.Common.RoleInheritsRole.Insert(new[] { new Common.RoleInheritsRole {
                                                                      UsersFromID = r1.ID, PermissionsFromID = r25.ID
                                                                  } });

                TestUtility.ShouldFail(() => repository.Common.RoleInheritsRole.Insert(new[] { new Common.RoleInheritsRole {
                                                                                                   UsersFromID = r25.ID, PermissionsFromID = r2.ID
                                                                                               } }), "UserException",
                                       "It is not allowed to add users or user groups here because this role is synchronized with an Active Directory group.",
                                       "Please change the user membership on Active Directory instead.");
            }
        }
Exemplo n.º 16
0
        public void ComputeOnInsertPrincipal()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();

                roles.Insert(r1, r2, r25);
                Assert.AreEqual(@"\r1, \r2, r25", scope.ReportRoles(roles.Load()), "roles created");

                principals.Insert(u1, u2, u3, u5);
                Assert.AreEqual(@"\u1, \u2, \u3, u5", scope.ReportPrincipals(principals.Load()), "principals created");

                // Recompute membership on insert domain users:

                Assert.AreEqual(@"\u1-\r1, \u2-\r2", scope.ReportMembership(), "auto-membership on insert");

                // Inserting non-domain users and roles:

                membership.Insert(new[] {
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                },
                                  checkUserPermissions: true);
                Assert.AreEqual(@"\u1-\r1, \u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "non-domain users and roles");
            }
        }
Exemplo n.º 17
0
        public void UniqueReference()
        {
            using (var scope = TestScope.Create())
            {
                var repository = scope.Resolve <Common.DomRepository>();

                var p = new TestBrowse.ParentBase {
                    Name = "p"
                };
                repository.TestBrowse.ParentBase.Insert(p);

                var pur = new TestBrowse.ParentUniqueReference {
                    ID = p.ID, Name3 = "pur"
                };
                repository.TestBrowse.ParentUniqueReference.Insert(pur);

                Assert.AreEqual("p-pur", repository.TestBrowse.ParentUniqueReferenceBrowse1.Query(item => item.ID == p.ID).Select(item => item.Name + "-" + item.Name3).Single());
                Assert.AreEqual("p-pur", repository.TestBrowse.ParentUniqueReferenceBrowse2.Query(item => item.ID == p.ID).Select(item => item.Name + "-" + item.Name3).Single());
            }
        }
Exemplo n.º 18
0
        public void RecomputeMembership()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2"))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var repository = scope.Resolve <Common.DomRepository>();
                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = repository.Common.PrincipalHasRole;

                roles.Insert(r1, r2, r25);
                principals.Insert(u1, u2, u3, u5);
                membership.Insert(new[] { // Non-domain users and roles.
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                });

                // Recompute membership relations:

                var u1r1 = membership.Query(m => m.Principal.Name.Contains(@"\u1")).Single();
                membership.Delete(new[] { u1r1 }, checkUserPermissions: false);
                Assert.AreEqual(@"\u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "modified membership");

                repository.Common.PrincipalHasRole.RecomputeFromActiveDirectoryPrincipalHasRole();
                Assert.AreEqual(@"\u1-\r1, \u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "recomputed membership");
            }
        }
Exemplo n.º 19
0
        public void SimpleReference()
        {
            using (var scope = TestScope.Create())
            {
                var repository = scope.Resolve <Common.DomRepository>();

                Guid refID = Guid.NewGuid();
                scope.Resolve <ISqlExecuter>().ExecuteSql(new[]
                {
                    "DELETE FROM TestBrowse.Source;",
                    "DELETE FROM TestBrowse.Other;",
                    "INSERT INTO TestBrowse.Other (ID, Name) SELECT '" + refID + "', 'abc';",
                    "INSERT INTO TestBrowse.Source (RefID) SELECT '" + refID + "';",
                });

                Assert.AreEqual("abc", repository.TestBrowse.Source.Query().ToArray().Select(item => item.Ref != null ? item.Ref.Name : null).Single(), "separated loading with null checking");
                Assert.AreEqual("abc", repository.TestBrowse.Source.Query().Select(item => item.Ref != null ? item.Ref.Name : null).Single(), "all in one query with null checking");

                Assert.AreEqual("abc", repository.TestBrowse.Source.Query().Select(item => item.Ref.Name).Single(), "all in one query");
                Assert.AreEqual("abc", repository.TestBrowse.Source.Query().ToArray().Select(item => item.Ref.Name).Single(), "separated loading");
            }
        }
Exemplo n.º 20
0
        public void ComputeOnAuthorization()
        {
            using (var scope = TestScope.Create("u1-r1 u1-r12 u2-r12 u2-r2", null))
            {
                // Insert test data:

                var u1  = scope.NewPrincipal("u1");
                var u2  = scope.NewPrincipal("u2");
                var u3  = scope.NewPrincipal("u3");
                var u5  = scope.NewPrincipal("u5", domain: false);
                var r1  = scope.NewRole("r1");
                var r2  = scope.NewRole("r2");
                var r25 = scope.NewRole("r25", domain: false);

                var roles      = scope.Resolve <GenericRepository <Common.Role> >();
                var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();

                roles.Insert(r1, r2, r25);
                principals.Insert(u1, u2, u3, u5);
                membership.Delete(membership.Load());
                membership.Insert(new[] { // Non-domain users and roles.
                    new Common.PrincipalHasRole {
                        PrincipalID = u2.ID, RoleID = r25.ID
                    },
                    new Common.PrincipalHasRole {
                        PrincipalID = u5.ID, RoleID = r25.ID
                    }
                });

                // Recompute membership on authorization:

                var authorizationProvider = scope.Resolve <CommonAuthorizationProvider>();

                Assert.AreEqual(@"\u2-r25, u5-r25", scope.ReportMembership());

                {
                    var userRoles = authorizationProvider.GetUsersRoles(u1);
                    Assert.AreEqual(@"\r1", scope.ReportRoles(userRoles));
                    Assert.AreEqual(@"\u1-\r1, \u2-r25, u5-r25", scope.ReportMembership(), "membership recomputed on authorization u1");
                }

                {
                    var userRoles = authorizationProvider.GetUsersRoles(u2);
                    Assert.AreEqual(@"\r2, r25", scope.ReportRoles(userRoles), "mixed membership");
                    Assert.AreEqual(@"\u1-\r1, \u2-\r2, \u2-r25, u5-r25", scope.ReportMembership(), "membership recomputed on authorization u2");
                }

                AuthorizationDataCache.ClearCache();
                membership.Delete(membership.Load());
                Assert.AreEqual(@"", scope.ReportMembership(), "membership deleted");

                {
                    var userRoles = authorizationProvider.GetUsersRoles(u1);
                    Assert.AreEqual(@"\r1", scope.ReportRoles(userRoles));
                    Assert.AreEqual(@"\u1-\r1", scope.ReportMembership(), "membership recomputed on authorization u1");
                }

                {
                    var userRoles = authorizationProvider.GetUsersRoles(u2);
                    Assert.AreEqual(@"\r2", scope.ReportRoles(userRoles));
                    Assert.AreEqual(@"\u1-\r1, \u2-\r2", scope.ReportMembership(), "membership recomputed on authorization u2");
                }
            }
        }
Exemplo n.º 21
0
        public void ParallelRequestsTryCreatePrincipal()
        {
            string u1Name           = TestName + "U";
            string r1Name           = TestName + "P";
            string commonTestSuffix = Guid.NewGuid().ToString().Replace("-", "");

            IPrincipal u1Prototype;

            Common.Role      r1;
            RhetosAppOptions rhetosAppOptions;

            using (var scope = TestScope.Create($"{u1Name}-{r1Name}", commonTestSuffix))
            {
                rhetosAppOptions = scope.Resolve <RhetosAppOptions>();

                u1Prototype = scope.NewPrincipal(u1Name);

                var roles = scope.Resolve <GenericRepository <Common.Role> >();
                r1 = scope.NewRole(r1Name);
                roles.Insert(r1);

                scope.CommitAndClose();
            }

            rhetosAppOptions.AuthorizationAddUnregisteredPrincipals = true;

            for (int test = 0; test < 5; test++)
            {
                Console.WriteLine("Test: " + test);

                // Test setup: PrincipalHasRole is deleted to make sure it is not up-to-date.
                // PrincipalHasRole will be recomputed when reading PrincipalHasRole.
                using (var scope = TestScope.Create($"{u1Name}-{r1Name}", commonTestSuffix))
                {
                    var principals = scope.Resolve <GenericRepository <IPrincipal> >();
                    principals.Delete(principals.Load(p => p.Name.Contains(TestName)));

                    var membership = scope.Resolve <GenericRepository <Common.PrincipalHasRole> >();
                    membership.Delete(membership.Load());
                    Assert.AreEqual(@"", scope.ReportMembership(), "Initial empty membership.");
                    AuthorizationDataCache.ClearCache();

                    scope.CommitAndClose();
                }

                // Recompute membership on authorization with multiple parallel requests:
                Parallel.For(0, 4, thread =>
                {
                    using (var scope = TestScope.Create($"{u1Name}-{r1Name}", commonTestSuffix,
                                                        builder => builder.RegisterInstance(rhetosAppOptions).ExternallyOwned()))
                    {
                        var authorizationData     = scope.Resolve <IAuthorizationData>();
                        var authorizationProvider = scope.Resolve <CommonAuthorizationProvider>();

                        PrincipalInfo u1 = authorizationData.GetPrincipal(u1Prototype.Name); // First call will automatically create a new principal, see AuthorizationAddUnregisteredPrincipals above.
                        var userRoles    = authorizationProvider.GetUsersRoles(u1);
                        Assert.AreEqual($@"\{r1Name}", scope.ReportRoles(userRoles), "User's roles should be recomputed.");
                        Assert.AreEqual($@"\{u1Name}-\{r1Name}", scope.ReportMembership(), "Updated role membership");

                        scope.CommitAndClose();
                    }
                });
            }
        }