mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-26 17:23:50 -04:00
add tests for org invite rest, and update conftest
This commit is contained in:
@@ -29,7 +29,7 @@ register_errors(organisation_invite_blueprint)
|
|||||||
|
|
||||||
|
|
||||||
@organisation_invite_blueprint.route('', methods=['POST'])
|
@organisation_invite_blueprint.route('', methods=['POST'])
|
||||||
def create_invited_org_user(organisation_id):
|
def invite_user_to_org(organisation_id):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
validate(data, post_create_invited_org_user_status_schema)
|
validate(data, post_create_invited_org_user_status_schema)
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ def create_invited_org_user(organisation_id):
|
|||||||
service=template.service,
|
service=template.service,
|
||||||
personalisation={
|
personalisation={
|
||||||
'user_name': invited_org_user.invited_by.name,
|
'user_name': invited_org_user.invited_by.name,
|
||||||
'org_name': invited_org_user.organisation.name,
|
'organisation_name': invited_org_user.organisation.name,
|
||||||
'url': invited_org_user_url(
|
'url': invited_org_user_url(
|
||||||
invited_org_user.id,
|
invited_org_user.id,
|
||||||
data.get('invite_link_host'),
|
data.get('invite_link_host'),
|
||||||
@@ -58,7 +58,7 @@ def create_invited_org_user(organisation_id):
|
|||||||
notification_type=EMAIL_TYPE,
|
notification_type=EMAIL_TYPE,
|
||||||
api_key_id=None,
|
api_key_id=None,
|
||||||
key_type=KEY_TYPE_NORMAL,
|
key_type=KEY_TYPE_NORMAL,
|
||||||
reply_to_text=invited_org_user.from_user.email_address
|
reply_to_text=invited_org_user.invited_by.email_address
|
||||||
)
|
)
|
||||||
|
|
||||||
send_notification_to_queue(saved_notification, research_mode=False, queue=QueueNames.NOTIFY)
|
send_notification_to_queue(saved_notification, research_mode=False, queue=QueueNames.NOTIFY)
|
||||||
@@ -73,7 +73,7 @@ def get_invited_org_users_by_organisation(organisation_id):
|
|||||||
|
|
||||||
|
|
||||||
@organisation_invite_blueprint.route('/<invited_org_user_id>', methods=['POST'])
|
@organisation_invite_blueprint.route('/<invited_org_user_id>', methods=['POST'])
|
||||||
def update_invited_org_user_status(organisation_id, invited_org_user_id):
|
def update_invite_status(organisation_id, invited_org_user_id):
|
||||||
fetched = get_invited_org_user(organisation_id=organisation_id, invited_org_user_id=invited_org_user_id)
|
fetched = get_invited_org_user(organisation_id=organisation_id, invited_org_user_id=invited_org_user_id)
|
||||||
|
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|||||||
@@ -59,7 +59,8 @@ from tests.app.db import (
|
|||||||
create_service,
|
create_service,
|
||||||
create_api_key,
|
create_api_key,
|
||||||
create_inbound_number,
|
create_inbound_number,
|
||||||
create_letter_contact
|
create_letter_contact,
|
||||||
|
create_invited_org_user,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -737,6 +738,16 @@ def sample_invited_user(notify_db,
|
|||||||
return invited_user
|
return invited_user
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def sample_invited_org_user(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_user,
|
||||||
|
sample_organisation
|
||||||
|
):
|
||||||
|
return create_invited_org_user(sample_organisation, sample_user)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
def sample_permission(notify_db,
|
def sample_permission(notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
@@ -910,6 +921,20 @@ def invitation_email_template(notify_db,
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def org_invite_email_template(notify_db, notify_db_session):
|
||||||
|
service, user = notify_service(notify_db, notify_db_session)
|
||||||
|
|
||||||
|
return create_custom_template(
|
||||||
|
service=service,
|
||||||
|
user=user,
|
||||||
|
template_config_name='ORGANISATION_INVITATION_EMAIL_TEMPLATE_ID',
|
||||||
|
content='((user_name)) ((organisation_name)) ((url))',
|
||||||
|
subject='Invitation to ((organisation_name))',
|
||||||
|
template_type='email'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
def password_reset_email_template(notify_db,
|
def password_reset_email_template(notify_db,
|
||||||
notify_db_session):
|
notify_db_session):
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from app.dao.jobs_dao import dao_create_job
|
|||||||
from app.dao.service_inbound_api_dao import save_service_inbound_api
|
from app.dao.service_inbound_api_dao import save_service_inbound_api
|
||||||
from app.dao.service_callback_api_dao import save_service_callback_api
|
from app.dao.service_callback_api_dao import save_service_callback_api
|
||||||
from app.dao.service_sms_sender_dao import update_existing_sms_sender_with_inbound_number, dao_update_service_sms_sender
|
from app.dao.service_sms_sender_dao import update_existing_sms_sender_with_inbound_number, dao_update_service_sms_sender
|
||||||
|
from app.dao.invited_org_user_dao import save_invited_org_user
|
||||||
from app.models import (
|
from app.models import (
|
||||||
ApiKey,
|
ApiKey,
|
||||||
InboundSms,
|
InboundSms,
|
||||||
@@ -30,7 +31,8 @@ from app.models import (
|
|||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
KEY_TYPE_NORMAL,
|
KEY_TYPE_NORMAL,
|
||||||
AnnualBilling,
|
AnnualBilling,
|
||||||
LetterRate
|
LetterRate,
|
||||||
|
InvitedOrganisationUser,
|
||||||
)
|
)
|
||||||
from app.dao.users_dao import save_model_user
|
from app.dao.users_dao import save_model_user
|
||||||
from app.dao.notifications_dao import (
|
from app.dao.notifications_dao import (
|
||||||
@@ -492,3 +494,13 @@ def create_organisation(name='test_org_1', active=True):
|
|||||||
dao_create_organisation(organisation)
|
dao_create_organisation(organisation)
|
||||||
|
|
||||||
return organisation
|
return organisation
|
||||||
|
|
||||||
|
|
||||||
|
def create_invited_org_user(organisation, invited_by, email_address='invite@example.com'):
|
||||||
|
invited_org_user = InvitedOrganisationUser(
|
||||||
|
email_address=email_address,
|
||||||
|
invited_by=invited_by,
|
||||||
|
organisation=organisation,
|
||||||
|
)
|
||||||
|
save_invited_org_user(invited_org_user)
|
||||||
|
return invited_org_user
|
||||||
|
|||||||
147
tests/app/organisation/test_invite_rest.py
Normal file
147
tests/app/organisation/test_invite_rest.py
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from app.models import Notification, INVITE_PENDING
|
||||||
|
|
||||||
|
from tests.app.db import create_invited_org_user
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('extra_args, expected_start_of_invite_url', [
|
||||||
|
(
|
||||||
|
{},
|
||||||
|
'http://localhost:6012/organisation-invitation/'
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{'invite_link_host': 'https://www.example.com'},
|
||||||
|
'https://www.example.com/organisation-invitation/'
|
||||||
|
),
|
||||||
|
])
|
||||||
|
def test_create_invited_org_user(
|
||||||
|
admin_request,
|
||||||
|
sample_organisation,
|
||||||
|
sample_user,
|
||||||
|
mocker,
|
||||||
|
org_invite_email_template,
|
||||||
|
extra_args,
|
||||||
|
expected_start_of_invite_url,
|
||||||
|
):
|
||||||
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
|
email_address = 'invited_user@example.com'
|
||||||
|
|
||||||
|
data = dict(
|
||||||
|
organisation=str(sample_organisation.id),
|
||||||
|
email_address=email_address,
|
||||||
|
invited_by=str(sample_user.id),
|
||||||
|
**extra_args
|
||||||
|
)
|
||||||
|
|
||||||
|
json_resp = admin_request.post(
|
||||||
|
'organisation_invite.invite_user_to_org',
|
||||||
|
organisation_id=sample_organisation.id,
|
||||||
|
_data=data,
|
||||||
|
_expected_status=201
|
||||||
|
)
|
||||||
|
|
||||||
|
assert json_resp['data']['organisation'] == str(sample_organisation.id)
|
||||||
|
assert json_resp['data']['email_address'] == email_address
|
||||||
|
assert json_resp['data']['invited_by'] == str(sample_user.id)
|
||||||
|
assert json_resp['data']['status'] == INVITE_PENDING
|
||||||
|
assert json_resp['data']['id']
|
||||||
|
|
||||||
|
notification = Notification.query.first()
|
||||||
|
|
||||||
|
assert notification.reply_to_text == sample_user.email_address
|
||||||
|
|
||||||
|
assert len(notification.personalisation.keys()) == 3
|
||||||
|
assert notification.personalisation['organisation_name'] == 'sample organisation'
|
||||||
|
assert notification.personalisation['user_name'] == 'Test User'
|
||||||
|
assert notification.personalisation['url'].startswith(expected_start_of_invite_url)
|
||||||
|
assert len(notification.personalisation['url']) > len(expected_start_of_invite_url)
|
||||||
|
|
||||||
|
mocked.assert_called_once_with([(str(notification.id))], queue="notify-internal-tasks")
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_invited_user_invalid_email(admin_request, sample_organisation, sample_user, mocker):
|
||||||
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||||
|
email_address = 'notanemail'
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'service': str(sample_organisation.id),
|
||||||
|
'email_address': email_address,
|
||||||
|
'invited_by': str(sample_user.id),
|
||||||
|
}
|
||||||
|
|
||||||
|
json_resp = admin_request.post(
|
||||||
|
'organisation_invite.invite_user_to_org',
|
||||||
|
organisation_id=sample_organisation.id,
|
||||||
|
_data=data,
|
||||||
|
_expected_status=400
|
||||||
|
)
|
||||||
|
|
||||||
|
assert json_resp['errors'][0]['message'] == 'email_address Not a valid email address'
|
||||||
|
assert mocked.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_all_invited_users_by_service(admin_request, sample_organisation, sample_user):
|
||||||
|
for i in range(5):
|
||||||
|
create_invited_org_user(
|
||||||
|
sample_organisation,
|
||||||
|
sample_user,
|
||||||
|
email_address='invited_user_{}@service.gov.uk'.format(i)
|
||||||
|
)
|
||||||
|
|
||||||
|
json_resp = admin_request.get(
|
||||||
|
'organisation_invite.get_invited_org_users_by_organisation',
|
||||||
|
organisation_id=sample_organisation.id
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(json_resp['data']) == 5
|
||||||
|
for invite in json_resp['data']:
|
||||||
|
assert invite['organisation'] == str(sample_organisation.id)
|
||||||
|
assert invite['invited_by'] == str(sample_user.id)
|
||||||
|
assert invite['id']
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_invited_users_by_service_with_no_invites(admin_request, sample_organisation):
|
||||||
|
json_resp = admin_request.get(
|
||||||
|
'organisation_invite.get_invited_org_users_by_organisation',
|
||||||
|
organisation_id=sample_organisation.id
|
||||||
|
)
|
||||||
|
assert len(json_resp['data']) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_invited_user_set_status_to_cancelled(admin_request, sample_invited_org_user):
|
||||||
|
data = {'status': 'cancelled'}
|
||||||
|
|
||||||
|
json_resp = admin_request.post(
|
||||||
|
'organisation_invite.update_invite_status',
|
||||||
|
organisation_id=sample_invited_org_user.organisation_id,
|
||||||
|
invited_org_user_id=sample_invited_org_user.id,
|
||||||
|
_data=data
|
||||||
|
)
|
||||||
|
assert json_resp['data']['status'] == 'cancelled'
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_invited_user_for_wrong_service_returns_404(admin_request, sample_invited_org_user, fake_uuid):
|
||||||
|
data = {'status': 'cancelled'}
|
||||||
|
|
||||||
|
json_resp = admin_request.post(
|
||||||
|
'organisation_invite.update_invite_status',
|
||||||
|
organisation_id=fake_uuid,
|
||||||
|
invited_org_user_id=sample_invited_org_user.id,
|
||||||
|
_data=data,
|
||||||
|
_expected_status=404
|
||||||
|
)
|
||||||
|
assert json_resp['message'] == 'No result found'
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_invited_user_for_invalid_data_returns_400(admin_request, sample_invited_org_user):
|
||||||
|
data = {'status': 'garbage'}
|
||||||
|
|
||||||
|
json_resp = admin_request.post(
|
||||||
|
'organisation_invite.update_invite_status',
|
||||||
|
organisation_id=sample_invited_org_user.organisation_id,
|
||||||
|
invited_org_user_id=sample_invited_org_user.id,
|
||||||
|
_data=data,
|
||||||
|
_expected_status=404
|
||||||
|
)
|
||||||
|
assert json_resp['message'] == 'No result found'
|
||||||
@@ -104,7 +104,8 @@ def notify_db_session(notify_db):
|
|||||||
"dvla_organisation",
|
"dvla_organisation",
|
||||||
"notification_status_types",
|
"notification_status_types",
|
||||||
"service_permission_types",
|
"service_permission_types",
|
||||||
"auth_type"]:
|
"auth_type",
|
||||||
|
"invite_status_type"]:
|
||||||
notify_db.engine.execute(tbl.delete())
|
notify_db.engine.execute(tbl.delete())
|
||||||
notify_db.session.commit()
|
notify_db.session.commit()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user