summaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/log4js/test/hipchatAppender-test.js
blob: 4769c3a2349e58876d9830ac21a763155dfe2ee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use strict";
var vows = require('vows'),
    assert = require('assert'),
    log4js = require('../lib/log4js'),
    sandbox = require('sandboxed-module');

function setupLogging(category, options) {
  var lastRequest = {};

  var fakeRequest = function(args, level){
    lastRequest.notifier = this;
    lastRequest.body = args[0];
    lastRequest.callback = args[1];
    lastRequest.level = level;
  };

  var fakeHipchatNotifier = {
    'make': function(room, token, from, host, notify){
      return {
        'room': room,
        'token': token,
        'from': from || '',
        'host': host || 'api.hipchat.com',
        'notify': notify || false,
        'setRoom': function(val){ this.room = val; },
        'setFrom': function(val){ this.from = val; },
        'setHost': function(val){ this.host = val; },
        'setNotify': function(val){ this.notify = val; },
        'info': function(){ fakeRequest.call(this, arguments, 'info'); },
        'warning': function(){ fakeRequest.call(this, arguments, 'warning'); },
        'failure': function(){ fakeRequest.call(this, arguments, 'failure'); },
        'success': function(){ fakeRequest.call(this, arguments, 'success'); }
      };
    }
  };

  var hipchatModule = sandbox.require('../lib/appenders/hipchat', {
    requires: {
      'hipchat-notifier': fakeHipchatNotifier
    }
  });
  log4js.clearAppenders();
  log4js.addAppender(hipchatModule.configure(options), category);

  return {
    logger: log4js.getLogger(category),
    lastRequest: lastRequest
  };
}

vows.describe('HipChat appender').addBatch({
  'when logging to HipChat v2 API': {
    topic: function() {
      var customCallback = function(err, res, body){ return 'works'; };

      var setup = setupLogging('myCategory', {
         "type": "hipchat",
         "hipchat_token": "User_Token_With_Notification_Privs",
         "hipchat_room": "Room_ID_Or_Name",
         "hipchat_from": "Log4js_Test",
         "hipchat_notify": true,
         "hipchat_host": "hipchat.your-company.tld",
         "hipchat_response_callback": customCallback
      });
      setup.logger.warn('Log event #1');
      return setup;
    },
    'a request to hipchat_host should be sent': function (topic) {
      assert.equal(topic.lastRequest.notifier.host, "hipchat.your-company.tld");
      assert.equal(topic.lastRequest.notifier.notify, true);
      assert.equal(topic.lastRequest.body, 'Log event #1');
      assert.equal(topic.lastRequest.level, 'warning');
    },
    'a custom callback to the HipChat response is supported': function(topic) {
      assert.equal(topic.lastRequest.callback(), 'works');
    }
  },
  'when missing options': {
    topic: function() {
      var setup = setupLogging('myLogger', {
          "type": "hipchat",
      });
      setup.logger.error('Log event #2');
      return setup;
    },
    'it sets some defaults': function (topic) {
      assert.equal(topic.lastRequest.notifier.host, "api.hipchat.com");
      assert.equal(topic.lastRequest.notifier.notify, false);
      assert.equal(topic.lastRequest.body, 'Log event #2');
      assert.equal(topic.lastRequest.level, 'failure');
    }
  },
  'when basicLayout is provided': {
    topic: function() {
      var setup = setupLogging('myLogger', {
          "type": "hipchat",
          "layout": log4js.layouts.basicLayout
      });
      setup.logger.debug('Log event #3');
      return setup;
    },
    'it should include the timestamp': function (topic) {

      // basicLayout adds [TIMESTAMP] [LEVEL] category - message
      // e.g. [2016-06-10 11:50:53.819] [DEBUG] myLogger - Log event #23

      assert.match(topic.lastRequest.body, /^\[[^\]]+\] \[[^\]]+\].*Log event \#3$/);
      assert.equal(topic.lastRequest.level, 'info');
    }
  }

}).export(module);