From 6e156194d1dd15745ede76a9dd388dcf6ae0a693 Mon Sep 17 00:00:00 2001 From: MohammedZuhairAhmed Date: Mon, 20 May 2024 17:57:38 +0530 Subject: [PATCH] FEAT : Integration tests completed --- jest.config.js | 1 + package-lock.json | 13 + package.json | 1 + .../integration/generateTS/generateTS.test.ts | 258 +++++------------- tests/integration/graphqlTS/graphqlTS.test.ts | 150 +++------- 5 files changed, 114 insertions(+), 309 deletions(-) diff --git a/jest.config.js b/jest.config.js index c550ee6..804ffdf 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,4 +4,5 @@ module.exports = { transform: { "^.+\\.(ts|tsx)$": "ts-jest", }, + setupFiles: ["dotenv/config"], }; diff --git a/package-lock.json b/package-lock.json index cc090de..9f385a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "@types/lodash": "^4.17.0", "@types/node": "^20.12.7", "cross-env": "^7.0.3", + "dotenv": "^16.4.5", "jest": "^29.7.0", "nock": "^13.5.4", "ts-jest": "^29.1.2", @@ -2856,6 +2857,18 @@ "url": "https://github.com/fb55/domutils?sponsor=1" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "dev": true, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/package.json b/package.json index 24873b6..383b89d 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@types/lodash": "^4.17.0", "@types/node": "^20.12.7", "cross-env": "^7.0.3", + "dotenv": "^16.4.5", "jest": "^29.7.0", "nock": "^13.5.4", "ts-jest": "^29.1.2", diff --git a/tests/integration/generateTS/generateTS.test.ts b/tests/integration/generateTS/generateTS.test.ts index ef8b818..6bca489 100644 --- a/tests/integration/generateTS/generateTS.test.ts +++ b/tests/integration/generateTS/generateTS.test.ts @@ -1,39 +1,15 @@ import { generateTS } from "../../../src/generateTS/index"; -import { contentTypes, globalFields } from "../mock"; -import nock from "nock"; - -type RegionUrlMap = { - [prop: string]: string; -}; - -const REGION_URL_MAPPING: RegionUrlMap = { - US: "https://cdn.contentstack.io", - EU: "https://eu-cdn.contentstack.com", - AZURE_NA: "https://azure-na-cdn.contentstack.com", - AZURE_EU: "https://azure-eu-cdn.contentstack.com", - GCP_NA: "https://gcp-na-cdn.contentstack.com", -}; +const dotenv = require("dotenv"); +dotenv.config({ path: "../../../.env" }); describe("generateTS function", () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it("generates type definitions", async () => { - const token = "valid-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; - const branch = "main"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, contentTypes); - - nock(REGION_URL_MAPPING[region]) - .get("/v3/global_fields?include_branch=false") - .reply(200, globalFields); + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; const generatedTS = await generateTS({ token, @@ -51,21 +27,13 @@ describe("generateTS function", () => { }); it("generates type definitions without Documentation", async () => { - const token = "valid-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; const includeDocumentation = false; - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, contentTypes); - - nock(REGION_URL_MAPPING[region]) - .get("/v3/global_fields?include_branch=false") - .reply(200, globalFields); - const generatedTS = await generateTS({ token, apiKey, @@ -82,21 +50,13 @@ describe("generateTS function", () => { }); it("generates type definitions with prefix", async () => { - const token = "valid-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; const prefix = "test"; - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, contentTypes); - - nock(REGION_URL_MAPPING[region]) - .get("/v3/global_fields?include_branch=false") - .reply(200, globalFields); - const generatedTS = await generateTS({ token, apiKey, @@ -113,21 +73,13 @@ describe("generateTS function", () => { }); it("generates type definitions with system fields", async () => { - const token = "valid-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; const systemFields = true; - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, contentTypes); - - nock(REGION_URL_MAPPING[region]) - .get("/v3/global_fields?include_branch=false") - .reply(200, globalFields); - const generatedTS = await generateTS({ token, apiKey, @@ -147,17 +99,13 @@ describe("generateTS function", () => { }); describe("generateTS function with errors", () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it("Check for if all the required fields are provided", async () => { const token = ""; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; - const branch = "main"; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await generateTS({ @@ -176,12 +124,12 @@ describe("generateTS function with errors", () => { }); it("Check for Invalid region", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "demo" as unknown as any; - const tokenType = "delivery"; - const branch = "main"; + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = "wrong" as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await generateTS({ @@ -200,16 +148,12 @@ describe("generateTS function with errors", () => { }); it("Check for empty content-type response", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; - const branch = "main"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, { content_types: [] }); + const token = process.env.TOKEN_WITH_NO_CT as unknown as any; + const apiKey = process.env.APIKEY_WITH_NO_CT as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await generateTS({ @@ -228,16 +172,12 @@ describe("generateTS function with errors", () => { }); it("Check for invalid api_key", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "GCP_NA"; - const tokenType = "delivery"; - const branch = "main"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(401); + const token = process.env.TOKEN as unknown as any; + const apiKey = "process.env.APIKEY" as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await generateTS({ @@ -250,22 +190,18 @@ describe("generateTS function with errors", () => { }); } catch (err: any) { expect(err.error_message).toEqual( - "Unauthorized: The apiKey, token or region is not valid." + "Invalid Credentials: Please check the provided apiKey, token and region." ); } }); it("Check for invalid delivery token", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "AZURE_EU"; - const tokenType = "delivery"; - const branch = "main"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(412); + const token = "csqw046a21crf2152b87d64r" as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await generateTS({ @@ -278,26 +214,18 @@ describe("generateTS function with errors", () => { }); } catch (err: any) { expect(err.error_message).toEqual( - "Invalid Credentials: Please check the provided apiKey, token and region." + "Unauthorized: The apiKey, token or region is not valid." ); } }); it("Check for default error", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "AZURE_NA"; - const tokenType = "delivery"; - const branch = "mai"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(422, { - error_message: - "Access denied. You have insufficient permissions to perform operation on this branch 'mai'.", - error_code: 901, - }); + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = "mai" as unknown as any; try { await generateTS({ @@ -315,53 +243,13 @@ describe("generateTS function with errors", () => { } }); - it("Check for TSGEN factory error", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "EU"; - const tokenType = "delivery"; - const branch = "main"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, contentTypes); - - nock(REGION_URL_MAPPING[region]) - .get("/v3/global_fields?include_branch=false") - .reply(200, { global_fields: [] }); - - try { - await generateTS({ - token, - apiKey, - environment, - region, - tokenType, - branch, - }); - } catch (err: any) { - expect(err.error_message).toEqual( - "Something went wrong, Schema not found for global field 'global_field. Did you forget to include it?" - ); - } - }); - - it("Check for global fields error", async () => { - const token = "your-token"; - const apiKey = "your-api-key"; - const environment = "development"; - const region = "US"; - const tokenType = "delivery"; - const branch = "main"; - - nock(REGION_URL_MAPPING[region]) - .get(`/v3/content_types/?environment=${environment}`) - .reply(200, contentTypes); - - nock(REGION_URL_MAPPING[region]) - .get("/v3/global_fields?include_branch=false") - .reply(401); + it("Check for default error", async () => { + const token = "process.env.TOKEN" as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const tokenType = process.env.TOKENTYPE as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await generateTS({ @@ -373,17 +261,7 @@ describe("generateTS function with errors", () => { branch, }); } catch (err: any) { - expect(err.error_message).toEqual( - "Unauthorized: The apiKey, token or region is not valid." - ); + expect(err.error_message).toEqual("Something went wrong, Bad Request"); } }); }); - -afterAll(() => { - nock.restore(); -}); - -afterEach(() => { - nock.cleanAll(); -}); diff --git a/tests/integration/graphqlTS/graphqlTS.test.ts b/tests/integration/graphqlTS/graphqlTS.test.ts index 725f59c..b17ac91 100644 --- a/tests/integration/graphqlTS/graphqlTS.test.ts +++ b/tests/integration/graphqlTS/graphqlTS.test.ts @@ -1,35 +1,14 @@ import { graphqlTS } from "../../../src/graphqlTS/index"; -import nock from "nock"; -import { graphql, invalid_graphql } from "../mock"; - -type RegionUrlMap = { - [prop: string]: string; -}; - -const GRAPHQL_REGION_URL_MAPPING: RegionUrlMap = { - US: "https://graphql.contentstack.com/stacks", - EU: "https://eu-graphql.contentstack.com/stacks", - AZURE_NA: "https://azure-na-graphql.contentstack.com/stacks", - AZURE_EU: "https://azure-eu-graphql.contentstack.com/stacks", - GCP_NA: "https://gcp-na-graphql.contentstack.com/stacks", - "wrong-region": "https://demo.com", -}; +const dotenv = require("dotenv"); +dotenv.config({ path: "../../../.env" }); describe("graphqlTS function", () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it("generates graphQL typeDef without namespace", async () => { - const token = "my-token"; - const apiKey = "my-api-key"; - const environment = "development"; - const region = "US"; - const branch = "main"; - - nock(GRAPHQL_REGION_URL_MAPPING[region]) - .post(`/${apiKey}?environment=${environment}`) - .reply(200, graphql); + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const branch = process.env.BRANCH as unknown as any; const generatedGraphql = await graphqlTS({ token, @@ -43,17 +22,13 @@ describe("graphqlTS function", () => { }); it("generates graphQL typeDef with namespace", async () => { - const token = "my-token"; - const apiKey = "my-api-key"; - const environment = "development"; - const region = "US"; - const branch = "main"; + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const branch = process.env.BRANCH as unknown as any; const namespace = "demo"; - nock(GRAPHQL_REGION_URL_MAPPING[region]) - .post(`/${apiKey}?environment=${environment}`) - .reply(200, graphql); - const generatedGraphql = await graphqlTS({ token, apiKey, @@ -68,16 +43,12 @@ describe("graphqlTS function", () => { }); describe("graphqlTS function with errors", () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it("check for whether all the required params are provided", async () => { - const token = ""; - const apiKey = "my-api-key"; - const environment = "development"; - const region = "US"; - const branch = "main"; + const token = "" as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await graphqlTS({ @@ -95,15 +66,11 @@ describe("graphqlTS function with errors", () => { }); it("check for if wrong apiKey, token and environment is provided", async () => { - const token = "my-token"; - const apiKey = "my-api-key"; - const environment = "development"; - const region = "US"; - const branch = "main"; - - nock(GRAPHQL_REGION_URL_MAPPING[region]) - .post(`/${apiKey}?environment=${environment}`) - .reply(401); + const token = process.env.TOKEN as unknown as any; + const apiKey = "process.env.APIKEY" as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const branch = process.env.BRANCH as unknown as any; try { await graphqlTS({ @@ -121,11 +88,11 @@ describe("graphqlTS function with errors", () => { }); it("check for if wrong region is provided", async () => { - const token = "my-token"; - const apiKey = "my-api-key"; - const environment = "development"; + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; const region = "wrong-region" as unknown as any; - const branch = "main"; + const branch = process.env.BRANCH as unknown as any; try { await graphqlTS({ @@ -142,59 +109,12 @@ describe("graphqlTS function with errors", () => { } }); - it("check for errors from gql2ts", async () => { - const token = "my-token"; - const apiKey = "my-api-key"; - const environment = "development"; - const region = "US"; - const branch = "main"; - - nock(GRAPHQL_REGION_URL_MAPPING[region]) - .post(`/${apiKey}?environment=${environment}`) - .reply(200, invalid_graphql); - - try { - await graphqlTS({ - token, - apiKey, - environment, - region, - branch, - }); - } catch (err: any) { - expect(err.error_message).toEqual( - "Invalid or incomplete schema, unknown type: Query. Ensure that a full introspection query is used in order to build a client schema." - ); - } - }); - it("check for graphql api errors", async () => { - const token = "my-token"; - const apiKey = "my-api-key"; - const environment = "development"; - const region = "US"; - const branch = "mai"; - - nock(GRAPHQL_REGION_URL_MAPPING[region]) - .post(`/${apiKey}?environment=${environment}`) - .reply(422, { - errors: [ - { - message: "Failed to run query.", - extensions: { - errors: [ - { - code: "INVALID_BRANCH", - message: "The queried branch 'mai' is invalid.", - details: { - hint: "The queried branch does not exist in our records. Contact your stack admin for branch name and details.", - }, - }, - ], - }, - }, - ], - }); + const token = process.env.TOKEN as unknown as any; + const apiKey = process.env.APIKEY as unknown as any; + const environment = process.env.ENVIRONMENT as unknown as any; + const region = process.env.REGION as unknown as any; + const branch = "mai" as unknown as any; try { await graphqlTS({ @@ -209,11 +129,3 @@ describe("graphqlTS function with errors", () => { } }); }); - -afterAll(() => { - nock.restore(); -}); - -afterEach(() => { - nock.cleanAll(); -});