aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/test/test_runner.ts
blob: 294cc35d324079ca257693febf9cb57611231d41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;
    }
}