using System; using Xunit; using Newtonsoft.Json.Linq; using FluentAssertions; using System.Collections.Generic; using System.Collections.Concurrent; namespace raven_integration { public class EventLog { /// /// /// [Fact] public async void ObjectLogWorks() { //CRUD a widget and confirm it logs properly //http://localhost:7575/api/v8.0/EventLog/UserLog?AyType=3&AyId=1 //http://localhost:7575/api/v8.0/EventLog/ObjectLog?AyType=2&AyId=242 //http://localhost:7575/api/v8.0/EventLog/UserLog?AyType=3&AyId=1&StartDate=2018-08-23&EndDate=2018-08-24 dynamic w = new JObject(); w.name = Util.Uniquify("EventLog Test WIDGET"); w.customFields = Util.WidgetRequiredCustomFieldsJsonString(); w.notes = "blah"; w.created = DateTime.Now.ToString(); w.dollarAmount = 2.22m; w.active = true; w.usertype = 1; //*** CREATED ApiResponse r2 = await Util.PostAsync("Widget", await Util.GetTokenAsync("InventoryFull"), w.ToString()); Util.ValidateDataReturnResponseOk(r2); long w2Id = r2.ObjectResponse["data"]["id"].Value(); ApiResponse EventLogResponse = await Util.GetAsync($"EventLog/ObjectLog?AyType=2&AyId={w2Id}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateHTTPStatusCode(EventLogResponse, 200); ((JArray)EventLogResponse.ObjectResponse["data"]["events"]).Count.Should().Be(1);//only one event so far EventLogResponse.ObjectResponse["data"]["events"][0]["date"].Value().Should().BeLessThan(new TimeSpan(1, 0, 0)).Before(DateTime.UtcNow);//should be less than one hour before now EventLogResponse.ObjectResponse["data"]["events"][0]["userId"].Should().NotBeNull(); EventLogResponse.ObjectResponse["data"]["events"][0]["event"].Value().Should().Be(1);//AyEvent 1 = created EventLogResponse.ObjectResponse["data"]["events"][0]["textra"].Should().BeNullOrEmpty(); //Get current user doing modifications ID long CurrentUserId = EventLogResponse.ObjectResponse["data"]["events"][0]["userId"].Value(); //*** RETRIEVED //Get one ApiResponse r3 = await Util.GetAsync("Widget/" + w2Id.ToString(), await Util.GetTokenAsync("InventoryFull")); Util.ValidateDataReturnResponseOk(r3); r3.ObjectResponse["data"]["name"].Value().Should().Be(w.name.ToString()); EventLogResponse = await Util.GetAsync($"EventLog/ObjectLog?AyType=2&AyId={w2Id}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateHTTPStatusCode(EventLogResponse, 200); //confirm event count, type and sort order (descending by date most recent first) ((JArray)EventLogResponse.ObjectResponse["data"]["events"]).Count.Should().Be(2); EventLogResponse.ObjectResponse["data"]["events"][0]["date"].Value().Should().BeLessThan(new TimeSpan(1, 0, 0)).Before(DateTime.UtcNow);//should be less than one hour before now EventLogResponse.ObjectResponse["data"]["events"][0]["userId"].Should().NotBeNull(); EventLogResponse.ObjectResponse["data"]["events"][0]["event"].Value().Should().Be(2);//AyEvent 2 = retrieved EventLogResponse.ObjectResponse["data"]["events"][0]["textra"].Should().BeNullOrEmpty(); //*** MODIFIED //PUT //update w2id w.name = Util.Uniquify("UPDATED VIA PUT EVENTLOG TEST WIDGET"); w.UserId = 1; w.concurrencyToken = r2.ObjectResponse["data"]["concurrencyToken"].Value(); ApiResponse PUTTestResponse = await Util.PutAsync("Widget/" + w2Id.ToString(), await Util.GetTokenAsync("InventoryFull"), w.ToString()); Util.ValidateHTTPStatusCode(PUTTestResponse, 200); //*** RETRIEVED //check PUT worked ApiResponse checkPUTWorked = await Util.GetAsync("Widget/" + w2Id.ToString(), await Util.GetTokenAsync("InventoryFull")); Util.ValidateNoErrorInResponse(checkPUTWorked); checkPUTWorked.ObjectResponse["data"]["name"].Value().Should().Be(w.name.ToString()); uint concurrencyToken = PUTTestResponse.ObjectResponse["data"]["concurrencyToken"].Value(); EventLogResponse = await Util.GetAsync($"EventLog/ObjectLog?AyType=2&AyId={w2Id}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateHTTPStatusCode(EventLogResponse, 200); ((JArray)EventLogResponse.ObjectResponse["data"]["events"]).Count.Should().Be(4); //put op is the second item in the list, top item is the recent fetch EventLogResponse.ObjectResponse["data"]["events"][1]["date"].Value().Should().BeLessThan(new TimeSpan(1, 0, 0)).Before(DateTime.UtcNow);//should be less than one hour before now EventLogResponse.ObjectResponse["data"]["events"][1]["userId"].Should().NotBeNull(); EventLogResponse.ObjectResponse["data"]["events"][1]["event"].Value().Should().Be(3);//AyEvent 3 = Modified EventLogResponse.ObjectResponse["data"]["events"][1]["textra"].Should().BeNullOrEmpty(); //Check user log for basic accessibility UserLog?UserId=7 EventLogResponse = await Util.GetAsync($"EventLog/UserLog?UserId={CurrentUserId}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateHTTPStatusCode(EventLogResponse, 200); ((JArray)EventLogResponse.ObjectResponse["data"]["events"]).Count.Should().BeGreaterOrEqualTo(4);//just one run of the above will be 4 events plus any others from other tests //Not sure of any easy way to assert the User log is correct other than the count as other tests running concurrently could easily skew this //DELETE ApiResponse DELETETestResponse = await Util.DeleteAsync("Widget/" + w2Id.ToString(), await Util.GetTokenAsync("InventoryFull")); Util.ValidateHTTPStatusCode(DELETETestResponse, 204); //All events should be cleared up on deletion with the sole exception of the deleted event EventLogResponse = await Util.GetAsync($"EventLog/ObjectLog?AyType=2&AyId={w2Id}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateHTTPStatusCode(EventLogResponse, 200); ((JArray)EventLogResponse.ObjectResponse["data"]["events"]).Count.Should().Be(1); EventLogResponse.ObjectResponse["data"]["events"][0]["date"].Value().Should().BeLessThan(new TimeSpan(1, 0, 0)).Before(DateTime.UtcNow);//should be less than one hour before now EventLogResponse.ObjectResponse["data"]["events"][0]["userId"].Should().NotBeNull(); EventLogResponse.ObjectResponse["data"]["events"][0]["event"].Value().Should().Be(0);//AyEvent 0 = deleted EventLogResponse.ObjectResponse["data"]["events"][0]["textra"].Value().Should().Be(w.name.ToString()); } /// /// /// [Fact] public async void EventLogLimitOffSetWorks() { var UniqueName = Util.Uniquify("EventLogLimitOffSetWorks"); //CREATE dynamic d = new JObject(); d.name = UniqueName; d.active = true; d.login = UniqueName; d.password = UniqueName; d.roles = 2;//bizadminfull needs widget rights d.userType = 3;//non scheduleable //Required by form custom rules d.notes = "notes"; d.customFields = Util.UserRequiredCustomFieldsJsonString(); ApiResponse a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), d.ToString()); Util.ValidateDataReturnResponseOk(a); long UserId = a.ObjectResponse["data"]["id"].Value(); //Loop and make 10 widgets for (int i = 0; i < 10; i++) { d = new JObject(); d.name = Util.Uniquify("EventLogLimitOffSetWorks"); d.customFields = Util.WidgetRequiredCustomFieldsJsonString(); d.dollarAmount = 1.11m; d.active = true; d.usertype = 1; d.notes = "note here"; a = await Util.PostAsync("Widget", await Util.GetTokenAsync(UniqueName, UniqueName), d.ToString()); Util.ValidateDataReturnResponseOk(a); } a = await Util.GetAsync($"EventLog/UserLog?UserId={UserId}&Offset=0&Limit=9", await Util.GetTokenAsync("BizAdminFull")); ((JArray)a.ObjectResponse["data"]["events"]).Count.Should().Be(9); //capture events, then compare to paged ones var eventList = ((JArray)a.ObjectResponse["data"]["events"]); List allEvents = new List(9); foreach (JObject o in eventList) { allEvents.Add(o["date"].Value() + o["objectType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } a = await Util.GetAsync($"EventLog/UserLog?UserId={UserId}&Offset=0&Limit=3", await Util.GetTokenAsync("BizAdminFull")); ((JArray)a.ObjectResponse["data"]["events"]).Count.Should().Be(3); var pageEventList = ((JArray)a.ObjectResponse["data"]["events"]); foreach (JObject o in pageEventList) { allEvents.Should().Contain(o["date"].Value() + o["objectType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } a = await Util.GetAsync($"EventLog/UserLog?UserId={UserId}&Offset=1&Limit=3", await Util.GetTokenAsync("BizAdminFull")); ((JArray)a.ObjectResponse["data"]["events"]).Count.Should().Be(3); pageEventList = ((JArray)a.ObjectResponse["data"]["events"]); foreach (JObject o in pageEventList) { allEvents.Should().Contain(o["date"].Value() + o["objectType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } a = await Util.GetAsync($"EventLog/UserLog?UserId={UserId}&Offset=2&Limit=3", await Util.GetTokenAsync("BizAdminFull")); ((JArray)a.ObjectResponse["data"]["events"]).Count.Should().Be(3); pageEventList = ((JArray)a.ObjectResponse["data"]["events"]); foreach (JObject o in pageEventList) { allEvents.Should().Contain(o["date"].Value() + o["objectType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } } //================================================== }//eoc }//eons