xoauth2-test.js
3.81 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
'use strict';
var chai = require('chai');
var expect = chai.expect;
chai.Assertion.includeStack = true;
var xoauth2 = require('../src/xoauth2');
var mockServer = require('./server');
describe('XOAuth2 tests', function() {
this.timeout(10000);
var server;
var users = {};
var XOAUTH_PORT = 8993;
beforeEach(function(done) {
server = mockServer({
port: XOAUTH_PORT,
onUpdate: function(username, accessToken) {
users[username] = accessToken;
}
});
server.addUser('test@example.com', 'saladus');
server.start(done);
});
afterEach(function(done) {
server.stop(done);
});
it('should get an existing access token', function(done) {
var xoauth2gen = xoauth2.createXOAuth2Generator({
user: 'test@example.com',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
refreshToken: 'saladus',
accessUrl: 'http://localhost:' + XOAUTH_PORT + '/',
accessToken: 'abc',
timeout: 3600
});
xoauth2gen.getToken(function(err, token, accessToken) {
expect(err).to.not.exist;
expect(accessToken).to.equal('abc');
done();
});
});
it('should get an existing access token, no timeout', function(done) {
var xoauth2gen = xoauth2.createXOAuth2Generator({
user: 'test@example.com',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
refreshToken: 'saladus',
accessUrl: 'http://localhost:' + XOAUTH_PORT + '/',
accessToken: 'abc'
});
xoauth2gen.getToken(function(err, token, accessToken) {
expect(err).to.not.exist;
expect(accessToken).to.equal('abc');
done();
});
});
it('should generate a fresh access token', function(done) {
var xoauth2gen = xoauth2.createXOAuth2Generator({
user: 'test@example.com',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
refreshToken: 'saladus',
accessUrl: 'http://localhost:' + XOAUTH_PORT + '/',
timeout: 3600
});
xoauth2gen.getToken(function(err, token, accessToken) {
expect(err).to.not.exist;
expect(accessToken).to.equal(users['test@example.com']);
done();
});
});
it('should generate a fresh access token after timeout', function(done) {
var xoauth2gen = xoauth2.createXOAuth2Generator({
user: 'test@example.com',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
refreshToken: 'saladus',
accessUrl: 'http://localhost:' + XOAUTH_PORT + '/',
accessToken: 'abc',
timeout: 1
});
setTimeout(function() {
xoauth2gen.getToken(function(err, token, accessToken) {
expect(err).to.not.exist;
expect(accessToken).to.equal(users['test@example.com']);
done();
});
}, 3000);
});
it('should emit access token update', function(done) {
var xoauth2gen = xoauth2.createXOAuth2Generator({
user: 'test@example.com',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
refreshToken: 'saladus',
accessUrl: 'http://localhost:' + XOAUTH_PORT + '/',
timeout: 3600
});
xoauth2gen.once('token', function(tokenData) {
expect(tokenData).to.deep.equal({
user: 'test@example.com',
accessToken: users['test@example.com'],
timeout: 3600
});
done();
});
xoauth2gen.getToken(function() {});
});
});