I was today years old when I realized /tenants/***/instances/... was not even selecting the specified tenant before fetching instances

This commit is contained in:
Michael Becker 2025-03-06 16:14:28 -05:00
parent 0d9bde6351
commit 3c056486f8
4 changed files with 94 additions and 10 deletions

View File

@ -31,6 +31,17 @@ public abstract class InstanceCommand : TenantedCommand
if (e.Context.Request.PathParts.Length < 5)
return true;
string tenantName = e.Context.Request.PathParts[2];
TenantHandle th = oms.GetTenantByName(tenantName);
if (th != TenantHandle.Empty)
{
oms.SelectTenant(th);
}
else
{
Console.Error.WriteLine("failed to select tenant '{0}'", tenantName);
}
string iid = e.Context.Request.PathParts[4];
if (oms.TryParseInstanceRef(iid, out InstanceHandle ih))
{

View File

@ -48,7 +48,17 @@ public class TenantsListCommand : OmsServerCommand
}
else if (e.Context.Request.PathParts.Length > 3)
{
if (e.Context.Request.PathParts[3] == "instances")
if (e.Context.Request.PathParts[3] == "create")
{
string tenantName = e.Context.Request.PathParts[2];
TenantHandle th = oms.CreateTenant(tenantName);
JsonObject obj = new JsonObject();
obj.Add("result", JsonValue.Create("success"));
obj.Add("tenantHandle", th.ToString());
sw.Write(obj.ToJsonString());
}
else if (e.Context.Request.PathParts[3] == "instances")
{
if (e.Context.Request.PathParts.Length > 4)
{

View File

@ -24,12 +24,12 @@ namespace Mocha.Core.Tests;
public class TenantedTests : OmsTestsBase
{
protected override void AfterSetup()
{
base.AfterSetup();
protected override void AfterSetup()
{
base.AfterSetup();
Oms.CreateTenant("omstest");
}
}
[Test]
public void Create_Class_On_First_Tenant_Doesnt_Show_Up_On_Second_Tenant()
@ -77,4 +77,18 @@ public class TenantedTests : OmsTestsBase
InstanceHandle att2 = Oms.GetInstance(KnownAttributeGuids.Text.ContentType);
Assert.That(val, Is.Not.EqualTo("Test Value"));
}
[Test]
public void Get_Instance_After_Create_Tenant_Doesnt_Trash_Super_Tenant()
{
Oms.SelectTenant(Oms.GetTenantByName("super"));
Class cls = Oms.GetInstance<Class>(TEST_CLASS_GUID);
Assert.That(cls, Is.Not.Null);
Oms.CreateTenant("dummy");
Class cls2 = Oms.GetInstance<Class>(TEST_CLASS_GUID);
Assert.That(cls2, Is.Not.Null);
}
}

View File

@ -63,6 +63,55 @@ public class RemoteTests
Assert.That(json["tenants"].AsArray()[0].ToString(), Is.EqualTo("super"));
}
/// <summary>
/// Checks to make sure that the class Class [1$1] on the super tenant exists.
/// </summary>
/// <returns></returns>
[Test]
public async Task Get_Class_Instance()
{
HttpClient client = new HttpClient();
HttpResponseMessage resp = client.Send(new HttpRequestMessage(HttpMethod.Get, BuildUrl("/tenants/super/instances/1$1")));
Assert.That(resp.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
Assert.That(resp.Content.Headers.ContentType.ToString(), Is.EqualTo("application/json"));
string content = await resp.Content.ReadAsStringAsync();
JsonObject json = JsonNode.Parse(content) as JsonObject;
Assert.That(json["globalIdentifier"].ToString(), Is.EqualTo(KnownInstanceGuids.Classes.Class.ToString()));
}
/// <summary>
/// Checks to make sure that the class Class [1$1] on the super tenant exists after a new tenant is created.
/// </summary>
/// <returns></returns>
[Test]
public async Task Get_Class_Instance_After_Create_Tenant()
{
// This test was failing not because the class did not exist, but because after the call to create a tenant,
// the call to request instance details WAS NOT SELECTING THE PROPER TENANT. In other words,
// /tenants/***/instances... would NEVER select the specified tenant. In this case, it was using the tenant
// already selected from the call to create, e.g. 'contoso1', rather than the tenant we requested in the API
// call, 'super'.
//
// This API call was ALWAYS broken!
HttpClient client = new HttpClient();
HttpResponseMessage resp1 = client.Send(new HttpRequestMessage(HttpMethod.Get, BuildUrl("/tenants/contoso1/create")));
string cvm = await resp1.Content.ReadAsStringAsync();
HttpResponseMessage resp = client.Send(new HttpRequestMessage(HttpMethod.Get, BuildUrl("/tenants/super/instances/1$1")));
Assert.That(resp.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
Assert.That(resp.Content.Headers.ContentType.ToString(), Is.EqualTo("application/json"));
string content = await resp.Content.ReadAsStringAsync();
JsonObject json = JsonNode.Parse(content) as JsonObject;
Assert.That(json["globalIdentifier"].ToString(), Is.EqualTo(KnownInstanceGuids.Classes.Class.ToString()));
}
[Test]
public async Task Instance_Json_Test()
{