Beispiel #1
0
        public void Test_FileDataProvider_Loop()
        {
            var loop = new Loop()
            {
                Id = 1
            };

            loop.Inner = loop;

            var fdp = new FileDataProvider()
            {
                DirectoryPath   = _directory,
                FilenameBuilder = x => x.EventType
            };

            Configuration.DataProvider = fdp;
            var guid = "x" + Guid.NewGuid().ToString();

            AuditScope.Log(guid, loop);

            var ev = fdp.GetEvent(Path.Combine(_directory, guid));

            Assert.IsNotNull(ev);
            Assert.AreEqual(guid, ev.EventType);
            Assert.AreEqual(2, ev.CustomFields.Count);
            Assert.AreEqual(1, ev.CustomFields["Id"]);
            Assert.AreEqual(JObject.Parse("{\"Id\": 1}").ToString(), ev.CustomFields["Inner"].ToString());
        }
Beispiel #2
0
        public void Test_SqlServer_DbConnection()
        {
            var sqlDp = new SqlDataProvider(_ => _
                                            .DbConnection(ev => GetConnection("data source=localhost;initial catalog=Audit;integrated security=true;"))
                                            .TableName(ev => "Event")
                                            .IdColumnName(ev => "EventId")
                                            .JsonColumnName(ev => "Data")
                                            .LastUpdatedColumnName("LastUpdatedDate")
                                            .CustomColumn("EventType", ev => ev.EventType));

            var ids = new List <object>();

            Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaved, scope =>
            {
                ids.Add(scope.EventId);
            });

            Audit.Core.Configuration.Setup()
            .UseCustomProvider(sqlDp);

            AuditScope.Log("test1", new { Name = "John" });
            AuditScope.Log("test2", new { Name = "Mary" });

            Assert.AreEqual(2, ids.Count);

            var ev1 = sqlDp.GetEvent(ids[0]);
            var ev2 = sqlDp.GetEvent(ids[1]);

            Assert.AreEqual("John", ev1.CustomFields["Name"].ToString());
            Assert.AreEqual("Mary", ev2.CustomFields["Name"].ToString());
        }
Beispiel #3
0
        public void Test_StartAndSave()
        {
            var provider = new Mock <AuditDataProvider>();

            provider.Setup(p => p.Serialize(It.IsAny <string>())).CallBase();

            var eventType = "event type";

            AuditScope.Log(eventType, new { ExtraField = "extra value" });

            AuditScope.CreateAndSave(eventType, new { Extra1 = new { SubExtra1 = "test1" }, Extra2 = "test2" }, provider.Object);
            provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Once);
            provider.Verify(p => p.ReplaceEvent(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never);
        }
Beispiel #4
0
        public void Test_AuditScope_Log()
        {
            Audit.Core.Configuration.SystemClock = new MyClock();
            var evs = new List <AuditEvent>();

            Audit.Core.Configuration.Setup()
            .Use(x => x.OnInsertAndReplace(ev => { evs.Add(ev); }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd);
            AuditScope.Log("test", new { field1 = "one" });

            Assert.AreEqual(1, evs.Count);
            Assert.AreEqual("test", evs[0].EventType);
            Assert.AreEqual("one", evs[0].CustomFields["field1"]);
            Assert.IsTrue(evs[0].Environment.CallingMethodName.Contains("Test_AuditScope_Log"));
        }
Beispiel #5
0
        public void Test_ScopeActionsStress()
        {
            int counter  = 0;
            int counter2 = 0;
            int counter3 = 0;
            int MAX      = 200;

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(_ => _.OnInsert(ev =>
            {
                System.Threading.Interlocked.Increment(ref counter);
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd)
            .WithAction(_ => _.OnEventSaving(ev =>
            {
                System.Threading.Interlocked.Increment(ref counter2);
            }))
            .WithAction(_ => _.OnScopeCreated(ev =>
            {
                System.Threading.Interlocked.Increment(ref counter3);
            }));

            var tasks = new List <Task>();

            for (int i = 0; i < MAX; i++)
            {
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    AuditScope.Log("LoginSuccess", new { username = "******", id = i });
                    Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, ev =>
                    {
                        //do nothing, just bother
                        var d = ev.Event.Duration * 1234567;
                    });
                    AuditScope.CreateAndSave("LoginFailed", new { username = "******", id = i * -1 });
                }));
            }
            Task.WaitAll(tasks.ToArray());
            Assert.AreEqual(MAX * 2, counter);
            Assert.AreEqual(MAX * 2, counter2);
            Assert.AreEqual(MAX * 2, counter3);
        }