'use strict'; const Bell = require('../..'); const Code = require('@hapi/code'); const Hapi = require('@hapi/hapi'); const Hoek = require('@hapi/hoek'); const Lab = require('@hapi/lab'); const Mock = require('../mock'); const internals = {}; const { describe, it } = exports.lab = Lab.script(); const expect = Code.expect; describe('instagram', () => { it('authenticates with mock', async (flags) => { const mock = await Mock.v2(flags); const server = Hapi.server({ host: 'localhost', port: 80 }); await server.register(Bell); const custom = Bell.providers.instagram(); Hoek.merge(custom, mock.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, h) { return request.auth.credentials; } } }); const res1 = await server.inject('/login'); const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';'; const res2 = await mock.server.inject(res1.headers.location); const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } }); expect(res3.result).to.equal({ provider: 'custom', token: '456', expiresIn: 3600, refreshToken: undefined, query: {}, profile: { id: '123456789', username: 'stevegraham', displayName: 'Steve Graham', raw: { property: 'something' } } }); }); it('authenticates with mock (without extended profile)', async (flags) => { const mock = await Mock.v2(flags); const server = Hapi.server({ host: 'localhost', port: 80 }); await server.register(Bell); const custom = Bell.providers.instagram({ extendedProfile: false }); Hoek.merge(custom, mock.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, h) { return request.auth.credentials; } } }); const res1 = await server.inject('/login'); const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';'; const res2 = await mock.server.inject(res1.headers.location); const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } }); expect(res3.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' } } }); }); });