public void AuditActionForIHasRootEntity_SetsUpdateValuesCorrectly()
        {
            // Arrange
            var entity = new HasRootEntityForAuditing();
            var ev     = new AuditEvent
            {
                Environment = new AuditEventEnvironment()
                {
                    UserName = "******"
                },
                CustomFields = new Dictionary <string, object> {
                }
            };
            var entry = new EventEntry
            {
                PrimaryKey = new Dictionary <string, object> {
                    { "EntityId", "123" }
                },
                EntityType = entity.GetType(),
                Action     = "Update",
                Table      = "EntityTable",
                Entity     = entity
            };
            var audit = new AuditLog();

            // Act
            EFAuditServiceExtensions.AuditAction(ev, entry, audit);

            // Assert
            Assert.Equal(TestHelper.HasRootEntityRootEntityId, audit.RootId);
            Assert.Equal(TestHelper.HasRootEntityTypeString, audit.RootEntity);
        }
        public void AuditAction_UsesOverrideUserInsteadOfAppUserWhenProvided()
        {
            // Arrange
            var ev = new AuditEvent
            {
                Environment = new AuditEventEnvironment
                {
                    UserName = "******"
                },
                CustomFields = new Dictionary <string, object>
                {
                    { CustomFields.AuditDetails, "Notified" },
                    { CustomFields.AppUser, "User 1" },
                    { CustomFields.OverrideUser, "SYSTEM" }
                }
            };
            var entry = new EventEntry
            {
                PrimaryKey = new Dictionary <string, object> {
                    { "EntityId", "123" }
                },
                EntityType = typeof(Entity),
                Action     = "Update",
                Table      = "EntityTable"
            };
            var audit = new AuditLog();

            // Act
            EFAuditServiceExtensions.AuditAction(ev, entry, audit);

            // Assert
            Assert.Equal("SYSTEM", audit.AuditUser);
        }
Exemplo n.º 3
0
        public void AuditAction_SetsAuditUserAndAuditDetailsCorrectlyWhenProvided()
        {
            // Arrange
            var ev = new AuditEvent()
            {
                Environment = new AuditEventEnvironment()
                {
                    UserName = "******"
                },
                CustomFields = new Dictionary <string, object>
                {
                    { CustomFields.AuditDetails, "Notified" },
                    { CustomFields.AppUser, "User 1" }
                }
            };
            EventEntry entry = new EventEntry
            {
                PrimaryKey = new Dictionary <string, object> {
                    { "EntityId", "123" }
                },
                EntityType = typeof(Entity),
                Action     = "Update",
                Table      = "EntityTable"
            };
            AuditLog audit = new AuditLog();

            // Act
            EFAuditServiceExtensions.AuditAction(ev, entry, audit);

            // Assert
            Assert.Equal("User 1", audit.AuditUser);
            Assert.Equal("Notified", audit.AuditDetails);
        }
        public void AuditAction_SetsEnumValuesCorrectly()
        {
            // Arrange
            var ev = new AuditEvent
            {
                Environment = new AuditEventEnvironment()
                {
                    UserName = "******"
                },
                CustomFields = new Dictionary <string, object> {
                }
            };
            var entry = new EventEntry
            {
                PrimaryKey = new Dictionary <string, object> {
                    { "EntityId", "123" }
                },
                EntityType = typeof(Entity),
                Action     = "Update",
                Table      = "EntityTable",
                Changes    = new List <EventEntryChange>
                {
                    new EventEntryChange
                    {
                        ColumnName = "NameOfFather", NewValue = TestEnum.HankMarvin
                    }
                },
                ColumnValues = new Dictionary <string, object>
                {
                    { "Column1", "Value1" },
                    { "Column2", "Value2" }
                }
            };
            var audit = new AuditLog();

            // Act
            EFAuditServiceExtensions.AuditAction(ev, entry, audit);

            // Assert
            Assert.Equal("123", audit.OriginalId);
            Assert.Equal("Entity", audit.EntityType);
            Assert.Equal("Update", audit.EventType);

            const string expectedFromNullChangesJson = @"{""ColumnName"":""NameOfFather"",""NewValue"":""HankMarvin""}";

            Assert.Contains(expectedFromNullChangesJson, audit.AuditData);
            const string notExpectedColumnValuesJson = @"{""Column1"":""Value1"",""Column2"":""Value2""}";

            Assert.DoesNotContain(notExpectedColumnValuesJson, audit.AuditData);

            // Close enough when not injecting time services into the class
            Assert.InRange(audit.AuditDateTime, DateTime.Now.AddMinutes(-1), DateTime.Now);
            Assert.Equal("Env user", audit.AuditUser);
            Assert.Null(audit.AuditDetails);
        }