// 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(recordedError).to.be.deep.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 ...');
  });
});

@@ -748,67 +748,195 @@
 ,%0A  
-nock = require('nock'),%0A  Robot = require('hubot/src/robot'
+sinon = require('sinon'),%0A  sinonChai = require('sinon-chai'),%0A  nock = require('nock'),%0A  Robot = require('hubot/src/robot'),%0A  Logger = require('./dummy-logger.js');%0A%0Achai.use(sinonChai
 );%0A%0A
@@ -1100,80 +1100,30 @@
     
-logs = %5B%5D,%0A    controlledLogger = function(msg) %7B logs.push(msg); %7D;%0A%0A  
+error_spy = sinon.spy(
 robo
@@ -1134,37 +1134,48 @@
 gger
-.
+, '
 error
- = controlledLogger;%0A  
+'),%0A    warning_spy = sinon.spy(
 robo
@@ -1186,39 +1186,47 @@
 gger
-.
+, '
 warning
- = controlledLogger;%0A  
+'),%0A    info_spy = sinon.spy(
 robo
@@ -1237,36 +1237,45 @@
 gger
-.
+, '
 info
- = controlledLogger;%0A  
+'),%0A    debug_spy = sinon.spy(
 robo
@@ -1286,35 +1286,19 @@
 gger
-.
+, '
 debug
- = controlledLogger;%0A
+');
 %0A%0A  
@@ -2597,17 +2597,16 @@
 %0A  %7D);%0A%0A
-%0A
   it(%22is
@@ -2718,32 +2718,49 @@
  expect(
-logs).to.contain
+info_spy).to.have.been.calledWith
 ('Using 
@@ -2907,24 +2907,42 @@
 ect(
-logs).to.include
+error_spy).to.have.been.calledWith
 ('Fa
@@ -3307,18 +3307,16 @@
 ;%0A  %7D);%0A
-  
 %0A  it(%22l
@@ -3369,24 +3369,41 @@
 ect(
-logs).to.contain
+info_spy).to.have.been.calledWith
 ('Hu

Use sinon.spy for robot loggers