-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (106 loc) · 3.6 KB
/
index.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
/**
* Dependencies
*/
var Path = require('path'),
mocha = require('mocha'),
utils = require('./utils.js'),
q = require('q')
function configure(options){
if(!options.definition) throw new Error('Must supply a provider definition');
this.name = options.name;
this.definition = options.definition;
this.config = options.config || {};
this.credentials = options.credentials || {};
this.filefog_options = options.filefog_options || {};
// Globalize Adapter
global.Name = this.name;
global.Definition = this.definition;
global.Configuration = this.config;
global.Credentials = this.credentials;
global.FilefogOptions = this.filefog_options;
if(options.definition.config && options.definition.config.interfaces)
{
global.Interfaces = options.definition.config.interfaces
}
else{
global.Interfaces = [];
}
}
/**
* Test Runner
* @api public
* @method exports
* @param {Object} options
* @return
*/
module.exports.TestRunner = function(options) {
configure(options);
// Build an array of files to test
var filter = '\\.(' + ['js'].join('|') + ')$';
var files = [];
var interfacePath = Path.resolve(__dirname,'./interfaces/base');
files = files.concat(utils.fileLookup(interfacePath, filter, true));
Interfaces.forEach(function(interface) {
console.log("Adding interface tests: "+interface)
var interfacePath = Path.resolve(__dirname,'./interfaces/' + interface);
files = files.concat(utils.fileLookup(interfacePath, filter, true));
});
// Build a Mocha Runner
var test = new mocha({
timeout: 6000,
reporter: 'spec'
});
//add files
files.forEach(function(file_path){
test.addFile(file_path);
})
// Allow Provider to be a global without warning about a leak
test.globals([Definition,Configuration,Credentials,Name]);
test.run(function(failures){
process.on('exit', function () {
process.exit(failures);
});
});
// var promise = q({})
// if(should_generate_credentials){
// promise = generate_credentials();
// }
// promise.then(run_tests)
};
/**
* Use this method as a shortcut to generate oauth tokens on demand. To keep it simple, it takes the same options that
* the TestRunner takes, so you can just replace the method call with this one.
* @param options
* @returns {*}
* @constructor
*/
module.exports.GenerateCredentials = function(options){
configure(options)
var readline = require('readline');
var filefog = require('filefog')
filefog.use('cred_load', Definition, Configuration);
var authProvider = filefog.provider("cred_load")
var is_oauth = Interfaces.indexOf("oauth") != -1;
if(!is_oauth){
console.log("Provider is not an oauth provider, generating credentials is not necessary")
return q({});
}
var authUrl = authProvider.oAuthGetAuthorizeUrl()
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Visit the following url in a browser, authenticate, then paste the code provided by the oauth service. \n\n" + authUrl+" \n\n", function(token) {
rl.close();
console.dir(token);
console.log(authProvider);
return authProvider.oAuthGetAccessToken(token)
.then(function(new_cred){
return utils.saveCredentials(new_cred)
})
.fail(function(err){
console.log("An error occured while saving credentials", err, err.stack);
})
.done();
});
}