public void EntityDeletedHandler_HasNoEntityPermissions()
        {
            var expPr  = new PermissionRecord("c", "r", "u", "d");
            var userId = "uId";
            var dbUp   = new UserPermissions
            {
                UserId = userId,
            };
            var pm = new Mock <IPermissionManager>();

            pm.Setup(p => p.GetUserPermissions(It.IsAny <string>())).ReturnsAsync(dbUp);
            var sp = new Mock <IServiceProvider>();

            sp.Setup(s => s.GetService(typeof(IPermissionManager))).Returns(pm.Object);
            var data = new TestClass
            {
                Value = 123
            };

            var ed = new DomainEvent
            {
                Data = data,
                PerformedByUserId = userId
            };
            var ph = new DefaultPermissionsEventsHandler(sp.Object);

            ph.PermissionDeletedHandler(ed);
            Thread.Sleep(50);

            pm.Verify(p => p.UpdateUserPermissions(It.IsAny <UserPermissions>()), Times.Never);
        }
        public void EntityDeletedHandler_NotDomainModel()
        {
            var ed = new DomainEvent
            {
                Data = "some=string",
                PerformedByUserId = "uId"
            };
            var ph = new DefaultPermissionsEventsHandler(null);

            ph.PermissionDeletedHandler(ed);
            Thread.Sleep(50);
        }
        public void EntityDeletedHandler_UpdatesExistsUserPermissions()
        {
            var eId = "eId";
            var ek  = "ek";

            var expPr = new PermissionRecord("c", "r", "u", "d");
            var ecr   = new EntityConfigRecord {
                Type = typeof(TestClass), PermissionRecord = expPr,
            };

            var sp = new Mock <IServiceProvider>();

            sp.Setup(s => s.GetService(typeof(IEnumerable <EntityConfigRecord>))).Returns(new[] { ecr });

            var expEntityPermissions = new EntityPermission
            {
                EntityId       = "eId1",
                EntityKey      = "ek1",
                PermissionKeys = new[] { "r1", "u1", "d1", },
            };

            ecr = new EntityConfigRecord {
                EntityKey = ek, Type = typeof(TestClass), PermissionRecord = expPr
            };
            var userId = "uId";
            var dbUp   = new UserPermissions
            {
                UserId            = userId,
                EntityPermissions = new[] {
                    expEntityPermissions,
                    new EntityPermission
                    {
                        EntityId       = eId,
                        EntityKey      = ek,
                        PermissionKeys = new[] { "r", "u", "d", },
                    },
                }
            };
            var pm = new Mock <IPermissionManager>();

            pm.Setup(p => p.GetUserPermissions(It.IsAny <string>())).ReturnsAsync(dbUp);
            sp.Setup(s => s.GetService(typeof(IPermissionManager))).Returns(pm.Object);
            var data = new TestClass
            {
                Id    = eId,
                Value = 123
            };

            var ed = new DomainEvent
            {
                Data = data,
                PerformedByUserId = userId,
                WorkContext       = new WorkContext {
                    CurrentEntityConfigRecord = ecr
                },
            };
            var ph = new DefaultPermissionsEventsHandler(sp.Object);

            ph.PermissionDeletedHandler(ed);
            Thread.Sleep(150);

            pm.Verify(p => p.UpdateUserPermissions(It.Is <UserPermissions>(
                                                       up => up.UserId == userId &&
                                                       up.EntityPermissions.Count() == 1 &&
                                                       up.EntityPermissions.ElementAt(0) == expEntityPermissions)),
                      Times.Once);
        }