/
instagram.js
executable file
·158 lines (123 loc) · 5.39 KB
/
instagram.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'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('instagram', () => {
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.instagram();
Hoek.merge(custom, provider);
const profile = {
meta: { code: 200 },
data: { property: 'something' }
};
Mock.override('https://api.instagram.com/v1/users/self', profile);
server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'instagram',
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',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: {
id: '123456789',
username: 'stevegraham',
displayName: 'Steve Graham',
raw: {
property: 'something'
}
}
});
mock.stop(done);
});
});
});
});
});
});
it('authenticates with mock (without extended profile)', { 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.instagram({ extendedProfile: false });
Hoek.merge(custom, provider);
server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'instagram',
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) => {
expect(response.result).to.equal({
provider: 'custom',
token: '456',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: {
id: '123456789',
username: 'stevegraham',
displayName: 'Steve Graham',
raw: {
id: '123456789',
username: 'stevegraham',
full_name: 'Steve Graham',
profile_picture: 'http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg'
}
}
});
mock.stop(done);
});
});
});
});
});
});
});