From c2995709c9062d42d537d22b93b22105727c0e8b Mon Sep 17 00:00:00 2001 From: Aydrian Date: Wed, 7 Sep 2016 08:03:05 -0400 Subject: [PATCH] Refactored sending domains library (#167) * Updated sending domains lib and tests * Updated sending domains doc and examples * refactored verify to accept all verify attributes * updated tests for payloads and cloneDeep * Updated sending domains verbs, tests, docs, and examples * Updated sending domains verbs, tests, docs, and examples --- docs/resources/sendingDomains.md | 60 ++--- examples/sendingDomains/create.js | 36 +++ .../sendingDomains/create_sendingDomain.js | 24 -- examples/sendingDomains/delete.js | 27 +++ .../sendingDomains/delete_sendingDomain.js | 14 -- examples/sendingDomains/get.js | 27 +++ .../sendingDomains/get_all_sendingDomains.js | 14 -- examples/sendingDomains/get_sendingDomain.js | 14 -- examples/sendingDomains/list.js | 27 +++ .../{update_sendingDomain.js => update.js} | 22 +- examples/sendingDomains/verify.js | 33 +++ .../verify_sendingDomain_default.js | 17 -- .../verify_sendingDomain_dkim_only.js | 18 -- .../verify_sendingDomain_spf_only.js | 18 -- lib/sendingDomains.js | 157 +++++++------ test/spec/sendingDomains.spec.js | 218 ++++++------------ 16 files changed, 345 insertions(+), 381 deletions(-) create mode 100644 examples/sendingDomains/create.js delete mode 100644 examples/sendingDomains/create_sendingDomain.js create mode 100644 examples/sendingDomains/delete.js delete mode 100644 examples/sendingDomains/delete_sendingDomain.js create mode 100644 examples/sendingDomains/get.js delete mode 100644 examples/sendingDomains/get_all_sendingDomains.js delete mode 100644 examples/sendingDomains/get_sendingDomain.js create mode 100644 examples/sendingDomains/list.js rename examples/sendingDomains/{update_sendingDomain.js => update.js} (75%) create mode 100644 examples/sendingDomains/verify.js delete mode 100644 examples/sendingDomains/verify_sendingDomain_default.js delete mode 100644 examples/sendingDomains/verify_sendingDomain_dkim_only.js delete mode 100644 examples/sendingDomains/verify_sendingDomain_spf_only.js diff --git a/docs/resources/sendingDomains.md b/docs/resources/sendingDomains.md index a5199c8..3b3e6c3 100644 --- a/docs/resources/sendingDomains.md +++ b/docs/resources/sendingDomains.md @@ -1,51 +1,35 @@ # Sending Domains -This library provides easy access to the [Sending Domains](https://www.sparkpost.com/api#/reference/sending-domains/) Resource. +This library provides easy access to the [Sending Domains](https://developers.sparkpost.com/api/sending-domains) Resource. + +*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* ## Methods -* **all(callback)** +* **list()**
List an overview of all sending domains in the account. - * `callback` - executed after task is completed. **required** - * standard `callback(err, data)` - * `err` - any error that occurred - * `data` - full response from request client -* **find(domain, callback)** + +* **get(domain)**
Retrieve a sending domain by its domain name - * `domain` - the name of the domain you want to look up **required** - * `callback` - see all function -* **create(domainBody, callback)** + * `domain` - the domain you want to look up **required** + +* **create(createOpts)**
Create a new sending domain - * `domainBody` - a sending domain object **required** - * `callback` - see all function -* **update(domainBody, callback)** + * `createOpts` - a hash of [sending domain attributes](https://developers.sparkpost.com/api/sending-domains#header-sending-domain-attributes) **required** + +* **update(domain, updateOpts)**
Update an existing sending domain - * `domainBody` - a sending domain object **required** - * `callback` - see all function -* **delete(domain, callback)** + * `domain` - the domain you want to update **required** + * `updateOpts` - a hash of [sending domain attributes](https://developers.sparkpost.com/api/sending-domains#header-sending-domain-attributes) **required** + +* **delete(domain)**
Delete an existing sending domain - * `domain` - the name of the domain you want to delete **required** - * `callback` - see all function -* **verify(options, callback)** + * `domain` - the domain you want to delete **required** + +* **verify(domain, options)**
Validate the specified verification field types for a sending domain - * `options.domain` - the name of the domain you want to verify **required** - * `options.verifyDKIM` - initiates a check against the DKIM record default: `true` - * `options.verifySPF` - initiates a check against the SPF record default: `true` + * `domain` - the domain you want to verify **required** + * `options` - a hash of [verify attributes](https://developers.sparkpost.com/api/sending-domains#header-verify-attributes) **required** ## Examples -```js -var SparkPost = require('sparkpost'); -var client = new SparkPost('YOUR_API_KEY'); - -client.sendingDomains.all(function(err, data) { - if(err) { - console.log(err); - return; - } - - console.log(data); -}); - -``` - -Check out all the examples provided [here](/examples/sendingDomains). +Visit our examples section to see all of [our sending domains resource examples](/examples/sendingDomains). diff --git a/examples/sendingDomains/create.js b/examples/sendingDomains/create.js new file mode 100644 index 0000000..3820150 --- /dev/null +++ b/examples/sendingDomains/create.js @@ -0,0 +1,36 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , createOpts = { + domain: 'example1.com', + dkim: { + 'private': 'MIICXgIBAAKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQABAoGBAITb3BCRPBi5lGhHdn+1RgC7cjUQEbSb4eFHm+ULRwQ0UIPWHwiVWtptZ09usHq989fKp1g/PfcNzm8c78uTS6gCxfECweFCRK6EdO6cCCr1cfWvmBdSjzYhODUdQeyWZi2ozqd0FhGWoV4VHseh4iLj36DzleTLtOZj3FhAo1WJAkEA68T+KkGeDyWwvttYtuSiQCCTrXYAWTQnkIUxduCp7Ap6tVeIDn3TaXTj74UbEgaNgLhjG4bX//fdeDW6PaK9YwJBAM6xJmwHLPMgwNVjiz3u/6fhY3kaZTWcxtMkXCjh1QE82KzDwqyrCg7EFjTtFysSHCAZxXZMcivGl4TZLHnydJUCQQCx16+M+mAatuiCnvxlQUMuMiSTNK6Amzm45u9v53nlZeY3weYMYFdHdfe1pebMiwrT7MI9clKebz6svYJVmdtXAkApDAc8VuR3WB7TgdRKNWdyGJGfoD1PO1ZE4iinOcoKV+IT1UCY99Kkgg6C7j62n/8T5OpRBvd5eBPpHxP1F9BNAkEA5Nf2VO9lcTetksHdIeKK+F7sio6UZn0Rv7iUo3ALrN1D1cGfWIh2dj3ko1iSreyNVSwGW0ePP27qDmU+u6/Y1g==', + 'public': 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQAB', + selector: 'brisbane', + headers: 'from:to:subject:date' + } + }; + +// Promise +client.sendingDomains.create(createOpts) + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback +client.sendingDomains.create(createOpts, function(err, data) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Congrats you can use our client library!'); + console.log(data); + } +}); diff --git a/examples/sendingDomains/create_sendingDomain.js b/examples/sendingDomains/create_sendingDomain.js deleted file mode 100644 index bcb2c86..0000000 --- a/examples/sendingDomains/create_sendingDomain.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key); - -var domain = { - domain: 'example1.com', - dkim: { - 'private': 'MIICXgIBAAKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQABAoGBAITb3BCRPBi5lGhHdn+1RgC7cjUQEbSb4eFHm+ULRwQ0UIPWHwiVWtptZ09usHq989fKp1g/PfcNzm8c78uTS6gCxfECweFCRK6EdO6cCCr1cfWvmBdSjzYhODUdQeyWZi2ozqd0FhGWoV4VHseh4iLj36DzleTLtOZj3FhAo1WJAkEA68T+KkGeDyWwvttYtuSiQCCTrXYAWTQnkIUxduCp7Ap6tVeIDn3TaXTj74UbEgaNgLhjG4bX//fdeDW6PaK9YwJBAM6xJmwHLPMgwNVjiz3u/6fhY3kaZTWcxtMkXCjh1QE82KzDwqyrCg7EFjTtFysSHCAZxXZMcivGl4TZLHnydJUCQQCx16+M+mAatuiCnvxlQUMuMiSTNK6Amzm45u9v53nlZeY3weYMYFdHdfe1pebMiwrT7MI9clKebz6svYJVmdtXAkApDAc8VuR3WB7TgdRKNWdyGJGfoD1PO1ZE4iinOcoKV+IT1UCY99Kkgg6C7j62n/8T5OpRBvd5eBPpHxP1F9BNAkEA5Nf2VO9lcTetksHdIeKK+F7sio6UZn0Rv7iUo3ALrN1D1cGfWIh2dj3ko1iSreyNVSwGW0ePP27qDmU+u6/Y1g==', - 'public': 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQAB', - selector: 'brisbane', - headers: 'from:to:subject:date' - } -}; - -client.sendingDomains.create(domain, function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/examples/sendingDomains/delete.js b/examples/sendingDomains/delete.js new file mode 100644 index 0000000..6e1f0aa --- /dev/null +++ b/examples/sendingDomains/delete.js @@ -0,0 +1,27 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +// Promise +client.sendingDomains.delete('example1.com') + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback +client.sendingDomains.delete('example1.com', function(err, data) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Congrats you can use our client library!'); + console.log(data); + } +}); diff --git a/examples/sendingDomains/delete_sendingDomain.js b/examples/sendingDomains/delete_sendingDomain.js deleted file mode 100644 index 1da0693..0000000 --- a/examples/sendingDomains/delete_sendingDomain.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key); - -client.sendingDomains.delete('example1.com', function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/examples/sendingDomains/get.js b/examples/sendingDomains/get.js new file mode 100644 index 0000000..46af967 --- /dev/null +++ b/examples/sendingDomains/get.js @@ -0,0 +1,27 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +// Promise +client.sendingDomains.get('example1.com') + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback +client.sendingDomains.get('example1.com', function(err, data) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Congrats you can use our client library!'); + console.log(data); + } +}); diff --git a/examples/sendingDomains/get_all_sendingDomains.js b/examples/sendingDomains/get_all_sendingDomains.js deleted file mode 100644 index c9d9e6b..0000000 --- a/examples/sendingDomains/get_all_sendingDomains.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key); - -client.sendingDomains.all(function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/examples/sendingDomains/get_sendingDomain.js b/examples/sendingDomains/get_sendingDomain.js deleted file mode 100644 index 945e153..0000000 --- a/examples/sendingDomains/get_sendingDomain.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key); - -client.sendingDomains.find('example1.com', function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/examples/sendingDomains/list.js b/examples/sendingDomains/list.js new file mode 100644 index 0000000..873c07a --- /dev/null +++ b/examples/sendingDomains/list.js @@ -0,0 +1,27 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +// Promise +client.sendingDomains.list() + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback +client.sendingDomains.list(function(err, data) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Congrats you can use our client library!'); + console.log(data); + } +}); diff --git a/examples/sendingDomains/update_sendingDomain.js b/examples/sendingDomains/update.js similarity index 75% rename from examples/sendingDomains/update_sendingDomain.js rename to examples/sendingDomains/update.js index bb29ce8..97a36a1 100644 --- a/examples/sendingDomains/update_sendingDomain.js +++ b/examples/sendingDomains/update.js @@ -2,10 +2,8 @@ var key = 'YOURAPIKEY' , SparkPost = require('sparkpost') - , client = new SparkPost(key); - - var domain = { - domain: 'example1.com', + , client = new SparkPost(key) + , updateOpts = { dkim: { 'private': 'MIICXgIBAAKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQABAoGBAITb3BCRPBi5lGhHdn+1RgC7cjUQEbSb4eFHm+ULRwQ0UIPWHwiVWtptZ09usHq989fKp1g/PfcNzm8c78uTS6gCxfECweFCRK6EdO6cCCr1cfWvmBdSjzYhODUdQeyWZi2ozqd0FhGWoV4VHseh4iLj36DzleTLtOZj3FhAo1WJAkEA68T+KkGeDyWwvttYtuSiQCCTrXYAWTQnkIUxduCp7Ap6tVeIDn3TaXTj74UbEgaNgLhjG4bX//fdeDW6PaK9YwJBAM6xJmwHLPMgwNVjiz3u/6fhY3kaZTWcxtMkXCjh1QE82KzDwqyrCg7EFjTtFysSHCAZxXZMcivGl4TZLHnydJUCQQCx16+M+mAatuiCnvxlQUMuMiSTNK6Amzm45u9v53nlZeY3weYMYFdHdfe1pebMiwrT7MI9clKebz6svYJVmdtXAkApDAc8VuR3WB7TgdRKNWdyGJGfoD1PO1ZE4iinOcoKV+IT1UCY99Kkgg6C7j62n/8T5OpRBvd5eBPpHxP1F9BNAkEA5Nf2VO9lcTetksHdIeKK+F7sio6UZn0Rv7iUo3ALrN1D1cGfWIh2dj3ko1iSreyNVSwGW0ePP27qDmU+u6/Y1g==', 'public': 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQAB', @@ -14,11 +12,23 @@ var key = 'YOURAPIKEY' } }; -client.sendingDomains.update(domain, function(err, data) { +client.sendingDomains.update('example1.com', updateOpts) + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Using a callback +client.sendingDomains.update('example1.com', updateOpts, function(err, data) { if (err) { + console.log('Whoops! Something went wrong'); console.log(err); } else { - console.log(data); console.log('Congrats you can use our client library!'); + console.log(data); } }); diff --git a/examples/sendingDomains/verify.js b/examples/sendingDomains/verify.js new file mode 100644 index 0000000..a86ab03 --- /dev/null +++ b/examples/sendingDomains/verify.js @@ -0,0 +1,33 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + dkim_verify: true, + spf_verify: true, + abuse_at_verify: true, + postmaster_at_verify: true + }; + +// Promise +client.sendingDomains.verify('example1.com', options) + .then(data => { + console.log('Congrats you can use our client library!'); + console.log(data); + }) + .catch(err => { + console.log('Whoops! Something went wrong'); + console.log(err); + }); + +// Callback +client.sendingDomains.verify('example1.com', options, function(err, data) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Congrats you can use our client library!'); + console.log(data); + } +}); diff --git a/examples/sendingDomains/verify_sendingDomain_default.js b/examples/sendingDomains/verify_sendingDomain_default.js deleted file mode 100644 index 9184b51..0000000 --- a/examples/sendingDomains/verify_sendingDomain_default.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key) - , options = { - domain: 'example1.com' - }; - -client.sendingDomains.verify(options, function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/examples/sendingDomains/verify_sendingDomain_dkim_only.js b/examples/sendingDomains/verify_sendingDomain_dkim_only.js deleted file mode 100644 index 36328ab..0000000 --- a/examples/sendingDomains/verify_sendingDomain_dkim_only.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key) - , options = { - domain: 'example1.com' - , verifySPF: false - }; - -client.sendingDomains.verify(options, function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/examples/sendingDomains/verify_sendingDomain_spf_only.js b/examples/sendingDomains/verify_sendingDomain_spf_only.js deleted file mode 100644 index 245c501..0000000 --- a/examples/sendingDomains/verify_sendingDomain_spf_only.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -var key = 'YOURAPIKEY' - , SparkPost = require('sparkpost') - , client = new SparkPost(key) - , options = { - domain: 'example1.com' - , verifyDKIM: false - }; - -client.sendingDomains.verify(options, function(err, data) { - if (err) { - console.log(err); - } else { - console.log(data); - console.log('Congrats you can use our client library!'); - } -}); diff --git a/lib/sendingDomains.js b/lib/sendingDomains.js index 1731145..faf3c70 100644 --- a/lib/sendingDomains.js +++ b/lib/sendingDomains.js @@ -1,125 +1,142 @@ 'use strict'; -var api = 'sending-domains' - , Promise = require('./Promise') - , toApiFormat = require('./toApiFormat'); - -/* - * "Class" declaration, Sending Domains API exposes five functions: - * - create: creates a new sending domain - * - update: updates an existing sending domain - * - delete: deletes an existing sending domain - * - verify: validates specified verification field types on a sending domain - * - all: retreives a list of sending domains - * - find: retreives info about a specific sending domain - */ +const Promise = require('./Promise'); +const api = 'sending-domains'; + module.exports = function(client) { - var sendingDomains = { - all: function(callback) { //list - var options = { + return { + /** + * Lists all sending domains + * + * @param {RequestCb} [callback] + * @return {Promise} + */ + list: function(callback) { + let options = { uri: api }; + return client.get(options).asCallback(callback); }, - find: function(domain, callback) { //retrieve - var options; - - if(typeof domain === 'function') { - callback = domain; - domain = null; - } - if(!domain) { + /** + * Get a single sending domain, by domain + * + * @param {string} domain - The domain name to get + * @param {RequestCb} [callback] + * @return {Promise} + */ + get: function(domain, callback) { + let options; + + if (!domain || typeof domain === 'function') { return Promise.reject(new Error('domain is required')).asCallback(callback); } options = { uri: api + '/' + domain }; + return client.get(options).asCallback(callback); }, - create: function(domainBody, callback) { - var options; - if(typeof domainBody === 'function') { - callback = domainBody; - domainBody = null; - } - - if(!domainBody) { - return Promise.reject(new Error('domainBody is required')).asCallback(callback); - } - - if(!domainBody.domain) { - return Promise.reject(new Error('domain is required in the domainBody')).asCallback(callback); + /** + * Creates a new sending domain + * + * @param {Object} createOpts - attributes used to create the new domain + * @param {RequestCb} [callback] + * @return {Promise} + */ + create: function(createOpts, callback) { + let options; + + if (!createOpts || typeof createOpts !== 'object') { + return Promise.reject(new Error('create options are required')).asCallback(callback); } options = { - uri: api - , json: toApiFormat(domainBody) + uri: api, + json: createOpts }; + return client.post(options).asCallback(callback); }, - update: function(domainBody, callback) { - var obj, options; - if(typeof domainBody === 'function') { - callback = domainBody; - domainBody = null; - } - - if(!domainBody) { - return Promise.reject(new Error('domainBody is required')).asCallback(callback); + /** + * Update an existing sending domain + * + * @param {string} domain - The domain to update + * @param {Object} updateOpts - Hash of the sending domain attributes to update + * @param {RequestCb} [callback] + * @return {Promise} + */ + update: function(domain, updateOpts, callback) { + let options; + + if (typeof domain !== 'string') { + return Promise.reject(new Error('domain is required')).asCallback(callback); } - if(!domainBody.domain) { - return Promise.reject(new Error('domain is required in the domainBody')).asCallback(callback); + if (!updateOpts || typeof updateOpts !== 'object') { + return Promise.reject(new Error('update options are required')).asCallback(callback); } - obj = toApiFormat(domainBody); options = { - uri: api + '/' + obj.domain - , json: toApiFormat(domainBody) + uri: api + '/' + domain, + json: updateOpts }; + return client.put(options).asCallback(callback); }, - delete: function(domain, callback) { - var options; - if (typeof domain === 'function') { - callback = domain; - domain = null; - } + /** + * Delete an existing sending domain + * + * @param {string} domain - The domain to delete + * @param {RequestCb} [callback] + * @return {Promise} + */ + delete: function(domain, callback) { + let options; - if (!domain) { + if (typeof domain !== 'string') { return Promise.reject(new Error('domain is required')).asCallback(callback); } options = { uri: api + '/' + domain }; + return client.delete(options).asCallback(callback); }, - verify: function(options, callback) { - var reqOpts; - options = options || {}; - if(!options.domain) { + /** + * Verify an existing sending domain + * + * @param {string} domain - The domain to verify + * @param {Object} options - Hash of options to include in verification request + * @param {RequestCb} [callback] + * @return {Promise} + */ + verify: function(domain, options, callback) { + let reqOpts; + + if (typeof domain !== 'string') { return Promise.reject(new Error('domain is required')).asCallback(callback); } + if (!options || typeof options !== 'object') { + return Promise.reject(new Error('verification options are required')).asCallback(callback); + } + reqOpts = { - uri: api + '/' + options.domain + '/verify', - json: { - dkim_verify: options.verifyDKIM !== false, - spf_verify: options.verifySPF !== false - } + uri: api + '/' + domain + '/verify', + json: options }; return client.post(reqOpts).asCallback(callback); } }; - return sendingDomains; }; diff --git a/test/spec/sendingDomains.spec.js b/test/spec/sendingDomains.spec.js index c127be8..e8d3aa7 100644 --- a/test/spec/sendingDomains.spec.js +++ b/test/spec/sendingDomains.spec.js @@ -1,202 +1,124 @@ -var chai = require('chai') +'use strict'; + +var _ = require('lodash') + , chai = require('chai') , expect = chai.expect - , sinon = require('sinon') - , sinonChai = require('sinon-chai') - , Promise = require('../../lib/Promise'); + , sinon = require('sinon'); + +require('sinon-as-promised'); -chai.use(sinonChai); +chai.use(require('sinon-chai')); +chai.use(require('chai-as-promised')); describe('Sending Domains Library', function() { var client, sendingDomains; beforeEach(function() { client = { - get: sinon.stub().returns(Promise.resolve({})), - post: sinon.stub().returns(Promise.resolve({})), - put: sinon.stub().returns(Promise.resolve({})), - delete: sinon.stub().returns(Promise.resolve({})) + get: sinon.stub().resolves({}), + post: sinon.stub().resolves({}), + put: sinon.stub().resolves({}), + delete: sinon.stub().resolves({}) }; sendingDomains = require('../../lib/sendingDomains')(client); }); - describe('all Method', function() { - it('should call client get method with the appropriate uri', function(done) { - sendingDomains.all().then(function() { - expect(client.get.firstCall.args[0]).to.deep.equal({uri:'sending-domains'}); - done(); - }); + describe('list', function() { + it('should call client get method with the appropriate uri', function() { + return sendingDomains.list() + .then(function() { + expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'sending-domains'}); + }); }); }); - describe('find Method', function() { - it('should call client get method with the appropriate uri', function(done) { - sendingDomains.find('test').then(function() { - expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'sending-domains/test'}); - done(); - }); + describe('get', function() { + it('should call client get method with the appropriate uri', function() { + return sendingDomains.get('test') + .then(function() { + expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'sending-domains/test'}); + }); }); - it('should throw an error if domain is null', function(done) { - sendingDomains.find(null).catch(function(err) { - expect(err.message).to.equal('domain is required'); - expect(client.get).not.to.have.been.called; - done(); - }); - }); - - it('should throw an error if domain is missing', function(done) { - sendingDomains.find(function(err) { - expect(err.message).to.equal('domain is required'); - expect(client.get).not.to.have.been.called; - done(); - }); + it('should throw an error if domain is missing', function() { + return expect(sendingDomains.get()).to.be.rejectedWith('domain is required'); }); }); - describe('create Method', function() { - it('should call client post method with the appropriate uri', function(done) { - var domainBody = { - domain: "test" + describe('create', function() { + it('should call client post method with the appropriate uri and payload', function() { + var sendingDomain = { + domain: 'test' }; - sendingDomains.create(domainBody).then(function(data) { + return sendingDomains.create(sendingDomain).then(function() { expect(client.post.firstCall.args[0].uri).to.equal('sending-domains'); - done(); + expect(client.post.firstCall.args[0].json).to.deep.equal(sendingDomain); }); }); - it('should throw an error if domainBody is null', function(done) { - sendingDomains.create(null, function(err) { - expect(err.message).to.equal('domainBody is required'); - expect(client.post).not.to.have.been.called; - done(); - }); - }); - - it('should throw an error if domainBody is missing', function(done) { - sendingDomains.create(function(err) { - expect(err.message).to.equal('domainBody is required'); - expect(client.post).not.to.have.been.called; - done(); - }); - }); - - it('should throw an error if domain is missing from domainBody', function(done) { - sendingDomains.create({}, function(err){ - expect(err.message).to.equal('domain is required in the domainBody'); - expect(client.post).not.to.have.been.called; - done(); - }); + it('should throw an error if create options are missing', function() { + return expect(sendingDomains.create()).to.be.rejectedWith('create options are required'); }); }); - describe('update Method', function() { - it('should call client put method with the appropriate uri', function(done) { - var domainBody = { - domain: "test" + describe('update', function() { + it('should call client put method with the appropriate uri and payload', function() { + var sendingDomain = { + tracking_domain: 'click.example1.com' }; - sendingDomains.update(domainBody, function(err, data) { - expect(client.put.firstCall.args[0].uri).to.equal('sending-domains/test'); - done(); - }); + return sendingDomains.update('test', sendingDomain) + .then(function() { + expect(client.put.firstCall.args[0].uri).to.equal('sending-domains/test'); + expect(client.put.firstCall.args[0].json).to.deep.equal(_.omit(sendingDomain, 'domain')); + }); }); - it('should throw an error if domainBody is null', function(done) { - sendingDomains.update(null, function(err) { - expect(err.message).to.equal('domainBody is required'); - expect(client.put).not.to.have.been.called; - done(); - }); + it('should throw an error if update options are missing', function() { + return expect(sendingDomains.update('test')).to.be.rejectedWith('update options are required'); }); - it('should throw an error if domainBody is missing', function(done) { - sendingDomains.update(function(err) { - expect(err.message).to.equal('domainBody is required'); - expect(client.put).not.to.have.been.called; - done(); - }); - }); - - it('should throw an error if domain is missing from domainBody', function(done) { - sendingDomains.update({}, function(err){ - expect(err.message).to.equal('domain is required in the domainBody'); - expect(client.put).not.to.have.been.called; - done(); - }); + it('should throw an error if domain is missing', function() { + return expect(sendingDomains.update()).to.be.rejectedWith('domain is required'); }); }); - describe('delete Method', function() { - it('should call client delete method with the appropriate uri', function(done) { - sendingDomains.delete('test', function(err, data) { - expect(client.delete.firstCall.args[0].uri).to.equal('sending-domains/test'); - done(); - }); - }); - - it('should throw an error if domain is null', function(done) { - sendingDomains.delete(null, function(err) { - expect(err.message).to.equal('domain is required'); - expect(client.delete).not.to.have.been.called; - done(); - }); + describe('delete', function() { + it('should call client delete method with the appropriate uri', function() { + return sendingDomains.delete('test') + .then(function() { + expect(client.delete.firstCall.args[0].uri).to.equal('sending-domains/test'); + }); }); - it('should throw an error if domain is missing', function(done) { - sendingDomains.delete(function(err) { - expect(err.message).to.equal('domain is required'); - expect(client.delete).not.to.have.been.called; - done(); - }); + it('should throw an error if domain is missing', function() { + return expect(sendingDomains.delete()).to.be.rejectedWith('domain is required'); }); }); - describe('verify Method', function() { - it('should call client post method with the appropriate uri', function(done) { + describe('verify', function() { + it('should call client post method with the appropriate uri and payload', function() { var options = { - domain: 'test' + dkim_verify: true, + spf_verify: true }; - sendingDomains.verify(options, function() { - expect(client.post.firstCall.args[0].uri).to.equal('sending-domains/test/verify'); - done(); - }); - }); - - it('should throw an error if domain is missing', function(done) { - sendingDomains.verify(null, function(err) { - expect(err.message).to.equal('domain is required'); - expect(client.post).not.to.have.been.called; - done(); - }); + return sendingDomains.verify('test', options) + .then(function() { + expect(client.post.firstCall.args[0].uri).to.equal('sending-domains/test/verify'); + expect(client.post.firstCall.args[0].json).to.deep.equal(_.omit(options, 'domain')); + }); }); - it('should default verifyDKIM and verifySPF to be true', function(done) { - var options = { - domain: 'test' - }; - - sendingDomains.verify(options, function() { - expect(client.post.firstCall.args[0].json.dkim_verify).to.be.true; - expect(client.post.firstCall.args[0].json.spf_verify).to.be.true; - done(); - }); + it('should throw an error if domain is missing', function() { + return expect(sendingDomains.verify()).to.be.rejectedWith('domain is required'); }); - it('should allow a user to set verifyDKIM and verifySPF', function(done){ - var options = { - domain: 'test', - verifyDKIM: false, - verifySPF: false - }; - - sendingDomains.verify(options, function() { - expect(client.post.firstCall.args[0].json.dkim_verify).to.be.false; - expect(client.post.firstCall.args[0].json.spf_verify).to.be.false; - done(); - }); + it('should throw an error if verification options are missing', function() { + return expect(sendingDomains.verify('test')).to.be.rejectedWith('verification options are required'); }); }); + });