Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"eslint/eqeqeq": ["off", "smart"],
"eslint/func-style": ["off"],
"eslint/max-lines-per-function": "off",
"eslint/max-lines": "off",
"eslint/id-length": ["warn", { "exceptionPatterns": ["^_", "^[Tertv]$"] }],
"eslint/init-declarations": "off",
"eslint/max-params": ["warn", { "max": 4 }],
Expand Down Expand Up @@ -68,6 +69,7 @@
"import/prefer-default-export": "off",
"jsdoc/require-param-type": "off",
"jsdoc/require-returns-type": "off",
"node/no-top-level-await": "off",
"promise/avoid-new": "warn"
},
"overrides": [
Expand Down
5 changes: 2 additions & 3 deletions packages/drfed/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async function runServer(options: ServerOptions) {
if (options.seed) {
await seedData(options.drizzle.db);
}
const { mailer } = options;
const yogaServer = createYogaServer(options.drizzle.db, { mailer });
const { mailer, root } = options;
const yogaServer = createYogaServer(options.drizzle.db, { root, mailer });
const server = serve({
fetch: yogaServer.fetch.bind(yogaServer),
hostname: options.address.host,
Expand Down Expand Up @@ -76,7 +76,6 @@ async function runSchemaGenerator(
await writeFile(options.outputFile, schemaCode, { encoding: "utf-8" });
}

// oxlint-disable-next-line max-lines-per-function
export async function main(): Promise<void> {
const options: Options = run(program, {
help: "option",
Expand Down
9 changes: 8 additions & 1 deletion packages/drfed/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { message, optionNames } from "@optique/core/message";
import { map, optional, withDefault } from "@optique/core/modifiers";
import type { InferValue } from "@optique/core/parser";
import { flag, option } from "@optique/core/primitives";
import { socketAddress, url } from "@optique/core/valueparser";
import { domain, socketAddress, url } from "@optique/core/valueparser";
import { loggingOptions } from "@optique/logtape";
import { path } from "@optique/run/valueparser";
import { LogTapeTransport } from "@upyo/logtape";
Expand Down Expand Up @@ -105,6 +105,12 @@ const seedParser = option("--dev-seed", {
hidden: true,
});

const rootParser = optional(
option("--root-domain", "-r", domain({ lowercase: true }), {
description: message`The root domain of host.`,
}),
);

const serverParser = object("DrFed server", {
address: withDefault(
option("--listen", "-l", socketAddress({ requirePort: true }), {
Expand All @@ -126,6 +132,7 @@ const serverParser = object("DrFed server", {
),
}),
),
root: rootParser,
mailer: smtpParser,
seed: seedParser,
});
Expand Down
48 changes: 32 additions & 16 deletions packages/graphql/src/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const accountInstancesQuery = `
admin
node {
uuid
slug
location
host
}
}
}
Expand Down Expand Up @@ -82,7 +83,8 @@ const accountInstancesResponse = {
admin: true,
node: {
uuid: acceptedInstanceId,
slug: "test-instance",
location: "Local",
host: "test-instance.drfed.org",
},
},
],
Expand Down Expand Up @@ -148,20 +150,7 @@ async function seedAccounts(db: Database): Promise<void> {

async function seedMembershipGraph(db: Database): Promise<void> {
await seedAccounts(db);
await db.insert(schema.instances).values([
{
id: acceptedInstanceId,
slug: "test-instance",
created,
expires,
},
{
id: pendingInstanceId,
slug: "pending-instance",
created,
expires,
},
]);
await seedLocalInstances(db);
await db.insert(schema.instanceMembers).values([
{
accountId,
Expand Down Expand Up @@ -193,3 +182,30 @@ async function seedMembershipGraph(db: Database): Promise<void> {
},
]);
}

async function seedLocalInstances(db: Database): Promise<void> {
await db.insert(schema.instances).values([
{
id: acceptedInstanceId,
location: "Local",
created,
},
{
id: pendingInstanceId,
location: "Local",
created,
},
]);
await db.insert(schema.localInstances).values([
{
id: acceptedInstanceId,
slug: "test-instance",
expires,
},
{
id: pendingInstanceId,
slug: "pending-instance",
expires,
},
]);
}
1 change: 0 additions & 1 deletion packages/graphql/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const accountInstancesConnection = drizzleConnectionHelpers(
},
);

// oxlint-disable-next-line max-lines-per-function
builder.drizzleObjectField(AccountRef, "instances", (t) =>
t.connection(
{
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// oxlint-disable max-lines-per-function max-statements no-magic-numbers
// oxlint-disable max-statements no-magic-numbers

import { deepEqual, equal, ok } from "node:assert/strict";

Expand Down
5 changes: 5 additions & 0 deletions packages/graphql/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface ServerContext {
* Origin list.
*/
readonly origins: ReadonlySet<string>;

/**
* Root domain.
*/
readonly root: string;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export interface YogaServerOptions {
* Origin list.
*/
origins?: ReadonlySet<string>;

/**
* Root domain.
*/
root?: string | undefined;
}

/**
Expand Down Expand Up @@ -104,6 +109,7 @@ const fillOptions = (opt: YogaServerOptions): Required<YogaServerOptions> => ({
"http://localhost:5173",
"http://0.0.0.0:5173",
]),
root: opt?.root ?? "drfed.org",
});

const getAccessToken = (headers: Headers) =>
Expand Down
119 changes: 113 additions & 6 deletions packages/graphql/src/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// oxlint-disable max-lines max-lines-per-function no-underscore-dangle
// oxlint-disable no-underscore-dangle
import assert from "node:assert/strict";

import { type Database, schema } from "@drfed/models";
import { describe, it } from "@logtape/testing-node/autoload";
import { DrizzleQueryError } from "drizzle-orm";

import { withTestHarness } from "./harness.test.ts";

Expand All @@ -31,9 +32,26 @@ const accountId = "00000000-0000-4000-8000-000000000001";
const memberId = "00000000-0000-4000-8000-000000000002";
const pendingMemberId = "00000000-0000-4000-8000-000000000003";
const instanceId = "00000000-0000-4000-8000-000000000101";
const duplicateRemoteInstanceId = "00000000-0000-4000-8000-000000000102";
const sessionId = "00000000-0000-4000-8000-000000000201";
const accessToken = "test-access-token";

const remoteInstanceQuery = `
query RemoteInstance($uuid: UUID!) {
accountByUuid(uuid: $uuid) {
instances {
edges {
node {
uuid
location
host
}
}
}
}
}
`;

const instanceMembersQuery = `
query InstanceMembers($uuid: UUID!) {
accountByUuid(uuid: $uuid) {
Expand Down Expand Up @@ -122,7 +140,8 @@ const createInstanceMutation = `
__typename
... on Instance {
uuid
slug
location
host
}
... on CreateInstanceError {
type
Expand All @@ -146,12 +165,18 @@ describe("Mutation.createInstance", () => {
const body = await response.json();
assert.equal(body.errors, undefined);
assert.equal(body.data.createInstance.__typename, "Instance");
assert.equal(body.data.createInstance.slug, "my-instance");
assert.equal(body.data.createInstance.location, "Local");
assert.equal(body.data.createInstance.host, "my-instance.drfed.org");
assert.equal(typeof body.data.createInstance.uuid, "string");

const instances = await db.select().from(schema.instances);
assert.equal(instances.length, 1);
assert.equal(instances[0]?.slug, "my-instance");
const instance = instances[0]!;
assert.equal(instance.location, "Local");
const local = await db.query.localInstances.findFirst({
where: { id: instance.id },
});
assert.equal(local?.slug, "my-instance");

const members = await db.select().from(schema.instanceMembers);
assert.equal(members.length, 1);
Expand Down Expand Up @@ -226,6 +251,61 @@ describe("Mutation.createInstance", () => {
});
});

describe("Remote instance", () => {
it("returns a created remote instance", async () => {
await withTestHarness(async ({ db, post }) => {
await seedRemoteInstance(db);

const response = await post({
query: remoteInstanceQuery,
variables: { uuid: accountId },
});

assert.equal(response.status, ok);
assert.deepEqual(await response.json(), {
data: {
accountByUuid: {
instances: {
edges: [
{
node: {
uuid: instanceId,
location: "Remote",
host: "remote.example.com",
},
},
],
},
},
},
});
});
});

it("requires a unique host", async () => {
await withTestHarness(async ({ db }) => {
await seedRemoteInstance(db);
await db.insert(schema.instances).values({
id: duplicateRemoteInstanceId,
location: "Remote",
created,
});

await assert.rejects(
db.insert(schema.remoteInstances).values({
id: duplicateRemoteInstanceId,
host: "remote.example.com",
}),
(error: unknown) =>
error instanceof DrizzleQueryError &&
error.cause != null &&
"constraint" in error.cause &&
error.cause.constraint === "remote_instances_host_key",
);
});
});
});

/**
* Seeds an account and an authenticated session, then returns the request
* options carrying the session's bearer token.
Expand Down Expand Up @@ -267,7 +347,6 @@ async function hashSecret(raw: string): Promise<string> {
).toHex();
}

// oxlint-disable-next-line max-lines-per-function
async function seedInstanceMembers(db: Database): Promise<void> {
await db.insert(schema.accounts).values([
{
Expand All @@ -294,8 +373,12 @@ async function seedInstanceMembers(db: Database): Promise<void> {
]);
await db.insert(schema.instances).values({
id: instanceId,
slug: "test-instance",
location: "Local",
created,
});
await db.insert(schema.localInstances).values({
id: instanceId,
slug: "test-instance",
expires,
});
await db.insert(schema.instanceMembers).values([
Expand All @@ -322,3 +405,27 @@ async function seedInstanceMembers(db: Database): Promise<void> {
},
]);
}

async function seedRemoteInstance(db: Database): Promise<void> {
await db.insert(schema.accounts).values({
id: accountId,
email: "owner@example.com",
name: "Owner",
created,
});
await db.insert(schema.instances).values({
id: instanceId,
location: "Remote",
created,
});
await db.insert(schema.remoteInstances).values({
id: instanceId,
host: "remote.example.com",
});
await db.insert(schema.instanceMembers).values({
accountId,
instanceId,
accepted,
created,
});
}
Loading