test(g1): in-memory NV$ money payout, policy, portal auth and idempotency HTTP regressions

This commit is contained in:
OpenGenesis (EULA) committed 2026-11-25 21:02:03 +01:00
1 parent 01d90ea876
commit 5ec824bc01
1 file changed
+116 -1
+116 -1
View File
@@ -10,6 +10,7 @@ using OpenMetaverse;
using OpenSim.Services.Interfaces;
using OpenGenesisLINK.SysApi;
using NexVerse.Core.Messaging;
using NexVerse.Core.Economy;
using OpenSim.Framework;
using System.Security.Cryptography;
@@ -76,11 +77,24 @@ free.Start();
int port = ((IPEndPoint)free.LocalEndpoint).Port;
free.Stop();
var alertSink = new TestAlertPublisher();
// In-memory test ledger only; absolutely no production NV$ or SQL connections.
var fakeMoneyStore = new InMemoryNexLedgerStore();
var fakeEconomy = new NexEconomyService(
new NexDoubleEntryLedger(fakeMoneyStore), fakeMoneyStore);
var moneyBridge = new SysApiNvMoney(
DispatchProxy.Create<IUserAccountService, AccountProxy>(),
adminEnabled:true, portalEnabled:true, maxNv:10000,
portalToken:new string('C', 64), getEconomy:()=>fakeEconomy);
if (SysApiNvMoney.NvPerEuro != 100 ||
SysApiNvMoney.EuroCentsToNv(100) != 100 ||
SysApiNvMoney.EuroCentsToNv(150) != 150)
throw new Exception("100 NV$ per EUR fixed-point conversion regression");
using var host = new SysApiHost(port, "https://sysapi.example.test:8850",
DispatchProxy.Create<IUserAccountService, AccountProxy>(),
DispatchProxy.Create<IAuthenticationService, AuthenticationProxy>(),
first, new FakeSysApiReadModel(), accessPolicy: new FakeSysApiAccessPolicyStore(),
policyReadToken: new string('A',64), alertPublisher: alertSink);
policyReadToken: new string('A',64), alertPublisher: alertSink,
nvMoney: moneyBridge);
host.Start();
try
{
@@ -91,6 +105,19 @@ try
};
static void Expect(HttpStatusCode actual, HttpStatusCode desired, string test)
{ if (actual != desired) throw new Exception($"{test}: expected {desired}, got {actual}"); }
using (var anonymousMoney = await client.GetAsync("/api/admin/money/status"))
Expect(anonymousMoney.StatusCode,HttpStatusCode.Unauthorized,"money status auth");
using (var anonymousPortal = await client.PostAsync("/api/v1/portal/money/payouts",
new StringContent("{}", Encoding.UTF8, "application/json")))
Expect(anonymousPortal.StatusCode,HttpStatusCode.Unauthorized,"portal money token required");
using(var moneyJs=await client.GetAsync("/money.js"))
{
Expect(moneyJs.StatusCode,HttpStatusCode.OK,"embedded money JavaScript");
string script=await moneyJs.Content.ReadAsStringAsync();
if(!script.Contains("/api/admin/money/payouts") ||
!script.Contains("crypto.randomUUID()") ||script.Contains("innerHTML"))
throw new Exception("Money UI does not use secure payout request contract");
}
using var index = await client.GetAsync("/");
Expect(index.StatusCode, HttpStatusCode.OK, "Index");
if (!(await index.Content.ReadAsStringAsync()).Contains("Control the"))
@@ -203,6 +230,94 @@ try
new StringContent("{\"command\":\"alert WARNUNG: Test\"}", Encoding.UTF8, "application/json")))
Expect(withoutCsrf.StatusCode, HttpStatusCode.Forbidden, "alert CSRF required");
client.DefaultRequestHeaders.Add("X-OGL-CSRF", csrf);
// Real ledger bookkeeping and replay/reconciliation tests on a volatile
// IN-MEMORY store. No production user, SQL row, payment or funds changed.
using (var moneyStatus = await client.GetAsync("/api/admin/money/status"))
{
Expect(moneyStatus.StatusCode,HttpStatusCode.OK,"money status admin");
if (!(await moneyStatus.Content.ReadAsStringAsync()).Contains("100"))
throw new Exception("Invalid fixed rate in money status");
}
Guid payoutRequestId = Guid.NewGuid();
var moneyPayload = new {
avatarId = AccountProxy.Principal.ToString(), amountNv = 125,
description = "Automated test payout", requestId = payoutRequestId.ToString("D"),
source = "admin-manual"
};
async Task<HttpResponseMessage> SendMoney(object body) =>
await client.PostAsync("/api/admin/money/payouts",
new StringContent(JsonSerializer.Serialize(body),Encoding.UTF8,"application/json"));
using (var firstPayout = await SendMoney(moneyPayload))
Expect(firstPayout.StatusCode,HttpStatusCode.Created,"first money credit");
if (fakeEconomy.GetBalance(AccountProxy.Principal.Guid) != 125)
throw new Exception("Incorrect ledger balance after first admin payout");
using (var duplicatePayout = await SendMoney(moneyPayload))
Expect(duplicatePayout.StatusCode,HttpStatusCode.OK,"duplicate money idempotency");
if (fakeEconomy.GetBalance(AccountProxy.Principal.Guid) != 125)
throw new Exception("Duplicate payout credited more than once");
using (var alteredPayout = await SendMoney(new {
avatarId=AccountProxy.Principal.ToString(),amountNv=126,
description="Automated test payout",requestId=payoutRequestId.ToString("D"),
source="admin-manual"
}))
Expect(alteredPayout.StatusCode,HttpStatusCode.Conflict,"replay with altered payout rejected");
using (var fakeProviderPayment = await SendMoney(new {
avatarId=AccountProxy.Principal.ToString(),amountNv=100,
description="pretend stripe payment",requestId=Guid.NewGuid().ToString("D"),
source="stripe"
}))
Expect(fakeProviderPayment.StatusCode,HttpStatusCode.BadRequest,
"unverified Stripe payment must never mint NV$");
using (var amountTooLarge = await SendMoney(new {
avatarId=AccountProxy.Principal.ToString(),amountNv=10001,
description="too large amount",requestId=Guid.NewGuid().ToString("D"),
source="admin-manual"
}))
Expect(amountTooLarge.StatusCode,HttpStatusCode.BadRequest,"limit enforced");
using (var portalWithoutProxy = new HttpRequestMessage(
HttpMethod.Post,"/api/v1/portal/money/payouts"))
{
portalWithoutProxy.Headers.Authorization=new("Bearer",new string('C',64));
portalWithoutProxy.Content=new StringContent(JsonSerializer.Serialize(new {
avatarId=AccountProxy.Principal.ToString(),amountNv=50,
description="Portal test grant",requestId=Guid.NewGuid().ToString("D"),
source="portal-manual"}),Encoding.UTF8,"application/json");
using var rejection=await client.SendAsync(portalWithoutProxy);
Expect(rejection.StatusCode,HttpStatusCode.Unauthorized,
"portal token must never bypass trusted TLS proxy");
}
if(fakeEconomy.GetBalance(AccountProxy.Principal.Guid)!=125)
throw new Exception("Unauthorized portal call changed NV$ balance");
// Valid backend credential is distinct from the general read-only portal token.
// It may authorize an explicit manual grant, but not a claimed provider settlement.
Guid portalRequestId=Guid.NewGuid();
async Task<HttpResponseMessage> PortalMoney(string source,long amount,Guid reqId)
{
var msg=new HttpRequestMessage(HttpMethod.Post,"/api/v1/portal/money/payouts");
msg.Headers.Authorization=new("Bearer",new string('C',64));
msg.Headers.TryAddWithoutValidation("X-Forwarded-Proto","https");
msg.Headers.TryAddWithoutValidation("X-Forwarded-Host","sysapi.example.test:8850");
msg.Content=new StringContent(JsonSerializer.Serialize(new{
avatarId=AccountProxy.Principal.ToString(),amountNv=amount,
description="Portal test grant",requestId=reqId.ToString("D"),
source=source
}),Encoding.UTF8,"application/json");
using(msg) return await client.SendAsync(msg);
}
using(var settled=await PortalMoney("portal-manual",50,portalRequestId))
Expect(settled.StatusCode,HttpStatusCode.Created,"authenticated portal grant");
using(var repeated=await PortalMoney("portal-manual",50,portalRequestId))
Expect(repeated.StatusCode,HttpStatusCode.OK,"portal at most one grant");
using(var alteredPortal=await PortalMoney("portal-manual",75,portalRequestId))
Expect(alteredPortal.StatusCode,HttpStatusCode.Conflict,"portal replay mismatch rejected");
using(var unverified=await PortalMoney("paypal",50,Guid.NewGuid()))
Expect(unverified.StatusCode,HttpStatusCode.BadRequest,"claimed Paypal settlement never accepted");
if(fakeEconomy.GetBalance(AccountProxy.Principal.Guid)!=175)
throw new Exception("Portal payments failed double-entry or idempotency");
Console.WriteLine("PASS: SYS.API portal token isolation, TLS proxy gate, in-memory ledger credits and payment-provider denial");
Console.WriteLine("PASS: SYS.API NV$ exact 100:1 rate, admin ledger credit, idempotency, mismatch rejection, portal auth and fake-payment denial");
async Task<HttpResponseMessage> PostAlert(string command) =>
await client.PostAsync("/api/admin/alerts",
new StringContent(JsonSerializer.Serialize(new { command }), Encoding.UTF8, "application/json"));