Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 25 additions & 42 deletions tests/gqltest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require("dotenv").config();
const fetch = require("node-fetch");
const { expect } = require("chai");
const { execSync } = require("child_process");
const { URL } = require("url");
const path = require("node:path");

const {
GQLHeaders,
executeOK,
logOnFail,
} = require('gqltest/packages/gqltest/gqltest.js')

const authTypes = {
adminKey: 1,
apiKey: 2,
Expand All @@ -15,10 +19,8 @@ Object.freeze(authTypes);

// We use admin key to test because there is a cache optimization for apikey's that is not conducive
// to rapid deploy and run cycles that occur with this type of testing
const adminKey =
`apikey ` + execSync(`stepzen whoami --adminkey`).toString().trim();
const apiKey =
`apikey ` + execSync(`stepzen whoami --apikey`).toString().trim();
const adminKey = execSync(`stepzen whoami --adminkey`).toString().trim();
const apiKey = execSync(`stepzen whoami --apikey`).toString().trim();

const endpoint = process.env.STEPZEN_ENDPOINT;

Expand All @@ -45,49 +47,32 @@ function deployEndpoint(endpoint, dirname) {
// as a test returning the response.
// The test will fail if the request does not
// have status 200 or has any GraphQL errors.
function runGqlOk(authType, endpoint, query, variables, operationName) {
async function runGqlOk(authType, endpoint, query, variables, operationName, expected) {
let headers = new GQLHeaders();
switch (authType) {
case authTypes.adminKey:
authValue = adminKey;
headers.withAPIKey(adminKey);
break;
case authTypes.apiKey:
authValue = apiKey;
headers.withAPIKey(apiKey);
break;
// Have not implemented jwt and noAuth yet
case authTypes.jwt:
case authTypes.noAuth:
default:
authValue = "";
}
return fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: authValue,
},
body: JSON.stringify({
await executeOK({
test: this,
endpoint,
headers,
request: {
query: query,
variables: variables,
operationName: operationName,
}),
},
expected,
})
.then(function (result) {
expect(result.status).to.equal(200);
return result;
})
.then(function (result) {
return result.json();
})
.then(function (response) {
expect(response.errors, `no errors should exist: ${JSON.stringify(response.errors)}`).to.be.undefined;
return response;
});
}

// tests that the data key in a GraphQL response
// is equal to value.
function expectData(response, value) {
expect(response.data).to.eql(value);
}

// deploys graphql schema located in dirname to the test endpoint provided by the environment (process.env.STEPZEN_ENDPOINT),
Expand All @@ -101,19 +86,19 @@ function deployAndRun(dirname, tests) {
return deployEndpoint(endpoint, dirname);
});

afterEach('log-failure', logOnFail)
tests.forEach(
({ label, query, variables, operationName, expected, authType }) => {
it(label, function () {
it(label, async function () {
this.timeout(4000); // Occasional requests take > 2s
return runGqlOk(
return await runGqlOk(
authType,
endpoint,
query,
variables,
operationName
).then(function (response) {
expectData(response, expected);
});
operationName,
expected,
);
});
}
);
Expand All @@ -127,8 +112,6 @@ function getTestDescription(testRoot, fullDirName) {
return segments.slice(rootIndex + 1, -1).join("/");
}

exports.runGqlOk = runGqlOk;
exports.expectData = expectData;
exports.deployAndRun = deployAndRun;
exports.authTypes = authTypes;
exports.getTestDescription = getTestDescription;
Loading