mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
test: Use Components.js in AuthenticatedLdpHandler.
This commit is contained in:
parent
147f3cf0c7
commit
d1d29a3f52
@ -20,10 +20,10 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"@type": "LinkRelMetadataWriter",
|
"@type": "LinkRelMetadataWriter",
|
||||||
"LinkRelMetadataWriter:_headerMap": [
|
"LinkRelMetadataWriter:_linkRelMap": [
|
||||||
{
|
{
|
||||||
"LinkRelMetadataWriter:_headerMap_key": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
|
"LinkRelMetadataWriter:_linkRelMap_key": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
|
||||||
"LinkRelMetadataWriter:_headerMap_value": "type"
|
"LinkRelMetadataWriter:_linkRelMap_value": "type"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { getLoggerFor } from '../../../logging/LogUtil';
|
||||||
import type { HttpResponse } from '../../../server/HttpResponse';
|
import type { HttpResponse } from '../../../server/HttpResponse';
|
||||||
import { addHeader } from '../../../util/HeaderUtil';
|
import { addHeader } from '../../../util/HeaderUtil';
|
||||||
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
|
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
|
||||||
@ -9,6 +10,7 @@ import { MetadataWriter } from './MetadataWriter';
|
|||||||
*/
|
*/
|
||||||
export class LinkRelMetadataWriter extends MetadataWriter {
|
export class LinkRelMetadataWriter extends MetadataWriter {
|
||||||
private readonly linkRelMap: Record<string, string>;
|
private readonly linkRelMap: Record<string, string>;
|
||||||
|
protected readonly logger = getLoggerFor(this);
|
||||||
|
|
||||||
public constructor(linkRelMap: Record<string, string>) {
|
public constructor(linkRelMap: Record<string, string>) {
|
||||||
super();
|
super();
|
||||||
@ -16,9 +18,12 @@ export class LinkRelMetadataWriter extends MetadataWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async handle(input: { response: HttpResponse; metadata: RepresentationMetadata }): Promise<void> {
|
public async handle(input: { response: HttpResponse; metadata: RepresentationMetadata }): Promise<void> {
|
||||||
for (const key of Object.keys(this.linkRelMap)) {
|
const keys = Object.keys(this.linkRelMap);
|
||||||
|
this.logger.debug(`Available link relations: ${keys.length}`);
|
||||||
|
for (const key of keys) {
|
||||||
const values = input.metadata.getAll(key).map((term): string => `<${term.value}>; rel="${this.linkRelMap[key]}"`);
|
const values = input.metadata.getAll(key).map((term): string => `<${term.value}>; rel="${this.linkRelMap[key]}"`);
|
||||||
if (values.length > 0) {
|
if (values.length > 0) {
|
||||||
|
this.logger.debug(`Adding Link header ${values}`);
|
||||||
addHeader(input.response, 'link', values);
|
addHeader(input.response, 'link', values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
import type {
|
|
||||||
HttpHandler,
|
|
||||||
ResourceStore,
|
|
||||||
} from '../../src/index';
|
|
||||||
import {
|
|
||||||
AllowEverythingAuthorizer,
|
|
||||||
AuthenticatedLdpHandler,
|
|
||||||
EmptyCredentialsExtractor,
|
|
||||||
MethodPermissionsExtractor,
|
|
||||||
} from '../../src/index';
|
|
||||||
import type { ServerConfig } from './ServerConfig';
|
|
||||||
import { getOperationHandler, getInMemoryResourceStore, getBasicRequestParser, getResponseWriter } from './Util';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BasicConfig works with
|
|
||||||
* - an AllowEverythingAuthorizer (no acl)
|
|
||||||
* - an InMemoryResourceStore
|
|
||||||
* - GET, POST & DELETE operation handlers
|
|
||||||
*/
|
|
||||||
|
|
||||||
export class BasicConfig implements ServerConfig {
|
|
||||||
public store: ResourceStore;
|
|
||||||
|
|
||||||
public constructor() {
|
|
||||||
this.store = getInMemoryResourceStore();
|
|
||||||
}
|
|
||||||
|
|
||||||
public getHttpHandler(): HttpHandler {
|
|
||||||
const requestParser = getBasicRequestParser();
|
|
||||||
|
|
||||||
const credentialsExtractor = new EmptyCredentialsExtractor();
|
|
||||||
const permissionsExtractor = new MethodPermissionsExtractor();
|
|
||||||
const authorizer = new AllowEverythingAuthorizer();
|
|
||||||
|
|
||||||
const operationHandler = getOperationHandler(this.store);
|
|
||||||
|
|
||||||
const responseWriter = getResponseWriter();
|
|
||||||
|
|
||||||
const handler = new AuthenticatedLdpHandler({
|
|
||||||
requestParser,
|
|
||||||
credentialsExtractor,
|
|
||||||
permissionsExtractor,
|
|
||||||
authorizer,
|
|
||||||
operationHandler,
|
|
||||||
responseWriter,
|
|
||||||
});
|
|
||||||
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
import type {
|
|
||||||
HttpHandler,
|
|
||||||
ResourceStore,
|
|
||||||
} from '../../src/index';
|
|
||||||
import {
|
|
||||||
AllowEverythingAuthorizer,
|
|
||||||
AuthenticatedLdpHandler,
|
|
||||||
EmptyCredentialsExtractor,
|
|
||||||
MethodPermissionsExtractor,
|
|
||||||
QuadToRdfConverter,
|
|
||||||
RawBodyParser,
|
|
||||||
RdfToQuadConverter,
|
|
||||||
SparqlUpdateBodyParser,
|
|
||||||
SparqlPatchPermissionsExtractor,
|
|
||||||
WaterfallHandler,
|
|
||||||
} from '../../src/index';
|
|
||||||
|
|
||||||
import type { ServerConfig } from './ServerConfig';
|
|
||||||
import {
|
|
||||||
getInMemoryResourceStore,
|
|
||||||
getOperationHandler,
|
|
||||||
getConvertingStore,
|
|
||||||
getPatchingStore,
|
|
||||||
getBasicRequestParser,
|
|
||||||
getResponseWriter,
|
|
||||||
} from './Util';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BasicHandlersConfig works with
|
|
||||||
* - an AllowEverythingAuthorizer (no acl)
|
|
||||||
* - an InMemoryResourceStore wrapped in a converting store & wrapped in a patching store
|
|
||||||
* - GET, POST, PUT, PATCH & DELETE operation handlers
|
|
||||||
*/
|
|
||||||
|
|
||||||
export class BasicHandlersConfig implements ServerConfig {
|
|
||||||
public store: ResourceStore;
|
|
||||||
|
|
||||||
public constructor() {
|
|
||||||
const convertingStore = getConvertingStore(
|
|
||||||
getInMemoryResourceStore(),
|
|
||||||
[ new QuadToRdfConverter(), new RdfToQuadConverter() ],
|
|
||||||
);
|
|
||||||
this.store = getPatchingStore(convertingStore);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getHttpHandler(): HttpHandler {
|
|
||||||
const requestParser = getBasicRequestParser([
|
|
||||||
new SparqlUpdateBodyParser(),
|
|
||||||
new RawBodyParser(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const credentialsExtractor = new EmptyCredentialsExtractor();
|
|
||||||
const permissionsExtractor = new WaterfallHandler([
|
|
||||||
new MethodPermissionsExtractor(),
|
|
||||||
new SparqlPatchPermissionsExtractor(),
|
|
||||||
]);
|
|
||||||
const authorizer = new AllowEverythingAuthorizer();
|
|
||||||
|
|
||||||
const operationHandler = getOperationHandler(this.store);
|
|
||||||
|
|
||||||
const responseWriter = getResponseWriter();
|
|
||||||
|
|
||||||
const handler = new AuthenticatedLdpHandler({
|
|
||||||
requestParser,
|
|
||||||
credentialsExtractor,
|
|
||||||
permissionsExtractor,
|
|
||||||
authorizer,
|
|
||||||
operationHandler,
|
|
||||||
responseWriter,
|
|
||||||
});
|
|
||||||
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,24 +3,23 @@ import * as url from 'url';
|
|||||||
import { namedNode, quad } from '@rdfjs/data-model';
|
import { namedNode, quad } from '@rdfjs/data-model';
|
||||||
import { Parser } from 'n3';
|
import { Parser } from 'n3';
|
||||||
import type { MockResponse } from 'node-mocks-http';
|
import type { MockResponse } from 'node-mocks-http';
|
||||||
import { RootContainerInitializer } from '../../src/init/RootContainerInitializer';
|
import type { HttpHandler } from '../../src/server/HttpHandler';
|
||||||
import { LDP } from '../../src/util/UriConstants';
|
import { LDP } from '../../src/util/UriConstants';
|
||||||
import { BasicConfig } from '../configs/BasicConfig';
|
import { BASE, instantiateFromConfig } from '../configs/Util';
|
||||||
import { BasicHandlersConfig } from '../configs/BasicHandlersConfig';
|
|
||||||
import { BASE } from '../configs/Util';
|
|
||||||
import { call } from '../util/Util';
|
import { call } from '../util/Util';
|
||||||
|
|
||||||
describe('An integrated AuthenticatedLdpHandler', (): void => {
|
describe('An integrated AuthenticatedLdpHandler', (): void => {
|
||||||
|
let handler: HttpHandler;
|
||||||
|
|
||||||
|
beforeAll(async(): Promise<void> => {
|
||||||
|
handler = await instantiateFromConfig(
|
||||||
|
'urn:solid-server:default:LdpHandler', 'auth-allow-all.json', {
|
||||||
|
'urn:solid-server:default:variable:baseUrl': BASE,
|
||||||
|
},
|
||||||
|
) as HttpHandler;
|
||||||
|
});
|
||||||
|
|
||||||
describe('with simple handlers', (): void => {
|
describe('with simple handlers', (): void => {
|
||||||
const config = new BasicConfig();
|
|
||||||
const handler = config.getHttpHandler();
|
|
||||||
|
|
||||||
beforeAll(async(): Promise<void> => {
|
|
||||||
// Initialize store
|
|
||||||
const initializer = new RootContainerInitializer(BASE, config.store);
|
|
||||||
await initializer.handleSafe();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can add, read and delete data based on incoming requests.', async(): Promise<void> => {
|
it('can add, read and delete data based on incoming requests.', async(): Promise<void> => {
|
||||||
// POST
|
// POST
|
||||||
let requestUrl = new URL('http://test.com/');
|
let requestUrl = new URL('http://test.com/');
|
||||||
@ -70,15 +69,6 @@ describe('An integrated AuthenticatedLdpHandler', (): void => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('with simple PATCH handlers', (): void => {
|
describe('with simple PATCH handlers', (): void => {
|
||||||
const config = new BasicHandlersConfig();
|
|
||||||
const handler = config.getHttpHandler();
|
|
||||||
|
|
||||||
beforeAll(async(): Promise<void> => {
|
|
||||||
// Initialize store
|
|
||||||
const initializer = new RootContainerInitializer(BASE, config.store);
|
|
||||||
await initializer.handleSafe();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can handle simple SPARQL updates.', async(): Promise<void> => {
|
it('can handle simple SPARQL updates.', async(): Promise<void> => {
|
||||||
// POST
|
// POST
|
||||||
let requestUrl = new URL('http://test.com/');
|
let requestUrl = new URL('http://test.com/');
|
||||||
@ -142,15 +132,6 @@ describe('An integrated AuthenticatedLdpHandler', (): void => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('with simple PUT handlers', (): void => {
|
describe('with simple PUT handlers', (): void => {
|
||||||
const config = new BasicHandlersConfig();
|
|
||||||
const handler = config.getHttpHandler();
|
|
||||||
|
|
||||||
beforeAll(async(): Promise<void> => {
|
|
||||||
// Initialize store
|
|
||||||
const initializer = new RootContainerInitializer(BASE, config.store);
|
|
||||||
await initializer.handleSafe();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should overwrite the content on PUT request.', async(): Promise<void> => {
|
it('should overwrite the content on PUT request.', async(): Promise<void> => {
|
||||||
// POST
|
// POST
|
||||||
let requestUrl = new URL('http://test.com/');
|
let requestUrl = new URL('http://test.com/');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user