コード例 #1
0
ファイル: AuditLogger.cs プロジェクト: zarinbal/MVC5.Template
        public virtual void Log(LoggableEntity entity)
        {
            AuditLog log = new AuditLog();
            log.AccountId = AccountId ?? HttpContext.Current.User.Identity.Name;
            log.AccountId = !String.IsNullOrEmpty(log.AccountId) ? log.AccountId : null;
            log.Changes = entity.ToString();
            log.EntityName = entity.Name;
            log.Action = entity.Action;
            log.EntityId = entity.Id;

            Context.Set<AuditLog>().Add(log);
        }
コード例 #2
0
        public virtual void Log(LoggableEntity entity)
        {
            AuditLog log = new AuditLog();

            log.AccountId  = AccountId ?? HttpContext.Current.User.Identity.Name;
            log.AccountId  = !String.IsNullOrEmpty(log.AccountId) ? log.AccountId : null;
            log.Changes    = entity.ToString();
            log.EntityName = entity.Name;
            log.Action     = entity.Action;
            log.EntityId   = entity.Id;

            Context.Set <AuditLog>().Add(log);
        }
コード例 #3
0
        public void Log_Added()
        {
            entry.State = EntityState.Added;

            logger.Log(new[] { entry });
            logger.Save();

            LoggableEntity expected = new LoggableEntity(entry);
            AuditLog actual = context.Set<AuditLog>().Single();

            Assert.Equal(expected.ToString(), actual.Changes);
            Assert.Equal(expected.Name, actual.EntityName);
            Assert.Equal(expected.Action, actual.Action);
            Assert.Equal(expected.Id(), actual.EntityId);
            Assert.Equal(1, actual.AccountId);
        }
コード例 #4
0
ファイル: AuditLogger.cs プロジェクト: zarinbal/MVC5.Template
 public virtual void Log(IEnumerable<DbEntityEntry<BaseModel>> entries)
 {
     foreach (DbEntityEntry<BaseModel> entry in entries)
     {
         switch (entry.State)
         {
             case EntityState.Added:
             case EntityState.Deleted:
             case EntityState.Modified:
                 LoggableEntity entity = new LoggableEntity(entry);
                 if (entity.Properties.Any())
                     Log(entity);
                 break;
         }
     }
 }
コード例 #5
0
        public void LoggableEntity_CreatesPropertiesForModifiedEntity()
        {
            String title = model.Title;
            entry.State = EntityState.Modified;
            entry.CurrentValues["Title"] = "Role";
            entry.OriginalValues["Title"] = "Role";

            IEnumerator<LoggableProperty> expected = new List<LoggableProperty> { new LoggableProperty(entry.Property("Title"), title) }.GetEnumerator();
            IEnumerator<LoggableProperty> actual = new LoggableEntity(entry).Properties.GetEnumerator();

            while (expected.MoveNext() | actual.MoveNext())
            {
                Assert.Equal(expected.Current.IsModified, actual.Current.IsModified);
                Assert.Equal(expected.Current.ToString(), actual.Current.ToString());
            }
        }
コード例 #6
0
 public void Log(IEnumerable <DbEntityEntry <BaseModel> > entries)
 {
     foreach (DbEntityEntry <BaseModel> entry in entries)
     {
         switch (entry.State)
         {
         case EntityState.Added:
         case EntityState.Deleted:
         case EntityState.Modified:
             LoggableEntity entity = new LoggableEntity(entry);
             if (entity.Properties.Any())
             {
                 Log(entity);
             }
             break;
         }
     }
 }
コード例 #7
0
        public void LoggableEntity_CreatesPropertiesForAttachedEntity()
        {
            context.Dispose();
            String title = model.Title;
            context = new TestingContext();
            context.Set<Role>().Attach(model);

            entry = context.Entry<BaseModel>(model);
            entry.OriginalValues["Title"] = "Role";
            entry.CurrentValues["Title"] = "Role";
            entry.State = EntityState.Modified;

            IEnumerator<LoggableProperty> expected = new List<LoggableProperty> { new LoggableProperty(entry.Property("Title"), title) }.GetEnumerator();
            IEnumerator<LoggableProperty> actual = new LoggableEntity(entry).Properties.GetEnumerator();

            while (expected.MoveNext() | actual.MoveNext())
            {
                Assert.Equal(expected.Current.IsModified, actual.Current.IsModified);
                Assert.Equal(expected.Current.ToString(), actual.Current.ToString());
            }
        }
コード例 #8
0
        public void Log_DoesNotSave()
        {
            entry.State = EntityState.Added;
            LoggableEntity entity = new LoggableEntity(entry);
            HttpContext.Current = HttpContextFactory.CreateHttpContext();

            logger.Log(entity);

            Assert.Empty(context.Set<AuditLog>());
        }
コード例 #9
0
        public void Log_AddsLogToTheSet(String accountId, String identityName, String expectedAccountId)
        {
            HttpContext.Current = HttpContextFactory.CreateHttpContext();
            HttpContext.Current.User.Identity.Name.Returns(identityName);
            LoggableEntity entity = new LoggableEntity(entry);
            logger = new AuditLogger(context, accountId);

            logger.Log(entity);

            AuditLog actual = context.ChangeTracker.Entries<AuditLog>().First().Entity;
            LoggableEntity expected = entity;

            Assert.Equal(expectedAccountId, actual.AccountId);
            Assert.Equal(expected.ToString(), actual.Changes);
            Assert.Equal(expected.Name, actual.EntityName);
            Assert.Equal(expected.Action, actual.Action);
            Assert.Equal(expected.Id, actual.EntityId);
        }
コード例 #10
0
 public void Log(LoggableEntity entity)
 {
     Entities.Add(entity);
 }
コード例 #11
0
        public void LoggableEntity_SetsEntityTypeNameFromProxy()
        {
            String actual = new LoggableEntity(entry).Name;
            String expected = typeof(Role).Name;

            Assert.Equal("System.Data.Entity.DynamicProxies", entry.Entity.GetType().Namespace);
            Assert.Equal(expected, actual);
        }
コード例 #12
0
        public void LoggableEntity_SetsEntityId()
        {
            Int32 actual = new LoggableEntity(entry).Id();
            Int32 expected = model.Id;

            Assert.Equal(expected, actual);
        }
コード例 #13
0
        public void Save_LogsOnce(Int32? accountId, String identityName, Int32? expectedAccountId)
        {
            HttpContext.Current = HttpContextFactory.CreateHttpContext();
            HttpContext.Current.User.Identity.Name.Returns(identityName);
            LoggableEntity entity = new LoggableEntity(entry);
            logger = new AuditLogger(context, accountId);

            logger.Log(entity);
            logger.Save();
            logger.Save();

            AuditLog actual = context.Set<AuditLog>().Single();
            LoggableEntity expected = entity;

            Assert.Equal(expectedAccountId, actual.AccountId);
            Assert.Equal(expected.ToString(), actual.Changes);
            Assert.Equal(expected.Name, actual.EntityName);
            Assert.Equal(expected.Action, actual.Action);
            Assert.Equal(expected.Id(), actual.EntityId);
        }
コード例 #14
0
        public void LoggableEntity_SetsAction()
        {
            entry.State = EntityState.Deleted;

            String actual = new LoggableEntity(entry).Action;
            String expected = entry.State.ToString();

            Assert.Equal(expected, actual);
        }
コード例 #15
0
        private void AsssertProperties(DbPropertyValues originalValues)
        {
            IEnumerable<String> properties = originalValues.PropertyNames;

            IEnumerator<LoggableProperty> actual = new LoggableEntity(entry).Properties.GetEnumerator();
            IEnumerator<LoggableProperty> expected = properties.Where(property => property != "Id")
                .Select(name => new LoggableProperty(entry.Property(name), originalValues[name])).GetEnumerator();

            while (expected.MoveNext() | actual.MoveNext())
            {
                Assert.Equal(expected.Current.IsModified, actual.Current.IsModified);
                Assert.Equal(expected.Current.ToString(), actual.Current.ToString());
            }
        }
コード例 #16
0
        public void ToString_FormsEntityChanges()
        {
            StringBuilder changes = new StringBuilder();
            LoggableEntity loggableEntity = new LoggableEntity(entry);
            foreach (LoggableProperty property in loggableEntity.Properties)
                changes.AppendFormat("{0}{1}", property, Environment.NewLine);

            String actual = loggableEntity.ToString();
            String expected = changes.ToString();

            Assert.Equal(expected, actual);
        }
コード例 #17
0
        private void Logs(DbEntityEntry<BaseModel> entry)
        {
            LoggableEntity expected = new LoggableEntity(entry);
            logger.When(sub => sub.Log(Arg.Any<LoggableEntity>())).DoNotCallBase();
            logger.When(sub => sub.Log(Arg.Any<LoggableEntity>())).Do(info =>
            {
                LoggableEntity actual = info.Arg<LoggableEntity>();

                Assert.Equal(expected.ToString(), actual.ToString());
                Assert.Equal(expected.Action, actual.Action);
                Assert.Equal(expected.Name, actual.Name);
                Assert.Equal(expected.Id, actual.Id);
            });

            logger.Log(new[] { entry });

            logger.ReceivedWithAnyArgs().Log(expected);
        }
コード例 #18
0
 public void Log(LoggableEntity entity)
 {
     Entities.Add(entity);
 }
コード例 #19
0
        public void Log_Entity()
        {
            LoggableEntity entity = new LoggableEntity(entry);

            logger.Log(entity);
            logger.Save();

            AuditLog actual = context.Set<AuditLog>().Single();
            LoggableEntity expected = entity;

            Assert.Equal(expected.ToString(), actual.Changes);
            Assert.Equal(expected.Name, actual.EntityName);
            Assert.Equal(expected.Action, actual.Action);
            Assert.Equal(expected.Id(), actual.EntityId);
            Assert.Equal(1, actual.AccountId);
        }
コード例 #20
0
ファイル: AuditLogger.cs プロジェクト: olegil/MVC.Template
 public virtual void Log(LoggableEntity entity)
 {
     context.Set <AuditLog>().Add(new AuditLog(entity.Action, entity.Name, entity.Id, entity.ToString()));
 }
コード例 #21
0
        public void LoggableEntity_SetsEntityId()
        {
            String actual = new LoggableEntity(entry).Id;
            String expected = model.Id;

            Assert.Equal(expected, actual);
        }
コード例 #22
0
        public void LoggableEntity_SetsEntityTypeName()
        {
            entry = context.Entry<BaseModel>(context.Set<Role>().Add(new Role()));

            String actual = new LoggableEntity(entry).Name;
            String expected = typeof(Role).Name;

            Assert.NotEqual("System.Data.Entity.DynamicProxies", entry.Entity.GetType().Namespace);
            Assert.Equal(expected, actual);
        }