import { expect } from 'chai'; import { TestConfiguration } from '../../src'; import { CustomerSsoService } from '../../src'; import { BadRequestError } from '../../src/errors'; import * as nock from 'nock'; describe('Customer SSO service', () => { let config: any; let subject: any; beforeEach(() => { config = new TestConfiguration({ customerSsoApiKey: 'foobar', originSystemId: 'n-membership-sdk-tests', }); subject = new CustomerSsoService(config); }); afterEach(() => { nock.cleanAll(); }); describe('constructor', () => { context('if the customerSsoApiKey property is missing from the config', () => { it('should throw an error', () => { try { new CustomerSsoService(new TestConfiguration()); expect.fail('It should throw'); } catch (_) {} }); }); }); describe('linkUserAndStudentBeans', () => { context('given userId and sbid_number arguments', () => { let mock: any; let response: any; beforeEach(async () => { mock = nock('https://api-t.ft.com', { reqheaders: { 'X-api-key': config.get('customerSsoApiKey'), 'content-type': 'application/json', }}) .post('/sso/students/foo', JSON.stringify( { sbid_number: 'bar' })) .reply(200); }); it('pass the arguments on correctly and returns a true', async () => { response = await subject.linkUserAndStudentBeans('foo', 'bar'); expect(mock.isDone()).to.be.true; expect(response).to.be.true; }); }); context('given the request is unsuccessful', () => { beforeEach(() => { nock('https://api-t.ft.com', { reqheaders: { 'X-api-key': config.get('customerSsoApiKey'), 'content-type': 'application/json', }}) .post('/sso/students/foo', JSON.stringify({ sbid_number: 'bar' })) .reply(400); }); it('throws an error when make the request', async () => { try { await subject.linkUserAndStudentBeans('foo', 'bar'); expect.fail('Should have thrown error'); } catch (error) { expect(error).to.be.instanceOf(BadRequestError); } }); }); }); describe('updateUserData', () => { context('given userId and samlUserId arguments', () => { let mock: any; beforeEach(async () => { mock = nock('https://api-t.ft.com') .post('/sso/ssoUserData/foo', { samlUserId: 'bar' }) .reply(200); await subject.updateUserData('foo', 'bar', undefined); }); it('should pass the arguments on correctly', () => { expect(mock.isDone()).to.be.true; }); }); context('given the request is unsuccessful', () => { beforeEach(() => { nock('https://api-t.ft.com') .post('/sso/ssoUserData/foo', { samlUserId: 'bar' }) .reply(400); }); it('should throw error', async () => { try { await subject.updateUserData('foo', 'bar', undefined); expect.fail('Should have thrown error'); } catch (error) { expect(error).to.be.instanceOf(BadRequestError); } }); }); context('given the request is successful', () => { beforeEach(() => { nock('https://api-t.ft.com') .post('/sso/ssoUserData/foo', { samlUserId: 'bar' }) .reply(200); }); it('should return true', async () => { expect(await subject.updateUserData('foo', 'bar', undefined)).to.be.true; }); }); context('given the request has emailBySso parameter', () => { beforeEach(() => { nock('https://api-t.ft.com') .post('/sso/ssoUserData/foo', { samlUserId: 'bar', email: 'foo@mail.com', }) .reply(200); }); it('should return true', async () => { expect(await subject.updateUserData('foo', 'bar', 'foo@mail.com')).to.be.true; }); }); }); });