mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
test: Remove node-mocks-http references from integration tests
This commit is contained in:
parent
d04ab2951b
commit
6cd2806c2f
@ -1 +0,0 @@
|
|||||||
TEST
|
|
@ -1 +0,0 @@
|
|||||||
TESTFILE0
|
|
@ -1 +0,0 @@
|
|||||||
TESTFILE1
|
|
@ -1 +0,0 @@
|
|||||||
TESTFILE2
|
|
@ -3,8 +3,8 @@ import fetch from 'cross-fetch';
|
|||||||
import type { Initializer, ResourceStore } from '../../src/';
|
import type { Initializer, ResourceStore } from '../../src/';
|
||||||
import { BasicRepresentation } from '../../src/';
|
import { BasicRepresentation } from '../../src/';
|
||||||
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
||||||
|
import { AclHelper } from '../util/AclHelper';
|
||||||
import { deleteResource, getResource, postResource, putResource } from '../util/FetchUtil';
|
import { deleteResource, getResource, postResource, putResource } from '../util/FetchUtil';
|
||||||
import { AclHelper } from '../util/TestHelpers';
|
|
||||||
import { getPort } from '../util/Util';
|
import { getPort } from '../util/Util';
|
||||||
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
|
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
|
||||||
|
|
||||||
|
@ -3,8 +3,7 @@ import fetch from 'cross-fetch';
|
|||||||
import type { RedisResourceLocker } from '../../src';
|
import type { RedisResourceLocker } from '../../src';
|
||||||
import { joinFilePath } from '../../src';
|
import { joinFilePath } from '../../src';
|
||||||
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
||||||
import { describeIf } from '../util/TestHelpers';
|
import { describeIf, getPort } from '../util/Util';
|
||||||
import { getPort } from '../util/Util';
|
|
||||||
import { instantiateFromConfig } from './Config';
|
import { instantiateFromConfig } from './Config';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,15 +1,24 @@
|
|||||||
import type { HttpHandler, Initializer, ResourceStore } from '../../src/';
|
import { promises as fs } from 'fs';
|
||||||
import { describeIf, ResourceHelper } from '../util/TestHelpers';
|
import type { Server } from 'http';
|
||||||
import { BASE, instantiateFromConfig } from './Config';
|
import { joinFilePath } from '../../src/';
|
||||||
|
import type { Initializer, ResourceStore } from '../../src/';
|
||||||
|
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
||||||
|
import { putResource } from '../util/FetchUtil';
|
||||||
|
import { describeIf, getPort } from '../util/Util';
|
||||||
|
import { instantiateFromConfig } from './Config';
|
||||||
|
|
||||||
|
const port = getPort('SparqlStorage');
|
||||||
|
const baseUrl = `http://localhost:${port}/`;
|
||||||
|
|
||||||
describeIf('docker', 'A server with a SPARQL endpoint as storage', (): void => {
|
describeIf('docker', 'A server with a SPARQL endpoint as storage', (): void => {
|
||||||
let handler: HttpHandler;
|
let server: Server;
|
||||||
let resourceHelper: ResourceHelper;
|
let initializer: Initializer;
|
||||||
|
let factory: HttpServerFactory;
|
||||||
|
|
||||||
beforeAll(async(): Promise<void> => {
|
beforeAll(async(): Promise<void> => {
|
||||||
// Set up the internal store
|
|
||||||
const variables: Record<string, any> = {
|
const variables: Record<string, any> = {
|
||||||
'urn:solid-server:default:variable:baseUrl': BASE,
|
'urn:solid-server:default:variable:port': port,
|
||||||
|
'urn:solid-server:default:variable:baseUrl': baseUrl,
|
||||||
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:4000/sparql',
|
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:4000/sparql',
|
||||||
};
|
};
|
||||||
const internalStore = await instantiateFromConfig(
|
const internalStore = await instantiateFromConfig(
|
||||||
@ -19,24 +28,29 @@ describeIf('docker', 'A server with a SPARQL endpoint as storage', (): void => {
|
|||||||
) as ResourceStore;
|
) as ResourceStore;
|
||||||
variables['urn:solid-server:default:variable:store'] = internalStore;
|
variables['urn:solid-server:default:variable:store'] = internalStore;
|
||||||
|
|
||||||
// Create and initialize the HTTP handler and related components
|
// Create and initialize the server
|
||||||
let initializer: Initializer;
|
|
||||||
const instances = await instantiateFromConfig(
|
const instances = await instantiateFromConfig(
|
||||||
'urn:solid-server:test:Instances',
|
'urn:solid-server:test:Instances',
|
||||||
'ldp-with-auth.json',
|
'ldp-with-auth.json',
|
||||||
variables,
|
variables,
|
||||||
) as Record<string, any>;
|
) as Record<string, any>;
|
||||||
({ handler, initializer } = instances);
|
({ factory, initializer } = instances);
|
||||||
await initializer.handleSafe();
|
|
||||||
|
|
||||||
// Create test helpers for manipulating the components
|
await initializer.handleSafe();
|
||||||
resourceHelper = new ResourceHelper(handler, BASE);
|
server = factory.startServer(port);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can add a Turtle file to the store.', async():
|
afterAll(async(): Promise<void> => {
|
||||||
Promise<void> => {
|
await new Promise((resolve, reject): void => {
|
||||||
|
server.close((error): void => error ? reject(error) : resolve());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can add a Turtle file to the store.', async(): Promise<void> => {
|
||||||
// PUT
|
// PUT
|
||||||
const response = await resourceHelper.createResource('../assets/person.ttl', 'person', 'text/turtle');
|
const documentUrl = `${baseUrl}person`;
|
||||||
|
const body = (await fs.readFile(joinFilePath(__dirname, '../assets/person.ttl'))).toString('utf-8');
|
||||||
|
const response = await putResource(documentUrl, { contentType: 'text/turtle', body });
|
||||||
expect(response).toBeTruthy();
|
expect(response).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
60
test/util/AclHelper.ts
Normal file
60
test/util/AclHelper.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import type { ResourceStore, PermissionSet } from '../../src/';
|
||||||
|
import { BasicRepresentation } from '../../src/';
|
||||||
|
|
||||||
|
export class AclHelper {
|
||||||
|
public readonly store: ResourceStore;
|
||||||
|
|
||||||
|
public constructor(store: ResourceStore) {
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setSimpleAcl(
|
||||||
|
resource: string,
|
||||||
|
options: {
|
||||||
|
permissions: Partial<PermissionSet>;
|
||||||
|
agentClass?: 'agent' | 'authenticated';
|
||||||
|
agent?: string;
|
||||||
|
accessTo?: boolean;
|
||||||
|
default?: boolean;
|
||||||
|
},
|
||||||
|
): Promise<void> {
|
||||||
|
if (!options.agentClass && !options.agent) {
|
||||||
|
throw new Error('At least one of agentClass or agent have to be provided.');
|
||||||
|
}
|
||||||
|
if (!options.accessTo && !options.default) {
|
||||||
|
throw new Error('At least one of accessTo or default have to be true.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const acl: string[] = [
|
||||||
|
'@prefix acl: <http://www.w3.org/ns/auth/acl#>.\n',
|
||||||
|
'@prefix foaf: <http://xmlns.com/foaf/0.1/>.\n',
|
||||||
|
'<http://test.com/#auth> a acl:Authorization',
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const perm of [ 'Read', 'Append', 'Write', 'Control' ]) {
|
||||||
|
if (options.permissions[perm.toLowerCase() as keyof PermissionSet]) {
|
||||||
|
acl.push(`;\n acl:mode acl:${perm}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (options.accessTo) {
|
||||||
|
acl.push(`;\n acl:accessTo <${resource}>`);
|
||||||
|
}
|
||||||
|
if (options.default) {
|
||||||
|
acl.push(`;\n acl:default <${resource}>`);
|
||||||
|
}
|
||||||
|
if (options.agentClass) {
|
||||||
|
acl.push(
|
||||||
|
`;\n acl:agentClass ${
|
||||||
|
options.agentClass === 'agent' ? 'foaf:Agent' : 'foaf:AuthenticatedAgent'
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (options.agent) {
|
||||||
|
acl.push(`;\n acl:agent ${options.agent}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
acl.push('.');
|
||||||
|
|
||||||
|
await this.store.setRepresentation({ path: `${resource}.acl` }, new BasicRepresentation(acl, 'text/turtle'));
|
||||||
|
}
|
||||||
|
}
|
@ -1,210 +0,0 @@
|
|||||||
import { EventEmitter } from 'events';
|
|
||||||
import { promises as fs } from 'fs';
|
|
||||||
import type { IncomingHttpHeaders } from 'http';
|
|
||||||
import { Readable } from 'stream';
|
|
||||||
import type { MockResponse } from 'node-mocks-http';
|
|
||||||
import { createResponse } from 'node-mocks-http';
|
|
||||||
import type { ResourceStore, PermissionSet, HttpHandler, HttpRequest } from '../../src/';
|
|
||||||
import { BasicRepresentation, joinFilePath } from '../../src/';
|
|
||||||
import { performRequest } from './Util';
|
|
||||||
|
|
||||||
/* eslint-disable jest/no-standalone-expect */
|
|
||||||
export class AclHelper {
|
|
||||||
public readonly store: ResourceStore;
|
|
||||||
|
|
||||||
public constructor(store: ResourceStore) {
|
|
||||||
this.store = store;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async setSimpleAcl(
|
|
||||||
resource: string,
|
|
||||||
options: {
|
|
||||||
permissions: Partial<PermissionSet>;
|
|
||||||
agentClass?: 'agent' | 'authenticated';
|
|
||||||
agent?: string;
|
|
||||||
accessTo?: boolean;
|
|
||||||
default?: boolean;
|
|
||||||
},
|
|
||||||
): Promise<void> {
|
|
||||||
if (!options.agentClass && !options.agent) {
|
|
||||||
throw new Error('At least one of agentClass or agent have to be provided for this to make sense.');
|
|
||||||
}
|
|
||||||
if (!options.accessTo && !options.default) {
|
|
||||||
throw new Error('At least one of accessTo or default have to be true for this to make sense.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const acl: string[] = [
|
|
||||||
'@prefix acl: <http://www.w3.org/ns/auth/acl#>.\n',
|
|
||||||
'@prefix foaf: <http://xmlns.com/foaf/0.1/>.\n',
|
|
||||||
'<http://test.com/#auth> a acl:Authorization',
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const perm of [ 'Read', 'Append', 'Write', 'Control' ]) {
|
|
||||||
if (options.permissions[perm.toLowerCase() as keyof PermissionSet]) {
|
|
||||||
acl.push(`;\n acl:mode acl:${perm}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (options.accessTo) {
|
|
||||||
acl.push(`;\n acl:accessTo <${resource}>`);
|
|
||||||
}
|
|
||||||
if (options.default) {
|
|
||||||
acl.push(`;\n acl:default <${resource}>`);
|
|
||||||
}
|
|
||||||
if (options.agentClass) {
|
|
||||||
acl.push(
|
|
||||||
`;\n acl:agentClass ${
|
|
||||||
options.agentClass === 'agent' ? 'foaf:Agent' : 'foaf:AuthenticatedAgent'
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (options.agent) {
|
|
||||||
acl.push(`;\n acl:agent ${options.agent}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
acl.push('.');
|
|
||||||
|
|
||||||
await this.store.setRepresentation({ path: `${resource}.acl` }, new BasicRepresentation(acl, 'text/turtle'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ResourceHelper {
|
|
||||||
public readonly handler: HttpHandler;
|
|
||||||
public readonly baseUrl: URL;
|
|
||||||
|
|
||||||
public constructor(handler: HttpHandler, baseUrl: string) {
|
|
||||||
this.handler = handler;
|
|
||||||
this.baseUrl = new URL(baseUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async performRequest(
|
|
||||||
requestUrl: URL,
|
|
||||||
method: string,
|
|
||||||
headers: IncomingHttpHeaders,
|
|
||||||
): Promise<MockResponse<any>> {
|
|
||||||
return performRequest(this.handler, requestUrl, method, headers, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async performRequestWithBody(
|
|
||||||
requestUrl: URL,
|
|
||||||
method: string,
|
|
||||||
headers: IncomingHttpHeaders,
|
|
||||||
data: Buffer,
|
|
||||||
): Promise<MockResponse<any>> {
|
|
||||||
const request = Readable.from([ data ]) as HttpRequest;
|
|
||||||
request.url = `${requestUrl.pathname}${requestUrl.search}`;
|
|
||||||
request.method = method;
|
|
||||||
request.headers = headers;
|
|
||||||
request.headers.host = requestUrl.host;
|
|
||||||
const response: MockResponse<any> = createResponse({
|
|
||||||
eventEmitter: EventEmitter,
|
|
||||||
});
|
|
||||||
|
|
||||||
const endPromise = new Promise((resolve): void => {
|
|
||||||
response.on('end', (): void => {
|
|
||||||
expect(response._isEndCalled()).toBeTruthy();
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.handler.handleSafe({ request, response });
|
|
||||||
await endPromise;
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createResource(fileLocation: string, path: string, contentType: string, mayFail = false):
|
|
||||||
Promise<MockResponse<any>> {
|
|
||||||
const fileData = await fs.readFile(
|
|
||||||
joinFilePath(__dirname, fileLocation),
|
|
||||||
);
|
|
||||||
|
|
||||||
const response: MockResponse<any> = await this.performRequestWithBody(
|
|
||||||
new URL(path, this.baseUrl),
|
|
||||||
'PUT',
|
|
||||||
{ 'content-type': contentType,
|
|
||||||
'transfer-encoding': 'chunked' },
|
|
||||||
fileData,
|
|
||||||
);
|
|
||||||
if (!mayFail) {
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async replaceResource(fileLocation: string, requestUrl: string, contentType: string):
|
|
||||||
Promise<MockResponse<any>> {
|
|
||||||
const fileData = await fs.readFile(
|
|
||||||
joinFilePath(__dirname, fileLocation),
|
|
||||||
);
|
|
||||||
|
|
||||||
const putUrl = new URL(requestUrl);
|
|
||||||
|
|
||||||
const response: MockResponse<any> = await this.performRequestWithBody(
|
|
||||||
putUrl,
|
|
||||||
'PUT',
|
|
||||||
{ 'content-type': contentType, 'transfer-encoding': 'chunked' },
|
|
||||||
fileData,
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getResource(requestUrl: string): Promise<MockResponse<any>> {
|
|
||||||
const getUrl = new URL(requestUrl);
|
|
||||||
|
|
||||||
const response = await this.performRequest(getUrl, 'GET', { accept: '*/*' });
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async deleteResource(requestUrl: string, mayFail = false): Promise<MockResponse<any>> {
|
|
||||||
const deleteUrl = new URL(requestUrl);
|
|
||||||
|
|
||||||
const response = await this.performRequest(deleteUrl, 'DELETE', {});
|
|
||||||
if (!mayFail) {
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createContainer(path: string): Promise<MockResponse<any>> {
|
|
||||||
const response: MockResponse<any> = await this.performRequest(
|
|
||||||
new URL(path, this.baseUrl),
|
|
||||||
'PUT',
|
|
||||||
{
|
|
||||||
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',
|
|
||||||
'content-type': 'text/turtle',
|
|
||||||
'transfer-encoding': 'chunked',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getContainer(requestUrl: string): Promise<MockResponse<any>> {
|
|
||||||
const getUrl = new URL(requestUrl);
|
|
||||||
|
|
||||||
// `n-quads` allow for easy testing if a triple is present
|
|
||||||
return await this.performRequest(getUrl, 'GET', { accept: 'application/n-quads' });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async shouldNotExist(requestUrl: string): Promise<MockResponse<any>> {
|
|
||||||
const getUrl = new URL(requestUrl);
|
|
||||||
|
|
||||||
const response = await this.performRequest(getUrl, 'GET', { accept: '*/*' });
|
|
||||||
expect(response.statusCode).toBe(404);
|
|
||||||
expect(response._getData()).toContain('NotFoundHttpError');
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function describeIf(envFlag: string, name: string, fn: () => void): void {
|
|
||||||
const flag = `TEST_${envFlag.toUpperCase()}`;
|
|
||||||
const enabled = !/^(|0|false)$/iu.test(process.env[flag] ?? '');
|
|
||||||
// eslint-disable-next-line jest/valid-describe, jest/valid-title, jest/no-disabled-tests
|
|
||||||
return enabled ? describe(name, fn) : describe.skip(name, fn);
|
|
||||||
}
|
|
@ -1,12 +1,6 @@
|
|||||||
import { EventEmitter } from 'events';
|
|
||||||
import type { Stats } from 'fs';
|
import type { Stats } from 'fs';
|
||||||
import type { IncomingHttpHeaders } from 'http';
|
|
||||||
import { PassThrough } from 'stream';
|
import { PassThrough } from 'stream';
|
||||||
import type { MockResponse } from 'node-mocks-http';
|
|
||||||
import { createResponse } from 'node-mocks-http';
|
|
||||||
import streamifyArray from 'streamify-array';
|
import streamifyArray from 'streamify-array';
|
||||||
import type { HttpHandler } from '../../src/server/HttpHandler';
|
|
||||||
import type { HttpRequest } from '../../src/server/HttpRequest';
|
|
||||||
import type { SystemError } from '../../src/util/errors/SystemError';
|
import type { SystemError } from '../../src/util/errors/SystemError';
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/naming-convention */
|
/* eslint-disable @typescript-eslint/naming-convention */
|
||||||
@ -18,6 +12,7 @@ const portNames = [
|
|||||||
'PodCreation',
|
'PodCreation',
|
||||||
'RedisResourceLocker',
|
'RedisResourceLocker',
|
||||||
'ServerFetch',
|
'ServerFetch',
|
||||||
|
'SparqlStorage',
|
||||||
'Subdomains',
|
'Subdomains',
|
||||||
'WebSocketsProtocol',
|
'WebSocketsProtocol',
|
||||||
] as const;
|
] as const;
|
||||||
@ -31,33 +26,11 @@ export function getPort(name: typeof portNames[number]): number {
|
|||||||
return 6000 + idx;
|
return 6000 + idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function performRequest(
|
export function describeIf(envFlag: string, name: string, fn: () => void): void {
|
||||||
handler: HttpHandler,
|
const flag = `TEST_${envFlag.toUpperCase()}`;
|
||||||
requestUrl: URL,
|
const enabled = !/^(|0|false)$/iu.test(process.env[flag] ?? '');
|
||||||
method: string,
|
// eslint-disable-next-line jest/valid-describe, jest/valid-title, jest/no-disabled-tests
|
||||||
headers: IncomingHttpHeaders,
|
return enabled ? describe(name, fn) : describe.skip(name, fn);
|
||||||
data: string[],
|
|
||||||
): Promise<MockResponse<any>> {
|
|
||||||
const request = streamifyArray(data) as HttpRequest;
|
|
||||||
request.url = `${requestUrl.pathname}${requestUrl.search}`;
|
|
||||||
request.method = method;
|
|
||||||
request.headers = headers;
|
|
||||||
request.headers.host = requestUrl.host;
|
|
||||||
const response: MockResponse<any> = createResponse({
|
|
||||||
eventEmitter: EventEmitter,
|
|
||||||
});
|
|
||||||
|
|
||||||
const endPromise = new Promise((resolve): void => {
|
|
||||||
response.on('end', (): void => {
|
|
||||||
expect(response._isEndCalled()).toBeTruthy();
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await handler.handleSafe({ request, response });
|
|
||||||
await endPromise;
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user