From d415287e7029e9bbb33d949d3ec45b863a6cccd0 Mon Sep 17 00:00:00 2001 From: Theo Truong Date: Wed, 29 May 2024 12:17:32 -0600 Subject: [PATCH] # updated ChapterReader with env vars and authentication Signed-off-by: Theo Truong --- tools/src/tester/ChapterReader.ts | 13 +++++++++++-- tools/src/tester/TestsRunner.ts | 3 +-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/src/tester/ChapterReader.ts b/tools/src/tester/ChapterReader.ts index 495233785..f8a962709 100644 --- a/tools/src/tester/ChapterReader.ts +++ b/tools/src/tester/ChapterReader.ts @@ -1,12 +1,16 @@ import axios from 'axios' import { type ChapterRequest, type ActualResponse, type Parameter } from './types/story.types' +import { Agent } from 'https' // A lightweight client for testing the API export default class ChapterReader { url: string + admin_password: string - constructor (url: string) { - this.url = url + constructor () { + this.url = process.env.OPENSEARCH_URL ?? 'https://localhost:9200' + if (process.env.OPENSEARCH_PASSWORD == null) throw new Error('OPENSEARCH_PASSWORD is not set') + this.admin_password = process.env.OPENSEARCH_PASSWORD } async read (chapter: ChapterRequest): Promise { @@ -14,6 +18,11 @@ export default class ChapterReader { const [url, params] = this.#parse_url(chapter.path, chapter.parameters ?? {}) await axios.request({ url, + auth: { + username: 'admin', + password: this.admin_password + }, + httpsAgent: new Agent({ rejectUnauthorized: false }), method: chapter.method, params, data: chapter.request_body?.payload diff --git a/tools/src/tester/TestsRunner.ts b/tools/src/tester/TestsRunner.ts index 6f54c92d4..cdc513dcd 100644 --- a/tools/src/tester/TestsRunner.ts +++ b/tools/src/tester/TestsRunner.ts @@ -21,8 +21,7 @@ export default class TestsRunner { this.path = resolve(path) this.opts = opts - // TODO: Grab server URL from environment variable and add authentication. - const chapter_reader = new ChapterReader('http://localhost:9200') + const chapter_reader = new ChapterReader() const spec_parser = new SpecParser(spec) const schema_validator = new SchemaValidator(spec) SharedResources.create_instance({ chapter_reader, schema_validator, spec_parser })