Membros → Pacotes FTP (the old app's pacotes), enforced through SFTPGo's auth hook; downloads per file set in the shop settings. Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
84 lines
5.8 KiB
TypeScript
84 lines
5.8 KiB
TypeScript
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { bearer, createTestApp, login, resetDb, seedUser, type TestContext } from "./helpers.js";
|
|
|
|
const KEY = "hook-key-0123456789abcdefghij";
|
|
|
|
describe("FTP logins through SFTPGo's external authentication", () => {
|
|
let ctx: TestContext;
|
|
beforeAll(async () => {
|
|
ctx = await createTestApp({ SFTPGO_HOOK_KEY: KEY, INGEST_ROOT: "/srv/photoreport-data/ingest" });
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.app.close();
|
|
await ctx.db.$disconnect();
|
|
});
|
|
beforeEach(() => resetDb(ctx.db));
|
|
|
|
const hook = (body: unknown, key = KEY) => ctx.app.inject({ method: "POST", url: `/v1/sftpgo/auth?key=${key}`, payload: body as object });
|
|
|
|
it("lets a camera in with the generated password, into its own upload-only folder", async () => {
|
|
const { org } = await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "ADMIN" });
|
|
const { user } = await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "PHOTOGRAPHER" });
|
|
const token = (await login(ctx.app, "[email protected]")).body.accessToken;
|
|
const created = await ctx.app.inject({ method: "POST", url: `/v1/orgs/${org!.id}/ftp-accounts`, headers: bearer(token), payload: { userId: user.id, login: "r1-peter" } });
|
|
const { id, password } = created.json() as { id: string; password: string };
|
|
expect(password).toMatch(/^[a-zA-Z2-9]{14}$/);
|
|
expect(JSON.stringify((await ctx.app.inject({ method: "GET", url: `/v1/orgs/${org!.id}/ftp-accounts`, headers: bearer(token) })).json())).not.toContain("passwordHash");
|
|
|
|
const ok = await hook({ username: "r1-peter", password, protocol: "FTP" });
|
|
expect(ok.statusCode).toBe(200);
|
|
expect(ok.json()).toMatchObject({ status: 1, username: "r1-peter", home_dir: "/srv/photoreport-data/ingest/r1-peter", permissions: { "/": expect.not.arrayContaining(["delete"]) } });
|
|
expect((await hook({ username: "r1-peter", password: "wrong" })).statusCode).toBe(401);
|
|
expect((await hook({ username: "nobody", password })).statusCode).toBe(401);
|
|
expect((await hook({ username: "r1-peter", password }, "bad-key")).statusCode).toBe(401);
|
|
|
|
const reset = (await ctx.app.inject({ method: "POST", url: `/v1/orgs/${org!.id}/ftp-accounts/${id}/password`, headers: bearer(token) })).json() as { password: string };
|
|
expect((await hook({ username: "r1-peter", password })).statusCode).toBe(401);
|
|
expect((await hook({ username: "r1-peter", password: reset.password })).statusCode).toBe(200);
|
|
});
|
|
|
|
it("gives a login a plan's days and quotas, refuses it once ended, and renews it", async () => {
|
|
const { org } = await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "ADMIN" });
|
|
const { user } = await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "PHOTOGRAPHER" });
|
|
const token = (await login(ctx.app, "[email protected]")).body.accessToken;
|
|
const api = (method: "GET" | "POST" | "PATCH" | "DELETE", url: string, payload?: object) => ctx.app.inject({ method, url: `/v1/orgs/${org!.id}${url}`, headers: bearer(token), ...(payload && { payload }) });
|
|
|
|
const plan = (await api("POST", "/ftp-plans", { name: "Jornada", days: 2, quotaMb: 2048, quotaFiles: 500 })).json() as { id: string; accounts: number };
|
|
expect(plan.accounts).toBe(0);
|
|
const created = (await api("POST", "/ftp-accounts", { userId: user.id, login: "r2-ana", planId: plan.id })).json() as { id: string; password: string; expiresAt: string };
|
|
const days = (Date.parse(created.expiresAt) - Date.now()) / 86_400_000;
|
|
expect(days).toBeGreaterThan(1.99);
|
|
expect(days).toBeLessThan(2.01);
|
|
|
|
const ok = (await hook({ username: "r2-ana", password: created.password })).json() as { quota_size: number; quota_files: number; expiration_date: number };
|
|
expect(ok.quota_size).toBe(2048 * 1024 * 1024);
|
|
expect(ok.quota_files).toBe(500);
|
|
expect(ok.expiration_date).toBe(Date.parse(created.expiresAt));
|
|
expect(((await api("GET", "/ftp-plans")).json() as { plans: { accounts: number }[] }).plans[0]!.accounts).toBe(1);
|
|
|
|
// Ended: refused; renewing counts from now.
|
|
await api("PATCH", `/ftp-accounts/${created.id}`, { expiresAt: new Date(Date.now() - 60_000).toISOString() });
|
|
expect((await hook({ username: "r2-ana", password: created.password })).statusCode).toBe(401);
|
|
const renewed = (await api("POST", `/ftp-accounts/${created.id}/renew`)).json() as { expiresAt: string };
|
|
expect(Date.parse(renewed.expiresAt)).toBeGreaterThan(Date.now() + 1.99 * 86_400_000);
|
|
expect((await hook({ username: "r2-ana", password: created.password })).statusCode).toBe(200);
|
|
// Renewing before the end adds to it.
|
|
const again = (await api("POST", `/ftp-accounts/${created.id}/renew`)).json() as { expiresAt: string };
|
|
expect(Date.parse(again.expiresAt) - Date.parse(renewed.expiresAt)).toBe(2 * 86_400_000);
|
|
|
|
// Without a plan: no end, no quotas.
|
|
await api("PATCH", `/ftp-accounts/${created.id}`, { planId: null });
|
|
const free = (await hook({ username: "r2-ana", password: created.password })).json() as { quota_size: number; expiration_date: number };
|
|
expect(free).toMatchObject({ quota_size: 0, expiration_date: 0 });
|
|
expect((await api("POST", `/ftp-accounts/${created.id}/renew`)).statusCode).toBe(400);
|
|
|
|
// Another organization's plan is refused; deleting a plan frees its logins.
|
|
const { org: other } = await seedUser(ctx.db, "[email protected]", { org: "other", role: "ADMIN" });
|
|
const foreign = await ctx.db.ftpPlan.create({ data: { organizationId: other!.id, name: "X", days: 1 } });
|
|
expect((await api("PATCH", `/ftp-accounts/${created.id}`, { planId: foreign.id })).statusCode).toBe(400);
|
|
await api("PATCH", `/ftp-accounts/${created.id}`, { planId: plan.id });
|
|
expect((await api("DELETE", `/ftp-plans/${plan.id}`)).statusCode).toBe(204);
|
|
expect((await ctx.db.ftpAccount.findUniqueOrThrow({ where: { id: created.id } })).planId).toBeNull();
|
|
});
|
|
});
|