using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace raven_integration
{
public class FormCustom
{
///
/// Test create or update
///
[Fact]
public async void FormCustomCreateUpdate()
{
//This is a special case, you can create or PUT a formcustom, but you can't delete one and you can't create one if one already exists
//so this test will either create or update depending upon if it's already created one or not
dynamic d = new JObject();
d.formkey = "user";
/*
Example:
{template:[{fld:"ltkeyfieldname",hide:"true/false",required:"true/false", type:"bool"},{fld:"ltkeyfieldname",hide:"true/false",required:"true/false", type:"text"]}
*/
dynamic dtemplate = new JArray();
dynamic dt = new JObject();
dt.fld = "UserCustom1";
dt.hide = false;
dt.required = true;
dt.type = "text";
dtemplate.Add(dt);
dt = new JObject();
dt.fld = "UserNotes";
dt.required = true;
dtemplate.Add(dt);
dt = new JObject();
dt.fld = "UserCustom2";
dt.hide = true;
dt.required = false;
dt.type = "bool";
dtemplate.Add(dt);
d.template = dtemplate.ToString();//it expects it to be a json string, not actual json
ISN"T THIS WRONG? It's a direct array, examine the json here
//Is there one already?
//RETRIEVE
//Get one
ApiResponse a = await Util.GetAsync("FormCustom/user", await Util.GetTokenAsync("BizAdminFull"));
bool Exists = ((int)a.HttpResponse.StatusCode) == 200;
if (Exists)
{
//Update
d.concurrencyToken = a.ObjectResponse["data"]["concurrencyToken"].Value();
a = await Util.PutAsync("FormCustom/user", await Util.GetTokenAsync("BizAdminFull"), d.ToString());
Util.ValidateHTTPStatusCode(a, 200);
}
else
{
//Create it
a = await Util.PostAsync("FormCustom", await Util.GetTokenAsync("BizAdminFull"), d.ToString());
Util.ValidateDataReturnResponseOk(a);
}
//check the concurrency token cache scheme
uint token = a.ObjectResponse["data"]["concurrencyToken"].Value();
//This should return a 304 not modified
a = await Util.GetAsync($"FormCustom/user?concurrencyToken={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($"FormCustom/user?concurrencyToken={token}", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateDataReturnResponseOk(a);
}
///
///
///
[Fact]
public async void InvalidAvailableFieldsFormKeyShouldFail()
{
ApiResponse a = await Util.GetAsync("FormCustom/AvailableFields/nonexistent", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateErrorCodeResponse(a, 2010, 404);
}
///
///
///
[Fact]
public async void AvailableFieldsWorks()
{
ApiResponse a = await Util.GetAsync("FormCustom/AvailableFields/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("FormCustom/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("FormCustom/AvailableCustomTypes", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateDataReturnResponseOk(a);
((JArray)a.ObjectResponse["data"]).Count.Should().BeGreaterThan(4);
}
//==================================================
}//eoc
}//eons