Repository: Kinto/kinto-emailer

Issue:
Add a «test send» command 
```
$ kinto-emailer-test config/kinto.ini test@restmail.net
Email sent successfully.
```

Comments:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 5536013..6e5ce32 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -9,6 +9,7 @@ This document describes changes between each past release.
 **New features**
 
 - Add a ``validate_setup.py`` script to check that server can actually send emails
+- Add a ``kinto-send-email`` command to test the configuration (fixes #35)
 
 **Bug fixes**
 
diff --git a/README.rst b/README.rst
index 199f545..43aca1e 100644
--- a/README.rst
+++ b/README.rst
@@ -45,6 +45,15 @@ If ``mail.queue_path`` is set, the emails are storage in a local Maildir queue.
 
 See `more details about Pyramid Mailer configuration <http://docs.pylonsproject.org/projects/pyramid_mailer/en/latest/#configuration>`_.
 
+Validate configuration
+----------------------
+
+The following command will send a dummy email to the specified recipient or will fail if the configuration is not correct:
+
+::
+
+    $ kinto-send-email config/kinto.ini testemailer@restmail.net
+
 
 Development
 -----------
diff --git a/kinto_emailer/command_send.py b/kinto_emailer/command_send.py
new file mode 100644
index 0000000..6bf78d8
--- /dev/null
+++ b/kinto_emailer/command_send.py
@@ -0,0 +1,33 @@
+import sys
+
+from pyramid.paster import bootstrap
+from pyramid_mailer import get_mailer
+from pyramid_mailer.message import Message
+
+
+subject = "[kinto-emailer] Test"
+body = "If you received this email, the server is well configured."
+
+
+def main(args=None):
+    if args is None:
+        args = sys.argv[1:]
+    try:
+        config_file, recipient = args
+    except ValueError:
+        print("Usage: %s CONFIG RECIPIENT" % sys.argv[0])
+        return 1
+
+    print("Load config...")
+    env = bootstrap(config_file)
+
+    print("Send email to %r" % recipient)
+    registry = env['registry']
+    mailer = get_mailer(registry)
+
+    message = Message(subject=subject,
+                      recipients=[recipient],
+                      body=body)
+    mailer.send_immediately(message, fail_silently=False)
+    print("Done.")
+    return 0
diff --git a/setup.py b/setup.py
index 933eb1f..981e05b 100644
--- a/setup.py
+++ b/setup.py
@@ -20,6 +20,12 @@ REQUIREMENTS = [
     'pyramid_mailer',
 ]
 
+ENTRY_POINTS = {
+    'console_scripts': [
+        'kinto-send-email = kinto_emailer.command_send:main'
+    ],
+}
+
 setup(name='kinto-emailer',
       version='0.4.0.dev0',
       description='Kinto emailer plugin',
@@ -38,4 +44,5 @@ setup(name='kinto-emailer',
       packages=find_packages(),
       include_package_data=True,
       zip_safe=False,
-      install_requires=REQUIREMENTS)
+      install_requires=REQUIREMENTS,
+      entry_points=ENTRY_POINTS)


Test patch:
diff --git a/kinto_emailer/tests/test_command_send.py b/kinto_emailer/tests/test_command_send.py
new file mode 100644
index 0000000..0bba54e
--- /dev/null
+++ b/kinto_emailer/tests/test_command_send.py
@@ -0,0 +1,21 @@
+import mock
+import unittest
+
+from kinto_emailer import command_send
+
+
+class CommandTest(unittest.TestCase):
+    def test_uses_sys_args_by_default(self):
+        assert command_send.main() > 0  # will fail
+
+    def test_returns_non_zero_if_not_enough_args(self):
+        assert command_send.main([]) > 0
+
+    def test_calls_send_immmediately_with_parameters(self):
+        with mock.patch('kinto_emailer.command_send.bootstrap'):
+            with mock.patch('kinto_emailer.command_send.get_mailer') as get_mailer:
+                command_send.main(["config.ini", "me@restmail.net"])
+
+                args, kwargs = get_mailer().send_immediately.call_args_list[0]
+                assert "kinto-emailer" in args[0].subject
+                assert not kwargs["fail_silently"]