using System; using Xunit; using Newtonsoft.Json.Linq; using FluentAssertions; using System.Collections.Generic; using System.Collections.Concurrent; namespace raven_integration { public class FormCustom { public enum AyaUiFieldDataType : int { NoType = 0, DateTime = 1, Date = 2, Time = 3, Text = 4, Integer = 5, Bool = 6, Decimal = 7, Currency = 8, Tags = 9, Enum = 10, EmailAddress = 11 } /// /// Test create or update /// [Fact] public async void FormCustomUpdate() { //This is a special case, you can PUT a formcustom, but you can't delete one and you can't create one /* */ dynamic d = new JObject(); d.formkey = "User"; dynamic dtemplate = new JArray(); dynamic dt = new JObject(); dt.fld = "UserCustom1"; dt.hide = false; dt.required = true; dt.type = AyaUiFieldDataType.Text; dtemplate.Add(dt); dt = new JObject(); dt.fld = "Notes"; dt.required = true; dtemplate.Add(dt); dt = new JObject(); dt.fld = "UserCustom2"; dt.hide = true; dt.required = false; dt.type = AyaUiFieldDataType.Bool; dtemplate.Add(dt); d.template = dtemplate.ToString();//it expects it to be a json string, not actual json //RETRIEVE //Get the current one (server will create if non-existent) ApiResponse a = await Util.GetAsync("form-custom/User", await Util.GetTokenAsync("BizAdminFull")); //Update d.concurrency = a.ObjectResponse["data"]["concurrency"].Value(); a = await Util.PutAsync("form-custom/User", await Util.GetTokenAsync("BizAdminFull"), d.ToString()); Util.ValidateHTTPStatusCode(a, 200); //check the concurrency token cache scheme uint token = a.ObjectResponse["data"]["concurrency"].Value(); //This should return a 304 not modified a = await Util.GetAsync($"form-custom/User?concurrency={token}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateHTTPStatusCode(a, 304); //and this should return the whole object token--;//make the token not match //This should return a 200 and the whole object a = await Util.GetAsync($"form-custom/User?concurrency={token}", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateDataReturnResponseOk(a); } /// /// Ensure validation works in FormCustombiz /// [Fact] public async void ValidatesProperly() { dynamic d = new JObject(); d.formkey = "User"; dynamic dtemplate = new JArray(); dynamic dt = new JObject(); dt.fld = string.Empty;//expected ApiErrorCode.VALIDATION_REQUIRED Missing key 2 errors 2201 and 2203 dt.hide = false; dt.required = true; dtemplate.Add(dt); dt = new JObject(); dt.fld = "ThisFieldKeyDoesNotExist";//expected ApiErrorCode.VALIDATION_INVALID_VALUE Bad key dt.required = true; dtemplate.Add(dt); dt = new JObject(); dt.fld = "Name";//expect ApiErrorCode.VALIDATION_INVALID_VALUE required field not hideable dt.hide = true; dt.required = true; dtemplate.Add(dt); dt = new JObject(); dt.fld = "UserCustom1";//expected ApiErrorCode.VALIDATION_INVALID_VALUE type missing for custom field dt.hide = false; dt.required = false; dtemplate.Add(dt); dt = new JObject(); dt.fld = "EmployeeNumber";//expect ApiErrorCode.VALIDATION_INVALID_VALUE not custom field but type specified anyway dt.hide = true; dt.required = false; dt.type = AyaUiFieldDataType.EmailAddress;//type specified (doesn't matter what type) dtemplate.Add(dt); dt = new JObject(); dt.fld = "UserCustom2";//expected ApiErrorCode.VALIDATION_INVALID_VALUE type missing for custom field dt.hide = false; dt.required = false; dtemplate.Add(dt); dt.type = 999;//not a valid type dt = new JObject(); dt.fld = "Notes";//expect ApiErrorCode.VALIDATION_REQUIRED required property is always required (no idea why, just designed that way and that's the rule) dt.hide = true; // dt.required = false; Deliberately not set dtemplate.Add(dt); d.template = dtemplate.ToString();//it expects it to be a json string, not actual json //RETRIEVE //Get the current one (server will create if non-existent) ApiResponse a = await Util.GetAsync("form-custom/User", await Util.GetTokenAsync("BizAdminFull")); //Update d.concurrency = a.ObjectResponse["data"]["concurrency"].Value(); a = await Util.PutAsync("form-custom/User", await Util.GetTokenAsync("BizAdminFull"), d.ToString()); Util.ValidateHTTPStatusCode(a, 400); Util.ShouldContainValidationError(a, "Template", "2201", "Template array item 0, \"fld\" property exists but is empty, a value is required"); Util.ShouldContainValidationError(a, "Template", "2203", "Template array item 0, fld property value \"\" is not a valid form field value for formKey specified"); Util.ShouldContainValidationError(a, "Template", "2203", "Template array item 1, fld property value \"ThisFieldKeyDoesNotExist\" is not a valid form field value for formKey specified"); Util.ShouldContainValidationError(a, "Template", "2203", "Template array item 2 (\"Name\"), \"hide\" property value of \"True\" is not valid, this field is core and cannot be hidden"); Util.ShouldContainValidationError(a, "Template", "2203", "Template array item 3 (\"UserCustom1\"), \"type\" property value is MISSING for custom field, Custom fields MUST have types specified"); Util.ShouldContainValidationError(a, "Template", "2203", "Template array item 4 (\"EmployeeNumber\"), \"type\" property value is not valid, only Custom fields can have types specified"); Util.ShouldContainValidationError(a, "Template", "2203", "Template array item 5 (\"UserCustom2\"), \"type\" property value of \"999\" is not a valid custom field type"); Util.ShouldContainValidationError(a, "Template", "2201", "Template array item 6, object is missing \"required\" property. All items must contain this property."); /* "{\"error\":{\"code\":\"2200\",\"details\":[ {\"message\":\"Template array item 0, \\\"fld\\\" property exists but is empty, a value is required\",\"target\":\"Template\",\"error\":\"2201\"}, {\"message\":\"Template array item 0, fld property value \\\"\\\" is not a valid form field value for formKey specified\",\"target\":\"Template\",\"error\":\"2203\"}, {\"message\":\"Template array item 1, fld property value \\\"ThisFieldKeyDoesNotExist\\\" is not a valid form field value for formKey specified\",\"target\":\"Template\",\"error\":\"2203\"}, {\"message\":\"Template array item 2 (\\\"Name\\\"), \\\"hide\\\" property value of \\\"True\\\" is not valid, this field is core and cannot be hidden\",\"target\":\"Template\",\"error\":\"2203\"}, {\"message\":\"Template array item 3 (\\\"UserCustom1\\\"), \\\"type\\\" property value is MISSING for custom filed, Custom fields MUST have types specified\",\"target\":\"Template\",\"error\":\"2203\"}, {\"message\":\"Template array item 4 (\\\"EmployeeNumber\\\"), \\\"type\\\" property value is not valid, only Custom fields can have types specified\",\"target\":\"Template\",\"error\":\"2203\"}, {\"message\":\"Template array item 5 (\\\"UserCustom2\\\"), \\\"type\\\" property value of \\\"999\\\" is not a valid custom field type\",\"target\":\"Template\",\"error\":\"2203\"}, {\"message\":\"Template array item 6, object is missing \\\"required\\\" property. All items must contain this property. \",\"target\":\"Template\",\"error\":\"2201\"} ],\"message\":\"Object did not pass validation\"}}" */ } /// /// /// [Fact] public async void InvalidObjectFieldsFormKeyShouldFail() { ApiResponse a = await Util.GetAsync("form-field-definition/nonexistent", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateErrorCodeResponse(a, 2010, 404); } /// /// /// [Fact] public async void ObjectFieldsWorks() { ApiResponse a = await Util.GetAsync("form-field-definition/Widget", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["data"]).Count.Should().BeGreaterThan(25); } /// /// /// [Fact] public async void AvailableCustomizableFormKeysWorks() { ApiResponse a = await Util.GetAsync("form-custom/availablecustomizableformkeys", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["data"]).Count.Should().BeGreaterThan(1);//is 2 as of writing (widget,user) } /// /// /// [Fact] public async void AvailableCustomTypesWorks() { ApiResponse a = await Util.GetAsync("form-custom/availablecustomtypes", await Util.GetTokenAsync("BizAdminFull")); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["data"]).Count.Should().BeGreaterThan(4); } //================================================== }//eoc }//eons