/
bitbucket.js
executable file
·94 lines (73 loc) · 3.1 KB
/
bitbucket.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
'use strict';
// Load modules
const Bell = require('../../');
const { expect } = require('code');
const { Server } = require('hapi');
const Hoek = require('hoek');
const Lab = require('lab');
const Mock = require('../mock');
// Test shortcuts
const { describe, it } = exports.lab = Lab.script();
describe('bitbucket', () => {
it('authenticates with mock', { parallel: false }, (done) => {
const mock = new Mock.V2();
mock.start((provider) => {
const server = Server({ host: 'localhost', port: 80 });
server.register(Bell, (err) => {
expect(err).to.not.exist();
const custom = Bell.providers.bitbucket();
Hoek.merge(custom, provider);
Mock.override('https://api.bitbucket.org/2.0/user', {
repositories: [{}],
uuid: '1E9C5160-E436-11E5-9897-4FCB70D5A8C7',
username: 'steve',
display_name: 'steve'
});
server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'bitbucket',
clientSecret: 'secret',
provider: custom
});
server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, reply) {
reply(request.auth.credentials);
}
}
});
server.inject('/login', (res) => {
const cookie = res.headers['set-cookie'][0].split(';')[0] + ';';
mock.server.inject(res.headers.location, (mockRes) => {
server.inject({ url: mockRes.headers.location, headers: { cookie } }, (response) => {
Mock.clear();
expect(response.result).to.equal({
provider: 'custom',
token: '456',
refreshToken: undefined,
expiresIn: 3600,
query: {},
profile: {
id: '1E9C5160-E436-11E5-9897-4FCB70D5A8C7',
username: 'steve',
displayName: 'steve',
raw: {
repositories: [{}],
uuid: '1E9C5160-E436-11E5-9897-4FCB70D5A8C7',
username: 'steve',
display_name: 'steve'
}
}
});
mock.stop(done);
});
});
});
});
});
});
});