-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from StackStorm/fix/exit-on-unauthorized
Exit hubot on Unauthorized errors
- Loading branch information
Showing
4 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "hubot-stackstorm", | ||
"description": "A hubot plugin for integrating with StackStorm event-driven infrastructure automation platform.", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"author": "StackStorm, Inc. <[email protected]>", | ||
"license": "Apache-2.0", | ||
"keywords": [ | ||
|
@@ -27,7 +27,7 @@ | |
"coffee-script": "1.12.7", | ||
"lodash": "^4.17.11", | ||
"rsvp": "^4.8.4", | ||
"st2client": "^1.1.2", | ||
"st2client": "^1.1.3", | ||
"truncate": "^2.0.1", | ||
"uuid": "^3.0.0" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2019 Extreme Networks, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/*jshint quotmark:false*/ | ||
/*jshint -W030*/ | ||
/*global describe, it, before, after*/ | ||
'use strict'; | ||
|
||
var chai = require('chai'), | ||
expect = chai.expect, | ||
nock = require('nock'), | ||
Robot = require('hubot/src/robot'); | ||
|
||
describe("auth with invalid st2 API key", function() { | ||
var stop; | ||
var robot = new Robot(null, "mock-adapter", true, "Hubot"); | ||
var recordedError = null, | ||
logs = [], | ||
controlledLogger = function(msg) { logs.push(msg); }; | ||
|
||
robot.logger.error = controlledLogger; | ||
robot.logger.warning = controlledLogger; | ||
robot.logger.info = controlledLogger; | ||
robot.logger.debug = controlledLogger; | ||
|
||
|
||
before(function(done) { | ||
process.env.ST2_API_KEY = 'aaaa'; | ||
|
||
// emulate ST2 API response | ||
nock('http://localhost:9101') | ||
.get('/v1/actionalias') | ||
.query({"limit":"-1","offset":"0"}) | ||
.reply(401, {"faultstring":"Unauthorized - ApiKey with key_hash=123 not found."}); | ||
|
||
// emulate ST2 STREAM response | ||
nock('http://localhost:9102') | ||
.get('/v1/stream') | ||
.query({"st2-api-key":"aaa"}) | ||
.reply(401, ""); | ||
|
||
// hack to detect uncaught exceptions | ||
var originalException = process.listeners('uncaughtException').pop(); | ||
process.removeListener('uncaughtException', originalException); | ||
process.prependOnceListener('uncaughtException', function (error) { | ||
process.listeners('uncaughtException').push(originalException); | ||
recordedError = error; | ||
//done(); | ||
}); | ||
|
||
var stackstorm = require("../scripts/stackstorm.js"); | ||
stackstorm(robot).then(function (result) { | ||
stop = result; | ||
done(); | ||
}); | ||
robot.run(); | ||
}); | ||
|
||
after(function() { | ||
stop && stop(); | ||
robot.server.close(); | ||
robot.shutdown(); | ||
// Remove stackstorm.js from the require cache | ||
// https://medium.com/@gattermeier/invalidate-node-js-require-cache-c2989af8f8b0 | ||
delete require.cache[require.resolve("../scripts/stackstorm.js")]; | ||
}); | ||
|
||
|
||
it("is using ST2_API_KEY as authentication", function () { | ||
// debug, if needed | ||
//console.log(logs); | ||
expect(logs).to.contain('Using ST2_API_KEY as authentication. Expiry will lead to bot exit.'); | ||
}); | ||
|
||
it("fails to retrieve the commands from API", function () { | ||
expect(logs).to.include('Failed to retrieve commands from "http://localhost:9101": Unauthorized - ApiKey with key_hash=123 not found.'); | ||
}); | ||
|
||
it("throws an 'Unauthorized' error", function () { | ||
expect(JSON.stringify(recordedError)).to.be.equal( | ||
'{"name":"APIError","status":401,"message":"Unauthorized - ApiKey with key_hash=123 not found."}' | ||
); | ||
}); | ||
|
||
it("leads to Hubot shutdown", function () { | ||
expect(logs).to.contain('Hubot will shut down ...'); | ||
}); | ||
}); |