mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
Merge branch 'main' into versions/6.0.0
This commit is contained in:
7
test/assets/contexts/test.jsonld
Normal file
7
test/assets/contexts/test.jsonld
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"@context": {
|
||||
"@version": 1.1,
|
||||
"test": "http://example.com/context#",
|
||||
"testPredicate": { "@id": "test:predicate" }
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ describe('An instantiated CliResolver', (): void => {
|
||||
'-s', 's',
|
||||
'-w', '2',
|
||||
]);
|
||||
expect(shorthand.config).toBe('c');
|
||||
expect(shorthand.config).toEqual([ 'c' ]);
|
||||
expect(shorthand.mainModulePath).toBe('m');
|
||||
expect(shorthand.loggingLevel).toBe('l');
|
||||
expect(shorthand.baseUrl).toBe('b');
|
||||
|
||||
@@ -621,9 +621,13 @@ describe('A Solid server with IDP', (): void => {
|
||||
});
|
||||
|
||||
it('should return correct error output.', async(): Promise<void> => {
|
||||
const res = await fetch(`${baseUrl}.oidc/auth`);
|
||||
expect(res.status).toBe(400);
|
||||
await expect(res.text()).resolves.toContain('InvalidRequest: invalid_request');
|
||||
const res = await fetch(`${baseUrl}.oidc/foo`, { headers: { accept: 'application/json' }});
|
||||
expect(res.status).toBe(404);
|
||||
const json = await res.json();
|
||||
expect(json.name).toBe(`InvalidRequest`);
|
||||
expect(json.message).toBe(`invalid_request - unrecognized route or not allowed method (GET on /.oidc/foo)`);
|
||||
expect(json.statusCode).toBe(404);
|
||||
expect(json.stack).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Readable } from 'stream';
|
||||
import type * as Koa from 'koa';
|
||||
import type { errors, Configuration, KoaContextWithOIDC } from 'oidc-provider';
|
||||
import type { ErrorHandler } from '../../../../src/http/output/error/ErrorHandler';
|
||||
import type { ResponseWriter } from '../../../../src/http/output/ResponseWriter';
|
||||
@@ -14,7 +15,8 @@ import { FoundHttpError } from '../../../../src/util/errors/FoundHttpError';
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
jest.mock('oidc-provider', (): any => ({
|
||||
Provider: jest.fn().mockImplementation((issuer: string, config: Configuration): any => ({ issuer, config })),
|
||||
Provider: jest.fn().mockImplementation((issuer: string, config: Configuration): any =>
|
||||
({ issuer, config, use: jest.fn() })),
|
||||
}));
|
||||
|
||||
const routes = {
|
||||
@@ -58,6 +60,7 @@ describe('An IdentityProviderFactory', (): void => {
|
||||
request: {
|
||||
href: 'http://example.com/idp/',
|
||||
},
|
||||
accepts: jest.fn().mockReturnValue('type'),
|
||||
} as any;
|
||||
|
||||
interactionHandler = {
|
||||
@@ -146,10 +149,11 @@ describe('An IdentityProviderFactory', (): void => {
|
||||
});
|
||||
|
||||
// Test the renderError function
|
||||
await expect((config.renderError as any)(ctx, {}, 'error!')).resolves.toBeUndefined();
|
||||
const error = new Error('error!');
|
||||
await expect((config.renderError as any)(ctx, {}, error)).resolves.toBeUndefined();
|
||||
expect(errorHandler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(errorHandler.handleSafe)
|
||||
.toHaveBeenLastCalledWith({ error: 'error!', request: ctx.req });
|
||||
.toHaveBeenLastCalledWith({ error, request: ctx.req });
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(responseWriter.handleSafe).toHaveBeenLastCalledWith({ response: ctx.res, result: { statusCode: 500 }});
|
||||
});
|
||||
@@ -230,4 +234,35 @@ describe('An IdentityProviderFactory', (): void => {
|
||||
expect(error.message).toBe('bad data - more info - more details');
|
||||
expect(error.stack).toContain('Error: bad data - more info - more details');
|
||||
});
|
||||
|
||||
it('adds middleware to make the OIDC provider think the request wants HTML.', async(): Promise<void> => {
|
||||
const provider = await factory.getProvider() as any;
|
||||
const use = provider.use as jest.Mock<ReturnType<Koa['use']>, Parameters<Koa['use']>>;
|
||||
expect(use).toHaveBeenCalledTimes(1);
|
||||
const middleware = use.mock.calls[0][0];
|
||||
|
||||
const oldAccept = ctx.accepts;
|
||||
const next = jest.fn();
|
||||
await expect(middleware(ctx, next)).resolves.toBeUndefined();
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(ctx.accepts('json', 'html')).toBe('html');
|
||||
expect(oldAccept).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('does not modify the context accepts function in other cases.', async(): Promise<void> => {
|
||||
const provider = await factory.getProvider() as any;
|
||||
const use = provider.use as jest.Mock<ReturnType<Koa['use']>, Parameters<Koa['use']>>;
|
||||
expect(use).toHaveBeenCalledTimes(1);
|
||||
const middleware = use.mock.calls[0][0];
|
||||
|
||||
const oldAccept = ctx.accepts;
|
||||
const next = jest.fn();
|
||||
await expect(middleware(ctx, next)).resolves.toBeUndefined();
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(ctx.accepts('something')).toBe('type');
|
||||
expect(oldAccept).toHaveBeenCalledTimes(1);
|
||||
expect(oldAccept).toHaveBeenLastCalledWith('something');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -281,6 +281,37 @@ describe('AppRunner', (): void => {
|
||||
expect(app.start).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('can apply multiple configurations.', async(): Promise<void> => {
|
||||
const params = [
|
||||
'node', 'script',
|
||||
'-c', 'config1.json', 'config2.json',
|
||||
];
|
||||
await expect(new AppRunner().createCli(params)).resolves.toBe(app);
|
||||
|
||||
expect(ComponentsManager.build).toHaveBeenCalledTimes(1);
|
||||
expect(ComponentsManager.build).toHaveBeenCalledWith({
|
||||
dumpErrorState: true,
|
||||
logLevel: 'info',
|
||||
mainModulePath: joinFilePath(__dirname, '../../../'),
|
||||
typeChecking: false,
|
||||
});
|
||||
expect(manager.configRegistry.register).toHaveBeenCalledTimes(2);
|
||||
expect(manager.configRegistry.register).toHaveBeenNthCalledWith(1, '/var/cwd/config1.json');
|
||||
expect(manager.configRegistry.register).toHaveBeenNthCalledWith(2, '/var/cwd/config2.json');
|
||||
expect(manager.instantiate).toHaveBeenCalledTimes(2);
|
||||
expect(manager.instantiate).toHaveBeenNthCalledWith(1, 'urn:solid-server-app-setup:default:CliResolver', {});
|
||||
expect(cliExtractor.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(cliExtractor.handleSafe).toHaveBeenCalledWith(params);
|
||||
expect(shorthandResolver.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(shorthandResolver.handleSafe).toHaveBeenCalledWith(defaultParameters);
|
||||
expect(manager.instantiate).toHaveBeenNthCalledWith(1, 'urn:solid-server-app-setup:default:CliResolver', {});
|
||||
expect(manager.instantiate).toHaveBeenNthCalledWith(2,
|
||||
'urn:solid-server:default:App',
|
||||
{ variables: defaultVariables });
|
||||
expect(app.clusterManager.isSingleThreaded()).toBeFalsy();
|
||||
expect(app.start).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('uses the default process.argv in case none are provided.', async(): Promise<void> => {
|
||||
const { argv } = process;
|
||||
const argvParameters = [
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'jest-rdf';
|
||||
import { Readable } from 'stream';
|
||||
import arrayifyStream from 'arrayify-stream';
|
||||
import fetch, { Headers } from 'cross-fetch';
|
||||
import { DataFactory } from 'n3';
|
||||
import rdfParser from 'rdf-parse';
|
||||
import { PREFERRED_PREFIX_TERM, SOLID_META } from '../../../../src';
|
||||
@@ -14,10 +15,25 @@ import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
const { namedNode, triple, literal, quad } = DataFactory;
|
||||
|
||||
describe('A RdfToQuadConverter', (): void => {
|
||||
const converter = new RdfToQuadConverter();
|
||||
const identifier: ResourceIdentifier = { path: 'path' };
|
||||
// All of this is necessary to not break the cross-fetch imports that happen in `rdf-parse`
|
||||
jest.mock('cross-fetch', (): any => {
|
||||
const mock = jest.fn();
|
||||
// Require the original module to not be mocked...
|
||||
const originalFetch = jest.requireActual('cross-fetch');
|
||||
return {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
__esModule: true,
|
||||
...originalFetch,
|
||||
fetch: mock,
|
||||
default: mock,
|
||||
};
|
||||
});
|
||||
|
||||
// Not mocking `fs` since this breaks the `rdf-parser` library
|
||||
describe('A RdfToQuadConverter', (): void => {
|
||||
const fetchMock: jest.Mock = fetch as any;
|
||||
const converter = new RdfToQuadConverter();
|
||||
const identifier: ResourceIdentifier = { path: 'http://example.com/resource' };
|
||||
it('supports serializing as quads.', async(): Promise<void> => {
|
||||
const types = rdfParser.getContentTypes()
|
||||
.then((inputTypes): string[] => inputTypes.filter((type): boolean => type !== 'application/json'));
|
||||
@@ -123,4 +139,43 @@ describe('A RdfToQuadConverter', (): void => {
|
||||
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
|
||||
await expect(arrayifyStream(result.data)).rejects.toThrow(BadRequestHttpError);
|
||||
});
|
||||
|
||||
it('can use locally stored contexts.', async(): Promise<void> => {
|
||||
const fetchedContext = {
|
||||
'@context': {
|
||||
'@version': 1.1,
|
||||
test: 'http://example.com/context2#',
|
||||
testPredicate2: { '@id': 'test:predicate2' },
|
||||
},
|
||||
};
|
||||
// This depends on the fields needed by the `jsonld-context-parser` so could break if library changes
|
||||
fetchMock.mockResolvedValueOnce({
|
||||
json: (): any => fetchedContext,
|
||||
status: 200,
|
||||
ok: true,
|
||||
headers: new Headers({ 'content-type': 'application/ld+json' }),
|
||||
});
|
||||
|
||||
const contextConverter = new RdfToQuadConverter(
|
||||
{ 'http://example.com/context.jsonld': '@css:test/assets/contexts/test.jsonld' },
|
||||
);
|
||||
const jsonld = {
|
||||
'@context': [ 'http://example.com/context.jsonld', 'http://example.com/context2.jsonld' ],
|
||||
'@id': 'http://example.com/resource',
|
||||
testPredicate: 123,
|
||||
testPredicate2: 456,
|
||||
};
|
||||
const representation = new BasicRepresentation(JSON.stringify(jsonld), 'application/ld+json');
|
||||
const preferences: RepresentationPreferences = { type: { [INTERNAL_QUADS]: 1 }};
|
||||
const result = await contextConverter.handle({ identifier, representation, preferences });
|
||||
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
|
||||
namedNode('http://example.com/resource'),
|
||||
namedNode('http://example.com/context#predicate'),
|
||||
literal(123),
|
||||
), triple(
|
||||
namedNode('http://example.com/resource'),
|
||||
namedNode('http://example.com/context2#predicate2'),
|
||||
literal(456),
|
||||
) ]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user