├── .babelrc ├── .codeclimate.yml ├── .github ├── contributing.md ├── issue_template.md └── pull_request_template.md ├── .gitignore ├── .istanbul.yml ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── example └── app.js ├── mocha.opts ├── package.json ├── src └── index.js └── test ├── index.test.js └── test-app.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["add-module-exports"], 3 | "presets": [ "es2015" ] 4 | } 5 | -------------------------------------------------------------------------------- /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | languages: 2 | JavaScript: true 3 | # exclude_paths: 4 | # - "foo/bar.rb" -------------------------------------------------------------------------------- /.github/contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Feathers 2 | 3 | Thank you for contributing to Feathers! :heart: :tada: 4 | 5 | This repo is the main core and where most issues are reported. Feathers embraces modularity and is broken up across many repos. To make this easier to manage we currently use [Zenhub](https://www.zenhub.com/) for issue triage and visibility. They have a free browser plugin you can install so that you can see what is in flight at any time, but of course you also always see current issues in Github. 6 | 7 | ## Report a bug 8 | 9 | Before creating an issue please make sure you have checked out the docs, specifically the [FAQ](https://docs.feathersjs.com/help/faq.html) section. You might want to also try searching Github. It's pretty likely someone has already asked a similar question. 10 | 11 | If you haven't found your answer please feel free to join our [slack channel](http://slack.feathersjs.com), create an issue on Github, or post on [Stackoverflow](http://stackoverflow.com) using the `feathers` or `feathersjs` tag. We try our best to monitor Stackoverflow but you're likely to get more immediate responses in Slack and Github. 12 | 13 | Issues can be reported in the [issue tracker](https://github.com/feathersjs/feathers/issues). Since feathers combines many modules it can be hard for us to assess the root cause without knowing which modules are being used and what your configuration looks like, so **it helps us immensely if you can link to a simple example that reproduces your issue**. 14 | 15 | ## Report a Security Concern 16 | 17 | We take security very seriously at Feathers. We welcome any peer review of our 100% open source code to ensure nobody's Feathers app is ever compromised or hacked. As a web application developer you are responsible for any security breaches. We do our very best to make sure Feathers is as secure as possible by default. 18 | 19 | In order to give the community time to respond and upgrade we strongly urge you report all security issues to us. Send one of the core team members a PM in [Slack](http://slack.feathersjs.com) or email us at hello@feathersjs.com with details and we will respond ASAP. 20 | 21 | For full details refer to our [Security docs](https://docs.feathersjs.com/SECURITY.html). 22 | 23 | ## Pull Requests 24 | 25 | We :heart: pull requests and we're continually working to make it as easy as possible for people to contribute, including a [Plugin Generator](https://github.com/feathersjs/generator-feathers-plugin) and a [common test suite](https://github.com/feathersjs/feathers-service-tests) for database adapters. 26 | 27 | We prefer small pull requests with minimal code changes. The smaller they are the easier they are to review and merge. A core team member will pick up your PR and review it as soon as they can. They may ask for changes or reject your pull request. This is not a reflection of you as an engineer or a person. Please accept feedback graciously as we will also try to be sensitive when providing it. 28 | 29 | Although we generally accept many PRs they can be rejected for many reasons. We will be as transparent as possible but it may simply be that you do not have the same context or information regarding the roadmap that the core team members have. We value the time you take to put together any contributions so we pledge to always be respectful of that time and will try to be as open as possible so that you don't waste it. :smile: 30 | 31 | **All PRs (except documentation) should be accompanied with tests and pass the linting rules.** 32 | 33 | ### Code style 34 | 35 | Before running the tests from the `test/` folder `npm test` will run ESlint. You can check your code changes individually by running `npm run lint`. 36 | 37 | ### ES6 compilation 38 | 39 | Feathers uses [Babel](https://babeljs.io/) to leverage the latest developments of the JavaScript language. All code and samples are currently written in ES2015. To transpile the code in this repository run 40 | 41 | > npm run compile 42 | 43 | __Note:__ `npm test` will run the compilation automatically before the tests. 44 | 45 | ### Tests 46 | 47 | [Mocha](http://mochajs.org/) tests are located in the `test/` folder and can be run using the `npm run mocha` or `npm test` (with ESLint and code coverage) command. 48 | 49 | ### Documentation 50 | 51 | Feathers documentation is contained in Markdown files in the [feathers-docs](https://github.com/feathersjs/feathers-docs) repository. To change the documentation submit a pull request to that repo, referencing any other PR if applicable, and the docs will be updated with the next release. 52 | 53 | ## External Modules 54 | 55 | If you're written something awesome for Feathers, the Feathers ecosystem, or using Feathers please add it to the [showcase](https://docs.feathersjs.com/why/showcase.html). You also might want to check out the [Plugin Generator](https://github.com/feathersjs/generator-feathers-plugin) that can be used to scaffold plugins to be Feathers compliant from the start. 56 | 57 | If you think it would be a good core module then please contact one of the Feathers core team members in [Slack](http://slack.feathersjs.com) and we can discuss whether it belongs in core and how to get it there. :beers: 58 | 59 | ## Contributor Code of Conduct 60 | 61 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 62 | 63 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 64 | 65 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 66 | 67 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 68 | 69 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 70 | 71 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 72 | -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | ### Steps to reproduce 2 | 3 | (First please check that this issue is not already solved as [described 4 | here](https://github.com/feathersjs/feathers/blob/master/.github/contributing.md#report-a-bug)) 5 | 6 | - [ ] Tell us what broke. The more detailed the better. 7 | - [ ] If you can, please create a simple example that reproduces the issue and link to a gist, jsbin, repo, etc. 8 | 9 | ### Expected behavior 10 | Tell us what should happen 11 | 12 | ### Actual behavior 13 | Tell us what happens instead 14 | 15 | ### System configuration 16 | 17 | Tell us about the applicable parts of your setup. 18 | 19 | **Module versions** (especially the part that's not working): 20 | 21 | **NodeJS version**: 22 | 23 | **Operating System**: 24 | 25 | **Browser Version**: 26 | 27 | **React Native Version**: 28 | 29 | **Module Loader**: -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ### Summary 2 | 3 | (If you have not already please refer to the contributing guideline as [described 4 | here](https://github.com/feathersjs/feathers/blob/master/.github/contributing.md#pull-requests)) 5 | 6 | - [ ] Tell us about the problem your pull request is solving. 7 | - [ ] Are there any open issues that are related to this? 8 | - [ ] Is this PR dependent on PRs in other repos? 9 | 10 | If so, please mention them to keep the conversations linked together. 11 | 12 | ### Other Information 13 | 14 | If there's anything else that's important and relevant to your pull 15 | request, mention that information here. This could include 16 | benchmarks, or other information. 17 | 18 | Your PR will be reviewed by a core team member and they will work with you to get your changes merged in a timely manner. If merged your PR will automatically be added to the changelog in the next release. 19 | 20 | If your changes involve documentation updates please mention that and link the appropriate PR in [feathers-docs](https://github.com/feathersjs/feathers-docs). 21 | 22 | Thanks for contributing to Feathers! :heart: -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # === Feathers NeDB === 2 | db-data 3 | 4 | # === Node === 5 | lib-cov 6 | *.seed 7 | *.log 8 | *.csv 9 | *.dat 10 | *.out 11 | *.pid 12 | *.gz 13 | 14 | pids 15 | logs 16 | results 17 | 18 | npm-debug.log 19 | node_modules 20 | 21 | # === Mac === 22 | .DS_Store 23 | .AppleDouble 24 | .LSOverride 25 | 26 | # Icon must ends with two \r. 27 | Icon 28 | 29 | 30 | # Thumbnails 31 | ._* 32 | 33 | # Files that might appear on external disk 34 | .Spotlight-V100 35 | .Trashes 36 | 37 | # === Vim === 38 | [._]*.s[a-w][a-z] 39 | [._]s[a-w][a-z] 40 | *.un~ 41 | Session.vim 42 | .netrwhist 43 | *~ 44 | 45 | # === Sublime === 46 | # workspace files are user-specific 47 | *.sublime-workspace 48 | 49 | # project files should be checked into the repository, unless a significant 50 | # proportion of contributors will probably not be using SublimeText 51 | # *.sublime-project 52 | 53 | # === Webstorm === 54 | .idea 55 | 56 | lib/ 57 | coverage -------------------------------------------------------------------------------- /.istanbul.yml: -------------------------------------------------------------------------------- 1 | verbose: false 2 | instrumentation: 3 | root: src/ 4 | excludes: 5 | - lib/ 6 | include-all-sources: true 7 | reporting: 8 | print: summary 9 | reports: 10 | - html 11 | - text 12 | - lcov 13 | watermarks: 14 | statements: [50, 80] 15 | lines: [50, 80] 16 | functions: [50, 80] 17 | branches: [50, 80] -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .babelrc 2 | .codeclimate.yml 3 | .travis.yml 4 | .idea/ 5 | src/ 6 | test/ 7 | !lib/ 8 | .github/ 9 | coverage 10 | .editorconfig 11 | .istanbul.yml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - node 5 | - '6' 6 | - '4' 7 | before_script: 8 | - 'npm install -g codeclimate-test-reporter' 9 | after_script: 10 | - 'codeclimate-test-reporter < coverage/lcov.info' 11 | addons: 12 | code_climate: 13 | repo_token: 67f78a8087d748aaea3fb36fa13fc3cf13db12c949eed3904463b1a679df0b65 14 | notifications: 15 | email: false 16 | slack: 17 | rooms: 18 | secure: j+8VT/re2Pz9RkE19FFOM8Obg/8VNnttrAdf4un2pXNesVHS8kAOJ6CquGCpgr7c+32bWCDE+dV7+sPhVt5wbGSKXiBUUQLBKPEwkNvqmzxGEO9cxuBkDmOofAxdccNgr7HPvJ61+hjCL+J2GtM1SFejIEP5Cg+4yWftK4NW2Kd7PbtuExDBgcHnbvEwH1WMLSgKyDQGdNaWyQkNu15Cn+ocAJMdVuEqfPyw9zhhilnJmAyNt5kAjZUkcRnoXOoqhOWh4vxIEiHBePv4ArVOWSes4lWhXEL2Mb1zSX9mRvie97c6gkYJ+tOWW5ikJqjV1TR1jg5v2S/4rnN3XJ0Vhn0a4pKeUpOjS1cV+jE3Xjwic0McXjjkhixEvZ9WOyhtnzbnbYC39pIe8ILJXgSlD4ZMiEoKBpJQIuW/JmdW3zsF2c9nCzKL9Y0EyTpToStbQDQbTWqYrJJpnnufTv7vNYxwIThTziwShGJFJ7NeQxLgmvhSpaq25SwhArDgMmS42TO9VZ8S0yr3OAKlxoVAKIQ5qmCeBkUHSDhSiEoiRwRGnMSsyBaf1AuRYj957AhP3fR8jG63eNMhfoJQqGdsVgr4/lwXyoAc+XsQPLytk6WMGpUVulvi04ONVsaQtWoIy5hZFth2z/TzXkfP0XRoOb88iEsYVoR/PzO4uDlSamk= 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 FeathersJS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # feathers-mailgun 2 | 3 | > A [Mailgun](https://www.mailgun.com) Service for [FeatherJS](https://github.com/feathersjs). 4 | 5 | ## Unmaintained 6 | 7 | > This module is no longer maintained. Use [feathers-mailer](https://github.com/feathersjs-ecosystem/feathers-mailer) with the [nodemailer-mailgun-transport](https://github.com/orliesaurus/nodemailer-mailgun-transport) instead. 8 | 9 | ## Installation 10 | 11 | ```bash 12 | npm install mailgun-js feathers-mailgun --save 13 | ``` 14 | 15 | ## Documentation 16 | 17 | ```js 18 | 19 | // Register the service, see below for an example 20 | app.use('/mailer', mailgunService({ 21 | apiKey: "YOUR_MAILGUN_API_KEY", 22 | domain: 'YOUR_MAILGUN_DOMAIN' // ex. your.domain.com 23 | } 24 | )); 25 | 26 | // Use the service 27 | var email = { 28 | from: 'FROM_EMAIL', 29 | to: 'TO_EMAIL', 30 | subject: 'Mailgun test', 31 | html: 'This is the email body' 32 | }; 33 | 34 | app.service('mailer').create(email).then(function (result) { 35 | console.log('Sent email', result); 36 | }).catch(err => { 37 | console.log(err); 38 | }); 39 | 40 | ``` 41 | 42 | ## Complete Example 43 | 44 | Here's an example of a Feathers server with a `mailer` Mailgun service. 45 | 46 | ```js 47 | import rest = from 'feathers-rest'; 48 | import feathers from 'feathers'; 49 | import bodyParser from 'body-parser'; 50 | import mailgunService from '../lib'; 51 | 52 | 53 | // Create a feathers instance. 54 | var app = feathers() 55 | // Enable REST services 56 | .configure(rest()) 57 | // Turn on JSON parser for REST services 58 | .use(bodyParser.json()) 59 | // Turn on URL-encoded parser for REST services 60 | .use(bodyParser.urlencoded({extended: true})); 61 | 62 | // Register the Mailgun service 63 | app.use('/mailer', mailgunService({ 64 | apiKey: "YOUR_MAILGUN_API_KEY", 65 | domain: 'YOUR_MAILGUN_DOMAIN' // ex. your.domain.com 66 | } 67 | )); 68 | 69 | // Use the service 70 | var email = { 71 | from: 'FROM_EMAIL', 72 | to: 'TO_EMAIL', 73 | subject: 'Mailgun test', 74 | html: 'This is the email body' 75 | }; 76 | 77 | app.service('mailer').create(email).then(function (result) { 78 | console.log('Sent email', result); 79 | }).catch(err => { 80 | console.log(err); 81 | }); 82 | 83 | // Start the server. 84 | var port = 3030; 85 | app.listen(port, function() { 86 | console.log(`Feathers server listening on port ${port}`); 87 | }); 88 | ``` 89 | 90 | You can run this example by using `node examples/app`. Make sure you've added your 91 | 92 | ## License 93 | 94 | Copyright (c) 2016 95 | 96 | Licensed under the [MIT license](LICENSE). 97 | 98 | 99 | ## Author 100 | 101 | [Cory Smith](https://github.com/corymsmith) 102 | -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | const feathers = require('feathers'); 2 | const rest = require('feathers-rest'); 3 | const socketio = require('feathers-socketio'); 4 | const bodyParser = require('body-parser'); 5 | const errorHandler = require('feathers-errors/handler'); 6 | const mailgunService = require('../lib'); 7 | 8 | // Create a feathers instance. 9 | var app = feathers() 10 | // Enable REST services 11 | .configure(rest()) 12 | // Enable Socket.io services 13 | .configure(socketio()) 14 | // Turn on JSON parser for REST services 15 | .use(bodyParser.json()) 16 | // Turn on URL-encoded parser for REST services 17 | .use(bodyParser.urlencoded({extended: true})); 18 | 19 | app.use('/mailer', mailgunService({ 20 | // apiKey: 'API_KEY', 21 | // domain: 'DOMAIN' // ex. your.domain.com 22 | } 23 | )); 24 | 25 | // Send an email! 26 | app.service('mailer').create({ 27 | // from: 'FROM_EMAIL', 28 | // to: 'TO_EMAIL', 29 | subject: 'Mailgun test', 30 | html: 'Email body' 31 | // 'h: Reply-To': 'REPLY_TO_EMAIL' 32 | }).then(function (result) { 33 | console.log('Sent email', result); 34 | }).catch(err => { 35 | console.log(err); 36 | }); 37 | 38 | app.use(errorHandler()); 39 | 40 | // Start the server. 41 | const port = 3030; 42 | 43 | app.listen(port, function () { 44 | console.log(`Feathers server listening on port ${port}`); 45 | }); 46 | -------------------------------------------------------------------------------- /mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive test/ 2 | --compilers js:babel-core/register -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "feathers-mailgun", 3 | "description": "Feathers Mailgun Service", 4 | "version": "0.1.2", 5 | "homepage": "https://github.com/feathersjs/feathers-mailgun", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/feathersjs/feathers-mailgun.git" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/feathersjs/feathers-mailgun/issues" 12 | }, 13 | "license": "MIT", 14 | "keywords": [ 15 | "feathers", 16 | "feathers-plugin", 17 | "REST", 18 | "Socket.io", 19 | "realtime", 20 | "mailgun", 21 | "service" 22 | ], 23 | "author": "Feathers (http://feathersjs.com)", 24 | "contributors": [ 25 | "Cory Smith (https://github.com/corymsmith)", 26 | "Marshall Thompson (https://github.com/marshallswain)", 27 | "Eric Kryski (http://erickryski.com)", 28 | "David Luecke (http://neyeon.com)" 29 | ], 30 | "main": "lib/", 31 | "scripts": { 32 | "prepublish": "npm run compile", 33 | "publish": "git push origin && git push origin --tags", 34 | "release:patch": "npm version patch && npm publish", 35 | "release:minor": "npm version minor && npm publish", 36 | "release:major": "npm version major && npm publish", 37 | "compile": "rimraf lib/ && babel -d lib/ src/", 38 | "watch": "babel --watch -d lib/ src/", 39 | "lint": "eslint-if-supported semistandard --fix", 40 | "mocha": "mocha --opts mocha.opts", 41 | "test": "rimraf db-data && npm run compile && npm run lint && npm run coverage", 42 | "start": "node example/app", 43 | "coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --opts mocha.opts" 44 | }, 45 | "semistandard": { 46 | "env": [ 47 | "mocha" 48 | ], 49 | "ignore": [ 50 | "/lib" 51 | ] 52 | }, 53 | "engines": { 54 | "node": ">= 4" 55 | }, 56 | "dependencies": { 57 | "babel-polyfill": "^6.3.14", 58 | "feathers-errors": "^2.0.1", 59 | "mailgun-js": "^0.13.0" 60 | }, 61 | "devDependencies": { 62 | "async": "^2.3.0", 63 | "babel-cli": "^6.3.17", 64 | "babel-core": "^6.3.26", 65 | "babel-plugin-add-module-exports": "^0.2.1", 66 | "babel-preset-es2015": "^6.3.13", 67 | "body-parser": "^1.13.2", 68 | "chai": "^4.1.0", 69 | "eslint-if-supported": "^1.0.1", 70 | "feathers": "^2.0.0-pre.4", 71 | "feathers-rest": "^1.1.1", 72 | "feathers-socketio": "^2.0.0", 73 | "istanbul": "^1.1.0-alpha.1", 74 | "mocha": "^5.0.0", 75 | "rimraf": "^2.5.4", 76 | "semistandard": "^12.0.0", 77 | "sinon": "^4.0.0", 78 | "sinon-chai": "^2.8.0" 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import errors from 'feathers-errors'; 2 | import Mailgun from 'mailgun-js'; 3 | 4 | if (!global._babelPolyfill) { require('babel-polyfill'); } 5 | 6 | class Service { 7 | constructor (options = {}) { 8 | if (!options.apiKey) { 9 | throw new Error('Mailgun `apiKey` needs to be provided'); 10 | } 11 | 12 | if (!options.domain) { 13 | throw new Error('Mailgun `domain` needs to be provided'); 14 | } 15 | 16 | this.options = options; 17 | this.mailgun = new Mailgun({apiKey: options.apiKey, domain: options.domain}); 18 | } 19 | 20 | _send (data, callback) { 21 | return this.mailgun.messages().send(data, callback); 22 | } 23 | 24 | create (data) { 25 | return new Promise((resolve, reject) => { 26 | this._validateParams(data); 27 | this._send(this._formatData(data), function (err, body) { 28 | if (err) { 29 | return reject(err); 30 | } else { 31 | return resolve(body); 32 | } 33 | }); 34 | }); 35 | } 36 | 37 | _validateParams (data) { 38 | if (!data.from) { 39 | throw new errors.BadRequest('`from` must be specified'); 40 | } 41 | 42 | if (!data.to) { 43 | throw new errors.BadRequest('`to` must be specified'); 44 | } 45 | 46 | if (!data.subject) { 47 | throw new errors.BadRequest('`subject` must be specified'); 48 | } 49 | 50 | if (!data.html) { 51 | throw new errors.BadRequest('`html` must be specified'); 52 | } 53 | } 54 | 55 | // Convert array of emails to comma delimited if needed 56 | _formatData (data) { 57 | var to = data.to; 58 | if (typeof data.to === 'object') { 59 | to = data.to.join(','); 60 | } 61 | 62 | return Object.assign(data, { to: to }); 63 | } 64 | } 65 | 66 | export default function init (options) { 67 | return new Service(options); 68 | } 69 | 70 | init.Service = Service; 71 | -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | import chai, { expect } from 'chai'; 2 | import assert from 'assert'; 3 | import sinon from 'sinon'; 4 | import sinonChai from 'sinon-chai'; 5 | import feathers from 'feathers'; 6 | 7 | import server from './test-app'; 8 | import service from '../src'; 9 | 10 | chai.use(sinonChai); 11 | 12 | const mailgun = service({apiKey: 'API_KEY', domain: 'DOMAIN'}); 13 | const app = feathers().use('/mailer', mailgun); 14 | 15 | const validParams = { 16 | from: 'from@from.com', 17 | to: 'to@to.com', 18 | subject: 'Email Subject', 19 | html: 'message html', 20 | 'h:Reply-To': 'reply-here@from.com', 21 | 'random field': 'This will be ignored' 22 | }; 23 | 24 | const validParamsWithArrayInToField = { 25 | from: 'from@from.com', 26 | to: ['to@to.com', 'to2@to.com'], 27 | subject: 'Email Subject', 28 | html: 'message html', 29 | 'h:Reply-To': 'reply-here@from.com', 30 | 'random field': 'This will be ignored' 31 | }; 32 | 33 | describe('Mailgun Service', function () { 34 | after(done => server.close(() => done())); 35 | 36 | describe('Initialization', () => { 37 | describe('without an api key', () => { 38 | it('throws an error', () => { 39 | expect(service.bind(null, {})).to.throw('Mailgun `apiKey` needs to be provided'); 40 | }); 41 | }); 42 | 43 | describe('without a domain', () => { 44 | it('throws an error', () => { 45 | expect(service.bind(null, {apiKey: 'API_KEY'})).to.throw('Mailgun `domain` needs to be provided'); 46 | }); 47 | }); 48 | }); 49 | 50 | describe('Validation', () => { 51 | describe('when missing `from` field', () => { 52 | it('throws an error', (done) => { 53 | app.service('mailer').create({}).then(done).catch(err => { 54 | assert.equal(err.code, 400); 55 | assert.equal(err.message, '`from` must be specified'); 56 | done(); 57 | }); 58 | }); 59 | }); 60 | 61 | describe('when missing `to` field', () => { 62 | it('throws an error', (done) => { 63 | app.service('mailer').create({from: 'from@from.com'}).then(done).catch(err => { 64 | assert.equal(err.code, 400); 65 | assert.equal(err.message, '`to` must be specified'); 66 | done(); 67 | }); 68 | }); 69 | }); 70 | 71 | describe('when missing `subject` field', () => { 72 | it('throws an error', (done) => { 73 | app.service('mailer').create({from: 'from@from.com', to: 'to@to.com'}).then(done).catch(err => { 74 | assert.equal(err.code, 400); 75 | assert.equal(err.message, '`subject` must be specified'); 76 | done(); 77 | }); 78 | }); 79 | }); 80 | 81 | describe('when missing `html` field', () => { 82 | it('throws an error', (done) => { 83 | app.service('mailer').create({ 84 | from: 'from@from.com', 85 | to: 'to@to.com', 86 | subject: 'Email Subject' 87 | }).then(done).catch(err => { 88 | assert.equal(err.code, 400); 89 | assert.equal(err.message, '`html` must be specified'); 90 | done(); 91 | }); 92 | }); 93 | }); 94 | }); 95 | 96 | describe('Sending messages', () => { 97 | var mailgunSend; 98 | beforeEach(function (done) { 99 | mailgunSend = 100 | sinon 101 | .stub(app.service('mailer'), '_send').callsFake(function (data, callback) { 102 | callback(null, {success: true}); 103 | }); 104 | done(); 105 | }); 106 | 107 | afterEach(function (done) { 108 | mailgunSend.restore(); 109 | done(); 110 | }); 111 | 112 | describe('when sending to an array of email addresses', () => { 113 | it('correctly parses into a comma delimited string', (done) => { 114 | var expectedParams = { 115 | from: validParamsWithArrayInToField.from, 116 | to: 'to@to.com,to2@to.com', 117 | subject: validParamsWithArrayInToField.subject, 118 | html: validParamsWithArrayInToField.html, 119 | 'h:Reply-To': validParamsWithArrayInToField['h:Reply-To'], 120 | 'random field': validParamsWithArrayInToField['random field'] // This will be ignored by mailgun-js 121 | }; 122 | app.service('mailer').create(validParamsWithArrayInToField).then(result => { 123 | expect(result.success).to.eql(true); 124 | expect(mailgunSend).to.have.been.calledWith(expectedParams); 125 | done(); 126 | }); 127 | }); 128 | }); 129 | 130 | describe('when all fields are valid', () => { 131 | it('successfully sends a message', (done) => { 132 | app.service('mailer').create(validParams).then(result => { 133 | expect(result.success).to.eql(true); 134 | expect(mailgunSend).to.have.been.calledWith(validParams); 135 | done(); 136 | }); 137 | }); 138 | }); 139 | }); 140 | 141 | describe('Common functionality', () => { 142 | it('is CommonJS compatible', () => { 143 | assert.ok(typeof require('../lib') === 'function'); 144 | }); 145 | }); 146 | }); 147 | -------------------------------------------------------------------------------- /test/test-app.js: -------------------------------------------------------------------------------- 1 | const feathers = require('feathers'); 2 | const service = require('../lib'); 3 | 4 | // Create the mailgun service 5 | const mailgunService = service({ 6 | apiKey: 'test', 7 | domain: 'mail.feathersjs.com' 8 | }); 9 | 10 | // Create a feathers instance with a mailer service 11 | var app = feathers() 12 | .use('/mailer', mailgunService); 13 | 14 | // Start the server. 15 | const port = 3030; 16 | 17 | module.exports = app.listen(port); 18 | --------------------------------------------------------------------------------