Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / mailgunAppender-test.js
1 "use strict";
2 var vows = require('vows');
3 var assert = require('assert');
4 var log4js = require('../lib/log4js');
5 var sandbox = require('sandboxed-module');
6
7 function setupLogging(category, options) {
8     var msgs = [];
9
10     var mailgunCredentials = {
11         apiKey: options.apikey,
12         domain: options.domain
13     };
14
15     var fakeMailgun = function (conf) {
16         return {
17             messages: function () {
18                 return {
19                     config: options,
20                     send: function (data, callback) {
21                         msgs.push(data);
22                         callback(false, {status:"OK"});
23                     }
24                 };
25             }
26         };
27     };
28
29     var fakeLayouts = {
30         layout: function (type, config) {
31             this.type = type;
32             this.config = config;
33             return log4js.layouts.messagePassThroughLayout;
34         },
35         basicLayout: log4js.layouts.basicLayout,
36         messagePassThroughLayout: log4js.layouts.messagePassThroughLayout
37     };
38
39     var fakeConsole = {
40         errors: [],
41         logs: [],
42         error: function (msg, value) {
43             this.errors.push({msg: msg, value: value});
44         },
45         log: function (msg, value) {
46             this.logs.push({msg: msg, value: value});
47         }
48     };
49
50
51     var mailgunModule = sandbox.require('../lib/appenders/mailgun', {
52         requires: {
53             'mailgun-js': fakeMailgun,
54             '../layouts': fakeLayouts
55         },
56         globals: {
57             console: fakeConsole
58         }
59     });
60
61
62     log4js.addAppender(mailgunModule.configure(options), category);
63
64     return {
65         logger: log4js.getLogger(category),
66         mailer: fakeMailgun,
67         layouts: fakeLayouts,
68         console: fakeConsole,
69         mails: msgs,
70         credentials: mailgunCredentials
71     };
72 }
73
74 function checkMessages(result) {
75     for (var i = 0; i < result.mails.length; ++i) {
76         assert.equal(result.mails[i].from, 'sender@domain.com');
77         assert.equal(result.mails[i].to, 'recepient@domain.com');
78         assert.equal(result.mails[i].subject, 'This is subject');
79         assert.ok(new RegExp('.+Log event #' + (i + 1)).test(result.mails[i].text));
80     }
81 }
82
83 log4js.clearAppenders();
84
85 vows.describe('log4js mailgunAppender').addBatch({
86     'mailgun setup': {
87         topic: setupLogging('mailgun setup', {
88                 apikey: 'APIKEY',
89                 domain: 'DOMAIN',
90                 from: 'sender@domain.com',
91                 to: 'recepient@domain.com',
92                 subject: 'This is subject'
93         }),
94         'mailgun credentials should match': function(result){
95             assert.equal(result.credentials.apiKey, 'APIKEY');
96             assert.equal(result.credentials.domain, 'DOMAIN');
97         }
98     },
99
100     'basic usage': {
101         topic: function(){
102             var setup = setupLogging('basic usage', {
103                 apikey: 'APIKEY',
104                 domain: 'DOMAIN',
105                 from: 'sender@domain.com',
106                 to: 'recepient@domain.com',
107                 subject: 'This is subject'
108             });
109
110             setup.logger.info("Log event #1");
111             return setup;
112         },
113         'there should be one message only': function (result) {
114             assert.equal(result.mails.length, 1);
115         },
116         'message should contain proper data': function (result) {
117             checkMessages(result);
118         }
119     },
120     'config with layout': {
121         topic: function () {
122             var setup = setupLogging('config with layout', {
123                 layout: {
124                     type: "tester"
125                 }
126             });
127             return setup;
128         },
129         'should configure layout': function (result) {
130             assert.equal(result.layouts.type, 'tester');
131         }
132     },
133     'error when sending email': {
134         topic: function () {
135             var setup = setupLogging('separate email for each event', {
136                 apikey: 'APIKEY',
137                 domain: 'DOMAIN',
138                 from: 'sender@domain.com',
139                 to: 'recepient@domain.com',
140                 subject: 'This is subject'
141             });
142
143             setup.mailer.messages = function () {
144                     return {
145                         send: function (msg, cb) {
146                             cb({msg: "log4js.mailgunAppender - Error happened"}, null);
147                         }
148                     };
149             };
150
151             setup.logger.info("This will break");
152             return setup.console;
153         },
154         'should be logged to console': function (cons) {
155             assert.equal(cons.errors.length, 1);
156             assert.equal(cons.errors[0].msg, 'log4js.mailgunAppender - Error happened');
157         }
158     },
159     'separate email for each event': {
160         topic: function () {
161             var self = this;
162             var setup = setupLogging('separate email for each event', {
163                 apikey: 'APIKEY',
164                 domain: 'DOMAIN',
165                 from: 'sender@domain.com',
166                 to: 'recepient@domain.com',
167                 subject: 'This is subject'
168             });
169             setTimeout(function () {
170                 setup.logger.info('Log event #1');
171             }, 0);
172             setTimeout(function () {
173                 setup.logger.info('Log event #2');
174             }, 500);
175             setTimeout(function () {
176                 setup.logger.info('Log event #3');
177             }, 1100);
178             setTimeout(function () {
179                 self.callback(null, setup);
180             }, 3000);
181         },
182         'there should be three messages': function (result) {
183             assert.equal(result.mails.length, 3);
184         },
185         'messages should contain proper data': function (result) {
186             checkMessages(result);
187         }
188     }
189
190 }).export(module);