diff --git a/client/app/pages/users/CreateUser.jsx b/client/app/pages/users/CreateUser.jsx index b05df35b57..2105f9f2a7 100644 --- a/client/app/pages/users/CreateUser.jsx +++ b/client/app/pages/users/CreateUser.jsx @@ -37,6 +37,13 @@ class CreateUser extends React.Component { { name: 'email', title: 'Email', type: 'email' }, ].map(field => ({ required: true, readOnly: !!user, ...field })); + const message = user && user.invite_link ? ( +
+ The user has been created. You can use the following link to invite them: + +
+ ) : The user has been created and should receive an invite email soon.; + return (
@@ -50,7 +57,7 @@ class CreateUser extends React.Component { /> {user && ( diff --git a/redash/handlers/users.py b/redash/handlers/users.py index 2d2feab9cb..cabbe39dc5 100644 --- a/redash/handlers/users.py +++ b/redash/handlers/users.py @@ -15,6 +15,7 @@ from redash.authentication.account import invite_link_for_user, send_invite_email, send_password_reset_email, send_verify_email from redash.settings import parse_boolean +from redash import settings # Ordering map for relationships @@ -36,9 +37,17 @@ ) -def invite_user(org, inviter, user): +def invite_user(org, inviter, user, send_email=True): + email_configured = settings.MAIL_DEFAULT_SENDER is not None + d = user.to_dict() + invite_url = invite_link_for_user(user) - send_invite_email(inviter, user, invite_url, org) + if email_configured and send_email: + send_invite_email(inviter, user, invite_url, org) + else: + d['invite_link'] = invite_url + + return d class UserListResource(BaseResource): @@ -137,19 +146,14 @@ def post(self): }) should_send_invitation = 'no_invite' not in request.args - if should_send_invitation: - invite_user(self.current_org, self.current_user, user) - - return user.to_dict() + return invite_user(self.current_org, self.current_user, user, send_email=should_send_invitation) class UserInviteResource(BaseResource): @require_admin def post(self, user_id): user = models.User.get_by_id_and_org(user_id, self.current_org) - invite_url = invite_user(self.current_org, self.current_user, user) - - return user.to_dict() + return invite_user(self.current_org, self.current_user, user) class UserResetPasswordResource(BaseResource): diff --git a/tests/handlers/test_users.py b/tests/handlers/test_users.py index e00bc55e7b..1541e12e2b 100644 --- a/tests/handlers/test_users.py +++ b/tests/handlers/test_users.py @@ -1,4 +1,4 @@ -from redash import models +from redash import models, settings from tests import BaseTestCase from mock import patch @@ -40,6 +40,34 @@ def test_creates_user(self): self.assertEqual(rv.json['name'], test_user['name']) self.assertEqual(rv.json['email'], test_user['email']) + def test_shows_invite_link_when_email_is_not_configured(self): + previous = settings.MAIL_DEFAULT_SENDER + settings.MAIL_DEFAULT_SENDER = None + + admin = self.factory.create_admin() + + test_user = {'name': 'User', 'email': 'user@example.com'} + rv = self.make_request('post', '/api/users', data=test_user, user=admin) + + self.assertEqual(rv.status_code, 200) + self.assertTrue('invite_link' in rv.json) + + settings.MAIL_DEFAULT_SENDER = previous + + def test_does_not_show_invite_link_when_email_is_configured(self): + previous = settings.MAIL_DEFAULT_SENDER + settings.MAIL_DEFAULT_SENDER = "john@doe.com" + + admin = self.factory.create_admin() + + test_user = {'name': 'User', 'email': 'user@example.com'} + rv = self.make_request('post', '/api/users', data=test_user, user=admin) + + self.assertEqual(rv.status_code, 200) + self.assertFalse('invite_link' in rv.json) + + settings.MAIL_DEFAULT_SENDER = previous + def test_creates_user_case_insensitive_email(self): admin = self.factory.create_admin()