mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
Email invitation to an invited user.
New celery task to send the email.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
import pytest
|
||||
from flask import current_app
|
||||
from app.celery.tasks import (send_sms, send_sms_code, send_email_code, send_email, process_job)
|
||||
from app.celery.tasks import (send_sms, send_sms_code, send_email_code, send_email, process_job, email_invited_user)
|
||||
from app import (firetext_client, aws_ses_client, encryption)
|
||||
from app.clients.email.aws_ses import AwsSesClientException
|
||||
from app.clients.sms.firetext import FiretextClientException
|
||||
@@ -11,7 +11,7 @@ from sqlalchemy.orm.exc import NoResultFound
|
||||
from app.celery.tasks import s3
|
||||
from app.celery import tasks
|
||||
from tests.app import load_example_csv
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from freezegun import freeze_time
|
||||
|
||||
|
||||
@@ -332,3 +332,29 @@ def test_should_send_email_code(mocker):
|
||||
"Verification code",
|
||||
verification['secret_code']
|
||||
)
|
||||
|
||||
|
||||
def test_email_invited_user_should_send_email(notify_api, mocker):
|
||||
with notify_api.test_request_context():
|
||||
invitation = {'to': 'new_person@it.gov.uk',
|
||||
'user_name': 'John Smith',
|
||||
'service_id': '123123',
|
||||
'service_name': 'Blacksmith Service',
|
||||
'token': 'the-token',
|
||||
'expiry_date': str(datetime.now() + timedelta(days=1))
|
||||
}
|
||||
|
||||
mocker.patch('app.aws_ses_client.send_email')
|
||||
mocker.patch('app.encryption.decrypt', return_value=invitation)
|
||||
url = tasks.invited_user_url(current_app.config['ADMIN_BASE_URL'], invitation['token'])
|
||||
expected_content = tasks.invitation_template(invitation['user_name'],
|
||||
invitation['service_name'],
|
||||
url,
|
||||
invitation['expiry_date'])
|
||||
|
||||
email_invited_user(encryption.encrypt(invitation))
|
||||
|
||||
aws_ses_client.send_email.assert_called_once_with(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
||||
invitation['to'],
|
||||
'Invitation to GOV.UK Notify',
|
||||
expected_content)
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from tests import create_authorization_header
|
||||
import app.celery.tasks
|
||||
|
||||
|
||||
def test_create_invited_user(notify_api, sample_service):
|
||||
def test_create_invited_user(notify_api, sample_service, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
mocker.patch('app.celery.tasks.email_invited_user.apply_async')
|
||||
mocker.patch('utils.url_safe_token.generate_token', return_value='the-token')
|
||||
email_address = 'invited_user@service.gov.uk'
|
||||
invite_from = sample_service.users[0]
|
||||
|
||||
@@ -37,12 +41,26 @@ def test_create_invited_user(notify_api, sample_service):
|
||||
assert json_resp['data']['email_address'] == email_address
|
||||
assert json_resp['data']['from_user'] == invite_from.id
|
||||
assert json_resp['data']['id']
|
||||
invitation_expiration_days = notify_api.config['INVITATION_EXPIRATION_DAYS']
|
||||
expiry_date = (datetime.now() + timedelta(days=invitation_expiration_days)).replace(hour=0, minute=0,
|
||||
second=0,
|
||||
microsecond=0)
|
||||
encrypted_invitation = {'to': email_address,
|
||||
'user_name': invite_from.name,
|
||||
'service_id': str(sample_service.id),
|
||||
'service_name': sample_service.name,
|
||||
'token': 'the-token',
|
||||
'expiry_date': expiry_date
|
||||
}
|
||||
app.celery.tasks.email_invited_user.apply_async.assert_called_once_with(
|
||||
encrypted_invitation=encrypted_invitation,
|
||||
queue_name='email-invited-user')
|
||||
|
||||
|
||||
def test_create_invited_user_invalid_email(notify_api, sample_service):
|
||||
def test_create_invited_user_invalid_email(notify_api, sample_service, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
mocker.patch('app.celery.tasks.email_invited_user.apply_async')
|
||||
email_address = 'notanemail'
|
||||
invite_from = sample_service.users[0]
|
||||
|
||||
@@ -69,6 +87,7 @@ def test_create_invited_user_invalid_email(notify_api, sample_service):
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == {'email_address': ['Invalid email']}
|
||||
app.celery.tasks.email_invited_user.apply_async.assert_not_called()
|
||||
|
||||
|
||||
def test_get_all_invited_users_by_service(notify_api, notify_db, notify_db_session, sample_service):
|
||||
|
||||
Reference in New Issue
Block a user