Skip to content

Commit 0444f73

Browse files
Merge pull request #1784 from markscott-ms/1778-calmhubauth
2 parents 6ef2270 + 153d0fa commit 0444f73

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

shared/src/document-loader/calmhub-document-loader.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import axios from 'axios';
22
import AxiosMockAdapter from 'axios-mock-adapter';
33
import { CalmHubDocumentLoader } from './calmhub-document-loader';
4+
import { SchemaDirectory } from '../schema-directory';
45

56
const calmHubBaseUrl = 'http://local-calmhub';
67

78
const ax = axios.create({ baseURL: calmHubBaseUrl });
89
const mock = new AxiosMockAdapter(ax);
910

10-
mock.onGet('/calm/schemas/2025-03/meta/core.json').reply(200, {
11+
mock.onGet('/schemas/2025-03/meta/core.json').reply(200, {
1112
'$id': 'https://calm.finos.org/calm/schemas/2025-03/meta/core.json',
1213
'value': 'test'
1314
});
1415

1516
describe('calmhub-document-loader', () => {
16-
let calmHubDocumentLoader;
17+
let calmHubDocumentLoader: CalmHubDocumentLoader;
18+
let schemaDirectory: SchemaDirectory;
1719
beforeEach(() => {
1820
calmHubDocumentLoader = new CalmHubDocumentLoader(calmHubBaseUrl, false, ax);
21+
calmHubDocumentLoader.initialise(schemaDirectory);
1922
});
2023

2124
it('loads a document from CalmHub', async () => {
22-
const calmHubUrl = 'https://calm.finos.org/calm/schemas/2025-03/meta/core.json';
25+
const calmHubUrl = 'calm:/schemas/2025-03/meta/core.json';
2326
const document = await calmHubDocumentLoader.loadMissingDocument(calmHubUrl, 'schema');
2427
expect(document).toEqual({
2528
'$id': 'https://calm.finos.org/calm/schemas/2025-03/meta/core.json',
@@ -28,7 +31,13 @@ describe('calmhub-document-loader', () => {
2831
});
2932

3033
it('throws an error when the document is not found', async () => {
31-
const calmHubUrl = 'https://calm.finos.org/calm/schemas/2025-03/meta/nonexistent.json';
34+
const calmHubUrl = 'calm:/schemas/2025-03/meta/nonexistent.json';
35+
36+
await expect(calmHubDocumentLoader.loadMissingDocument(calmHubUrl, 'schema')).rejects.toThrow();
37+
});
38+
39+
it('throws an error when the protocol is not calm:', async () => {
40+
const calmHubUrl = 'https://not.calmhub.com/schemas/2025-03/meta/nonexistent.json';
3241

3342
await expect(calmHubDocumentLoader.loadMissingDocument(calmHubUrl, 'schema')).rejects.toThrow();
3443
});

shared/src/document-loader/calmhub-document-loader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios, { Axios } from 'axios';
22
import { SchemaDirectory } from '../schema-directory';
3-
import { CalmDocumentType, DocumentLoader } from './document-loader';
3+
import { CalmDocumentType, DocumentLoader, CALM_HUB_PROTO } from './document-loader';
44
import { initLogger, Logger } from '../logger';
55

66
export class CalmHubDocumentLoader implements DocumentLoader {
@@ -47,6 +47,10 @@ export class CalmHubDocumentLoader implements DocumentLoader {
4747

4848
async loadMissingDocument(documentId: string, _: CalmDocumentType): Promise<object> {
4949
const url = new URL(documentId);
50+
const protocol = url.protocol;
51+
if (protocol !== CALM_HUB_PROTO) {
52+
throw new Error(`CalmHubDocumentLoader only loads documents with protocol '${CALM_HUB_PROTO}'. (Requested: ${protocol})`);
53+
}
5054
const path = url.pathname;
5155

5256
this.logger.debug(`Loading CALM schema from ${this.calmHubUrl}${path}`);

shared/src/document-loader/document-loader.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,24 @@ describe('DocumentLoader', () => {
3333
vi.resetModules();
3434
});
3535

36-
it('should create a FileSystemDocumentLoader when loadMode is "filesystem"', () => {
36+
it('should create a FileSystemDocumentLoader', () => {
3737

3838
const docLoaderOpts: DocumentLoaderOptions = {
39-
loadMode: 'filesystem',
4039
schemaDirectoryPath: 'schemas'
4140
};
4241

43-
buildDocumentLoader(docLoaderOpts, false);
42+
buildDocumentLoader(docLoaderOpts);
4443

4544
expect(mocks.fsDocLoader).toHaveBeenCalledWith([CALM_META_SCHEMA_DIRECTORY, 'schemas'], false);
4645
});
4746

48-
it('should create a CalmHubDocumentLoader when loadMode is "calmhub"', () => {
47+
it('should create a CalmHubDocumentLoader when calmHubUrl is defined in loader options', () => {
4948

5049
const docLoaderOpts: DocumentLoaderOptions = {
51-
loadMode: 'calmhub',
5250
calmHubUrl: 'https://example.com'
5351
};
5452

55-
buildDocumentLoader(docLoaderOpts, false);
53+
buildDocumentLoader(docLoaderOpts);
5654

5755
expect(mocks.calmHubDocLoader).toHaveBeenCalledWith('https://example.com', false);
5856
});

shared/src/document-loader/document-loader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { MultiStrategyDocumentLoader } from './multi-strategy-document-loader';
77

88
export type CalmDocumentType = 'architecture' | 'pattern' | 'schema';
99

10+
export const CALM_HUB_PROTO = 'calm:';
11+
1012
export interface DocumentLoader {
1113
initialise(schemaDirectory: SchemaDirectory): Promise<void>;
1214
loadMissingDocument(documentId: string, type: CalmDocumentType): Promise<object>;

0 commit comments

Comments
 (0)