Skip to content

Commit 74e9de0

Browse files
authored
Merge pull request #185 from StackStorm/add-tests
Add tests to improve coverage for scripts/stackstorm.js
2 parents a124052 + 86d85da commit 74e9de0

File tree

5 files changed

+180
-6
lines changed

5 files changed

+180
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"babel-eslint": "^10.0.1",
3838
"chai": "^4.2.0",
3939
"chai-as-promised": "^7.1.1",
40+
"chai-string": "^1.5.0",
4041
"eslint": "^5.16.0",
4142
"eslint-plugin-notice": "0.7.8",
4243
"gulp": "^3.9.1",

tests/test-st2-invalid-auth.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
Licensed to the StackStorm, Inc ('StackStorm') under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
/*jshint quotmark:false*/
19+
/*jshint -W030*/
20+
/*global describe, it, before, after, beforeEach, afterEach*/
21+
'use strict';
22+
23+
var chai = require("chai"),
24+
expect = chai.expect,
25+
chaiString = require("chai-string"),
26+
sinon = require('sinon'),
27+
sinonChai = require('sinon-chai'),
28+
chaiAsPromised = require('chai-as-promised'),
29+
mockedEnv = require('mocked-env'),
30+
Robot = require("hubot/src/robot"),
31+
Logger = require('./dummy-logger.js');
32+
33+
chai.use(sinonChai);
34+
chai.use(chaiString);
35+
chai.use(chaiAsPromised);
36+
37+
global.process.exit = sinon.spy();
38+
39+
describe("invalid st2 credential configuration", function() {
40+
var robot = new Robot(null, "mock-adapter", false, "Hubot");
41+
robot.logger = new Logger(true);
42+
var restore_env = null,
43+
info_spy = sinon.spy(robot.logger, 'info'),
44+
error_spy = sinon.spy(robot.logger, 'error');
45+
46+
beforeEach(function () {
47+
process.exit.resetHistory();
48+
});
49+
50+
afterEach(function() {
51+
restore_env && restore_env();
52+
error_spy.resetHistory();
53+
info_spy.resetHistory();
54+
// Remove stackstorm.js from the require cache
55+
// https://medium.com/@gattermeier/invalidate-node-js-require-cache-c2989af8f8b0
56+
delete require.cache[require.resolve("../scripts/stackstorm.js")];
57+
});
58+
59+
it("should error out with missing auth URL", function(done) {
60+
// Mock process.env for all modules
61+
// https://glebbahmutov.com/blog/mocking-process-env/
62+
restore_env = mockedEnv({
63+
ST2_AUTH_USERNAME: 'nonexistent-st2-auth-username',
64+
ST2_AUTH_PASSWORD: 'nonexistent-st2-auth-password'
65+
});
66+
67+
// Load script under test
68+
var stackstorm = require("../scripts/stackstorm.js");
69+
try {
70+
stackstorm(robot);
71+
done(new Error("The previous code should have thrown an exception"))
72+
} catch (err) {
73+
expect(err.message).to.equal("Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.");
74+
done();
75+
}
76+
});
77+
78+
it("should error out with missing auth username", function(done) {
79+
// Mock process.env for all modules
80+
// https://glebbahmutov.com/blog/mocking-process-env/
81+
restore_env = mockedEnv({
82+
ST2_AUTH_URL: 'https://nonexistent-st2-auth-url',
83+
ST2_AUTH_PASSWORD: 'nonexistent-st2-auth-password'
84+
});
85+
86+
// Load script under test
87+
var stackstorm = require("../scripts/stackstorm.js");
88+
try {
89+
stackstorm(robot);
90+
done(new Error("The previous code should have thrown an exception"))
91+
} catch (err) {
92+
expect(err.message).to.equal("Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.");
93+
done();
94+
}
95+
});
96+
97+
98+
it("should error out with missing auth password", function(done) {
99+
// Mock process.env for all modules
100+
// https://glebbahmutov.com/blog/mocking-process-env/
101+
restore_env = mockedEnv({
102+
ST2_AUTH_URL: 'https://nonexistent-st2-auth-url',
103+
ST2_AUTH_USERNAME: 'nonexistent-st2-auth-username'
104+
});
105+
106+
// Load script under test
107+
var stackstorm = require("../scripts/stackstorm.js");
108+
try {
109+
stackstorm(robot);
110+
done(new Error("The previous code should have thrown an exception"))
111+
} catch (err) {
112+
expect(err.message).to.equal("Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.");
113+
done();
114+
}
115+
});
116+
117+
it("should throw exception with bad auth URL", function(done) {
118+
restore_env = mockedEnv({
119+
ST2_AUTH_URL: 'https://nonexistent-st2-auth-url:9101',
120+
ST2_AUTH_USERNAME: 'nonexistent-st2-auth-username',
121+
ST2_AUTH_PASSWORD: 'nonexistent-st2-auth-password'
122+
});
123+
124+
// Load script under test
125+
var i, stackstorm = require("../scripts/stackstorm.js");
126+
stackstorm(robot).catch(function (err) {
127+
expect(error_spy.args).to.have.lengthOf(1);
128+
expect(error_spy.args[0][0]).to.be.a('string');
129+
expect(error_spy.args[0][0]).to.startWith('Failed to authenticate');
130+
131+
// On Mac, this is:
132+
// getaddrinfo ENOTFOUND nonexistent-st2-auth-url nonexistent-st2-auth-url:9101
133+
// But on Linux, it is:
134+
//
135+
// So instead of using basic string comparison, we use a regex and check for common substrings
136+
expect(err.message).to.match(/getaddrinfo ENOTFOUND nonexistent-st2-auth-url nonexistent-st2-auth-url:9101/);
137+
138+
done();
139+
});
140+
});
141+
});

tests/test-st2-unauthorized.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ chai.use(sinonChai);
3030
describe("auth with invalid st2 API key", function() {
3131
var stop;
3232
var robot = new Robot(null, "mock-adapter", true, "Hubot");
33+
robot.logger = new Logger(true);
3334
var recordedError = null,
3435
error_spy = sinon.spy(robot.logger, 'error'),
3536
warning_spy = sinon.spy(robot.logger, 'warning'),

tests/test-st2bot-envvars.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ describe("environment variable configuration", function () {
4646
// Remove stackstorm.js from the require cache
4747
// https://medium.com/@gattermeier/invalidate-node-js-require-cache-c2989af8f8b0
4848
delete require.cache[require.resolve("../scripts/stackstorm.js")];
49+
if (robot) {
50+
robot.shutdown();
51+
if (robot.server) {
52+
robot.server.close();
53+
}
54+
}
4955
});
5056

5157
it("should log a warning when ST2_API is used", function (done) {
@@ -82,7 +88,7 @@ describe("environment variable configuration", function () {
8288
// Load script under test
8389
var stackstorm = require("../scripts/stackstorm.js");
8490
stackstorm(robot).then(function (stop) {
85-
expect(info_spy).to.have.been.calledThrice;
91+
expect(info_spy.args).length.to.be.above(1);
8692
expect(info_spy).to.have.been.calledWith('Two-factor auth is enabled');
8793
expect(info_spy).to.have.been.calledWith('Loading commands....');
8894

tests/test-st2bot-sigusr2.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var chai = require("chai"),
2424
expect = chai.expect,
2525
sinon = require('sinon'),
2626
sinonChai = require('sinon-chai'),
27+
nock = require('nock'),
2728
Robot = require("hubot/src/robot"),
2829
Logger = require('./dummy-logger.js');
2930

@@ -35,9 +36,37 @@ describe("SIGUSR2", function () {
3536
var debug_spy = sinon.spy(robot.logger, 'debug'),
3637
info_spy = sinon.spy(robot.logger, 'info');
3738

39+
// Not required for SIGUSR2 to work, but mocking the response increases code
40+
// coverage and more closely approximates the real world
41+
beforeEach(function () {
42+
// emulate ST2 API response
43+
nock('http://localhost:9101')
44+
.get('/v1/actionalias')
45+
.times(2)
46+
.query({"limit": "-1", "offset": "0"})
47+
.reply(200, [
48+
{
49+
"name": "hello",
50+
"description": "long hello",
51+
"enabled": true,
52+
"formats": [
53+
{
54+
"display": "format_one",
55+
"representation": "format_one"
56+
},
57+
{
58+
"display": "format_two",
59+
"representation": "format_two"
60+
}
61+
]
62+
}
63+
]);
64+
});
65+
3866
afterEach(function() {
3967
info_spy.resetHistory();
4068
debug_spy.resetHistory();
69+
nock.cleanAll();
4170
// Remove stackstorm.js from the require cache
4271
// https://medium.com/@gattermeier/invalidate-node-js-require-cache-c2989af8f8b0
4372
delete require.cache[require.resolve("../scripts/stackstorm.js")];
@@ -51,16 +80,12 @@ describe("SIGUSR2", function () {
5180
expect(debug_spy).to.have.callCount(2);
5281
expect(debug_spy).to.have.been.calledWith('Using default post data handler.');
5382
expect(debug_spy).to.have.been.calledWith('Caught SIGUSR2, reloading commands');
54-
expect(info_spy).to.have.callCount(3);
83+
expect(info_spy.args).to.have.length.above(1);
5584
expect(info_spy).to.have.been.calledWith('Loading commands....');
5685

5786
stop();
5887

5988
done();
60-
}).catch(function (err) {
61-
console.log(err);
62-
stop && stop();
63-
done(err);
6489
});
6590
});
6691
});

0 commit comments

Comments
 (0)