-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbot-tests.js
More file actions
151 lines (137 loc) · 6.73 KB
/
bot-tests.js
File metadata and controls
151 lines (137 loc) · 6.73 KB
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
/* bot-tests.js
*
* A set of tests to validate framework functionality
* when framework is created using a bot token
*/
const Framework = require('../lib/framework');
const Webex = require('webex');
let MongoStore = {};
// Initialize the framework and user objects once for all the tests
let framework, userWebex;
require('dotenv').config();
// Run tests using bot token by default
let frameworkOptions = { token: process.env.BOT_API_TOKEN };
if (process.env.RUN_TEST_AS_USER) {
console.log('***********************************');
console.log('* Framework tests with user token...');
console.log('***********************************\n');
if ((typeof process.env.AUTHORIZED_USER_API_TOKEN !== 'string') &&
(typeof process.env.USER_API_TOKEN !== 'string') &&
(typeof process.env.HOSTED_FILE !== 'string')) {
console.error('Missing required environment variables:\n' +
'- AUTHORIZED_USER_API_TOKEN -- token associatd with a user who authorized a framework based integration\n' +
'- USER_API_TOKEN -- token associated with an existing user\n' +
'- HOSTED_FILE -- url to a file that can be attached to test messages\n' +
'The tests will create a new space with the bot and the user');
process.exit(-1);
}
frameworkOptions.token = process.env.AUTHORIZED_USER_API_TOKEN;
} else {
if (process.env.RUN_MONGO_TESTS) {
console.log('******************************************');
console.log('* Framework mongo storage adapter tests...');
console.log('******************************************\n');
MongoStore = require('../storage/mongo');
if (process.env.MONGO_URI) {
var mConfig = {};
mConfig.mongoUri = process.env.MONGO_URI;
if (process.env.MONGO_BOT_STORE) { mConfig.storageCollectionName = process.env.MONGO_BOT_STORE; }
if (process.env.MONGO_BOT_METRICS) { mConfig.metricsCollectionName = process.env.MONGO_BOT_METRICS; }
if (process.env.MONGO_SINGLE_INSTANCE_MODE) { mConfig.singleInstance = true; }
if (typeof process.env.INIT_STORAGE === 'string') {
try {
frameworkOptions.initBotStorageData = JSON.parse(process.env.INIT_STORAGE);
} catch (e) {
console.error(`Unable to parse INIT_STORAGE value:${process.env.INIT_STORAGE}`);
console.error(`${e.message}`);
console.error('Make sure to set this to optional environment to a ' +
'properly stringified JSON object in order to test that the storage adapter properly adds it to new bots.');
process.exit(-1);
}
}
} else {
console.error('The mongo storage driver requires the following environment variables:\n' +
'* MONGO_URI -- mongo connection URL see https://docs.mongodb.com/manual/reference/connection-string' +
'\n\nThe following optional environment variables will also be used if set:\n' +
'* MONGO_BOT_STORE -- name of collection for bot storage elements (will be created if does not exist). Will use "webexBotFrameworkStorage" if not set\n' +
'* MONGO_BOT_METRICS -- name of a collection to write bot metrics to (will be created if does not exist). bot.writeMetric() calls will fail if not set\n' +
'* MONGO_INIT_STORAGE -- stringified ojbect aassigned as the default startup config if non exists yet\n' +
'* MONGO_SINGLE_INSTANCE_MODE -- Optimize lookups speeds when only a single bot server instance is running\n\n' +
'Also note, the mongodb module v3.4 or higher must be available (this is not included in the framework\'s default dependencies)');
process.exit();
}
} else {
console.log('***********************************');
console.log('* Framework tests with bot token...');
console.log('***********************************\n');
}
if ((typeof process.env.BOT_API_TOKEN !== 'string') &&
(typeof process.env.USER_API_TOKEN !== 'string') &&
(typeof process.env.HOSTED_FILE !== 'string')) {
console.error('Missing required environment variables:\n' +
'- BOT_API_TOKEN -- token associatd with an existing bot\n' +
'- USER_API_TOKEN -- token associated with an existing user\n' +
'- HOSTED_FILE -- url to a file that can be attached to test messages\n' +
'The tests will create a new space with the bot and the user');
process.exit(-1);
}
}
if (typeof process.env.INIT_STORAGE === 'string') {
try {
frameworkOptions.initBotStorageData = JSON.parse(process.env.INIT_STORAGE);
} catch (e) {
console.error(`Unable to parse INIT_STORAGE value:${process.env.INIT_STORAGE}`);
console.error(`${e.message}`);
console.error('Make sure to set this to optional environment to a ' +
'properly stringified JSON object in order to test that the storage adapter properly adds it to new bots.');
process.exit(-1);
}
}
// Enable Message Process Speed Profiling in tests
frameworkOptions.profileMsgProcessingTime = true;
framework = new Framework(frameworkOptions);
let userOptions = {credentials: {access_token: process.env.USER_API_TOKEN}};
userWebex = Webex.init(userOptions);
// Load the common module which includes functions and variables
// shared by multiple tests
var common = require('./common/common');
common.setFramework(framework);
common.setUser(userWebex);
// Start up an instance of framework that we will use across multiple tests
describe('#framework', function() { // don't use arrow so this binds to mocha
common.setMochaTimeout(this.timeout());
if (process.env.RUN_MONGO_TESTS) {
before(() => {
if ('mongoUri' in mConfig) {
// Load and initalize the mongo storage driver
let mongoStore = new MongoStore(mConfig);
// Wait for the connection to the DB to initialize before starting framework
return mongoStore.initialize()
.then(() => framework.storageDriver(mongoStore))
.catch((e) => {
framework.debug(`Initialization with mongo storage failed: ${e.message}`);
return Promise.reject(e);
});
} else {
return Promise.reject(new Error('Invalid mongo configuration'));
}
});
}
// Validate that framework starts and that we have a valid user
before(() => common.initFramework('framework init', framework, userWebex));
//Stop framework to shut down the event listeners
after(() => common.stopFramework('shutdown framework', framework));
// Test bot's ability to hear and send messages in user and bot created test spaces
require('./common/bot-message-and-hear-tests.js');
// Test bot's storage and membership functions
require('./common/bot-membership-tests.js');
// Test bot functions for direct messaging
require('./common/bot-direct-message-tests.js');
});
// gracefully shutdown (ctrl-c)
process.on('SIGINT', function () {
framework.debug('stoppping...');
framework.stop().then(function () {
process.exit();
});
});