mirror of
https://github.com/nestriness/nestri.git
synced 2026-11-16 12:12:28 +02:00
feat(machine): rented hardware is open during a run you asked for
Fleet machines refused everyone, because nothing could yet say who had paid for them. A run on one can only exist once it passed the start check, so a starting or live run of your own box on that machine is now what grants access, and it ends with the run. Holding a box is not enough: a box costs nothing to hold.
This commit is contained in:
@@ -5,7 +5,10 @@ import { Box } from '@nestri/core/box/index';
|
||||
import { Fixtures } from '@nestri/core/db/fixtures';
|
||||
import { testDb } from '@nestri/core/db/test';
|
||||
import { Env } from '@nestri/core/env';
|
||||
import { Game } from '@nestri/core/game/index';
|
||||
import { Identifier } from '@nestri/core/id';
|
||||
import { Machine } from '@nestri/core/machine/index';
|
||||
import { Session } from '@nestri/core/session/index';
|
||||
|
||||
import { app } from '../app/index';
|
||||
import { TEST_FRONTEND_URL } from './setup';
|
||||
@@ -13,6 +16,7 @@ import { TEST_FRONTEND_URL } from './setup';
|
||||
const sql = testDb();
|
||||
const createdUserIds: string[] = [];
|
||||
const createdOrgIds: string[] = [];
|
||||
const createdGameIds: string[] = [];
|
||||
|
||||
function useFleet(organisationId?: string) {
|
||||
Env.init({
|
||||
@@ -53,7 +57,9 @@ function createBox(headers: Record<string, string>, body: object) {
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
await sql`delete from session where box_id in (select id from box where user_id = any(${createdUserIds}))`;
|
||||
await sql`delete from box where user_id = any(${createdUserIds})`;
|
||||
await sql`delete from game where id = any(${createdGameIds})`;
|
||||
await sql`delete from machine where owner_user_id = any(${createdUserIds})`;
|
||||
await sql`delete from organisation where id = any(${createdOrgIds})`;
|
||||
await sql`delete from "user" where id = any(${createdUserIds})`;
|
||||
@@ -125,3 +131,39 @@ describe('POST /box on the fleet', () => {
|
||||
expect(((await res.json()) as any).data.machineId).toBe(machineId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Entitlement on a fleet machine', () => {
|
||||
test('open during your own run on it, and closed before and after', async () => {
|
||||
const f = await fleet(1);
|
||||
useFleet(f.organisationId);
|
||||
const machineId = f.machines[0]!.id;
|
||||
const p = await person('entitle-run');
|
||||
const other = await person('entitle-other');
|
||||
const box = ((await (await createBox(p.headers, { on: 'fleet' })).json()) as any).data;
|
||||
|
||||
const may = (userId: string) => Machine.entitlement({ machineId, userId });
|
||||
expect(await may(p.owner.userId)).toEqual({ entitled: false, reason: 'fleet' });
|
||||
|
||||
const [game] = await Game.upsert({
|
||||
id: Identifier.ascending('game'),
|
||||
steamAppId: 990001,
|
||||
slug: 'fleet-entitle',
|
||||
name: 'Fleet Entitle'
|
||||
});
|
||||
createdGameIds.push(game!.id);
|
||||
const run = await Session.request({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId: game!.id,
|
||||
linkedAccountId: p.owner.linkedAccountId
|
||||
});
|
||||
await sql`update session set state = 'live' where id = ${run.id}`;
|
||||
|
||||
expect(await may(p.owner.userId)).toEqual({ entitled: true, reason: 'fleet' });
|
||||
// Somebody else's run on the same machine grants you nothing.
|
||||
expect((await may(other.owner.userId)).entitled).toBe(false);
|
||||
|
||||
await sql`update session set state = 'ended', time_stopped = now() where id = ${run.id}`;
|
||||
expect((await may(p.owner.userId)).entitled).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { randomBytes } from 'node:crypto';
|
||||
|
||||
import { and, eq, isNull, sql } from 'drizzle-orm';
|
||||
import { and, eq, inArray, isNull, sql } from 'drizzle-orm';
|
||||
import z from 'zod';
|
||||
|
||||
import { BoxTable } from '../box/box.sql.js';
|
||||
import { Database } from '../db/index.js';
|
||||
import { ErrorCodes, VisibleError } from '../error.js';
|
||||
import { Examples } from '../examples.js';
|
||||
import { fn } from '../fn.js';
|
||||
import { SessionTable } from '../session/session.sql.js';
|
||||
import { Member } from '../team/member.js';
|
||||
import { MachineTable } from './machine.sql.js';
|
||||
import { Slug } from './slug.js';
|
||||
@@ -398,12 +400,12 @@ export namespace Machine {
|
||||
* removing someone from a team takes their box access with it and nobody
|
||||
* has to remember to revoke anything.
|
||||
*
|
||||
* **Fleet hardware refuses everyone for now, and that is deliberate.** What
|
||||
* grants it is a plan, and nothing here can yet ask whether a user has one
|
||||
* — so the honest answer is no rather than a yes that would hand out metered
|
||||
* hardware for free. Failing closed on the expensive case is the cheap
|
||||
* mistake to make; the branch is written out so there is one obvious place
|
||||
* for the plan check to land. todo(d-0051)
|
||||
* **Fleet hardware is open only during a run you asked for.** The plan check
|
||||
* is made once, when the run is requested, and a run that passed it is what
|
||||
* grants access — so this asks for one, starting or live, of a box you own
|
||||
* on this machine. Holding a box is not enough: a box is free to hold, and
|
||||
* the hardware under it is not. When the run ends, so does the access.
|
||||
* ref(d-0051)
|
||||
*/
|
||||
export const entitlement = fn(
|
||||
z.object({ machineId: z.string(), userId: z.string() }),
|
||||
@@ -415,7 +417,23 @@ export namespace Machine {
|
||||
if (machine.organisationId) {
|
||||
// Fleet hardware. Not the owner's and not a team's, so neither
|
||||
// test below means anything here.
|
||||
return { entitled: false, reason: 'fleet' };
|
||||
const run = await Database.use((tx) =>
|
||||
tx
|
||||
.select({ id: SessionTable.id })
|
||||
.from(SessionTable)
|
||||
.innerJoin(BoxTable, eq(SessionTable.boxId, BoxTable.id))
|
||||
.where(
|
||||
and(
|
||||
eq(BoxTable.machineId, machine.id),
|
||||
eq(BoxTable.userId, input.userId),
|
||||
inArray(SessionTable.state, ['starting', 'live']),
|
||||
isNull(SessionTable.timeDeleted),
|
||||
isNull(BoxTable.timeDeleted)
|
||||
)
|
||||
)
|
||||
.limit(1)
|
||||
);
|
||||
return { entitled: run.length > 0, reason: 'fleet' };
|
||||
}
|
||||
if (machine.ownerUserId && machine.ownerUserId === input.userId) {
|
||||
return { entitled: true, reason: 'owner' };
|
||||
|
||||
@@ -194,9 +194,9 @@ describe('Fleet hardware', () => {
|
||||
});
|
||||
|
||||
describe('Entitlement on fleet hardware', () => {
|
||||
test('nobody is entitled yet, and the reason says why', async () => {
|
||||
// What grants a run on metered hardware is a plan, and there is nothing
|
||||
// to ask yet — so this fails closed rather than giving it away.
|
||||
test('nobody without a run on it is entitled, and the reason says why', async () => {
|
||||
// A run that passed the plan check is what grants rented hardware; with
|
||||
// none, this fails closed rather than giving it away.
|
||||
const orgId = await newOrg('org-entitle', 'entitle.example', true);
|
||||
const userId = await newUser('entitle', 'someone@entitle.example', true);
|
||||
const registered = await Machine.register({
|
||||
|
||||
Reference in New Issue
Block a user