Skip to content

Commit e192b94

Browse files
committed
Add tests for non-auth environment variables and SIGUSR2 handler
1 parent d226322 commit e192b94

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

tests/test-st2bot-envvars.js

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
sinon = require('sinon'),
26+
sinonChai = require('sinon-chai'),
27+
mockedEnv = require('mocked-env'),
28+
Robot = require("hubot/src/robot"),
29+
Logger = require('./dummy-logger.js');
30+
31+
chai.use(sinonChai);
32+
33+
describe("environment variable configuration", function () {
34+
var robot = new Robot(null, "mock-adapter", false, "Hubot");
35+
robot.logger = new Logger(true);
36+
var restore_env = null,
37+
debug_spy = sinon.spy(robot.logger, 'debug'),
38+
info_spy = sinon.spy(robot.logger, 'info'),
39+
error_spy = sinon.spy(robot.logger, 'error');
40+
41+
afterEach(function() {
42+
restore_env && restore_env();
43+
error_spy.resetHistory();
44+
info_spy.resetHistory();
45+
debug_spy.resetHistory();
46+
// Remove stackstorm.js from the require cache
47+
// https://medium.com/@gattermeier/invalidate-node-js-require-cache-c2989af8f8b0
48+
delete require.cache[require.resolve("../scripts/stackstorm.js")];
49+
});
50+
51+
it("should log a warning when ST2_API is used", function (done) {
52+
// Mock process.env for all modules
53+
// https://glebbahmutov.com/blog/mocking-process-env/
54+
restore_env = mockedEnv({
55+
ST2_API: 'https://nonexistent-st2-auth-url:9101'
56+
});
57+
58+
// Load script under test
59+
var stackstorm = require("../scripts/stackstorm.js");
60+
stackstorm(robot).then(function (stop) {
61+
expect(robot.logger.logs.warning).to.have.length.above(0);
62+
expect(robot.logger.logs.warning).to.contain(
63+
'ST2_API is deprecated and will be removed in a future releases. Instead, please use the '+
64+
'ST2_API_URL environment variable.');
65+
66+
stop();
67+
68+
done();
69+
}).catch(function (err) {
70+
console.log(err);
71+
done(err);
72+
});
73+
});
74+
75+
it("should log info when HUBOT_2FA is used", function (done) {
76+
// Mock process.env for all modules
77+
// https://glebbahmutov.com/blog/mocking-process-env/
78+
restore_env = mockedEnv({
79+
HUBOT_2FA: 'true'
80+
});
81+
82+
// Load script under test
83+
var stackstorm = require("../scripts/stackstorm.js");
84+
stackstorm(robot).then(function (stop) {
85+
expect(info_spy).to.have.been.calledThrice;
86+
expect(info_spy).to.have.been.calledWith('Two-factor auth is enabled');
87+
expect(info_spy).to.have.been.calledWith('Loading commands....');
88+
89+
stop();
90+
91+
done();
92+
}).catch(function (err) {
93+
console.log(err);
94+
done(err);
95+
});
96+
});
97+
});

tests/test-st2bot-sigusr2.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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, afterEach*/
21+
'use strict';
22+
23+
var chai = require("chai"),
24+
expect = chai.expect,
25+
sinon = require('sinon'),
26+
sinonChai = require('sinon-chai'),
27+
Robot = require("hubot/src/robot"),
28+
Logger = require('./dummy-logger.js');
29+
30+
chai.use(sinonChai);
31+
32+
describe("SIGUSR2", function () {
33+
var robot = new Robot(null, "mock-adapter", false, "Hubot");
34+
robot.logger = new Logger(true);
35+
var debug_spy = sinon.spy(robot.logger, 'debug'),
36+
info_spy = sinon.spy(robot.logger, 'info');
37+
38+
afterEach(function() {
39+
info_spy.resetHistory();
40+
debug_spy.resetHistory();
41+
// Remove stackstorm.js from the require cache
42+
// https://medium.com/@gattermeier/invalidate-node-js-require-cache-c2989af8f8b0
43+
delete require.cache[require.resolve("../scripts/stackstorm.js")];
44+
});
45+
46+
it("should run loadCommands() after receiving signal", function (done) {
47+
// Load script under test
48+
var stackstorm = require("../scripts/stackstorm.js");
49+
stackstorm(robot).then(function (stop) {
50+
process.emit('SIGUSR2');
51+
expect(debug_spy).to.have.callCount(2);
52+
expect(debug_spy).to.have.been.calledWith('Using default post data handler.');
53+
expect(debug_spy).to.have.been.calledWith('Caught SIGUSR2, reloading commands');
54+
expect(info_spy).to.have.callCount(3);
55+
expect(info_spy).to.have.been.calledWith('Loading commands....');
56+
57+
stop();
58+
59+
done();
60+
}).catch(function (err) {
61+
console.log(err);
62+
stop && stop();
63+
done(err);
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)