Files
raven-test-integration/Project/WidgetCrud.cs
2026-02-26 07:56:10 -08:00

172 lines
6.9 KiB
C#

using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
namespace raven_integration
{
public class ProjectCrud
{
/// <summary>
/// Test all CRUD routes for a project
/// </summary>
[Fact]
public async Task CRUD()
{
//Tags
var TagNameStart = Util.Uniquify("crud-tag-test-") + "-";//ensure this run gets it's own unique tags
List<string> TagsList =
[
TagNameStart + "Red Tag",
TagNameStart + "ORANGE IS THE NEW BLACK",
TagNameStart + "yellow",
TagNameStart + "green",
TagNameStart + "blue",
TagNameStart + "indigo",
TagNameStart + "VIOLET Tag",
];
var tagsJson = string.Join(", ", TagsList.Select(t => $"\"{t}\""));
//CREATE
var projectName = Util.Uniquify("First Test PROJECT");
var dateStarted = DateTime.Now.ToString("o");
var payload = $$"""
{"id":0,"concurrency":0,"name":"{{projectName}}","active":true,"notes":"The quick brown fox jumped over the six lazy dogs!","wiki":null,"customFields":"{}","tags":[{{tagsJson}}],"dateStarted":"{{dateStarted}}","dateCompleted":null,"projectOverseerId":null,"accountNumber":null}
""";
ApiResponse a = await Util.PostAsync("project", await Util.GetTokenAsync("superuser", "l3tm3in"), payload);
Util.ValidateDataReturnResponseOk(a);
long FirstObjectId = a.ObjectResponse["data"]["id"].Value<long>();
((long)a.ObjectResponse["data"]["serial"]).Should().NotBe(0);
//another
projectName = Util.Uniquify("Second Test PROJECT");
payload = $$"""
{"id":0,"concurrency":0,"name":"{{projectName}}","active":true,"notes":"What's the frequency Kenneth?","wiki":null,"customFields":"{}","tags":[{{tagsJson}}],"dateStarted":"{{dateStarted}}","dateCompleted":null,"projectOverseerId":null,"accountNumber":null}
""";
a = await Util.PostAsync("project", await Util.GetTokenAsync("superuser", "l3tm3in"), payload);
Util.ValidateDataReturnResponseOk(a);
long SecondObjectId = a.ObjectResponse["data"]["id"].Value<long>();
//RETRIEVE
a = await Util.GetAsync("project/" + SecondObjectId.ToString(), await Util.GetTokenAsync("superuser", "l3tm3in"));
Util.ValidateDataReturnResponseOk(a);
a.ObjectResponse["data"]["name"].Value<string>().Should().Be("Second Test PROJECT");
a.ObjectResponse["data"]["notes"].Value<string>().Should().Be("What's the frequency Kenneth?");
((JArray)a.ObjectResponse["data"]["tags"]).Count.Should().Be(7);
//UPDATE
var oUpdate = a.ObjectResponse["data"];
oUpdate["name"] = Util.Uniquify("UPDATED VIA PUT SECOND TEST WIDGET");
a = await Util.PutAsync("project", await Util.GetTokenAsync("superuser", "l3tm3in"), oUpdate.ToString());
Util.ValidateHTTPStatusCode(a, 200);
//check PUT worked
a = await Util.GetAsync("project/" + SecondObjectId.ToString(), await Util.GetTokenAsync("superuser", "l3tm3in"));
Util.ValidateNoErrorInResponse(a);
a.ObjectResponse["data"]["name"].Value<string>().Should().Be("UPDATED VIA PUT SECOND TEST WIDGET");
//DELETE
a = await Util.DeleteAsync("project/" + FirstObjectId.ToString(), await Util.GetTokenAsync("superuser", "l3tm3in"));
Util.ValidateHTTPStatusCode(a, 204);
a = await Util.DeleteAsync("project/" + SecondObjectId.ToString(), await Util.GetTokenAsync("superuser", "l3tm3in"));
Util.ValidateHTTPStatusCode(a, 204);
}
/// <summary>
/// Test not found
/// </summary>
[Fact]
public async Task GetNonExistentItemShouldError()
{
//Get non existant
//Should return status code 404, api error code 2010
ApiResponse a = await Util.GetAsync("project/999999", await Util.GetTokenAsync("superuser", "l3tm3in"));
Util.ValidateResponseNotFound(a);
}
// /// <summary>
// /// Test bad modelstate
// /// </summary>
// [Fact]
// public async Task GetBadModelStateShouldError()
// {
// //Get non existant
// //Should return status code 400, api error code 2200 and a first target in details of "id"
// ApiResponse a = await Util.GetAsync("project/2q2", await Util.GetTokenAsync("superuser", "l3tm3in"));
// Util.ValidateBadModelStateResponse(a, "id");
// }
// /// <summary>
// /// Test server exception
// /// </summary>
// [Fact]
// public async Task ServerExceptionShouldErrorProperty()
// {
// //Get non existant
// //Should return status code 400, api error code 2200 and a first target in details of "id"
// ApiResponse a = await Util.GetAsync("project/exception", await Util.GetTokenAsync("superuser", "l3tm3in"));
// Util.ValidateServerExceptionResponse(a);
// }
// /// <summary>
// /// Test server alt exception
// /// </summary>
// [Fact]
// public async Task ServerAltExceptionShouldErrorPropertly()
// {
// //Get non existant
// //Should return status code 400, api error code 2200 and a first target in details of "id"
// ApiResponse a = await Util.GetAsync("project/altexception", await Util.GetTokenAsync("superuser", "l3tm3in"));
// Util.ValidateServerExceptionResponse(a);
// }
/// <summary>
///
/// </summary>
[Fact]
public async Task PutConcurrencyViolationShouldFail()
{
//CREATE
dynamic w2 = new JObject();
w2.name = Util.Uniquify("PutConcurrencyViolationShouldFail");
w2.dollarAmount = 2.22m;
w2.active = true;
w2.usertype = 1;
w2.notes = "blah";
ApiResponse r2 = await Util.PostAsync("project", await Util.GetTokenAsync("superuser", "l3tm3in"), w2.ToString());
Util.ValidateDataReturnResponseOk(r2);
uint OriginalConcurrencyToken = r2.ObjectResponse["data"]["concurrency"].Value<uint>();
w2 = r2.ObjectResponse["data"];
//UPDATE
//PUT
w2.name = Util.Uniquify("PutConcurrencyViolationShouldFail UPDATE VIA PUT ");
w2.concurrency = OriginalConcurrencyToken - 1;//bad token
ApiResponse PUTTestResponse = await Util.PutAsync("project/", await Util.GetTokenAsync("superuser", "l3tm3in"), w2.ToString());
Util.ValidateConcurrencyError(PUTTestResponse);
}
//==================================================
}//eoc
}//eons