Skip to content

Commit

Permalink
Mock out http calls with record/replay functionality
Browse files Browse the repository at this point in the history
* Use talkback to record any http calls to a real server
  * Record them to the tapes/ directory
* This means it can be tested by those who don't have a server up and
running.
* Can remove the hidden variables in travis
* Will do real api calls when tapes don't exist and record new ones

Not all tapes committed cause I was concerned about leaking steamid and ips
  • Loading branch information
halkeye authored and niekcandaele committed Apr 27, 2020
1 parent b47fba7 commit 6cd0926
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 207 deletions.
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -34,6 +34,7 @@
"istanbul": "^0.4.5",
"mocha": "^7.0.0",
"shx": "^0.3.0",
"talkback": "^2.1.1",
"ts-node": "^8.3.0",
"typedoc": "^0.17.4",
"typescript": "^3.8.3"
Expand Down Expand Up @@ -75,4 +76,4 @@
"pre-push": "npm run lint"
}
}
}
}
84 changes: 84 additions & 0 deletions test/_globals.ts
@@ -0,0 +1,84 @@
/* eslint-env mocha */
/*
* Shareable items between tests
*/
import { URL } from 'url';
import * as querystring from 'querystring';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import TalkbackServer from 'talkback/server';
import { RecordMode } from 'talkback/options';
import Tape from 'talkback/tape';
import { Req } from 'talkback/types';
import { SdtdServer } from '../lib/index';

// talkback's typescript/import returns different stuff than the require, so disable the eslint alert for now
//eslint-disable-next-line @typescript-eslint/no-var-requires
const talkback = require('talkback');

class TestGlobals {
testServer: SdtdServer;
proxy: TalkbackServer;

constructor() {
this.testServer = {
ip: "localhost",
port: "5544",
adminUser: 'fakeadminuser',
adminToken: 'fakeadmintoken'
};
const host = `http://${process.env.TESTIP}:${process.env.TESTPORT}`;
this.proxy = talkback({
host: host,
record: RecordMode.NEW,
port: 5544,
path: `${__dirname}/tapes/`,
silent: true,
ignoreQueryParams: ['adminuser', 'admintoken'],
tapeNameGenerator: (tapeNumber: number, tape: Tape): string => {
const url = new URL(tape.req.url, host);
const query = querystring.parse(url.search.substr(1));
delete query.adminuser;
delete query.admintoken;
return url.pathname.replace(/\//g, '_') + '_' + querystring.stringify(query, '_', '_') + ".json5";
},
requestDecorator: function(req: Req) {
const url = new URL(req.url, host);
const query = querystring.parse(url.search.substr(1));
query.adminuser = process.env.TESTADMINUSER || 'unknown';
query.admintoken = process.env.TESTADMINTOKEN || 'unknown';
url.search = '?' + querystring.stringify(query);
req.url = url.toString().replace(host, '');
return req;
}
});
}

setTestServer(testServer: SdtdServer): void {
this.testServer = testServer;
}
getTestServer(): SdtdServer {
return this.testServer;
}

setProxy(proxy: TalkbackServer): void {
this.proxy = proxy;
}
getProxy(): TalkbackServer {
return this.proxy;
}
}

const testGlobals = new TestGlobals();
export default testGlobals;

require('dotenv').config();

chai.use(chaiAsPromised);

before(function(done) {
testGlobals.getProxy().start(() => done());
});
after(function() {
testGlobals.getProxy().close();
});
16 changes: 5 additions & 11 deletions test/executeConsoleCommand.test.ts
@@ -1,36 +1,30 @@
'use strict';
import g from './_globals';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { executeConsoleCommand, SdtdServer } from '../lib/index';
import { executeConsoleCommand } from '../lib/index';
import { fail } from 'assert';

require('dotenv').config();

chai.use(chaiAsPromised);

const testServer: SdtdServer = {
ip: process.env.TESTIP as string,
port: process.env.TESTPORT as string,
adminUser: process.env.TESTADMINUSER as string,
adminToken: process.env.TESTADMINTOKEN as string
};

describe('/api/executeconsolecommand', async () => {
it('Returns command, parameters and result', async () => {
const response = await executeConsoleCommand(testServer, "help");
const response = await executeConsoleCommand(g.getTestServer(), "help");
chai.expect(response.command).to.be.a('string');
chai.expect(response.parameters).to.be.a('string');
});

it('Executes a command correctly', async () => {
const response = await executeConsoleCommand(testServer, "help");
const response = await executeConsoleCommand(g.getTestServer(), "help");
chai.expect(response.command).to.eq('help');
chai.expect(response.parameters).to.eq('');
chai.expect(response.result).to.include("*** Generic Console Help ***");
});
it('Executes a an unknown command - foobar', async () => {
try {
await executeConsoleCommand(testServer, "foobar");
await executeConsoleCommand(g.getTestServer(), "foobar");
fail('should have failed');
} catch (e) {
chai.expect(e).to.be.a('error');
Expand Down
17 changes: 3 additions & 14 deletions test/getAllowedCommands.test.ts
@@ -1,22 +1,11 @@
'use strict';
import g from './_globals';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { getAllowedCommands, SdtdServer } from '../lib/index';

require('dotenv').config();

chai.use(chaiAsPromised);

const testServer: SdtdServer = {
ip: process.env.TESTIP as string,
port: process.env.TESTPORT as string,
adminUser: process.env.TESTADMINUSER as string,
adminToken: process.env.TESTADMINTOKEN as string
};
import { getAllowedCommands } from '../lib/index';

describe('/api/getAllowedCommands', async () => {
it('Returns an array', async () => {
const response = await getAllowedCommands(testServer);
const response = await getAllowedCommands(g.getTestServer());
chai.expect(response.commands).to.be.a('array');
});
});
19 changes: 4 additions & 15 deletions test/getAnimalsLocation.test.ts
@@ -1,28 +1,17 @@
'use strict';
import g from './_globals';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { getAnimalsLocation, SdtdServer } from '../lib/index';

require('dotenv').config();

chai.use(chaiAsPromised);

const testServer: SdtdServer = {
ip: process.env.TESTIP as string,
port: process.env.TESTPORT as string,
adminUser: process.env.TESTADMINUSER as string,
adminToken: process.env.TESTADMINTOKEN as string
};
import { getAnimalsLocation } from '../lib/index';

describe('/api/getAnimalsLocation', async () => {
it('Returns an array of animal info', async () => {
const response = await getAnimalsLocation(testServer);
const response = await getAnimalsLocation(g.getTestServer());
chai.expect(response).to.be.an('array');
});


it('Contains expected information', async () => {
const response = await getAnimalsLocation(testServer);
const response = await getAnimalsLocation(g.getTestServer());
if (response.length > 0) {
const animal = response[0];
chai.expect(animal.id).to.be.a('number');
Expand Down
4 changes: 1 addition & 3 deletions test/getBaseUrl.test.ts
@@ -1,10 +1,8 @@
'use strict';
import './_globals';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { getBaseUrl } from '../lib/index';

chai.use(chaiAsPromised);

describe('getBaseUrl', async () => {
it('standard returns http', async () => {
chai.expect(
Expand Down
19 changes: 4 additions & 15 deletions test/getHostileLocation.test.ts
@@ -1,28 +1,17 @@
'use strict';
import g from './_globals';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { getHostileLocation, SdtdServer } from '../lib/index';

require('dotenv').config();

chai.use(chaiAsPromised);

const testServer: SdtdServer = {
ip: process.env.TESTIP as string,
port: process.env.TESTPORT as string,
adminUser: process.env.TESTADMINUSER as string,
adminToken: process.env.TESTADMINTOKEN as string
};
import { getHostileLocation } from '../lib/index';

describe('/api/getHostileLocation', async () => {
it('Returns an array of hostile info', async () => {
const response = await getHostileLocation(testServer);
const response = await getHostileLocation(g.getTestServer());
chai.expect(response).to.be.an('array');
});


it('Contains expected information', async () => {
const response = await getHostileLocation(testServer);
const response = await getHostileLocation(g.getTestServer());
if (response.length > 0) {
const animal = response[0];
chai.expect(animal.id).to.be.a('number');
Expand Down
17 changes: 3 additions & 14 deletions test/getLandClaims.test.ts
@@ -1,22 +1,11 @@
'use strict';
import g from './_globals';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { getLandClaims, SdtdServer } from '../lib/index';

require('dotenv').config();

chai.use(chaiAsPromised);

const testServer: SdtdServer = {
ip: process.env.TESTIP as string,
port: process.env.TESTPORT as string,
adminUser: process.env.TESTADMINUSER as string,
adminToken: process.env.TESTADMINTOKEN as string
};
import { getLandClaims } from '../lib/index';

describe('/api/getLandClaims', async () => {
it('Returns an array of animal info', async () => {
const response = await getLandClaims(testServer);
const response = await getLandClaims(g.getTestServer());
chai.expect(response.claimowners).to.be.a('array');
chai.expect(response.claimsize).to.be.a('number');
});
Expand Down

0 comments on commit 6cd0926

Please sign in to comment.