using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
namespace raven_integration
{
public class UserOptions
{
///
/// Test all CRUD routes for a UserOptions object
///
[Fact]
public async void CRUD()
{
//CREATE a user
dynamic D1 = new JObject();
D1.name = Util.Uniquify("Test UserOptions User");
D1.active = true;
D1.login = Util.Uniquify("LOGIN");
D1.password = Util.Uniquify("PASSWORD");
D1.roles = 0;//norole
D1.userType = 3;//non scheduleable
D1.notes = "notes";
D1.customFields = Util.UserRequiredCustomFieldsJsonString();
ApiResponse R = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D1.ToString());
Util.ValidateDataReturnResponseOk(R);
long UserId = R.ObjectResponse["data"]["id"].Value();
//Now there should be a user options available for this user
//RETRIEVE companion USEROPTIONS object
R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
Util.ValidateDataReturnResponseOk(R);
//ensure the default value is set
R.ObjectResponse["data"]["uiColor"].Value().Should().Be("#000000");
uint concurrencyToken = R.ObjectResponse["data"]["concurrencyToken"].Value();
//UPDATE
//PUT
dynamic D2 = new JObject();
D2.translationId = 1;
D2.emailAddress = "testuseroptions@helloayanova.com";
D2.languageOverride = "de-DE";
D2.timeZoneOverride = "Europe/Berlin";
D2.currencyName = "EUR";
D2.hour12 = false;
D2.uiColor = "#ffaaff";
D2.concurrencyToken = concurrencyToken;
ApiResponse PUTTestResponse = await Util.PutAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"), D2.ToString());
Util.ValidateHTTPStatusCode(PUTTestResponse, 200);
//VALIDATE
R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
Util.ValidateDataReturnResponseOk(R);
//ensure the default value is set
/*
"{\"data\":{\"id\":44,\"concurrencyToken\":7144348,\"emailAddress\":null,\"uiColor\":0,\"languageOverride\":null,\"timeZoneOverride\":null,\"currencyName\":\"USD\",\"hour12\":true,\"userId\":44}}"
*/
R.ObjectResponse["data"]["emailAddress"].Value().Should().Be(D2.emailAddress.ToString());
R.ObjectResponse["data"]["languageOverride"].Value().Should().Be(D2.languageOverride.ToString());
R.ObjectResponse["data"]["timeZoneOverride"].Value().Should().Be(D2.timeZoneOverride.ToString());
R.ObjectResponse["data"]["currencyName"].Value().Should().Be(D2.currencyName.ToString());
R.ObjectResponse["data"]["hour12"].Value().Should().Be((bool)D2.hour12);
R.ObjectResponse["data"]["uiColor"].Value().Should().Be(D2.uiColor.ToString());
R.ObjectResponse["data"]["translationId"].Value().Should().Be((long)D2.translationId);
concurrencyToken = R.ObjectResponse["data"]["concurrencyToken"].Value();
//DELETE USER
ApiResponse DELETETestResponse = await Util.DeleteAsync("User/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
Util.ValidateHTTPStatusCode(DELETETestResponse, 204);
//CHECK DELETE USER REMOVED USEROPTIONS
R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
Util.ValidateResponseNotFound(R);
}
///
/// Test not found
///
[Fact]
public async void GetNonExistentItemShouldError()
{
//Get non existant
//Should return status code 404, api error code 2010
ApiResponse R = await Util.GetAsync("UserOptions/999999", await Util.GetTokenAsync("manager", "l3tm3in"));
Util.ValidateResponseNotFound(R);
}
///
/// Test bad modelstate
///
[Fact]
public async void GetBadModelStateShouldError()
{
//Get non existant
//Should return status code 400, api error code 2200 and a first target in details of "id"
ApiResponse R = await Util.GetAsync("UserOptions/2q2", await Util.GetTokenAsync("manager", "l3tm3in"));
Util.ValidateBadModelStateResponse(R, "id");
}
//==================================================
}//eoc
}//eons