From 44a8d36c918195ddc45a58dd3ed1811b7a4ec426 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 4 Jan 2022 18:23:45 +0000 Subject: [PATCH] Adding basic test cases for static and dynamic variables (backend). --- packages/server/__mocks__/node-fetch.ts | 6 ++ .../server/src/api/routes/tests/query.spec.js | 73 +++++++++++++++++++ packages/server/src/definitions/datasource.ts | 10 +++ .../src/tests/utilities/TestConfiguration.js | 10 +++ 4 files changed, 99 insertions(+) diff --git a/packages/server/__mocks__/node-fetch.ts b/packages/server/__mocks__/node-fetch.ts index 8badbd0156..6bd55186b5 100644 --- a/packages/server/__mocks__/node-fetch.ts +++ b/packages/server/__mocks__/node-fetch.ts @@ -57,6 +57,12 @@ module FetchMock { ], bookmark: "test", }) + } else if (url.includes("google.com")) { + return json({ + url, + opts, + value: "", + }) } return fetch(url, opts) } diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 37c969aba8..8589f4ded6 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -1,5 +1,6 @@ // Mock out postgres for this jest.mock("pg") +jest.mock("node-fetch") // Mock isProdAppID to we can later mock the implementation and pretend we are // using prod app IDs @@ -226,4 +227,76 @@ describe("/queries", () => { .expect(400) }) }) + + describe("test variables", () => { + async function restDatasource(cfg) { + return await config.createDatasource({ + datasource: { + ...basicDatasource().datasource, + source: "REST", + config: cfg || {}, + }, + }) + } + + it("should work with static variables", async () => { + const datasource = await restDatasource({ + staticVariables: { + variable: "google", + variable2: "1", + }, + }) + const res = await request + .post(`/api/queries/preview`) + .send({ + datasourceId: datasource._id, + parameters: {}, + fields: { + path: "www.{{ variable }}.com", + queryString: "test={{ variable2 }}", + }, + queryVerb: "read", + }) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + // these responses come from the mock + expect(res.body.schemaFields).toEqual(["url", "opts", "value"]) + expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1") + }) + + it("should work with dynamic variables", async () => { + const datasource = await restDatasource() + const basedOnQuery = await config.createQuery({ + ...basicQuery(datasource._id), + fields: { + path: "www.google.com", + }, + }) + await config.updateDatasource({ + ...datasource, + config: { + dynamicVariables: [ + { queryId: basedOnQuery._id, name: "variable3", value: "{{ data.0.[value] }}" } + ] + } + }) + const res = await request + .post(`/api/queries/preview`) + .send({ + datasourceId: datasource._id, + parameters: {}, + fields: { + path: "www.google.com", + queryString: "test={{ variable3 }}", + }, + queryVerb: "read", + }) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body.schemaFields).toEqual(["url", "opts", "value"]) + expect(res.body.rows[0].url).toContain("doctype html") + }) + }) }) diff --git a/packages/server/src/definitions/datasource.ts b/packages/server/src/definitions/datasource.ts index 148d8f2875..d281f94c8b 100644 --- a/packages/server/src/definitions/datasource.ts +++ b/packages/server/src/definitions/datasource.ts @@ -240,6 +240,16 @@ export interface RestConfig { [key: string]: any } authConfigs: AuthConfig[] + staticVariables: { + [key: string]: string + } + dynamicVariables: [ + { + name: string + queryId: string + value: string + } + ] } export interface Query { diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.js index 8f3583443c..f003a8a78d 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.js @@ -316,6 +316,16 @@ class TestConfiguration { return this.datasource } + async updateDatasource(datasource) { + const response = await this._req( + datasource, + { datasourceId: datasource._id }, + controllers.datasource.update + ) + this.datasource = response.datasource + return this.datasource + } + async createQuery(config = null) { if (!this.datasource && !config) { throw "No data source created for query."