feat: Remove setup

This commit is contained in:
Joachim Van Herwegen
2023-07-17 10:00:48 +02:00
parent ea83ea59a1
commit 5eff035cb3
51 changed files with 5 additions and 930 deletions

View File

@@ -1,128 +0,0 @@
import fetch from 'cross-fetch';
import type { App } from '../../src/init/App';
import { joinUrl } from '../../src/util/PathUtil';
import { getPort } from '../util/Util';
import { getDefaultVariables, getTestConfigPath, instantiateFromConfig } from './Config';
const port = getPort('SetupMemory');
const baseUrl = `http://localhost:${port}/`;
// Some tests with real Requests/Responses until the mocking library has been removed from the tests
describe('A Solid server with setup', (): void => {
const email = 'test@test.email';
const password = 'password!';
const podName = 'test';
const setupUrl = joinUrl(baseUrl, '/setup');
let app: App;
// `beforeEach` since the server needs to restart to reset setup
beforeEach(async(): Promise<void> => {
const instances = await instantiateFromConfig(
'urn:solid-server:test:Instances',
getTestConfigPath('setup-memory.json'),
getDefaultVariables(port, baseUrl),
) as Record<string, any>;
({ app } = instances);
await app.start();
});
afterEach(async(): Promise<void> => {
await app.stop();
});
it('catches all requests.', async(): Promise<void> => {
let res = await fetch(baseUrl, { method: 'GET', headers: { accept: 'text/html' }});
expect(res.status).toBe(200);
expect(res.url).toBe(setupUrl);
await expect(res.text()).resolves.toContain('Set up your Solid server');
res = await fetch(joinUrl(baseUrl, '/random/path/'), { method: 'GET', headers: { accept: 'text/html' }});
expect(res.status).toBe(200);
expect(res.url).toBe(setupUrl);
await expect(res.text()).resolves.toContain('Set up your Solid server');
res = await fetch(joinUrl(baseUrl, '/random/path/'), { method: 'PUT' });
expect(res.status).toBe(405);
expect(res.url).toBe(setupUrl);
await expect(res.json()).resolves.toEqual(expect.objectContaining({ name: 'MethodNotAllowedHttpError' }));
});
it('can create a server that disables root but allows registration.', async(): Promise<void> => {
let res = await fetch(setupUrl, { method: 'POST' });
expect(res.status).toBe(200);
await expect(res.json()).resolves.toEqual({ initialize: false, registration: false });
// Root access disabled
res = await fetch(baseUrl);
expect(res.status).toBe(401);
// Registration still possible
const registerParams = { email, podName, password, confirmPassword: password, createWebId: true };
res = await fetch(joinUrl(baseUrl, 'idp/register/'), {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(registerParams),
});
expect(res.status).toBe(200);
res = await fetch(joinUrl(baseUrl, podName, '/profile/card'));
expect(res.status).toBe(200);
await expect(res.text()).resolves.toContain('foaf:PersonalProfileDocument');
});
it('can create a server with a public root.', async(): Promise<void> => {
let res = await fetch(setupUrl, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ initialize: true }),
});
expect(res.status).toBe(200);
await expect(res.json()).resolves.toEqual({ initialize: true, registration: false });
// Root access enabled
res = await fetch(baseUrl);
expect(res.status).toBe(200);
await expect(res.text()).resolves.toContain('<> a <http://www.w3.org/ns/pim/space#Storage>');
// Root pod registration is never allowed
const registerParams = { email, podName, password, confirmPassword: password, createWebId: true, rootPod: true };
res = await fetch(joinUrl(baseUrl, 'idp/register/'), {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(registerParams),
});
expect(res.status).toBe(500);
});
it('can create a server with a root pod.', async(): Promise<void> => {
const registerParams = { email, podName, password, confirmPassword: password, createWebId: true, rootPod: true };
let res = await fetch(setupUrl, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ registration: true, initialize: true, ...registerParams }),
});
expect(res.status).toBe(200);
const json = await res.json();
expect(json).toEqual(expect.objectContaining({
registration: true,
initialize: false,
oidcIssuer: baseUrl,
webId: `${baseUrl}profile/card#me`,
email,
podBaseUrl: baseUrl,
}));
// Root profile created
res = await fetch(joinUrl(baseUrl, '/profile/card'));
expect(res.status).toBe(200);
await expect(res.text()).resolves.toContain('foaf:PersonalProfileDocument');
// Pod root is not accessible even though initialize was set to true
res = await fetch(joinUrl(baseUrl, 'resource'), {
method: 'PUT',
headers: { 'content-type': 'text/plain' },
body: 'random data',
});
expect(res.status).toBe(401);
});
});

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/default.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/simple.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/disabled.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/simple.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/disabled.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/legacy-websockets.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/default.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/simple.json",
"css:config/http/middleware/default.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/default.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/disabled.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/disabled.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/simple.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/simple.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/disabled.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/disabled.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/simple.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -1,47 +0,0 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^6.0.0/components/context.jsonld",
"import": [
"css:config/app/main/default.json",
"css:config/app/init/default.json",
"css:config/app/setup/required.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",
"css:config/http/server-factory/http.json",
"css:config/http/static/default.json",
"css:config/identity/access/public.json",
"css:config/identity/email/default.json",
"css:config/identity/handler/default.json",
"css:config/identity/ownership/token.json",
"css:config/identity/pod/static.json",
"css:config/identity/registration/enabled.json",
"css:config/ldp/authentication/dpop-bearer.json",
"css:config/ldp/authorization/webacl.json",
"css:config/ldp/handler/default.json",
"css:config/ldp/metadata-parser/default.json",
"css:config/ldp/metadata-writer/default.json",
"css:config/ldp/modes/default.json",
"css:config/storage/backend/memory.json",
"css:config/storage/key-value/resource-store.json",
"css:config/storage/middleware/default.json",
"css:config/util/auxiliary/acl.json",
"css:config/util/identifiers/suffix.json",
"css:config/util/index/default.json",
"css:config/util/logging/winston.json",
"css:config/util/representation-conversion/default.json",
"css:config/util/resource-locker/memory.json",
"css:config/util/variables/default.json"
],
"@graph": [
{
"@id": "urn:solid-server:test:Instances",
"@type": "RecordObject",
"record": [
{
"RecordObject:_record_key": "app",
"RecordObject:_record_value": { "@id": "urn:solid-server:default:App" }
}
]
}
]
}

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/webhooks.json",

View File

@@ -3,7 +3,6 @@
"import": [
"css:config/app/main/default.json",
"css:config/app/init/initialize-root.json",
"css:config/app/setup/disabled.json",
"css:config/http/handler/default.json",
"css:config/http/middleware/default.json",
"css:config/http/notifications/websockets.json",

View File

@@ -1,88 +0,0 @@
import type { Operation } from '../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import type { RegistrationResponse,
RegistrationManager } from '../../../../src/identity/interaction/email-password/util/RegistrationManager';
import type { Initializer } from '../../../../src/init/Initializer';
import { SetupHandler } from '../../../../src/init/setup/SetupHandler';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
import { readJsonStream } from '../../../../src/util/StreamUtil';
describe('A SetupHandler', (): void => {
let operation: Operation;
let details: RegistrationResponse;
let registrationManager: jest.Mocked<RegistrationManager>;
let initializer: jest.Mocked<Initializer>;
let handler: SetupHandler;
beforeEach(async(): Promise<void> => {
operation = {
method: 'POST',
target: { path: 'http://example.com/setup' },
preferences: {},
body: new BasicRepresentation(),
};
initializer = {
handleSafe: jest.fn(),
} as any;
details = {
email: 'alice@test.email',
createWebId: true,
register: true,
createPod: true,
};
registrationManager = {
validateInput: jest.fn((input): any => input),
register: jest.fn().mockResolvedValue(details),
} as any;
handler = new SetupHandler({ registrationManager, initializer });
});
it('error if no Initializer is defined and initialization is requested.', async(): Promise<void> => {
handler = new SetupHandler({});
operation.body = new BasicRepresentation(JSON.stringify({ initialize: true }), 'application/json');
await expect(handler.handle({ operation })).rejects.toThrow(NotImplementedHttpError);
});
it('error if no RegistrationManager is defined and registration is requested.', async(): Promise<void> => {
handler = new SetupHandler({});
operation.body = new BasicRepresentation(JSON.stringify({ registration: true }), 'application/json');
await expect(handler.handle({ operation })).rejects.toThrow(NotImplementedHttpError);
});
it('calls the Initializer when requested.', async(): Promise<void> => {
operation.body = new BasicRepresentation(JSON.stringify({ initialize: true }), 'application/json');
const result = await handler.handle({ operation });
await expect(readJsonStream(result.data)).resolves.toEqual({ initialize: true, registration: false });
expect(result.metadata.contentType).toBe('application/json');
expect(initializer.handleSafe).toHaveBeenCalledTimes(1);
expect(registrationManager.validateInput).toHaveBeenCalledTimes(0);
expect(registrationManager.register).toHaveBeenCalledTimes(0);
});
it('calls the RegistrationManager when requested.', async(): Promise<void> => {
const body = { registration: true, email: 'test@example.com' };
operation.body = new BasicRepresentation(JSON.stringify(body), 'application/json');
const result = await handler.handle({ operation });
await expect(readJsonStream(result.data)).resolves.toEqual({ initialize: false, registration: true, ...details });
expect(result.metadata.contentType).toBe('application/json');
expect(initializer.handleSafe).toHaveBeenCalledTimes(0);
expect(registrationManager.validateInput).toHaveBeenCalledTimes(1);
expect(registrationManager.register).toHaveBeenCalledTimes(1);
expect(registrationManager.validateInput).toHaveBeenLastCalledWith(body, true);
expect(registrationManager.register).toHaveBeenLastCalledWith(body, true);
});
it('defaults to an empty JSON body if no data is provided.', async(): Promise<void> => {
operation.body = new BasicRepresentation();
const result = await handler.handle({ operation });
await expect(readJsonStream(result.data)).resolves.toEqual({ initialize: false, registration: false });
expect(result.metadata.contentType).toBe('application/json');
expect(initializer.handleSafe).toHaveBeenCalledTimes(0);
expect(registrationManager.validateInput).toHaveBeenCalledTimes(0);
expect(registrationManager.register).toHaveBeenCalledTimes(0);
});
});

View File

@@ -1,119 +0,0 @@
import type { Operation } from '../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import type { Representation } from '../../../../src/http/representation/Representation';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import type { InteractionHandler } from '../../../../src/identity/interaction/InteractionHandler';
import { SetupHttpHandler } from '../../../../src/init/setup/SetupHttpHandler';
import type { HttpRequest } from '../../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../../src/server/HttpResponse';
import { getBestPreference } from '../../../../src/storage/conversion/ConversionUtil';
import type { RepresentationConverterArgs,
RepresentationConverter } from '../../../../src/storage/conversion/RepresentationConverter';
import type { KeyValueStorage } from '../../../../src/storage/keyvalue/KeyValueStorage';
import { APPLICATION_JSON, APPLICATION_X_WWW_FORM_URLENCODED } from '../../../../src/util/ContentTypes';
import { MethodNotAllowedHttpError } from '../../../../src/util/errors/MethodNotAllowedHttpError';
import { readableToString } from '../../../../src/util/StreamUtil';
import type { TemplateEngine } from '../../../../src/util/templates/TemplateEngine';
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
describe('A SetupHttpHandler', (): void => {
const request: HttpRequest = {} as any;
const response: HttpResponse = {} as any;
let operation: Operation;
const storageKey = 'completed';
let representation: Representation;
let interactionHandler: jest.Mocked<InteractionHandler>;
let templateEngine: jest.Mocked<TemplateEngine>;
let converter: jest.Mocked<RepresentationConverter>;
let storage: jest.Mocked<KeyValueStorage<string, any>>;
let handler: SetupHttpHandler;
beforeEach(async(): Promise<void> => {
operation = {
method: 'GET',
target: { path: 'http://example.com/setup' },
preferences: {},
body: new BasicRepresentation(),
};
templateEngine = {
handleSafe: jest.fn().mockReturnValue(Promise.resolve('<html>')),
} as any;
converter = {
handleSafe: jest.fn((input: RepresentationConverterArgs): Representation => {
// Just find the best match;
const type = getBestPreference(input.preferences.type!, { '*/*': 1 })!;
const metadata = new RepresentationMetadata(input.representation.metadata, { [CONTENT_TYPE]: type.value });
return new BasicRepresentation(input.representation.data, metadata);
}),
} as any;
representation = new BasicRepresentation();
interactionHandler = {
handleSafe: jest.fn().mockResolvedValue(representation),
} as any;
storage = new Map<string, any>() as any;
handler = new SetupHttpHandler({
converter,
storageKey,
storage,
handler: interactionHandler,
templateEngine,
});
});
it('only accepts GET and POST operations.', async(): Promise<void> => {
operation = {
method: 'DELETE',
target: { path: 'http://example.com/setup' },
preferences: {},
body: new BasicRepresentation(),
};
await expect(handler.handle({ operation, request, response })).rejects.toThrow(MethodNotAllowedHttpError);
});
it('calls the template engine for GET requests.', async(): Promise<void> => {
const result = await handler.handle({ operation, request, response });
expect(result.data).toBeDefined();
await expect(readableToString(result.data!)).resolves.toBe('<html>');
expect(result.metadata?.contentType).toBe('text/html');
// Setup is still enabled since this was a GET request
expect(storage.get(storageKey)).toBeUndefined();
});
it('returns the handler result as 200 response.', async(): Promise<void> => {
operation.method = 'POST';
const result = await handler.handle({ operation, request, response });
expect(result.statusCode).toBe(200);
expect(result.data).toBe(representation.data);
expect(result.metadata).toBe(representation.metadata);
expect(interactionHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(interactionHandler.handleSafe).toHaveBeenLastCalledWith({ operation });
// Handler is now disabled due to successful POST
expect(storage.get(storageKey)).toBe(true);
});
it('converts input bodies to JSON.', async(): Promise<void> => {
operation.method = 'POST';
operation.body.metadata.contentType = APPLICATION_X_WWW_FORM_URLENCODED;
const result = await handler.handle({ operation, request, response });
expect(result.statusCode).toBe(200);
expect(result.data).toBe(representation.data);
expect(result.metadata).toBe(representation.metadata);
expect(interactionHandler.handleSafe).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { body, ...partialOperation } = operation;
expect(interactionHandler.handleSafe).toHaveBeenLastCalledWith(
{ operation: expect.objectContaining(partialOperation) },
);
expect(interactionHandler.handleSafe.mock.calls[0][0].operation.body.metadata.contentType).toBe(APPLICATION_JSON);
// Handler is now disabled due to successful POST
expect(storage.get(storageKey)).toBe(true);
});
});