aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/test/test_runner.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-03-20 19:52:22 +0800
committerFabio Berger <me@fabioberger.com>2018-03-20 19:52:22 +0800
commit2f7c19e80d31cbcc09c6e87841682e8f35f0ecd5 (patch)
tree60d846e22cf80416f0dd43ead8dd494c5735152f /packages/sra-report/test/test_runner.ts
parent5468358f2142549c68ffae0085ce7d31ea271ea4 (diff)
parent7d9c357409c25996f9c448dffd69007f69b9d8d6 (diff)
downloaddexon-0x-contracts-2f7c19e80d31cbcc09c6e87841682e8f35f0ecd5.tar.gz
dexon-0x-contracts-2f7c19e80d31cbcc09c6e87841682e8f35f0ecd5.tar.zst
dexon-0x-contracts-2f7c19e80d31cbcc09c6e87841682e8f35f0ecd5.zip
Merge branch 'development' into addExtraDocs
* development: (29 commits) Set Lodash dep to an exact version since newer versions introduced breaking changes Update Kovan Ethertoken Address Update deployer version since manual re-publish Add new entry into CHANGELOG Fix bad merge Update yarn.lock Change title Add Blake and Zach to About page Re-size Jacob and Tom's images Manually publish 0x.js back to a working state Publish Publish Fix 0x.js assets Remove assets from connect and _bundles from packages that don't generate the folder Publish Fix packages that aren't working as expected Make new packages default to public on publish Add new public packages to top-level README Update top-level package.json Fix incorrect new versions ... # Conflicts: # packages/0x.js/package.json # packages/connect/package.json # packages/json-schemas/package.json # packages/sol-cov/package.json
Diffstat (limited to 'packages/sra-report/test/test_runner.ts')
-rw-r--r--packages/sra-report/test/test_runner.ts138
1 files changed, 138 insertions, 0 deletions
diff --git a/packages/sra-report/test/test_runner.ts b/packages/sra-report/test/test_runner.ts
new file mode 100644
index 000000000..bf9b923e7
--- /dev/null
+++ b/packages/sra-report/test/test_runner.ts
@@ -0,0 +1,138 @@
+import * as chai from 'chai';
+import * as chaiAsPromised from 'chai-as-promised';
+import * as dirtyChai from 'dirty-chai';
+import * as _ from 'lodash';
+import 'mocha';
+import { NewmanRunExecution, NewmanRunExecutionAssertion, NewmanRunSummary } from 'newman';
+import * as nock from 'nock';
+
+import * as sraReportCollectionJSON from '../../postman_collections/sra_report.postman_collection.json';
+import { utils } from '../src/utils';
+
+import * as postmanEnvironmentJSON from './environments/postman_environment.json';
+
+chai.config.includeStack = true;
+chai.use(dirtyChai);
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+const CONTENT_TYPE_ASSERTION_NAME = 'Has Content-Type header with value application/json';
+const SCHEMA_ASSERTION_NAME = 'Schema is valid';
+const baseNewmanRunOptions = {
+ collection: sraReportCollectionJSON,
+ environment: postmanEnvironmentJSON,
+ reporter: {
+ cli: {
+ noConsole: true,
+ },
+ },
+};
+
+export const testRunner = {
+ runContentTypeTests(
+ nockInterceptor: nock.Interceptor,
+ postmanCollectionFolderName: string,
+ postmanCollectionRequestName: string,
+ ) {
+ const newmanRunOptions = {
+ ...baseNewmanRunOptions,
+ folder: postmanCollectionFolderName,
+ };
+ describe(CONTENT_TYPE_ASSERTION_NAME, () => {
+ it('fails when there are no headers', async () => {
+ nockInterceptor.reply(200, {});
+ const summary = await utils.newmanRunAsync(newmanRunOptions);
+ const error = findAssertionErrorIfExists(
+ summary,
+ postmanCollectionRequestName,
+ CONTENT_TYPE_ASSERTION_NAME,
+ );
+ const errorMessage = _.get(error, 'message');
+ expect(error).to.not.be.undefined();
+ expect(errorMessage).to.equal(`expected response to have header with key 'Content-Type'`);
+ });
+ it('fails when Content-Type header exists but not with value application/json', async () => {
+ const headers = {
+ 'Content-Type': 'text/html',
+ };
+ nockInterceptor.reply(200, {}, headers);
+ const summary = await utils.newmanRunAsync(newmanRunOptions);
+ const error = findAssertionErrorIfExists(
+ summary,
+ postmanCollectionRequestName,
+ CONTENT_TYPE_ASSERTION_NAME,
+ );
+ const errorMessage = _.get(error, 'message');
+ expect(error).to.not.be.undefined();
+ expect(errorMessage).to.equal(`expected 'text/html' to include 'application/json'`);
+ });
+ it('passes when Content-Type header exists with value application/json', async () => {
+ const headers = {
+ 'Content-Type': 'charset=utf-8; application/json',
+ };
+ nockInterceptor.reply(200, {}, headers);
+ const summary = await utils.newmanRunAsync(newmanRunOptions);
+ const error = findAssertionErrorIfExists(
+ summary,
+ postmanCollectionRequestName,
+ CONTENT_TYPE_ASSERTION_NAME,
+ );
+ expect(error).to.be.undefined();
+ });
+ });
+ },
+ runSchemaTests(
+ nockInterceptor: nock.Interceptor,
+ postmanCollectionFolderName: string,
+ postmanCollectionRequestName: string,
+ malformedJson: object,
+ correctJson: object,
+ ) {
+ const newmanRunOptions = {
+ ...baseNewmanRunOptions,
+ folder: postmanCollectionFolderName,
+ };
+ describe(SCHEMA_ASSERTION_NAME, () => {
+ it('fails when schema is invalid', async () => {
+ nockInterceptor.reply(200, malformedJson);
+ const summary = await utils.newmanRunAsync(newmanRunOptions);
+ const error = findAssertionErrorIfExists(summary, postmanCollectionRequestName, SCHEMA_ASSERTION_NAME);
+ const errorMessage = _.get(error, 'message');
+ expect(error).to.not.be.undefined();
+ expect(errorMessage).to.equal('expected false to be true');
+ });
+ it('passes when schema is valid', async () => {
+ nockInterceptor.reply(200, correctJson);
+ const summary = await utils.newmanRunAsync(newmanRunOptions);
+ const error = findAssertionErrorIfExists(summary, postmanCollectionRequestName, SCHEMA_ASSERTION_NAME);
+ const errorMessage = _.get(error, 'message');
+ expect(error).to.be.undefined();
+ });
+ });
+ },
+};
+
+function findAssertionErrorIfExists(
+ summary: NewmanRunSummary,
+ postmanCollectionRequestName: string,
+ postmanCollectionAssertionName: string,
+) {
+ const matchingExecutionIfExists = _.find(summary.run.executions, (execution: NewmanRunExecution) => {
+ return execution.item.name === postmanCollectionRequestName;
+ });
+ if (_.isUndefined(matchingExecutionIfExists)) {
+ return undefined;
+ }
+ const matchingAssertionIfExists = _.find(
+ matchingExecutionIfExists.assertions,
+ (assertion: NewmanRunExecutionAssertion) => {
+ return assertion.assertion === postmanCollectionAssertionName;
+ },
+ );
+ if (_.isUndefined(matchingAssertionIfExists)) {
+ return undefined;
+ } else {
+ const error = matchingAssertionIfExists.error;
+ return error;
+ }
+}