forked from sandfox/nodejs-piwik
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
207 lines (184 loc) · 4.77 KB
/
test.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Name: piwik - test.js
Description: Access a Piwik API or track hits with node.js
Author: Franklin van de Meent (https://frankl.in)
Source & docs: https://github.com/fvdm/nodejs-piwik
Feedback: https://github.com/fvdm/nodejs-piwik/issues
License: Unlicense / Public Domain (see UNLICENSE file)
(https://github.com/fvdm/nodejs-piwik/raw/develop/UNLICENSE)
*/
// Setup
// $ env PIWIK_URL= PIWIK_TOKEN= PIWIK_SITEID= npm test
var url = process.env.PIWIK_URL || null;
var token = process.env.PIWIK_TOKEN || null;
var siteId = process.env.PIWIK_SITEID || null;
var timeout = process.env.PIWIK_TIMEOUT || 5000;
var piwik = require ('./') .setup (url, token, timeout);
var errors = 0;
var queue = [];
var next = 0;
// handle exits
process.on ('exit', function () {
if (errors === 0) {
console.log ('\n\u001b[1mDONE, no errors.\u001b[0m\n');
process.exit (0);
} else {
console.log ('\n\u001b[1mFAIL, ' + errors + ' error' + (errors > 1 ? 's' : '') + ' occurred!\u001b[0m\n');
process.exit (1);
}
});
// prevent errors from killing the process
process.on ('uncaughtException', function (err) {
console.log ();
console.error (err.stack);
console.trace ();
console.log ();
errors++;
});
// Queue to prevent flooding
function doNext () {
next++;
if (queue [next]) {
queue [next] ();
}
}
// doTest( passErr, 'methods', [
// ['feeds', typeof feeds === 'object']
// ])
function doTest (err, label, tests) {
var testErrors = [];
var i;
if (err instanceof Error) {
console.error ('\u001b[1m\u001b[31mERROR\u001b[0m - ' + label + '\n');
console.dir (err, { depth: null, colors: true });
console.log ();
console.error (err.stack);
console.log ();
errors++;
} else {
for (i = 0; i < tests.length; i++) {
if (tests [i] [1] !== true) {
testErrors.push (tests [i] [0]);
errors++;
}
}
if (testErrors.length === 0) {
console.log ('\u001b[1m\u001b[32mgood\u001b[0m - ' + label);
} else {
console.error ('\u001b[1m\u001b[31mFAIL\u001b[0m - ' + label + ' (' + testErrors.join (', ') + ')');
}
}
doNext ();
}
// ! API access
queue.push (function () {
piwik.api (
{
method: 'API.getPiwikVersion'
},
function (err) {
if (err) {
console.log ('\u001b[1m\u001b[31mFAIL\u001b[0m - API access (' + err.message + ')');
console.log (err.stack);
errors++;
process.exit (1);
} else {
console.log ('\u001b[1m\u001b[32mgood\u001b[0m - API access');
doNext ();
}
}
);
});
// ! API error
queue.push (function () {
piwik.api (
{
method: 'invalid method name'
},
function (err) {
doTest (null, 'API error', [
['type', err && err instanceof Error],
['message', err && err.message === 'api error']
]);
}
);
});
// ! Track one
queue.push (function () {
piwik.track (
{
idsite: siteId,
url: 'https://www.npmjs.com/package/piwik',
cvar: {
1: ['node test', process.version]
}
},
function (err, data) {
doTest (err, 'track one', [
['data type', typeof data === 'object'],
['data.status', data && data.status === 'success'],
['data.tracked', data && data.tracked === 1]
]);
}
);
});
// ! Track multi
queue.push (function () {
piwik.track (
[
{
idsite: siteId,
url: 'https://www.npmjs.com/package/piwik',
cvar: {
1: ['node test', process.version]
}
},
{
idsite: siteId,
url: 'https://github.com/fvdm/nodejs-piwik',
cvar: {
1: ['node test', process.version]
}
}
],
function (err, data) {
doTest (err, 'track multi', [
['data type', typeof data === 'object'],
['data.status', data && data.status === 'success'],
['data.tracked', data && data.tracked === 2]
]);
}
);
});
// ! API
queue.push (function () {
piwik.api (
{
method: 'Actions.getPageUrls',
idSite: siteId,
period: 'year',
date: 'today'
},
function (err, data) {
doTest (err, 'api', [
['data type', data instanceof Array],
['data length', data && data.length >= 1],
['item type', data && data [0] instanceof Object],
['item label', data && data [0] && typeof data [0] .label === 'string']
]);
}
);
});
// ! loadSpammers
queue.push (function () {
piwik.loadSpammers (function (err, data) {
doTest (err, 'loadSpammers', [
['data type', data instanceof Array],
['data length', data && data.length >= 1],
['item type', data && data [0] && typeof data [0] === 'string']
]);
});
});
// Start the tests
console.log ('Running tests...\n');
queue [0] ();