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