-
Notifications
You must be signed in to change notification settings - Fork 30
/
google-api-tests.js
121 lines (99 loc) · 3.05 KB
/
google-api-tests.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
// XXX: a lot of janky mocking here, not sure it's a great idea.
ServiceConfiguration.configurations.insert({
service: 'google',
client_id: 'foo',
client_secret: 'bar'
});
// mock out http
HTTP.nextResult = '';
HTTP.call = function(method, url, params, callback) {
var self = this;
if (_.isFunction(callback))
return callAync.call(this, method, url, params, callback);
return {statusCode: 200, data: {
access_token: 'good',
expires_in: 200
}};
}
// async version
var callAync = function(method, url, params, callback) {
var self = this;
Meteor.setTimeout(function() {
if (params.headers && params.headers.Authorization &&
params.headers.Authorization.match(/bad/)) {
// console.log('returning 401')
return callback({response: {statusCode: 401}});
}
// console.log('returning', self.nextResult, 'from Http.call')
return callback(null, {
statusCode: 200,
data: self.nextResult
});
});
}
if (Meteor.isServer) {
Meteor.publish('user', function() {
return Meteor.users.find(this.userId);
});
Meteor.methods({
mockUser: function(testId, token) {
var id = Meteor.users.insert({
_id: 'mockedUserId' + testId,
services: {google: {
accessToken: token,
refreshToken: '456'
}}
});
return id
},
loginAs: function(userId) {
this.setUserId(userId);
return {id: userId};
}
})
Tinytest.add('GoogleApi - Sync - get basic', function(test) {
var userId = Meteor.call('mockUser', test.id, 'good');
HTTP.nextResult = 'foo';
var result = GoogleApi.get('/foo/bar', {user: Meteor.users.findOne(userId)});
test.equal(result, 'foo');
});
Tinytest.add('GoogleApi - Sync - get with refresh', function(test) {
var userId = Meteor.call('mockUser', test.id, 'bad');
HTTP.nextResult = 'foo';
var result = GoogleApi.get('/foo/bar', {user: Meteor.users.findOne(userId)});
test.equal(result, 'foo');
});
} else {
var loginAs = function(userId, cb) {
Accounts.callLoginMethod({
methodName: 'loginAs',
methodArguments: [userId],
userCallback: function() { Meteor.subscribe('user', cb) }});
}
// same tests with promises
Tinytest.addAsync('GoogleApi - Promises - get basic', function(test, done) {
Meteor.call('mockUser', test.id, 'good', function(error, userId) {
loginAs(userId, function() {
HTTP.nextResult = 'foo';
GoogleApi.get('/foo/bar', {}).then(function(result) {
console.log(result)
test.equal(result, 'foo');
done();
}).fail(function(err) {
console.log(err)
});
});
});
});
Tinytest.addAsync('GoogleApi - Promises - get with refresh', function(test, done) {
Meteor.call('mockUser', test.id, 'bad', function(error, userId) {
loginAs(userId, function() {
HTTP.nextResult = 'foo';
GoogleApi.get('/foo/bar', {}).then(function(result) {
test.equal(result, 'foo');
done();
});
});
});
});
}