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
15 changes: 14 additions & 1 deletion packages/realm-server/setup-localhost-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@

if (process.env.BOXEL_ENVIRONMENT) {
try {
type UndiciModule = {
Agent: new (opts: {
connect: {
lookup: (
hostname: string,
options: any,
cb: (...args: any[]) => void,
) => void;
};
}) => unknown;
setGlobalDispatcher: (dispatcher: unknown) => void;
};

// eslint-disable-next-line @typescript-eslint/no-var-requires
const undici = require('undici') as typeof import('undici');
const undici = require('undici') as UndiciModule;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const dns = require('dns');

Expand Down
1 change: 1 addition & 0 deletions packages/realm-server/tests/card-endpoints-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ module(basename(__filename), function () {
assert,
getMessagesSince,
realm: testRealmHref,
timeout: 5000,
},
);
let id = incrementalEventContent.invalidations[0].split('/').pop()!;
Expand Down
11 changes: 8 additions & 3 deletions packages/realm-server/tests/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
copySync,
} from 'fs-extra';
import { NodeAdapter } from '../../node-realm';
import { join } from 'path';
import { dirname, join } from 'path';
import { createHash } from 'crypto';
import type {
LooseSingleCardDocument,
Expand Down Expand Up @@ -717,10 +717,12 @@ export async function createRealm({
}

for (let [filename, contents] of Object.entries(fileSystem)) {
let path = join(dir, filename);
ensureDirSync(dirname(path));
if (typeof contents === 'string') {
writeFileSync(join(dir, filename), contents);
writeFileSync(path, contents);
} else {
writeJSONSync(join(dir, filename), contents);
writeJSONSync(path, contents);
}
}

Expand Down Expand Up @@ -891,6 +893,7 @@ export async function runTestRealmServer({
testRealmHttpServer,
testRealmAdapter,
matrixClient,
virtualNetwork,
};
}

Expand Down Expand Up @@ -1596,6 +1599,7 @@ export function setupPermissionedRealm(
testRealmAdapter: RealmAdapter;
request: SuperTest<Test>;
dir: DirResult;
virtualNetwork: VirtualNetwork;
}) => void;
subscribeToRealmEvents?: boolean;
mode?: 'beforeEach' | 'before';
Expand Down Expand Up @@ -1643,6 +1647,7 @@ export function setupPermissionedRealm(
testRealmPath: testRealmServer.testRealmDir,
testRealmHttpServer: testRealmServer.testRealmHttpServer,
testRealmAdapter: testRealmServer.testRealmAdapter,
virtualNetwork: testRealmServer.virtualNetwork,
request,
dir,
});
Expand Down
29 changes: 17 additions & 12 deletions packages/realm-server/tests/helpers/indexing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@ interface IncrementalIndexEventTestContext {
getMessagesSince: (since: number) => Promise<MatrixEvent[]>;
realm: string;
type?: string;
timeout?: number;
}

export async function waitForIncrementalIndexEvent(
getMessagesSince: (since: number) => Promise<MatrixEvent[]>,
since: number,
timeout = 5000,
) {
await waitUntil(async () => {
let matrixMessages = await getMessagesSince(since);

return matrixMessages.some(
(m) =>
m.type === APP_BOXEL_REALM_EVENT_TYPE &&
m.content.eventName === 'index' &&
m.content.indexType === 'incremental',
);
});
await waitUntil(
async () => {
let matrixMessages = await getMessagesSince(since);

return matrixMessages.some(
(m) =>
m.type === APP_BOXEL_REALM_EVENT_TYPE &&
m.content.eventName === 'index' &&
m.content.indexType === 'incremental',
);
},
{ timeout },
);
}

export async function expectIncrementalIndexEvent(
url: string, // <>.gts OR <>.json OR <>.* OR <>/
since: number,
opts: IncrementalIndexEventTestContext,
) {
let { assert, getMessagesSince, realm, type } = opts;
let { assert, getMessagesSince, realm, type, timeout } = opts;

type = type ?? 'CardDef';

Expand All @@ -48,7 +53,7 @@ export async function expectIncrementalIndexEvent(
if (!hasExtension && !endsWithSlash) {
throw new Error('Invalid file path');
}
await waitForIncrementalIndexEvent(getMessagesSince, since);
await waitForIncrementalIndexEvent(getMessagesSince, since, timeout);

let messages = await getMessagesSince(since);
let incrementalIndexInitiationEventContent = findRealmEvent(
Expand Down
55 changes: 16 additions & 39 deletions packages/realm-server/tests/server-endpoints/authentication-test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { module, test } from 'qunit';
import { basename, join } from 'path';
import { basename } from 'path';
import type { Test, SuperTest } from 'supertest';
import supertest from 'supertest';
import { dirSync, type DirResult } from 'tmp';
import { copySync, ensureDirSync } from 'fs-extra';
import type { Server } from 'http';
import jwt from 'jsonwebtoken';
import { MatrixClient } from '@cardstack/runtime-common/matrix-client';
import type { RealmServerTokenClaim } from '../../utils/jwt';
import {
closeServer,
createVirtualNetwork,
matrixURL,
realmSecretSeed,
realmServerTestMatrix,
runTestRealmServer,
setupDB,
setupPermissionedRealmCached,
testRealmURL,
} from '../helpers';
import { createRealmServerSession } from './helpers';
Expand All @@ -25,41 +18,25 @@ import '@cardstack/runtime-common/helpers/code-equality-assertion';

module(`server-endpoints/${basename(__filename)}`, function () {
module('Realm server authentication', function (hooks) {
let testRealmServer: Server;
let request: SuperTest<Test>;
let dir: DirResult;
let dbAdapter: PgAdapter;

hooks.beforeEach(async function () {
dir = dirSync();
});
function onRealmSetup(args: {
testRealmHttpServer: Server;
request: SuperTest<Test>;
dbAdapter: PgAdapter;
}) {
dbAdapter = args.dbAdapter;
request = args.request;
}

setupDB(hooks, {
beforeEach: async (_dbAdapter, publisher, runner) => {
dbAdapter = _dbAdapter;
let testRealmDir = join(dir.name, 'realm_server_5', 'test');
ensureDirSync(testRealmDir);
copySync(join(__dirname, '..', 'cards'), testRealmDir);
testRealmServer = (
await runTestRealmServer({
virtualNetwork: createVirtualNetwork(),
testRealmDir,
realmsRootPath: join(dir.name, 'realm_server_5'),
realmURL: testRealmURL,
permissions: {
'@test_realm:localhost': ['read', 'realm-owner'],
},
dbAdapter,
publisher,
runner,
matrixURL,
})
).testRealmHttpServer;
request = supertest(testRealmServer);
},
afterEach: async () => {
await closeServer(testRealmServer);
setupPermissionedRealmCached(hooks, {
fileSystem: {},
permissions: {
'@test_realm:localhost': ['read', 'realm-owner'],
},
realmURL: testRealmURL,
onRealmSetup: onRealmSetup,
});

test('authenticates user and creates session room', async function (assert) {
Expand Down
26 changes: 13 additions & 13 deletions packages/realm-server/tests/server-endpoints/bot-commands-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module(`server-endpoints/${basename(__filename)}`, function () {
let context = setupServerEndpointsTest(hooks);

test('requires auth to add bot command', async function (assert) {
let response = await context.request2.post('/_bot-commands').send({});
let response = await context.request.post('/_bot-commands').send({});
assert.strictEqual(response.status, 401, 'HTTP 401 status');
});

test('requires auth to list bot commands', async function (assert) {
let response = await context.request2.get('/_bot-commands');
let response = await context.request.get('/_bot-commands');
assert.strictEqual(response.status, 401, 'HTTP 401 status');
});

Expand All @@ -39,7 +39,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -139,7 +139,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
]);

let commandSpecifier = '@cardstack/boxel-host/commands/show-card/default';
let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -245,7 +245,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let response = await context.request2
let response = await context.request
.get('/_bot-commands')
.set(
'Authorization',
Expand Down Expand Up @@ -291,7 +291,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -329,7 +329,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
'user@example.com',
);

let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -367,7 +367,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
'user@example.com',
);

let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -416,7 +416,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -460,7 +460,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let baseRequest = context.request2
let baseRequest = context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -527,7 +527,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let response = await context.request2
let response = await context.request
.post('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -594,7 +594,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let deleteResponse = await context.request2
let deleteResponse = await context.request
.delete('/_bot-registration')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down Expand Up @@ -663,7 +663,7 @@ module(`server-endpoints/${basename(__filename)}`, function () {
`)`,
]);

let response = await context.request2
let response = await context.request
.delete('/_bot-commands')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/vnd.api+json')
Expand Down
Loading