using System; using Xunit; using Newtonsoft.Json.Linq; using System.Linq; using FluentAssertions; using System.Collections.Generic; namespace raven_integration { public class SearchOps { /// /// Test simple phrase only search is fundamentally working /// [Fact] public async void PhraseOnlySearchShouldReturnCorrectResultsInOrder() { const string TEST_SEARCH_PHRASE = "simple dogs"; //CREATE A WIDGET dynamic D = new JObject(); D.name = Util.Uniquify("Search NOTES Test WIDGET"); D.dollarAmount = 1.11m; D.active = true; D.roles = 0; D.notes = "This record will match in notes: The quick brown and simple fox jumped over the six lazy dogs!"; ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); Util.ValidateDataReturnResponseOk(a); long MatchWidgetInNotesId = a.ObjectResponse["result"]["id"].Value(); //CREATE FIRST TEST USER WITH PHRASE IN NAME D = new JObject(); D.name = Util.Uniquify("Search NAME DOGS simple Test User"); D.notes = "This user has the match in it's name"; D.ownerId = 1L; D.active = true; D.login = Util.Uniquify("LOGIN"); D.password = Util.Uniquify("PASSWORD"); D.roles = 0;//norole D.localeId = 1;//random locale D.userType = 3;//non scheduleable a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); Util.ValidateDataReturnResponseOk(a); long MatchUserInNameId = a.ObjectResponse["result"]["id"].Value(); //CREATE A SECOND TEST USER WITH PHRASE IN NOTES D = new JObject(); D.name = Util.Uniquify("Search NOTES Test User"); D.notes = "This user has the match simple dogs in its notes"; D.ownerId = 1L; D.active = true; D.login = Util.Uniquify("LOGIN"); D.password = Util.Uniquify("PASSWORD"); D.roles = 0;//norole D.localeId = 1;//random locale D.userType = 3;//non scheduleable a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); Util.ValidateDataReturnResponseOk(a); long MatchUserInNotesId = a.ObjectResponse["result"]["id"].Value(); //CREATE A SECOND WIDGET D = new JObject(); D.name = Util.Uniquify("Search NAME simple as in dogs Test WIDGET"); D.dollarAmount = 1.11m; D.active = true; D.roles = 0; D.notes = "This Widget matches in name"; a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); Util.ValidateDataReturnResponseOk(a); long MatchWidgetInNameId = a.ObjectResponse["result"]["id"].Value(); //CREATE A THIRD WIDGET D = new JObject(); D.name = Util.Uniquify("Search NO-MATCH THIRD Test WIDGET"); D.dollarAmount = 1.11m; D.active = true; D.roles = 0; D.notes = "This Widget should not be returned in the search as it only contains a single keyword in the name not both"; a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); Util.ValidateDataReturnResponseOk(a); long MatchNothingWidgetId = a.ObjectResponse["result"]["id"].Value(); //Now see if can find those objects with a phrase search dynamic SearchParameters = new JObject(); SearchParameters.phrase = TEST_SEARCH_PHRASE; SearchParameters.nameOnly = false; SearchParameters.typeOnly = 0;//no type a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString()); Util.ValidateDataReturnResponseOk(a); //Now validate the return list ((JArray)a.ObjectResponse["result"]).Count.Should().BeGreaterOrEqualTo(3); //Turn the list into an array of id's var v = ((JArray)a.ObjectResponse["result"]); List MatchingIdList = new List(); foreach (JObject j in v) { MatchingIdList.Add(j["id"].Value()); } //Ensure the expected items are returned MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId"); MatchingIdList.Should().Contain(MatchWidgetInNameId, "ShouldContainMatchWidgetInNameId"); MatchingIdList.Should().Contain(MatchUserInNotesId, "ShouldContainMatchUserInNotesId"); MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId"); MatchingIdList.Should().NotContain(MatchNothingWidgetId, "ShouldNotContainMatchNothingWidgetId"); //Assert the order (roughly, this is kind of a waste of time, either the code is sorting or not, it's not going to change) //first item must be a widget a.ObjectResponse["result"][0]["type"].Value().Should().Be(2); //final item must be a user a.ObjectResponse["result"][MatchingIdList.Count - 1]["type"].Value().Should().Be(3); //FULL BODY SEARCH RIGHTS //First up test a full record search returns no results due to insufficient rights //even though the record exists //Just re-run the above search exactly but with a no rights to full User or Widget role instead //Only BizAdmin* roles can read a full user record but anyone should be able to see names //This search should return zero items a = await Util.PostAsync("Search", await Util.GetTokenAsync("SubContractorLimited"), SearchParameters.ToString()); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["result"]).Count.Should().Be(0, "User with no rights should not see any results in body search"); //NAME ONLY SEARCH SHOULD WORK WITH NO RIGHTS TO READ FULL RECORD //repeat same search but with nameOnly = true and should return at two records at least but not any of the body ones SearchParameters = new JObject(); SearchParameters.phrase = TEST_SEARCH_PHRASE; SearchParameters.nameOnly = true; SearchParameters.typeOnly = 0;//no type a = await Util.PostAsync("Search", await Util.GetTokenAsync("SubContractorLimited"), SearchParameters.ToString()); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["result"]).Count.Should().BeGreaterOrEqualTo(2); //Check that list does *not* include the notes only records MatchingIdList = new List(); foreach (JObject j in v) { MatchingIdList.Add(j["id"].Value()); } MatchingIdList.Should().NotContain(MatchWidgetInNotesId, "ShouldNotContainMatchWidgetInNotesId"); MatchingIdList.Should().Contain(MatchWidgetInNameId, "ShouldContainMatchWidgetInNameId"); MatchingIdList.Should().NotContain(MatchUserInNotesId, "ShouldContainMatchUserInNotesId"); MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId"); MatchingIdList.Should().NotContain(MatchNothingWidgetId, "ShouldNotContainThirdWidget"); }//eot /* {{Initial search results: borked! {{ "result": [ { "name": "Search NAME simple as in dogs Test WIDGET1537911374", "type": 2, "id": 202 }, { "name": "Search NOTES Test WIDGET1537911373", "type": 2, "id": 201 }, { "name": "Search NOTES Test User1537911374", "type": 3, "id": 22 }, { "name": "Search NAME DOGS simple Test User1537911374", "type": 3, "id": 21 } ] }} */ //TODO: TEST THAT NAME ONLY WORKS AS EXPECTED //TODO: TEST THAT NAME ONLY WORKS EVEN WITH NON-READ RIGHTS TO FULL OBJECT //TODO: WILDCARD SEARCH TEST //TODO: WILDCARD PLUS NON WILDCARD SEARCH TEST //TODO: TAG SEARCH ALONE TEST //TODO: TAG PLUS SEARCH PHRASE TEST //TODO: TAG PLUS REGULAR SEARCH PHRASE PLUS WILDCARD PHRASE TEST //TODO: TAG PLUS USER TEST OR MODIFY ALL OF THE ABOVE TO INCLUDE A USER INSTEAD OF A SECOND TEST WIDGET //================================================== }//eoc }//eons