CommunitySolidServer/test/util/TestHelpers.ts
Joachim Van Herwegen b1991cb08a
feat: More integration tests and test configs (#154)
* add todo

* Added configurations folder

* Added config to index

* changed /bin/server to use configfiles

* initiate acl with Setup component

* add last changes

* reset serverconfig

* move authenticatedLdpHandler configs to config files

* failed to read testss

* failed to read testss

* removed import part

* Fix merge conflicts

* add first FileResTests

* fix: fix fileresourcestore metadata error

* fix unit tests

* remove test files

* Added test and changed callFile

* Fix: metadata file error in FileResourceStore

* fix: ensure full test coverage

* added tests

* Fix get tests

* added testfiles

* changed config to use PasstrueStore

* to continue work on

* refactor fileresourcestore config

* refactor tests

* fix content-types, update tests

* replace sync methods with async methods

* move acl function to util

* added testfiles for Fileserver with acl

* update tests

* add first acl filestore test

* refactor

* add resource mapper

* refactor config files

* add more fileresourcestore acl tests

* add locking resource store test files

* move file mapping logic to resourcemapper

* added beforeAll for a permanent file in Auht tests

* make filestore dependent of resource mapper

* moved configs to test/configs

* set default contenttype

* refactor fileresourcemapper

* fix map function

* refactor

* fixed foldercreationtest

* changed some tests so the files are cleaned up when done testing

* add normalized parser

* Auhtenticationtests clean up acl file

* refactor unit test

* lost changes added again

* fix metadata problem

* refactor names

* reverse change

* add getters

* configs and start util

* add comments

* add comments, move code

* added acl helper and changed tests

* linter 7.7.0 -> 7.0.0

* moved test/tesfiles -> test/assets

* removed configs/**/*.ts from tsconfig.json

* Temporary changed threshold so cli test is ignored and commiting goes easier, will revert later

* added FileResourceStore to index

* Changed imports

* Changed imports for all configs

* Removed comment

* Changed names of configs

* added 'Config' to name and removed comment

* removed unused testfile and added testfile 0

* changed beforeAll to just copy permanent file

* change text/turtle to constant

* fix converter issue

* getHandler -> getHttpHandler, and updates to config

* removed ','

* removed trailing /

* changed imports for index.d.js problem

* removed duplicate file and added line that got removed in mergeconflicts

* add jest global teardown

* add ignore for CliRunner

* add changes

* fix copyfile error

* remove unused testfiles

* adding test with image

* add first util functions

* relative paths to absolute paths

* added 3 FileStoreTests

* more refactoring

* more absolute paths

* fix mkdir path

* added test

* add util for easy configs

* add comments

* added some testhelpers and refactor first test

* fix converter test error

* refactor FileResTests

* solved failing test because new converters

* removed afterAll()

* removed setAcl from util

* removed config from Authorization.test.ts

* changed strange linting

* refactored AuthenticatedFileResourceStore tests

* fixed unclear root variable

* fix: Use absolute test paths

* Mock fs correctly and remove teardown

* Clean up after tests

Co-authored-by: freyavs <freyavanspeybroeck@outlook.com>
Co-authored-by: thdossch <dossche.thor@gmail.com>
Co-authored-by: Freya <56410697+freyavs@users.noreply.github.com>
Co-authored-by: thdossch <49074469+thdossch@users.noreply.github.com>
Co-authored-by: Ruben Verborgh <ruben@verborgh.org>
2020-09-14 16:06:27 +02:00

212 lines
6.3 KiB
TypeScript

import { EventEmitter } from 'events';
import { promises as fs } from 'fs';
import { IncomingHttpHeaders } from 'http';
import { join } from 'path';
import * as url from 'url';
import { createResponse, MockResponse } from 'node-mocks-http';
import streamifyArray from 'streamify-array';
import { ResourceStore } from '../../index';
import { PermissionSet } from '../../src/ldp/permissions/PermissionSet';
import { HttpHandler } from '../../src/server/HttpHandler';
import { HttpRequest } from '../../src/server/HttpRequest';
import { call } from './Util';
export class AclTestHelper {
public readonly store: ResourceStore;
public id: string;
public constructor(store: ResourceStore, id: string) {
this.store = store;
this.id = id;
}
public async setSimpleAcl(
permissions: PermissionSet,
agentClass: 'agent' | 'authenticated',
): Promise<void> {
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', 'Delete' ]) {
if (permissions[perm.toLowerCase() as keyof PermissionSet]) {
acl.push(`;\n acl:mode acl:${perm}`);
}
}
acl.push(';\n acl:mode acl:Control');
acl.push(`;\n acl:accessTo <${this.id}>`);
acl.push(`;\n acl:default <${this.id}>`);
acl.push(
`;\n acl:agentClass ${
agentClass === 'agent' ? 'foaf:Agent' : 'foaf:AuthenticatedAgent'
}`,
);
acl.push('.');
const representation = {
binary: true,
data: streamifyArray(acl),
metadata: {
raw: [],
profiles: [],
contentType: 'text/turtle',
},
};
return this.store.setRepresentation(
{ path: `${this.id}.acl` },
representation,
);
}
}
export class FileTestHelper {
public readonly handler: HttpHandler;
public readonly baseUrl: URL;
public constructor(handler: HttpHandler, baseUrl: URL) {
this.handler = handler;
this.baseUrl = baseUrl;
}
public async simpleCall(
requestUrl: URL,
method: string,
headers: IncomingHttpHeaders,
): Promise<MockResponse<any>> {
return call(this.handler, requestUrl, method, headers, []);
}
public async callWithFile(
requestUrl: URL,
method: string,
headers: IncomingHttpHeaders,
data: Buffer,
): Promise<MockResponse<any>> {
const request = streamifyArray([ data ]) as HttpRequest;
request.url = requestUrl.pathname;
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 createFile(fileLocation: string, slug: string, mayFail = false): Promise<MockResponse<any>> {
const fileData = await fs.readFile(
join(__dirname, fileLocation),
);
const response: MockResponse<any> = await this.callWithFile(
this.baseUrl,
'POST',
{ 'content-type': 'application/octet-stream',
slug,
'transfer-encoding': 'chunked' },
fileData,
);
if (!mayFail) {
expect(response.statusCode).toBe(200);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toContain(url.format(this.baseUrl));
}
return response;
}
public async overwriteFile(fileLocation: string, requestUrl: string): Promise<MockResponse<any>> {
const fileData = await fs.readFile(
join(__dirname, fileLocation),
);
const putUrl = new URL(requestUrl);
const response: MockResponse<any> = await this.callWithFile(
putUrl,
'PUT',
{ 'content-type': 'application/octet-stream', 'transfer-encoding': 'chunked' },
fileData,
);
expect(response.statusCode).toBe(200);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toContain(url.format(putUrl));
return response;
}
public async getFile(requestUrl: string): Promise<MockResponse<any>> {
const getUrl = new URL(requestUrl);
return this.simpleCall(getUrl, 'GET', { accept: '*/*' });
}
public async deleteFile(requestUrl: string, mayFail = false): Promise<MockResponse<any>> {
const deleteUrl = new URL(requestUrl);
const response = await this.simpleCall(deleteUrl, 'DELETE', {});
if (!mayFail) {
expect(response.statusCode).toBe(200);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toBe(url.format(requestUrl));
}
return response;
}
public async createFolder(slug: string): Promise<MockResponse<any>> {
const response: MockResponse<any> = await this.simpleCall(
this.baseUrl,
'POST',
{
slug,
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',
'content-type': 'text/plain',
'transfer-encoding': 'chunked',
},
);
expect(response.statusCode).toBe(200);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toContain(url.format(this.baseUrl));
return response;
}
public async getFolder(requestUrl: string): Promise<MockResponse<any>> {
const getUrl = new URL(requestUrl);
return await this.simpleCall(getUrl, 'GET', { accept: 'text/turtle' });
}
public async deleteFolder(requestUrl: string): Promise<MockResponse<any>> {
const deleteUrl = new URL(requestUrl);
const response = await this.simpleCall(deleteUrl, 'DELETE', {});
expect(response.statusCode).toBe(200);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toBe(url.format(requestUrl));
return response;
}
public async shouldNotExist(requestUrl: string): Promise<MockResponse<any>> {
const getUrl = new URL(requestUrl);
const response = await this.simpleCall(getUrl, 'GET', { accept: '*/*' });
expect(response.statusCode).toBe(404);
expect(response._getData()).toContain('NotFoundHttpError');
return response;
}
}