This commit is contained in:
2018-09-04 18:58:10 +00:00
parent 077b9c7617
commit 21eb952e5a
5 changed files with 230 additions and 9 deletions

View File

@@ -0,0 +1,222 @@
using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
namespace raven_integration
{
public class UserCrud
{
/// <summary>
/// Test all CRUD routes for a User
/// </summary>
[Fact]
public async void CRUD()
{
/*
{
"id": 0,
"name": "string",
"dollarAmount": 0,
"active": true,
"roles": 0
}
*/
//CREATE
dynamic w1 = new JObject();
w1.name = Util.Uniquify("First Test User");
w1.dollarAmount = 1.11m;
w1.active = true;
w1.roles = 0;
ApiResponse r1 = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), w1.ToString());
Util.ValidateDataReturnResponseOk(r1);
long w1Id = r1.ObjectResponse["result"]["id"].Value<long>();
dynamic w2 = new JObject();
w2.name = Util.Uniquify("Second Test User");
w2.dollarAmount = 2.22m;
w2.active = true;
w2.roles = 0;
ApiResponse r2 = await Util.PostAsync("User", await Util.GetTokenAsync( "manager", "l3tm3in"), w2.ToString());
Util.ValidateDataReturnResponseOk(r2);
long w2Id = r2.ObjectResponse["result"]["id"].Value<long>();
//RETRIEVE
//Get one
ApiResponse r3 = await Util.GetAsync("User/" + w2Id.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateDataReturnResponseOk(r3);
r3.ObjectResponse["result"]["name"].Value<string>().Should().Be(w2.name.ToString());
//UPDATE
//PUT
//update w2id
w2.name = Util.Uniquify("UPDATED VIA PUT SECOND TEST User");
w2.OwnerId = 1;
w2.concurrencyToken = r2.ObjectResponse["result"]["concurrencyToken"].Value<uint>();
ApiResponse PUTTestResponse = await Util.PutAsync("User/" + w2Id.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"), w2.ToString());
Util.ValidateHTTPStatusCode(PUTTestResponse, 200);
//check PUT worked
ApiResponse checkPUTWorked = await Util.GetAsync("User/" + w2Id.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateNoErrorInResponse(checkPUTWorked);
checkPUTWorked.ObjectResponse["result"]["name"].Value<string>().Should().Be(w2.name.ToString());
uint concurrencyToken = PUTTestResponse.ObjectResponse["result"]["concurrencyToken"].Value<uint>();
//PATCH
var newName = Util.Uniquify("UPDATED VIA PATCH SECOND TEST User");
string patchJson = "[{\"value\": \"" + newName + "\",\"path\": \"/name\",\"op\": \"replace\"}]";
ApiResponse PATCHTestResponse = await Util.PatchAsync("User/" + w2Id.ToString() + "/" + concurrencyToken.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"), patchJson);
Util.ValidateHTTPStatusCode(PATCHTestResponse, 200);
//check PATCH worked
ApiResponse checkPATCHWorked = await Util.GetAsync("User/" + w2Id.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateNoErrorInResponse(checkPATCHWorked);
checkPATCHWorked.ObjectResponse["result"]["name"].Value<string>().Should().Be(newName);
//DELETE
ApiResponse DELETETestResponse = await Util.DeleteAsync("User/" + w2Id.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateHTTPStatusCode(DELETETestResponse, 204);
}
/// <summary>
/// Test not found
/// </summary>
[Fact]
public async void GetNonExistentItemShouldError()
{
//Get non existant
//Should return status code 404, api error code 2010
ApiResponse a = await Util.GetAsync("User/999999", await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateResponseNotFound(a);
}
/// <summary>
/// Test bad modelstate
/// </summary>
[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 a = await Util.GetAsync("User/2q2", await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateBadModelStateResponse(a, "id");
}
/// <summary>
/// Test server exception
/// </summary>
[Fact]
public async void ServerExceptionShouldErrorPropertly()
{
//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("User/exception", await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateServerExceptionResponse(a);
}
/// <summary>
/// Test server alt exception
/// </summary>
[Fact]
public async void 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("User/altexception", await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateServerExceptionResponse(a);
}
/// <summary>
///
/// </summary>
[Fact]
public async void PutConcurrencyViolationShouldFail()
{
//CREATE
dynamic w2 = new JObject();
w2.name = Util.Uniquify("PutConcurrencyViolationShouldFail");
w2.dollarAmount = 2.22m;
w2.active = true;
w2.roles = 0;
ApiResponse r2 = await Util.PostAsync("User", await Util.GetTokenAsync( "manager", "l3tm3in"), w2.ToString());
Util.ValidateDataReturnResponseOk(r2);
long w2Id = r2.ObjectResponse["result"]["id"].Value<long>();
uint OriginalConcurrencyToken = r2.ObjectResponse["result"]["concurrencyToken"].Value<uint>();
//UPDATE
//PUT
w2.name = Util.Uniquify("PutConcurrencyViolationShouldFail UPDATE VIA PUT ");
w2.OwnerId = 1;
w2.concurrencyToken = OriginalConcurrencyToken - 1;//bad token
ApiResponse PUTTestResponse = await Util.PutAsync("User/" + w2Id.ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"), w2.ToString());
Util.ValidateConcurrencyError(PUTTestResponse);
}
/// <summary>
///
/// </summary>
[Fact]
public async void PatchConcurrencyViolationShouldFail()
{
//CREATE
dynamic w2 = new JObject();
w2.name = Util.Uniquify("PatchConcurrencyViolationShouldFail");
w2.dollarAmount = 2.22m;
w2.active = true;
w2.roles = 0;
ApiResponse r2 = await Util.PostAsync("User", await Util.GetTokenAsync( "manager", "l3tm3in"), w2.ToString());
Util.ValidateDataReturnResponseOk(r2);
long w2Id = r2.ObjectResponse["result"]["id"].Value<long>();
uint OriginalConcurrencyToken = r2.ObjectResponse["result"]["concurrencyToken"].Value<uint>();
//PATCH
var newName = Util.Uniquify("PutConcurrencyViolationShouldFail UPDATED VIA PATCH");
string patchJson = "[{\"value\": \"" + newName + "\",\"path\": \"/name\",\"op\": \"replace\"}]";
ApiResponse PATCHTestResponse = await Util.PatchAsync("User/" + w2Id.ToString() + "/" + (OriginalConcurrencyToken - 1).ToString(), await Util.GetTokenAsync( "manager", "l3tm3in"), patchJson);
Util.ValidateConcurrencyError(PATCHTestResponse);
}
//==================================================
}//eoc
}//eons

View File

@@ -44,7 +44,7 @@ namespace raven_integration
public async void PagingShouldWorkAsExpected()
{
//Get all
ApiResponse a = await Util.GetAsync("Widget/list?Offset=2&Limit=3", await Util.GetTokenAsync( "manager", "l3tm3in"));
ApiResponse a = await Util.GetAsync("Widget/listwidgets?Offset=2&Limit=3", await Util.GetTokenAsync( "manager", "l3tm3in"));
Util.ValidateDataReturnResponseOk(a);
Util.ValidateHTTPStatusCode(a, 200);