public void Test_CustomAction_OnSaving() { var provider = new Mock <AuditDataProvider>(); provider.Setup(p => p.Serialize(It.IsAny <string>())).CallBase(); //provider.Setup(p => p.InsertEvent(It.IsAny<AuditEvent>())).Returns((AuditEvent e) => e.Comments); var eventType = "event type 1"; var target = "test"; var comment = "comment test"; Core.Configuration.AddCustomAction(ActionType.OnEventSaving, scope => { scope.Comment(comment); }); AuditEvent ev; using (var scope = new AuditScopeFactory().Create(eventType, () => target, EventCreationPolicy.Manual, provider.Object)) { ev = scope.Event; scope.Save(); } Core.Configuration.ResetCustomActions(); Assert.True(ev.Comments.Contains(comment)); provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Once); }
public void Test_ScopeSaveMode_Manual() { var modes = new List <SaveMode>(); Audit.Core.Configuration.Setup() .UseDynamicProvider(x => x .OnInsert(ev => { }) .OnReplace((id, ev) => { })) .WithCreationPolicy(EventCreationPolicy.Manual) .WithAction(a => a .OnEventSaving(scope => { modes.Add(scope.SaveMode); })); using (var scope = new AuditScopeFactory().Create(new AuditScopeOptions() { })) { scope.Save(); } Assert.AreEqual(1, modes.Count); Assert.AreEqual(SaveMode.Manual, modes[0]); }
public void Test_DynamicDataProvider() { int onInsertCount = 0, onReplaceCount = 0, onInsertOrReplaceCount = 0; Core.Configuration.Setup() .UseDynamicProvider(config => config .OnInsert(ev => onInsertCount++) .OnReplace((obj, ev) => onReplaceCount++) .OnInsertAndReplace(ev => onInsertOrReplaceCount++)); var scope = new AuditScopeFactory().Create("et1", null, EventCreationPolicy.Manual, null); scope.Save(); scope.SetCustomField("field", "value"); Assert.AreEqual(1, onInsertCount); Assert.AreEqual(0, onReplaceCount); Assert.AreEqual(1, onInsertOrReplaceCount); scope.Save(); Assert.AreEqual(1, onInsertCount); Assert.AreEqual(1, onReplaceCount); Assert.AreEqual(2, onInsertOrReplaceCount); }
public void Test_EventCreationPolicy_Manual() { var provider = new Mock <AuditDataProvider>(); provider.Setup(p => p.InsertEvent(It.IsAny <AuditEvent>())).Returns(() => Guid.NewGuid()); Core.Configuration.DataProvider = provider.Object; using (var scope = new AuditScopeFactory().Create("SomeEvent", () => "target", EventCreationPolicy.Manual, null)) { scope.Comment("test"); } provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Never); using (var scope = new AuditScopeFactory().Create("SomeEvent", () => "target", EventCreationPolicy.Manual, null)) { scope.Comment("test"); scope.Save(); scope.Comment("test2"); scope.Save(); } provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Once); provider.Verify(p => p.ReplaceEvent(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Once); }
public void Test_EventCreationPolicy_InsertOnEnd() { var provider = new Mock <AuditDataProvider>(); Core.Configuration.DataProvider = provider.Object; using (var scope = new AuditScopeFactory().Create("SomeEvent", () => "target", EventCreationPolicy.InsertOnEnd, null)) { scope.Comment("test"); scope.Save(); // this should do nothing because of the creation policy (this is no more true, since v 4.6.2) } provider.Verify(p => p.ReplaceEvent(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never); provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Exactly(2)); }
public void Test_TwoScopes() { var provider = new Mock <AuditDataProvider>(); provider.Setup(p => p.InsertEvent(It.IsAny <AuditEvent>())).Returns(() => Guid.NewGuid()); Core.Configuration.DataProvider = provider.Object; var scope1 = new AuditScopeFactory().Create(new AuditScopeOptions("SomeEvent1", null, new { @class = "class value1", DATA = 111 }, null, EventCreationPolicy.Manual)); scope1.Save(); var scope2 = new AuditScopeFactory().Create(new AuditScopeOptions("SomeEvent2", null, new { @class = "class value2", DATA = 222 }, null, EventCreationPolicy.Manual)); scope2.Save(); Assert.NotNull(scope1.EventId); Assert.NotNull(scope2.EventId); Assert.AreNotEqual(scope1.EventId, scope2.EventId); provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Exactly(2)); }
public void Test_AuditDisable_AllDisabled() { var list = new List <AuditEvent>(); Audit.Core.Configuration.Setup() .AuditDisabled(true) .Use(x => x .OnInsertAndReplace(ev => { list.Add(ev); })) .WithCreationPolicy(EventCreationPolicy.InsertOnStartReplaceOnEnd); using (var scope = new AuditScopeFactory().Create("", null, null, null, null)) { scope.Save(); scope.SaveAsync().Wait(); } Audit.Core.Configuration.AuditDisabled = false; Assert.AreEqual(0, list.Count); }
public void TestSave() { var provider = new Mock <AuditDataProvider>(); provider.Setup(p => p.Serialize(It.IsAny <string>())).CallBase(); Core.Configuration.DataProvider = provider.Object; var target = "initial"; var eventType = "SomeEvent"; AuditEvent ev; using (var scope = new AuditScopeFactory().Create(eventType, () => target, EventCreationPolicy.InsertOnEnd, null)) { ev = scope.Event; scope.Comment("test"); scope.SetCustomField <string>("custom", "value"); target = "final"; scope.Save(); // this should do nothing because of the creation policy (this no more true since v4.6.2) provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Once); } Assert.AreEqual(eventType, ev.EventType); Assert.True(ev.Comments.Contains("test")); provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Exactly(2)); }