Files
sencho/backend/src/__tests__/blueprints-authz.test.ts
T
Anso 865d792874 feat(pricing): collapse to two tiers (#1309)
* feat(pricing): collapse to two tiers (Community + Admiral)

Collapse Sencho's pricing from three tiers (Community / Skipper / Admiral)
to two: a generous free Community tier and a single paid Admiral tier. The
Skipper tier is removed.

Now free in Community: auto-heal, auto-update, scheduled operations,
webhooks, notification routing, Fleet Actions and bulk operations, SSO
preset providers (Google / GitHub / Okta), unlimited users with admin and
viewer roles, and deploy safety (atomic deploys, auto-rollback, and
one-click rollback).

Admiral (paid) is focused on running and governing a fleet: blueprints,
Fleet Secrets, deploy enforcement, vulnerability report export, audit log,
host console, private registries, mesh networking, node cordon, managed
cloud backup, LDAP / Active Directory SSO, and the advanced RBAC roles
(deployer, node-admin, auditor) with per-resource scoped assignments.

Internally the license variant distinction is removed so tier is binary
(community / paid). License validation still verifies the Lemon Squeezy
store and product before granting paid status.

Docs and the contributor guide are updated to the two-tier model.

* docs(pricing): correct licensing page to two-tier pricing and tidy stale tier wording

The licensing docs page kept the old Admiral pricing plus a Founder
Lifetime column and an Enterprise paragraph after the two-tier collapse.
Update it to $12/month or $99/year, drop the lifetime and Enterprise
content, and link to the pricing page for current pricing.

Also fix stale "Skipper" wording in CLA.md, SUPPORT.md, one test title,
and three test comments. Historical CHANGELOG entries and the
retired-Skipper license-guard test are intentionally left as-is.

* docs: align licensing and SSO pages with the two-tier model

Correct the SSO overview so the Google, GitHub, and Okta presets read as
available on every tier, matching the provider table; only LDAP and Active
Directory require Sencho Admiral. Remove the lifetime-plan references from the
licensing, settings, and troubleshooting pages so they reflect subscription-only
Admiral pricing.

* fix(rbac): omit scoped permissions from /me on the Community tier

Scoped role assignments only take effect on the paid tier, but GET /api/permissions/me returned them unconditionally, so a downgraded instance with leftover assignments rendered per-resource affordances the API then rejected with 403. The endpoint now mirrors the permission middleware and includes scoped permissions only on the paid tier. Adds a regression test covering the downgrade case.

* docs: use custom-pricing wording on the contact page

The two-tier model has no Enterprise tier; reword the contact page's enterprise pricing/deals to custom pricing/deals so it does not imply a tier that no longer exists.
2026-06-04 17:45:53 -04:00

219 lines
8.8 KiB
TypeScript

/**
* Authorization parity tests for /api/blueprints.
*
* The Blueprints UI gates affordances on the paid tier and admin role; these
* tests pin the matching server-side guards so a UI gate and a route guard
* cannot silently drift apart. Specifically:
* - PUT /:id/pin requires the paid tier AND admin role (the admin-role half is
* the parity gap the Federation pin control was hardened to match).
* - The mutation routes require admin role.
* - The read routes require paid tier but NOT admin role.
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
import request from 'supertest';
import type { LicenseTier } from '../services/license-types';
import { setupTestDb, cleanupTestDb, loginAsTestAdmin } from './helpers/setupTestDb';
let tmpDir: string;
let app: import('express').Express;
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
let LicenseService: typeof import('../services/LicenseService').LicenseService;
let BlueprintReconciler: typeof import('../services/BlueprintReconciler').BlueprintReconciler;
let adminCookie: string;
let viewerCookie: string;
let counter = 0;
function setLicense(tier: LicenseTier): void {
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue(tier);
}
function seedNode(): { id: number; name: string } {
counter += 1;
const name = `bp-authz-node-${counter}`;
const db = DatabaseService.getInstance().getDb();
const result = db.prepare(
`INSERT INTO nodes (name, type, mode, compose_dir, is_default, status, created_at)
VALUES (?, 'local', 'proxy', '/tmp/compose', 0, 'online', ?)`,
).run(name, Date.now());
return { id: result.lastInsertRowid as number, name };
}
function seedBlueprint(nodeIds: number[]) {
counter += 1;
return DatabaseService.getInstance().createBlueprint({
name: `bp-authz-${counter}`,
description: null,
compose_content: 'services:\n app:\n image: nginx\n',
selector: { type: 'nodes', ids: nodeIds },
drift_mode: 'suggest',
classification: 'stateless',
classification_reasons: [],
enabled: true,
created_by: 'admin',
});
}
async function seedAndLoginViewer(): Promise<string> {
const bcrypt = (await import('bcrypt')).default;
const supertest = (await import('supertest')).default;
const passwordHash = await bcrypt.hash('bp-viewer-pass', 1);
DatabaseService.getInstance().addUser({ username: 'bp-viewer', password_hash: passwordHash, role: 'viewer' });
const res = await supertest(app)
.post('/api/auth/login')
.send({ username: 'bp-viewer', password: 'bp-viewer-pass' });
const cookies = res.headers['set-cookie'] as string | string[];
return Array.isArray(cookies) ? cookies[0] : cookies;
}
beforeAll(async () => {
tmpDir = await setupTestDb();
({ DatabaseService } = await import('../services/DatabaseService'));
({ LicenseService } = await import('../services/LicenseService'));
({ BlueprintReconciler } = await import('../services/BlueprintReconciler'));
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('paid');
({ app } = await import('../index'));
adminCookie = await loginAsTestAdmin(app);
viewerCookie = await seedAndLoginViewer();
});
afterAll(() => cleanupTestDb(tmpDir));
beforeEach(() => {
vi.restoreAllMocks();
setLicense('paid');
// Neutralize the post-pin background reconcile so the 200 path has no side effects.
vi.spyOn(BlueprintReconciler.getInstance(), 'reconcileOne').mockResolvedValue(undefined);
const db = DatabaseService.getInstance().getDb();
db.prepare('DELETE FROM blueprint_deployments').run();
db.prepare('DELETE FROM blueprints').run();
db.prepare('DELETE FROM nodes WHERE is_default = 0').run();
});
describe('PUT /api/blueprints/:id/pin authorization', () => {
it('allows an admin on a paid license to pin a blueprint', async () => {
const node = seedNode();
const bp = seedBlueprint([node.id]);
const res = await request(app)
.put(`/api/blueprints/${bp.id}/pin`)
.set('Cookie', adminCookie)
.send({ nodeId: node.id });
expect(res.status).toBe(200);
expect(res.body.pinned_node_id).toBe(node.id);
});
it('allows an admin on a paid license to unpin (nodeId null)', async () => {
const node = seedNode();
const bp = seedBlueprint([node.id]);
DatabaseService.getInstance().setBlueprintPinnedNode(bp.id, node.id);
const res = await request(app)
.put(`/api/blueprints/${bp.id}/pin`)
.set('Cookie', adminCookie)
.send({ nodeId: null });
expect(res.status).toBe(200);
expect(res.body.pinned_node_id).toBeNull();
});
it('rejects an admin on a Community license with PAID_REQUIRED', async () => {
setLicense('community');
const node = seedNode();
const bp = seedBlueprint([node.id]);
const res = await request(app)
.put(`/api/blueprints/${bp.id}/pin`)
.set('Cookie', adminCookie)
.send({ nodeId: node.id });
expect(res.status).toBe(403);
expect(res.body.code).toBe('PAID_REQUIRED');
});
it('rejects a non-admin on a paid license with ADMIN_REQUIRED', async () => {
const node = seedNode();
const bp = seedBlueprint([node.id]);
const res = await request(app)
.put(`/api/blueprints/${bp.id}/pin`)
.set('Cookie', viewerCookie)
.send({ nodeId: node.id });
expect(res.status).toBe(403);
expect(res.body.code).toBe('ADMIN_REQUIRED');
});
});
describe('Blueprint mutation routes require admin role', () => {
// Tier is paid in beforeEach, so requirePaid passes and the admin guard is
// what rejects. The gate short-circuits before id parsing, so dummy ids are
// sufficient to prove the role boundary.
const mutations: Array<{ name: string; method: 'post' | 'put' | 'delete'; path: string }> = [
{ name: 'create', method: 'post', path: '/api/blueprints' },
{ name: 'update', method: 'put', path: '/api/blueprints/1' },
{ name: 'delete', method: 'delete', path: '/api/blueprints/1' },
{ name: 'apply', method: 'post', path: '/api/blueprints/1/apply' },
{ name: 'withdraw', method: 'post', path: '/api/blueprints/1/withdraw/1' },
{ name: 'accept', method: 'post', path: '/api/blueprints/1/accept/1' },
];
it.each(mutations)('rejects a non-admin on $name with ADMIN_REQUIRED', async ({ method, path }) => {
const res = await request(app)[method](path).set('Cookie', viewerCookie).send({});
expect(res.status).toBe(403);
expect(res.body.code).toBe('ADMIN_REQUIRED');
});
it('rejects an admin on a Community license from creating with PAID_REQUIRED', async () => {
setLicense('community');
const res = await request(app)
.post('/api/blueprints')
.set('Cookie', adminCookie)
.send({});
expect(res.status).toBe(403);
expect(res.body.code).toBe('PAID_REQUIRED');
});
});
describe('Blueprint read routes require paid tier but not admin role', () => {
it('lets a non-admin paid user list blueprints', async () => {
seedBlueprint([]);
const res = await request(app).get('/api/blueprints').set('Cookie', viewerCookie);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
it('lets a non-admin paid user fetch a blueprint detail', async () => {
const bp = seedBlueprint([]);
const res = await request(app).get(`/api/blueprints/${bp.id}`).set('Cookie', viewerCookie);
expect(res.status).toBe(200);
expect(res.body.blueprint.id).toBe(bp.id);
});
it('lets a non-admin paid user preview a blueprint', async () => {
const node = seedNode();
const bp = seedBlueprint([node.id]);
const res = await request(app).get(`/api/blueprints/${bp.id}/preview`).set('Cookie', viewerCookie);
expect(res.status).toBe(200);
expect(res.body.blueprintId).toBe(bp.id);
});
it('lets a non-admin paid user analyze compose', async () => {
const res = await request(app)
.post('/api/blueprints/analyze')
.set('Cookie', viewerCookie)
.send({ compose_content: 'services:\n app:\n image: nginx\n' });
expect(res.status).toBe(200);
expect(res.body.classification).toBeDefined();
});
it('rejects an admin on a Community license from listing with PAID_REQUIRED', async () => {
setLicense('community');
const res = await request(app).get('/api/blueprints').set('Cookie', adminCookie);
expect(res.status).toBe(403);
expect(res.body.code).toBe('PAID_REQUIRED');
});
});