web/website/tests/git-identity.test.mjs
unimatrixgit 77cdf19faa Checkpoint Alpha 1.0.0.3 graphical menus and moderation evidence
Preserve the current Alpha source and artwork snapshot. See handoff/alpha-1.0.0.3 for actual deployed hashes, checks, staged follow-ups and the production pause. This checkpoint does not claim every source feature is deployed.
2026-11-02 00:05:34 +01:00

29 lines
2.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {DatabaseSync} from 'node:sqlite';
import {mkdtemp,rm} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import {nativeGitIdentity,createGitIdentityRoute,gitProfileUrl} from '../node/git-identity.mjs';
test('ownership comes from immutable repository owner id, not collaborator access or a username',async()=>{
const dir=await mkdtemp(join(tmpdir(),'dune-git-')),path=join(dir,'native.db');
const db=new DatabaseSync(path);
db.exec('CREATE TABLE user(id INTEGER PRIMARY KEY,name TEXT,is_active INTEGER,is_admin INTEGER,prohibit_login INTEGER,type INTEGER); CREATE TABLE repository(id INTEGER PRIMARY KEY,owner_id INTEGER,name TEXT,description TEXT,is_private INTEGER); CREATE TABLE collaborator(user_id INTEGER,repo_id INTEGER);');
db.exec("INSERT INTO user VALUES(1,'actual_owner',1,0,0,0),(2,'collaborator',1,0,0,0); INSERT INTO repository VALUES(10,1,'private-code','Private project',1); INSERT INTO collaborator VALUES(2,10);");
try{
assert.equal((await nativeGitIdentity(1,path)).repositoryOwner,true);
const collaborator=await nativeGitIdentity(2,path);assert.equal(collaborator.ownedRepoCount,0);assert.deepEqual(collaborator.repos,[]);
db.exec("UPDATE user SET name='renamed_owner' WHERE id=1");assert.equal((await nativeGitIdentity(1,path)).profileUrl,'https://www.dunerealms.com/git/renamed_owner');
db.exec('DELETE FROM repository WHERE id=10');assert.equal((await nativeGitIdentity(1,path)).repositoryOwner,false);
db.exec('UPDATE user SET prohibit_login=1 WHERE id=1');assert.equal(await nativeGitIdentity(1,path),null);
}finally{db.close();await rm(dir,{recursive:true,force:true});}
});
test('self API fails closed without a central session and rejects mutations',async()=>{
const db={prepare(){throw Error('No database access should occur without session');}};
const route=createGitIdentityRoute(db,{});
assert.equal((await route(new Request('https://www.dunerealms.com/api/git/me'))).status,401);
assert.equal((await route(new Request('https://www.dunerealms.com/api/git/me',{method:'POST'}))).status,405);
assert.equal((await route(new Request('https://www.dunerealms.com/git/sso/callback?state=forged&code=forged'))).status,403);
assert.equal(gitProfileUrl('bad/name'),'https://www.dunerealms.com/git/bad%2Fname');
});