|
| 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 | +}); |
0 commit comments