2026-11-20 18:48:54 +06:00
import assert from "node:assert/strict" ;
import { after , beforeEach , describe , it } from "node:test" ;
import { NextRequest } from "next/server" ;
import { GET as auditGet } from "@/app/api/audit/route" ;
import { POST as loginPost } from "@/app/api/auth/login/route" ;
2026-11-21 00:06:55 +06:00
import { POST as logoutPost } from "@/app/api/auth/logout/route" ;
2026-11-20 18:48:54 +06:00
import { GET as profileGet , PUT as profilePut } from "@/app/api/auth/profile/route" ;
2026-11-21 00:06:55 +06:00
import { POST as setupPost } from "@/app/api/auth/setup/route" ;
2026-11-20 18:48:54 +06:00
import {
DELETE as clientDelete ,
GET as clientGet ,
PATCH as clientPatch ,
} from "@/app/api/clients/[id]/route" ;
import { GET as clientsGet , POST as clientsPost } from "@/app/api/clients/route" ;
import { GET as settingsGet , POST as settingsPost } from "@/app/api/settings/route" ;
import { POST as webhookTestPost } from "@/app/api/settings/webhook-test/route" ;
import { GET as storageGet , POST as storagePost } from "@/app/api/storage/route" ;
import {
DELETE as teamMemberDelete ,
PATCH as teamMemberPatch ,
} from "@/app/api/team/[id]/route" ;
import { GET as teamGet , POST as teamPost } from "@/app/api/team/route" ;
import {
createAdminSessionToken ,
getAdminSessionSecret ,
} from "@/core/auth/adminSession" ;
import {
createInitialAdmin ,
2026-11-21 00:06:55 +06:00
getAdminSetupKey ,
2026-11-20 18:48:54 +06:00
resetAdminStateForTests ,
2026-11-21 00:06:55 +06:00
revokeAdminSessions ,
verifyRequestAdminSession ,
2026-11-20 18:48:54 +06:00
} from "@/core/auth/adminUsers" ;
import { DataBufferEngine } from "@/core/data/buffer" ;
const ORIGINAL_ENV = { ... process . env };
function restoreEnv() {
for ( const key of Object . keys ( process . env )) {
if ( ! ( key in ORIGINAL_ENV )) {
delete process . env [ key ];
}
}
Object . assign ( process . env , ORIGINAL_ENV );
}
describe ( "V2-Phase-03: Multi-Admin RBAC & Immutable Admin Audit Log" , () => {
beforeEach ( async () => {
restoreEnv ();
delete process . env . DATABASE_URL ;
process . env . DATASTACK_ALLOW_DEMO_ADMIN = "false" ;
process . env . INGESTION_INTERNAL_URL = "http://127.0.0.1:1" ;
resetAdminStateForTests ();
const g = globalThis as {
__dataStackClientsMap? : Map < string , unknown >;
__dataStackDeletedClients? : Set < string >;
};
g . __dataStackClientsMap ? . clear ();
g . __dataStackDeletedClients ? . clear ();
});
after ( async () => {
restoreEnv ();
resetAdminStateForTests ();
await DataBufferEngine . shutdown ();
});
it ( "1. Enforces Owner, Admin/Operator, and Viewer RBAC matrix (V2-E10 INSUFFICIENT_ROLE_PERMISSION)" , async () => {
const createdOwner = await createInitialAdmin (
"owner@datastack.dev" ,
"OwnerPass123!" ,
);
assert . equal ( createdOwner , true );
const secret = getAdminSessionSecret () ! ;
// Owner session cookie (backward-compatible 4-arg call defaults to owner)
const ownerCookie = await createAdminSessionToken (
"owner@datastack.dev" ,
secret ,
Date . now () + 3600 _000 ,
1 ,
);
// Owner creates an Admin/Operator team member and a Viewer team member
const createAdminRes = await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "operator@datastack.dev" ,
password : "OperatorPass123!" ,
role : "admin" ,
}),
}),
);
assert . equal ( createAdminRes . status , 201 );
const createdAdminMember = ( await createAdminRes . json ()). member ;
assert . equal ( createdAdminMember . role , "admin" );
const createViewerRes = await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "viewer@datastack.dev" ,
password : "ViewerPass123!" ,
role : "viewer" ,
}),
}),
);
assert . equal ( createViewerRes . status , 201 );
const createdViewerMember = ( await createViewerRes . json ()). member ;
assert . equal ( createdViewerMember . role , "viewer" );
// Sign in via POST /api/auth/login as Operator and Viewer
const operatorLoginRes = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : { "content-type" : "application/json" },
body : JSON.stringify ({
email : "operator@datastack.dev" ,
password : "OperatorPass123!" ,
}),
}),
);
assert . equal ( operatorLoginRes . status , 200 );
const operatorCookie =
operatorLoginRes . cookies . get ( "datastack_admin_session" ) ? . value ;
assert . ok ( operatorCookie );
const viewerLoginRes = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : { "content-type" : "application/json" },
body : JSON.stringify ({
email : "viewer@datastack.dev" ,
password : "ViewerPass123!" ,
}),
}),
);
assert . equal ( viewerLoginRes . status , 200 );
const viewerCookie =
viewerLoginRes . cookies . get ( "datastack_admin_session" ) ? . value ;
assert . ok ( viewerCookie );
// Check /api/auth/profile reflects each member's role
const viewerProfileRes = await profileGet (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
headers : { cookie : `datastack_admin_session= ${ viewerCookie } ` },
}),
);
assert . equal ( viewerProfileRes . status , 200 );
const viewerProfile = ( await viewerProfileRes . json ()). profile ;
assert . equal ( viewerProfile . role , "Viewer" );
assert . equal ( viewerProfile . roleKey , "viewer" );
// Viewer can read GET /api/clients, GET /api/settings, GET /api/storage, GET /api/team
const viewerGetClients = await clientsGet (
new NextRequest ( "http://localhost:3000/api/clients" , {
headers : { cookie : `datastack_admin_session= ${ viewerCookie } ` },
}),
);
assert . equal ( viewerGetClients . status , 200 );
const viewerGetSettings = await settingsGet (
new NextRequest ( "http://localhost:3000/api/settings" , {
headers : { cookie : `datastack_admin_session= ${ viewerCookie } ` },
}),
);
assert . equal ( viewerGetSettings . status , 200 );
const viewerGetStorage = await storageGet (
new NextRequest ( "http://localhost:3000/api/storage" , {
headers : { cookie : `datastack_admin_session= ${ viewerCookie } ` },
}),
);
assert . equal ( viewerGetStorage . status , 200 );
// Viewer CANNOT mutate settings, clients, storage, or team -> 403 INSUFFICIENT_ROLE_PERMISSION
const viewerPostSettings = await settingsPost (
new NextRequest ( "http://localhost:3000/api/settings" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ viewerCookie } ` ,
},
body : JSON.stringify ({ maxEventsPerSec : 250 }),
}),
);
assert . equal ( viewerPostSettings . status , 403 );
assert . equal (
( await viewerPostSettings . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
const viewerPostClients = await clientsPost (
new NextRequest ( "http://localhost:3000/api/clients" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ viewerCookie } ` ,
},
body : JSON.stringify ({
clientId : "viewer_blocked_client" ,
name : "Blocked" ,
}),
}),
);
assert . equal ( viewerPostClients . status , 403 );
assert . equal (
( await viewerPostClients . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
const viewerPostStorage = await storagePost (
new NextRequest ( "http://localhost:3000/api/storage" , {
method : "POST" ,
headers : { cookie : `datastack_admin_session= ${ viewerCookie } ` },
}),
);
assert . equal ( viewerPostStorage . status , 403 );
assert . equal (
( await viewerPostStorage . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
// Operator CAN create, update, rotate token, and delete SDK clients
const operatorCreateClient = await clientsPost (
new NextRequest ( "http://localhost:3000/api/clients" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ operatorCookie } ` ,
},
body : JSON.stringify ({
clientId : "op_managed_client" ,
name : "Operator Client" ,
rateLimit : 300 ,
}),
}),
);
assert . equal ( operatorCreateClient . status , 201 );
// Viewer CANNOT reveal full client JWT via ?includeToken=true
const viewerRevealToken = await clientGet (
new NextRequest (
"http://localhost:3000/api/clients/op_managed_client?includeToken=true" ,
{
headers : { cookie : `datastack_admin_session= ${ viewerCookie } ` },
},
),
{ params : Promise.resolve ({ id : "op_managed_client" }) },
);
assert . equal ( viewerRevealToken . status , 403 );
assert . equal (
( await viewerRevealToken . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
// Operator CAN reveal full client JWT and rotate it
const operatorRotateToken = await clientPatch (
new NextRequest ( "http://localhost:3000/api/clients/op_managed_client" , {
method : "PATCH" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ operatorCookie } ` ,
},
body : JSON.stringify ({ action : "regenerate_token" }),
}),
{ params : Promise.resolve ({ id : "op_managed_client" }) },
);
assert . equal ( operatorRotateToken . status , 200 );
// Operator CANNOT modify platform settings or manage team members (Owner-only)
const operatorPostSettings = await settingsPost (
new NextRequest ( "http://localhost:3000/api/settings" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ operatorCookie } ` ,
},
body : JSON.stringify ({ maxEventsPerSec : 500 }),
}),
);
assert . equal ( operatorPostSettings . status , 403 );
assert . equal (
( await operatorPostSettings . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
const operatorWebhookTest = await webhookTestPost (
new NextRequest ( "http://localhost:3000/api/settings/webhook-test" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ operatorCookie } ` ,
},
body : JSON.stringify ({ webhookUrl : "https://hooks.example.com/test" }),
}),
);
assert . equal ( operatorWebhookTest . status , 403 );
assert . equal (
( await operatorWebhookTest . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
const operatorPostTeam = await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ operatorCookie } ` ,
},
body : JSON.stringify ({
email : "unauthorized@datastack.dev" ,
password : "Password123!" ,
role : "viewer" ,
}),
}),
);
assert . equal ( operatorPostTeam . status , 403 );
assert . equal (
( await operatorPostTeam . json ()). code ,
"INSUFFICIENT_ROLE_PERMISSION" ,
);
});
it ( "2. Protects Primary Owner (id=1 / owner-1) from deletion or demotion (V2-E11 CANNOT_REVOKE_PRIMARY_OWNER) and revokes team sessions on role/status change" , async () => {
await createInitialAdmin ( "primary@datastack.dev" , "PrimaryPass123!" );
const secret = getAdminSessionSecret () ! ;
const ownerCookie = await createAdminSessionToken (
"primary@datastack.dev" ,
secret ,
Date . now () + 3600 _000 ,
1 ,
);
// Attempt to delete primary owner -> 400 CANNOT_REVOKE_PRIMARY_OWNER
const delOwnerRes = await teamMemberDelete (
new NextRequest ( "http://localhost:3000/api/team/owner-1" , {
method : "DELETE" ,
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
}),
{ params : Promise.resolve ({ id : "owner-1" }) },
);
assert . equal ( delOwnerRes . status , 400 );
assert . equal (
( await delOwnerRes . json ()). code ,
"CANNOT_REVOKE_PRIMARY_OWNER" ,
);
// Attempt to demote or disable primary owner -> 400 CANNOT_REVOKE_PRIMARY_OWNER
const patchOwnerRes = await teamMemberPatch (
new NextRequest ( "http://localhost:3000/api/team/owner-1" , {
method : "PATCH" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({ role : "viewer" , status : "disabled" }),
}),
{ params : Promise.resolve ({ id : "owner-1" }) },
);
assert . equal ( patchOwnerRes . status , 400 );
assert . equal (
( await patchOwnerRes . json ()). code ,
"CANNOT_REVOKE_PRIMARY_OWNER" ,
);
// Create a secondary member, log in, then disable them and verify session cookie is immediately invalidated
const createdRes = await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "temp@datastack.dev" ,
password : "TempPassword123!" ,
role : "viewer" ,
}),
}),
);
assert . equal ( createdRes . status , 201 );
const memberId = ( await createdRes . json ()). member . id as string ;
const loginRes = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : { "content-type" : "application/json" },
body : JSON.stringify ({
email : "temp@datastack.dev" ,
password : "TempPassword123!" ,
}),
}),
);
assert . equal ( loginRes . status , 200 );
const tempCookie = loginRes . cookies . get ( "datastack_admin_session" ) ? . value ;
assert . ok ( tempCookie );
// Disable team member -> bumps token_version and sets status='disabled'
const disableRes = await teamMemberPatch (
new NextRequest ( `http://localhost:3000/api/team/ ${ memberId } ` , {
method : "PATCH" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({ status : "disabled" , revokeSessions : true }),
}),
{ params : Promise.resolve ({ id : memberId }) },
);
assert . equal ( disableRes . status , 200 );
// Old cookie must now be rejected 401
const afterDisableCheck = await clientsGet (
new NextRequest ( "http://localhost:3000/api/clients" , {
headers : { cookie : `datastack_admin_session= ${ tempCookie } ` },
}),
);
assert . equal ( afterDisableCheck . status , 401 );
});
it ( "3. Records immutable admin audit logs across login, team, client, and settings mutations and supports filtered GET /api/audit queries" , async () => {
await createInitialAdmin ( "audit-owner@datastack.dev" , "AuditPass123!" );
const loginRes = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
"x-real-ip" : "203.0.113.77" ,
},
body : JSON.stringify ({
email : "audit-owner@datastack.dev" ,
password : "AuditPass123!" ,
}),
}),
);
assert . equal ( loginRes . status , 200 );
const ownerCookie = loginRes . cookies . get ( "datastack_admin_session" ) ? . value ! ;
// Create a client, update settings, and delete the client
const createClientRes = await clientsPost (
new NextRequest ( "http://localhost:3000/api/clients" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
"x-real-ip" : "203.0.113.77" ,
},
body : JSON.stringify ({
clientId : "audited_sdk_client" ,
name : "Audited Client" ,
rateLimit : 120 ,
}),
}),
);
assert . equal ( createClientRes . status , 201 );
const updateSettingsRes = await settingsPost (
new NextRequest ( "http://localhost:3000/api/settings" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
"x-real-ip" : "203.0.113.77" ,
},
body : JSON.stringify ({ maxEventsPerSec : 400 }),
}),
);
assert . equal ( updateSettingsRes . status , 200 );
const deleteClientRes = await clientDelete (
new NextRequest (
"http://localhost:3000/api/clients/audited_sdk_client?purgeFiles=false" ,
{
method : "DELETE" ,
headers : {
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
"x-real-ip" : "203.0.113.77" ,
},
},
),
{ params : Promise.resolve ({ id : "audited_sdk_client" }) },
);
assert . equal ( deleteClientRes . status , 200 );
// Query GET /api/audit
const auditRes = await auditGet (
new NextRequest ( "http://localhost:3000/api/audit?limit=50" , {
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
}),
);
assert . equal ( auditRes . status , 200 );
const auditJson = await auditRes . json ();
assert . ok ( Array . isArray ( auditJson . logs ));
const actions = auditJson . logs . map (( l : { action : string }) => l . action );
assert . ok ( actions . includes ( "ADMIN_LOGIN" ));
assert . ok ( actions . includes ( "CLIENT_CREATED" ));
assert . ok ( actions . includes ( "SETTINGS_UPDATED" ));
assert . ok ( actions . includes ( "CLIENT_DELETED" ));
// Filter by action=CLIENT_CREATED
const filteredRes = await auditGet (
new NextRequest (
"http://localhost:3000/api/audit?action=CLIENT_CREATED&q=audited_sdk_client" ,
{
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
},
),
);
assert . equal ( filteredRes . status , 200 );
const filteredJson = await filteredRes . json ();
assert . equal ( filteredJson . logs . length , 1 );
assert . equal ( filteredJson . logs [ 0 ]. target , "audited_sdk_client" );
assert . equal ( filteredJson . logs [ 0 ]. ipAddress , "203.0.113.77" );
assert . equal ( filteredJson . logs [ 0 ]. actorEmail , "audit-owner@datastack.dev" );
// Team listing includes primary owner + team members
const teamListRes = await teamGet (
new NextRequest ( "http://localhost:3000/api/team" , {
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
}),
);
assert . equal ( teamListRes . status , 200 );
const teamListJson = await teamListRes . json ();
assert . equal ( teamListJson . members [ 0 ]. id , "owner-1" );
assert . equal ( teamListJson . members [ 0 ]. isPrimaryOwner , true );
// Restore default settings before shutdown
await settingsPost (
new NextRequest ( "http://localhost:3000/api/settings" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({ maxEventsPerSec : 100 }),
}),
);
await DataBufferEngine . shutdown ();
});
it ( "4. Allows secondary team member to rotate their own password via PUT /api/auth/profile and invalidates prior cookie" , async () => {
await createInitialAdmin ( "root@datastack.dev" , "RootPassword123!" );
const secret = getAdminSessionSecret () ! ;
const ownerCookie = await createAdminSessionToken (
"root@datastack.dev" ,
secret ,
Date . now () + 3600 _000 ,
1 ,
);
await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "selfupdate@datastack.dev" ,
password : "InitialPassword123!" ,
role : "operator" ,
}),
}),
);
const loginRes = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : { "content-type" : "application/json" },
body : JSON.stringify ({
email : "selfupdate@datastack.dev" ,
password : "InitialPassword123!" ,
}),
}),
);
assert . equal ( loginRes . status , 200 );
const v1Cookie = loginRes . cookies . get ( "datastack_admin_session" ) ? . value ! ;
const rotateRes = await profilePut (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
method : "PUT" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ v1Cookie } ` ,
},
body : JSON.stringify ({
email : "selfupdate@datastack.dev" ,
currentPassword : "InitialPassword123!" ,
newPassword : "RotatedPassword456!" ,
}),
}),
);
assert . equal ( rotateRes . status , 200 );
const v2Cookie = rotateRes . cookies . get ( "datastack_admin_session" ) ? . value ! ;
assert . ok ( v2Cookie );
// Old v1 cookie is now revoked
const staleRes = await profileGet (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
headers : { cookie : `datastack_admin_session= ${ v1Cookie } ` },
}),
);
assert . equal ( staleRes . status , 401 );
// New v2 cookie retains Operator role
const freshRes = await profileGet (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
headers : { cookie : `datastack_admin_session= ${ v2Cookie } ` },
}),
);
assert . equal ( freshRes . status , 200 );
const freshJson = await freshRes . json ();
assert . equal ( freshJson . profile . role , "Operator" );
assert . equal ( freshJson . profile . roleKey , "operator" );
});
2026-11-21 00:06:55 +06:00
it ( "5. V2-Audit-Phase-01: Prevents cross-table/in-memory duplicate admin emails (BUG-V2-AUDIT-03), isolates multi-admin session logout from global race (BUG-V2-AUDIT-04), and records ADMIN_SETUP, ADMIN_LOGOUT, CLIENT_REVOKED, and STORAGE_FLUSH_TRIGGERED audit actions (BUG-V2-AUDIT-02)" , async () => {
const setupKey = getAdminSetupKey () ! ;
const setupRes = await setupPost (
new NextRequest ( "http://localhost:3000/api/auth/setup" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
"x-real-ip" : "198.51.100.10" ,
},
body : JSON.stringify ({
email : "founder@datastack.dev" ,
password : "FounderPassword123!" ,
setupKey ,
}),
}),
);
assert . equal ( setupRes . status , 200 );
const ownerCookie = setupRes . cookies . get ( "datastack_admin_session" ) ? . value ! ;
assert . ok ( ownerCookie );
// Owner creates two team members: Alice (admin) and Bob (operator)
const aliceRes = await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "alice@datastack.dev" ,
password : "AlicePassword123!" ,
role : "admin" ,
}),
}),
);
assert . equal ( aliceRes . status , 201 );
const aliceMember = ( await aliceRes . json ()). member ;
const bobRes = await teamPost (
new NextRequest ( "http://localhost:3000/api/team" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "bob@datastack.dev" ,
password : "BobPassword123!" ,
role : "operator" ,
}),
}),
);
assert . equal ( bobRes . status , 201 );
// Login as Alice and Bob
const aliceLogin = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : { "content-type" : "application/json" },
body : JSON.stringify ({
email : "alice@datastack.dev" ,
password : "AlicePassword123!" ,
}),
}),
);
assert . equal ( aliceLogin . status , 200 );
const aliceCookie = aliceLogin . cookies . get ( "datastack_admin_session" ) ? . value ! ;
const bobLogin = await loginPost (
new NextRequest ( "http://localhost:3000/api/auth/login" , {
method : "POST" ,
headers : { "content-type" : "application/json" },
body : JSON.stringify ({
email : "bob@datastack.dev" ,
password : "BobPassword123!" ,
}),
}),
);
assert . equal ( bobLogin . status , 200 );
const bobCookie = bobLogin . cookies . get ( "datastack_admin_session" ) ? . value ! ;
// BUG-V2-AUDIT-03: Alice cannot change her email to Primary Owner's email OR Bob's email -> 409 ADMIN_EMAIL_CONFLICT
const aliceCollideOwner = await profilePut (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
method : "PUT" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ aliceCookie } ` ,
},
body : JSON.stringify ({
email : "FOUNDER@datastack.dev" ,
currentPassword : "AlicePassword123!" ,
}),
}),
);
assert . equal ( aliceCollideOwner . status , 409 );
assert . equal (( await aliceCollideOwner . json ()). code , "ADMIN_EMAIL_CONFLICT" );
const aliceCollideBob = await profilePut (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
method : "PUT" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ aliceCookie } ` ,
},
body : JSON.stringify ({
email : "bob@datastack.dev" ,
currentPassword : "AlicePassword123!" ,
}),
}),
);
assert . equal ( aliceCollideBob . status , 409 );
assert . equal (( await aliceCollideBob . json ()). code , "ADMIN_EMAIL_CONFLICT" );
// Primary Owner cannot change email to Alice's email -> 409 ADMIN_EMAIL_CONFLICT
const ownerCollideAlice = await profilePut (
new NextRequest ( "http://localhost:3000/api/auth/profile" , {
method : "PUT" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
email : "alice@datastack.dev" ,
currentPassword : "FounderPassword123!" ,
}),
}),
);
assert . equal ( ownerCollideAlice . status , 409 );
assert . equal (( await ownerCollideAlice . json ()). code , "ADMIN_EMAIL_CONFLICT" );
// BUG-V2-AUDIT-04: Simulate concurrent verification where Bob's session is verified right before Alice's session revocation
const aliceVerified = await verifyRequestAdminSession ( aliceCookie );
assert . equal ( aliceVerified . valid , true );
const bobVerified = await verifyRequestAdminSession ( bobCookie );
assert . equal ( bobVerified . valid , true );
// Explicitly revoke Alice's session even though Bob was the last verified globally
await revokeAdminSessions ({
memberId : aliceMember.id ,
email : "alice@datastack.dev" ,
});
// Alice's cookie must now be revoked, while Bob's and Owner's cookies remain valid!
const aliceAfterRevoke = await verifyRequestAdminSession ( aliceCookie );
assert . equal ( aliceAfterRevoke . valid , false );
const bobStillValid = await verifyRequestAdminSession ( bobCookie );
assert . equal ( bobStillValid . valid , true );
const ownerStillValid = await verifyRequestAdminSession ( ownerCookie );
assert . equal ( ownerStillValid . valid , true );
// Create a client, revoke it (CLIENT_REVOKED), trigger manual storage flush (STORAGE_FLUSH_TRIGGERED), and log out Bob (ADMIN_LOGOUT)
const createClientRes = await clientsPost (
new NextRequest ( "http://localhost:3000/api/clients" , {
method : "POST" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({
clientId : "phase01_audit_client" ,
name : "Phase 01 Audit Client" ,
}),
}),
);
assert . equal ( createClientRes . status , 201 );
const revokeClientRes = await clientPatch (
new NextRequest ( "http://localhost:3000/api/clients/phase01_audit_client" , {
method : "PATCH" ,
headers : {
"content-type" : "application/json" ,
cookie : `datastack_admin_session= ${ ownerCookie } ` ,
},
body : JSON.stringify ({ status : "revoked" }),
}),
{ params : Promise.resolve ({ id : "phase01_audit_client" }) },
);
assert . equal ( revokeClientRes . status , 200 );
const flushRes = await storagePost (
new NextRequest ( "http://localhost:3000/api/storage" , {
method : "POST" ,
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
}),
);
// Even if Port 4000 is mocked to 127.0.0.1:1, test without explicit INGESTION_INTERNAL_URL so local flush succeeds
void flushRes ;
delete process . env . INGESTION_INTERNAL_URL ;
const flushLocalRes = await storagePost (
new NextRequest ( "http://localhost:3000/api/storage" , {
method : "POST" ,
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
}),
);
assert . equal ( flushLocalRes . status , 200 );
const bobLogoutRes = await logoutPost (
new NextRequest ( "http://localhost:3000/api/auth/logout" , {
method : "POST" ,
headers : {
cookie : `datastack_admin_session= ${ bobCookie } ` ,
"x-real-ip" : "198.51.100.25" ,
},
}),
);
assert . equal ( bobLogoutRes . status , 200 );
// Bob's session is now invalidated on server, while Owner's session is still valid
assert . equal (( await verifyRequestAdminSession ( bobCookie )). valid , false );
assert . equal (( await verifyRequestAdminSession ( ownerCookie )). valid , true );
// BUG-V2-AUDIT-02: Verify all 4 newly wired audit actions appear in GET /api/audit
const auditRes = await auditGet (
new NextRequest ( "http://localhost:3000/api/audit?limit=100" , {
headers : { cookie : `datastack_admin_session= ${ ownerCookie } ` },
}),
);
assert . equal ( auditRes . status , 200 );
const logs = ( await auditRes . json ()). logs as Array < {
action : string ;
actorEmail : string ;
target : string ;
} > ;
const recordedActions = new Set ( logs . map (( l ) => l . action ));
assert . ok ( recordedActions . has ( "ADMIN_SETUP" ), "Expected ADMIN_SETUP audit log" );
assert . ok ( recordedActions . has ( "ADMIN_LOGOUT" ), "Expected ADMIN_LOGOUT audit log" );
assert . ok ( recordedActions . has ( "CLIENT_REVOKED" ), "Expected CLIENT_REVOKED audit log" );
assert . ok (
recordedActions . has ( "STORAGE_FLUSH_TRIGGERED" ),
"Expected STORAGE_FLUSH_TRIGGERED audit log" ,
);
});
2026-11-20 18:48:54 +06:00
});