-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use UserAccountEmail from utils for supplier contrib invite
The mechanism for sending invites to new suppliers user contributors is very similar to the mechanism for inviting a supplier user during supplier account creation, so we can reuse the code. The Notify template ID's for the preview and staging/production versions of the invite template are added to the config.
- Loading branch information
Showing
3 changed files
with
64 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
from flask import current_app | ||
|
||
from dmapiclient.audit import AuditTypes | ||
from dmutils.email.exceptions import EmailError | ||
|
||
|
@@ -52,8 +54,8 @@ def test_should_be_an_error_for_missing_email(self): | |
assert res.status_code == 400 | ||
|
||
@mock.patch('app.main.views.login.data_api_client') | ||
@mock.patch('app.main.views.login.send_email') | ||
def test_should_redirect_to_list_users_on_success_invite(self, send_email, data_api_client): | ||
@mock.patch('app.main.views.login.UserAccountEmail') | ||
def test_should_redirect_to_list_users_on_success_invite(self, UserAccountEmail, data_api_client): | ||
with self.app.app_context(): | ||
self.login() | ||
res = self.client.post( | ||
|
@@ -66,8 +68,8 @@ def test_should_redirect_to_list_users_on_success_invite(self, send_email, data_ | |
assert res.location == 'http://localhost/suppliers/users' | ||
|
||
@mock.patch('app.main.views.login.data_api_client') | ||
@mock.patch('app.main.views.login.send_email') | ||
def test_should_strip_whitespace_surrounding_invite_user_email_address_field(self, send_email, data_api_client): | ||
@mock.patch('app.main.views.login.UserAccountEmail') | ||
def test_should_strip_whitespace_around_invite_user_email_address_field(self, UserAccountEmail, data_api_client): | ||
with self.app.app_context(): | ||
self.login() | ||
self.client.post( | ||
|
@@ -76,46 +78,18 @@ def test_should_strip_whitespace_surrounding_invite_user_email_address_field(sel | |
'email_address': ' [email protected] ' | ||
} | ||
) | ||
send_email.assert_called_once_with( | ||
'[email protected]', | ||
mock.ANY, | ||
mock.ANY, | ||
mock.ANY, | ||
mock.ANY, | ||
mock.ANY, | ||
mock.ANY, | ||
) | ||
|
||
@mock.patch('app.main.views.login.data_api_client') | ||
@mock.patch('app.main.views.login.generate_token') | ||
@mock.patch('app.main.views.login.send_email') | ||
def test_should_call_generate_token_with_correct_params(self, send_email, generate_token, data_api_client): | ||
with self.app.app_context(): | ||
|
||
self.app.config['SHARED_EMAIL_KEY'] = "KEY" | ||
self.app.config['INVITE_EMAIL_SALT'] = "SALT" | ||
|
||
self.login() | ||
res = self.client.post( | ||
'/suppliers/invite-user', | ||
data={ | ||
'email_address': '[email protected]' | ||
}) | ||
assert res.status_code == 302 | ||
generate_token.assert_called_once_with( | ||
UserAccountEmail.assert_called_once_with( | ||
{ | ||
"role": "supplier", | ||
"supplier_id": 1234, | ||
"supplier_name": "Supplier NĀme", | ||
"email_address": "[email protected]" | ||
'role': 'supplier', | ||
'supplier_id': mock.ANY, | ||
'supplier_name': mock.ANY, | ||
'email_address': '[email protected]' | ||
}, | ||
'KEY', | ||
'SALT' | ||
current_app.config['NOTIFY_TEMPLATES']['invite_contributor'] | ||
) | ||
|
||
@mock.patch('app.main.views.login.send_email') | ||
@mock.patch('app.main.views.login.generate_token') | ||
def test_should_not_generate_token_or_send_email_if_invalid_email(self, send_email, generate_token): | ||
@mock.patch('app.main.views.login.UserAccountEmail') | ||
def test_should_not_send_email_if_invalid_email(self, UserAccountEmail): | ||
with self.app.app_context(): | ||
|
||
self.login() | ||
|
@@ -125,16 +99,17 @@ def test_should_not_generate_token_or_send_email_if_invalid_email(self, send_ema | |
'email_address': 'total rubbish' | ||
}) | ||
assert res.status_code == 400 | ||
assert send_email.called is False | ||
assert generate_token.called is False | ||
assert UserAccountEmail.send_email.called is False | ||
|
||
@mock.patch('dmutils.email.user_account_email.DMNotifyClient') | ||
def test_should_be_an_error_if_send_invitation_email_fails(self, DMNotifyClient): | ||
notify_client_mock = mock.Mock() | ||
notify_client_mock.send_email.side_effect = EmailError() | ||
DMNotifyClient.return_value = notify_client_mock | ||
|
||
@mock.patch('app.main.views.login.send_email') | ||
def test_should_be_an_error_if_send_invitation_email_fails(self, send_email): | ||
with self.app.app_context(): | ||
self.login() | ||
|
||
send_email.side_effect = EmailError(Exception('API is down')) | ||
|
||
res = self.client.post( | ||
'/suppliers/invite-user', | ||
data={'email_address': '[email protected]', 'name': 'valid'} | ||
|
@@ -143,37 +118,40 @@ def test_should_be_an_error_if_send_invitation_email_fails(self, send_email): | |
assert res.status_code == 503 | ||
|
||
@mock.patch('app.main.views.login.data_api_client') | ||
@mock.patch('app.main.views.login.send_email') | ||
def test_should_call_send_invitation_email_with_correct_params(self, send_email, data_api_client): | ||
with self.app.app_context(): | ||
@mock.patch('app.main.views.login.UserAccountEmail') | ||
def test_should_send_invitation_email_with_correct_params(self, UserAccountEmail, data_api_client): | ||
user_invite_email_mock = mock.Mock() | ||
user_invite_email_mock.token = 'pretty-good-made-up-token' | ||
UserAccountEmail.return_value = user_invite_email_mock | ||
|
||
with self.app.app_context(): | ||
self.login() | ||
|
||
self.app.config['DM_MANDRILL_API_KEY'] = "API KEY" | ||
self.app.config['INVITE_EMAIL_SUBJECT'] = "SUBJECT" | ||
self.app.config['INVITE_EMAIL_FROM'] = "EMAIL FROM" | ||
self.app.config['INVITE_EMAIL_NAME'] = "EMAIL NAME" | ||
|
||
res = self.client.post( | ||
'/suppliers/invite-user', | ||
data={'email_address': '[email protected]', 'name': 'valid'} | ||
) | ||
|
||
assert res.status_code == 302 | ||
|
||
send_email.assert_called_once_with( | ||
"[email protected]", | ||
mock.ANY, | ||
"API KEY", | ||
"SUBJECT", | ||
"EMAIL FROM", | ||
"EMAIL NAME", | ||
["user-invite"] | ||
UserAccountEmail.assert_called_once_with( | ||
{ | ||
'role': 'supplier', | ||
'supplier_id': 1234, | ||
'supplier_name': 'Supplier NĀme', | ||
'email_address': '[email protected]' | ||
}, | ||
current_app.config['NOTIFY_TEMPLATES']['invite_contributor'] | ||
) | ||
|
||
user_invite_email_mock.send_email.assert_called_once_with({ | ||
'user': 'Năme', | ||
'supplier': 'Supplier NĀme', | ||
'url': 'http://localhost/user/create/pretty-good-made-up-token' | ||
}) | ||
|
||
@mock.patch('app.main.views.login.data_api_client') | ||
@mock.patch('app.main.views.login.send_email') | ||
def test_should_create_audit_event(self, send_email, data_api_client): | ||
@mock.patch('app.main.views.login.UserAccountEmail') | ||
def test_should_create_audit_event(self, UserAccountEmail, data_api_client): | ||
with self.app.app_context(): | ||
self.login() | ||
|
||
|