|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const expect = require('chai').expect |
| 4 | +const apiKey = 'test_7aff7fc0142c65f86a00' |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +describe('authentication', () => { |
| 9 | + |
| 10 | + it('should authenticate with a global api key configured', done => { |
| 11 | + const emailable = require('../lib/emailable')(apiKey) |
| 12 | + |
| 13 | + Promise.all([ |
| 14 | + emailable.verify(email).then(response => { |
| 15 | + expect(response.domain).to.be.a('string') |
| 16 | + }), |
| 17 | + |
| 18 | + emailable.account().then(response => { |
| 19 | + expect(response.owner_email).to.be.a('string') |
| 20 | + }), |
| 21 | + |
| 22 | + emailable.batches.verify(emails) |
| 23 | + .then(response => { |
| 24 | + expect(response.id).to.have.lengthOf(24) |
| 25 | + return emailable.batches.status(response.id) |
| 26 | + }) |
| 27 | + .then(statusResponse => { |
| 28 | + expect(statusResponse.id).to.be.a('string') |
| 29 | + }) |
| 30 | + ]) |
| 31 | + .then(() => done()) |
| 32 | + .catch(done) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should authenticate with an api key passed in at request time', done => { |
| 36 | + const emailable = require('../lib/emailable')() |
| 37 | + |
| 38 | + Promise.all([ |
| 39 | + emailable.verify(email, { apiKey: apiKey }).then(response => { |
| 40 | + expect(response.domain).to.be.a('string') |
| 41 | + }), |
| 42 | + |
| 43 | + emailable.account({ apiKey: apiKey }).then(response => { |
| 44 | + expect(response.owner_email).to.be.a('string') |
| 45 | + }), |
| 46 | + |
| 47 | + emailable.batches.verify(emails, { apiKey: apiKey }) |
| 48 | + .then(response => { |
| 49 | + expect(response.id).to.have.lengthOf(24) |
| 50 | + return emailable.batches.status(response.id, { apiKey: apiKey }) |
| 51 | + }) |
| 52 | + .then(statusResponse => { |
| 53 | + expect(statusResponse.id).to.be.a('string') |
| 54 | + }) |
| 55 | + ]) |
| 56 | + .then(() => done()) |
| 57 | + .catch(done) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should prioritize request time authentication over global', done => { |
| 61 | + const emailable = require('../lib/emailable')('invalid_api_key') |
| 62 | + |
| 63 | + Promise.all([ |
| 64 | + emailable.verify(email, { apiKey: apiKey }).then(response => { |
| 65 | + expect(response.domain).to.be.a('string') |
| 66 | + }), |
| 67 | + |
| 68 | + emailable.account({ apiKey: apiKey }).then(response => { |
| 69 | + expect(response.owner_email).to.be.a('string') |
| 70 | + }), |
| 71 | + |
| 72 | + emailable.batches.verify(emails, { apiKey: apiKey }) |
| 73 | + .then(response => { |
| 74 | + expect(response.id).to.have.lengthOf(24) |
| 75 | + return emailable.batches.status(response.id, { apiKey: apiKey }) |
| 76 | + }) |
| 77 | + .then(statusResponse => { |
| 78 | + expect(statusResponse.id).to.be.a('string') |
| 79 | + }) |
| 80 | + ]) |
| 81 | + .then(() => done()) |
| 82 | + .catch(done) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should not modify the original params object passed in', done => { |
| 86 | + const emailable = require('../lib/emailable')() |
| 87 | + const params = { apiKey: apiKey } |
| 88 | + |
| 89 | + Promise.all([ |
| 90 | + emailable.verify(email, params).then(() => { |
| 91 | + expect(params.apiKey).to.equal(apiKey) |
| 92 | + }), |
| 93 | + |
| 94 | + emailable.account(params).then(() => { |
| 95 | + expect(params.apiKey).to.equal(apiKey) |
| 96 | + }) |
| 97 | + ]) |
| 98 | + .then(() => done()) |
| 99 | + .catch(done) |
| 100 | + }) |
| 101 | + |
| 102 | +}) |
0 commit comments