'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('salesforce', () => { it('fails with extendedProfile false and identityServiceProfile true', () => { const throws = () => { Bell.providers.salesforce({ extendedProfile: false, identityServiceProfile: true }); }; expect(throws).to.throw(Error); }); 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.salesforce(); expect(custom.auth).to.equal('https://login.salesforce.com/services/oauth2/authorize'); expect(custom.token).to.equal('https://login.salesforce.com/services/oauth2/token'); Hoek.merge(custom, mock.provider); const profile = { user_id: '1234567890', username: 'steve', display_name: 'steve', first_name: 'steve', last_name: 'smith', email: 'steve@example.com' }; Mock.override('https://login.salesforce.com/services/oauth2/userinfo', profile); server.auth.strategy('custom', 'bell', { password: 'cookie_encryption_password_secure', isSecure: false, clientId: 'salesforce', 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 }); }); it('authenticates with mock and custom uri', async (flags) => { const mock = await Mock.v2(flags); const server = Hapi.server({ host: 'localhost', port: 80 }); await server.register(Bell); const custom = Bell.providers.salesforce({ identityServiceProfile: true }); expect(custom.auth).to.equal('https://login.salesforce.com/services/oauth2/authorize'); expect(custom.token).to.equal('https://login.salesforce.com/services/oauth2/token'); Hoek.merge(custom, mock.provider); const profile = { user_id: '1234567890', username: 'steve', display_name: 'steve', first_name: 'steve', last_name: 'smith', email: 'steve@example.com' }; Mock.override('https://login.salesforce.com/id/foo/bar', profile); server.auth.strategy('custom', 'bell', { password: 'cookie_encryption_password_secure', isSecure: false, clientId: 'salesforce', 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 }); }); it('authenticates with mock and identityServiceProfile', async (flags) => { const mock = await Mock.v2(flags); const server = Hapi.server({ host: 'localhost', port: 80 }); await server.register(Bell); const custom = Bell.providers.salesforce({ uri: 'http://example.com' }); expect(custom.auth).to.equal('http://example.com/services/oauth2/authorize'); expect(custom.token).to.equal('http://example.com/services/oauth2/token'); Hoek.merge(custom, mock.provider); const profile = { user_id: '1234567890', username: 'steve', display_name: 'steve', first_name: 'steve', last_name: 'smith', email: 'steve@example.com' }; Mock.override('http://example.com/services/oauth2/userinfo', profile); server.auth.strategy('custom', 'bell', { password: 'cookie_encryption_password_secure', isSecure: false, clientId: 'salesforce', 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 }); }); 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.salesforce({ extendedProfile: false }); expect(custom.auth).to.equal('https://login.salesforce.com/services/oauth2/authorize'); expect(custom.token).to.equal('https://login.salesforce.com/services/oauth2/token'); Hoek.merge(custom, mock.provider); const profile = { user_id: '1234567890', username: 'steve', display_name: 'steve', first_name: 'steve', last_name: 'smith', email: 'steve@example.com' }; Mock.override('https://login.salesforce.com/services/oauth2/userinfo', profile); server.auth.strategy('custom', 'bell', { password: 'cookie_encryption_password_secure', isSecure: false, clientId: 'salesforce', 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: {} }); }); });