using System; using Xunit; using Newtonsoft.Json.Linq; using FluentAssertions; using System.Collections.Generic; using System.Collections.Concurrent; using System.ComponentModel.DataAnnotations; namespace raven_integration { public class EventLog { /// /// /// [Fact] public async Task ObjectLogWorks() { //CRUD a project and confirm it logs properly var projectName = Util.Uniquify("EventLogTestProject"); var dateStarted = DateTime.Now.ToString("o"); var payload = $$""" {"id":0,"concurrency":0,"name":"{{projectName}}","active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"dateStarted":"{{dateStarted}}","dateCompleted":null,"projectOverseerId":null,"accountNumber":null} """; //DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") //*** CREATED ApiResponse r2 = await Util.PostAsync("project", await Util.GetTokenAsync("Service"), payload); Util.ValidateDataReturnResponseOk(r2); long projectId = r2.ObjectResponse["data"]["id"].Value(); ApiResponse EventLogResponse = await Util.GetAsync($"event-log/objectlog?AyaType=25&AyId={projectId}", await Util.GetTokenAsync("BizAdmin")); 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("project/" + projectId.ToString(), await Util.GetTokenAsync("Service")); Util.ValidateDataReturnResponseOk(r3); r3.ObjectResponse["data"]["name"].Value().Should().Be(projectName); //w = r3.ObjectResponse["data"]; EventLogResponse = await Util.GetAsync($"event-log/objectlog?AyaType=25&AyId={projectId}", await Util.GetTokenAsync("BizAdmin")); 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 var newName = Util.Uniquify("UPDATED VIA PUT EVENTLOG TEST PROJECT"); payload = $$""" {"id":{{projectId}},"concurrency":{{r2.ObjectResponse["data"]["concurrency"].Value()}},"name":"{{newName}}","active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"dateStarted":"{{dateStarted}}","dateCompleted":null,"projectOverseerId":null,"projectOverseerViz":null,"accountNumber":null} """; // w.name = Util.Uniquify("UPDATED VIA PUT EVENTLOG TEST PROJECT"); // w.UserId = 1; // w.concurrency = r2.ObjectResponse["data"]["concurrency"].Value(); ApiResponse PUTTestResponse = await Util.PutAsync("project", await Util.GetTokenAsync("Service"), payload); Util.ValidateHTTPStatusCode(PUTTestResponse, 200); //*** RETRIEVED //check PUT worked ApiResponse checkPUTWorked = await Util.GetAsync("project/" + projectId, await Util.GetTokenAsync("Service")); Util.ValidateNoErrorInResponse(checkPUTWorked); checkPUTWorked.ObjectResponse["data"]["name"].Value().Should().Be(newName); uint concurrency = PUTTestResponse.ObjectResponse["data"]["concurrency"].Value(); EventLogResponse = await Util.GetAsync($"event-log/objectlog?AyaType=25&AyId={projectId}", await Util.GetTokenAsync("BizAdmin")); 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($"event-log/userlog?UserId={CurrentUserId}", await Util.GetTokenAsync("BizAdmin")); 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("project/" + projectId, await Util.GetTokenAsync("Service")); Util.ValidateHTTPStatusCode(DELETETestResponse, 204); //All events should be cleared up on deletion with the sole exception of the deleted event EventLogResponse = await Util.GetAsync($"event-log/objectlog?AyaType=25&AyId={projectId}", await Util.GetTokenAsync("BizAdmin")); 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().Contain(newName); } /// /// /// [Fact] public async Task UserLogWorks() { //get admin log, sb lots of shit ApiResponse a = await Util.GetAsync($"event-log/userlog?UserId=1&Offset=0&Limit=999", await Util.GetTokenAsync("BizAdmin")); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["data"]["events"]).Count.Should().BeGreaterThan(90); } /// /// /// [Fact] public async Task EventLogLimitOffSetWorks() { //CREATE USER var userName = Util.Uniquify("EventLogLimitOffSetWorks"); var payload = $$""" {"id":0,"concurrency":0,"active":true,"allowLogin":true,"name":"{{userName}}","roles":8,"userType":1,"employeeNumber":null,"notes":null,"customerId":null,"headOfficeId":null,"vendorId":null,"wiki":null,"customFields":"{}","tags":[],"lastLogin":null,"password":"{{userName}}","login":"{{userName}}"} """; ApiResponse a = await Util.PostAsync("User", await Util.GetTokenAsync("superuser", "l3tm3in"), payload); Util.ValidateDataReturnResponseOk(a); long UserId = a.ObjectResponse["data"]["id"].Value(); var dateStarted = DateTime.Now.ToString("o"); //CREATE SOME PROJECTS FOR EVENT LOG for (int i = 0; i < 10; i++) { payload = $$""" {"id":0,"concurrency":0,"name":"{{Util.Uniquify("EventLogLimitOffSetWorks")}}","active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"dateStarted":"{{dateStarted}}","dateCompleted":null,"projectOverseerId":null,"accountNumber":null} """; a = await Util.PostAsync("project", await Util.GetTokenAsync(userName, userName), payload); Util.ValidateDataReturnResponseOk(a); } a = await Util.GetAsync($"event-log/userlog?UserId={UserId}&Offset=0&Limit=9", await Util.GetTokenAsync("BizAdmin")); ((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["aType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } a = await Util.GetAsync($"event-log/userlog?UserId={UserId}&Offset=0&Limit=3", await Util.GetTokenAsync("BizAdmin")); ((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["aType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } a = await Util.GetAsync($"event-log/userlog?UserId={UserId}&Offset=1&Limit=3", await Util.GetTokenAsync("BizAdmin")); ((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["aType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } a = await Util.GetAsync($"event-log/userlog?UserId={UserId}&Offset=2&Limit=3", await Util.GetTokenAsync("BizAdmin")); ((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["aType"].Value() + o["objectId"].Value() + o["name"].Value() + o["event"].Value() ); } } //================================================== }//eoc }//eons