public static void SeedUserWithTwoRoles(this ExtraAuthorizeDbContext context, string userId = "userId")
        {
            var userStatus = RoleToPermissions.CreateRoleWithPermissions(
                "TestRole1", "TestRole1", new List <Permissions> {
                Permissions.StockRead
            }, context);

            userStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(userStatus.Result);
            userStatus = RoleToPermissions.CreateRoleWithPermissions(
                "TestRole2", "TestRole1", new List <Permissions> {
                Permissions.SalesSell
            }, context);
            userStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(userStatus.Result);

            var roleStatus = UserToRole.AddRoleToUser(userId, "TestRole1", context);

            roleStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(roleStatus.Result);
            roleStatus = UserToRole.AddRoleToUser(userId, "TestRole2", context);
            roleStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(roleStatus.Result);

            context.SaveChanges();
        }
        public async Task TestAddRoleToUseTrigger()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, new AuthChanges()))
            {
                context.Database.EnsureCreated();
                var rolToPer = RoleToPermissions.CreateRoleWithPermissions
                                   ("test", "test", new List <Permissions> {
                    Permissions.AccessAll
                }, context).Result;
                context.Add(rolToPer);
                context.SaveChanges();

                //ATTEMPT
                var userToRole = new UserToRole("test", rolToPer);
                context.Add(userToRole);
                await context.SaveChangesAsync();

                //VERIFY
                context.TimeStores.Count().ShouldEqual(1);
                context.UserToRoles.Count().ShouldEqual(1);
            }
        }
Exemple #3
0
        public void TestUpdatePermissionsInRole()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, null))
            {
                context.Database.EnsureCreated();

                var createStatus = RoleToPermissions.CreateRoleWithPermissions(
                    "test", "test", new List <Permissions> {
                    Permissions.StockRead
                }, context);
                createStatus.IsValid.ShouldBeTrue(createStatus.GetAllErrors());
                context.Add(createStatus.Result);
                context.SaveChanges();

                //ATTEMPT
                var roleToUpdate = context.Find <RoleToPermissions>("test");
                roleToUpdate.UpdatePermissionsInRole(new List <Permissions> {
                    Permissions.StockAddNew, Permissions.StockRemove
                });
                context.SaveChanges();
            }
            using (var context = new ExtraAuthorizeDbContext(options, null))
            {
                //VERIFY
                context.RolesToPermissions.Single().PermissionsInRole
                .ShouldEqual(new List <Permissions> {
                    Permissions.StockAddNew, Permissions.StockRemove
                });
            }
        }
        public void TestUpdateRoleTrigger()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, new AuthChanges()))
            {
                context.Database.EnsureCreated();
                var rolToPer = RoleToPermissions.CreateRoleWithPermissions
                                   ("test", "test", new List <Permissions> {
                    Permissions.AccessAll
                }, context).Result;
                context.Add(rolToPer);
                context.SaveChanges();

                //ATTEMPT
                rolToPer.Update("test", new List <Permissions> {
                    Permissions.EmployeeRead
                });
                context.SaveChanges();

                //VERIFY
                context.TimeStores.Count().ShouldEqual(1);
                context.RolesToPermissions.Count().ShouldEqual(1);
            }
        }
Exemple #5
0
        public void TestDeleteRole()
        {
            //SETUP
            var fakeAuthChanges = new FakeAuthChanges();
            var options         = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, fakeAuthChanges))
            {
                context.Database.EnsureCreated();
                var createStatus = RoleToPermissions.CreateRoleWithPermissions(
                    "test", "test", new List <Permissions> {
                    Permissions.StockRead
                }, context);
                createStatus.IsValid.ShouldBeTrue(createStatus.GetAllErrors());
                context.Add(createStatus.Result);
                context.SaveChanges();

                //ATTEMPT
                var roleToDelete = context.Find <RoleToPermissions>("test");
                context.Remove(roleToDelete);
                context.SaveChanges();

                //VERIFY
                context.RolesToPermissions.Any().ShouldBeFalse();
            }
        }
        public void TestUpdateRoleTrigger()
        {
            //SETUP
            var fakeCache = new FakeSimpleCache();
            var options   = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, fakeCache))
            {
                context.Database.EnsureCreated();
                var rolToPer = RoleToPermissions.CreateRoleWithPermissions
                                   ("test", "test", new List <Permissions> {
                    Permissions.AccessAll
                }, context).Result;
                context.Add(rolToPer);
                context.SaveChanges();

                //ATTEMPT
                rolToPer.UpdatePermissionsInRole(new List <Permissions> {
                    Permissions.EmployeeRead
                });
                context.SaveChanges();

                //VERIFY
                fakeCache.AddOrUpdateCalled.ShouldBeTrue();
                context.RolesToPermissions.Count().ShouldEqual(1);
            }
        }
        public void CheckAddNewRole(string roleName, string description, ICollection <Permissions> permissions)
        {
            var status = RoleToPermissions.CreateRoleWithPermissions(roleName, description, permissions, _context);

            if (status.IsValid)
            {
                //Note that CreateRoleWithPermissions will return a invalid status if the role is already present.
                _context.Add(status.Result);
            }
        }
        public static void SeedCacheRole(this ExtraAuthorizeDbContext context, bool hasCache2, string userId = "userId")
        {
            var permissions = new List <Permissions> {
                Permissions.Cache1
            };

            if (hasCache2)
            {
                permissions.Add(Permissions.Cache2);
            }
            var userStatus = RoleToPermissions.CreateRoleWithPermissions(
                CacheRoleService.CacheRoleName, CacheRoleService.CacheRoleName, permissions, context);

            userStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(userStatus.Result);

            context.SaveChanges();
        }
Exemple #9
0
        public async Task TestSaveChangesAsyncOk()
        {
            //SETUP
            var options = this.CreateUniqueClassOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, null))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var entity = RoleToPermissions.CreateRoleWithPermissions(Guid.NewGuid().ToString(), "description",
                                                                         new List <Permissions> {
                    Permissions.Cache1
                }, context).Result;
                context.Add(entity);
                var numWritten = await context.SaveChangesAsync();

                //VERIFY
                numWritten.ShouldEqual(1);
            }
        }
        public void TestAddRoleNotTrigger()
        {
            //SETUP
            var fakeAuthChanges = new FakeAuthChanges();
            var options         = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, fakeAuthChanges))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var rolToPer = RoleToPermissions.CreateRoleWithPermissions
                                   ("test", "test", new List <Permissions> {
                    Permissions.AccessAll
                }, context).Result;
                context.Add(rolToPer);
                context.SaveChanges();

                //VERIFY
                fakeAuthChanges.CacheValueSet.ShouldBeFalse();
                context.RolesToPermissions.Count().ShouldEqual(1);
            }
        }
        public static void SeedUserWithDefaultPermissions(this ExtraAuthorizeDbContext context,
                                                          PaidForModules modules = PaidForModules.None, string userId = "userId")
        {
            var defaultPermissions = new List <Permissions> {
                Permissions.StockRead, Permissions.Feature1Access
            };

            var roleStatus = RoleToPermissions.CreateRoleWithPermissions(
                "TestRole1", "TestRole1", defaultPermissions, context);

            roleStatus.IsValid.ShouldBeTrue(roleStatus.GetAllErrors());
            context.Add(roleStatus.Result);

            var moduleUser = new ModulesForUser(userId, modules);

            context.Add(moduleUser);

            var userStatus = UserToRole.AddRoleToUser(userId, "TestRole1", context);

            userStatus.IsValid.ShouldBeTrue(roleStatus.GetAllErrors());
            context.Add(userStatus.Result);

            context.SaveChanges();
        }