Merge pull request #3475 from alphagov/request-invite

Allow invite form to be pre-populated with an existing user
This commit is contained in:
Chris Hill-Scott
2021-01-08 11:16:18 +00:00
committed by GitHub
7 changed files with 302 additions and 10 deletions

View File

@@ -7,6 +7,8 @@ from flask import url_for
import app
from app.utils import is_gov_user
from tests.conftest import (
ORGANISATION_ID,
ORGANISATION_TWO_ID,
SERVICE_ONE_ID,
USER_ONE_ID,
create_active_user_empty_permissions,
@@ -806,6 +808,166 @@ def test_should_show_page_for_inviting_user(
assert not page.find('div', class_='checkboxes-nested')
def test_should_show_page_for_inviting_user_with_email_prefilled(
client_request,
mocker,
service_one,
mock_get_template_folders,
fake_uuid,
active_user_with_permissions,
active_user_with_permission_to_other_service,
mock_get_organisation_by_domain,
mock_get_invites_for_service,
):
service_one['organisation'] = ORGANISATION_ID
mocker.patch('app.models.user.user_api_client.get_user', side_effect=[
# First call is to get the current user
active_user_with_permissions,
# Second call gets the user to invite
active_user_with_permission_to_other_service,
])
page = client_request.get(
'main.invite_user',
service_id=SERVICE_ONE_ID,
user_id=fake_uuid,
# We have the users name in the H1 but dont want it duplicated
# in the page title
_test_page_title=False,
)
assert normalize_spaces(page.select_one('title').text).startswith(
'Invite a team member'
)
assert normalize_spaces(page.select_one('h1').text) == (
'Invite Service Two User'
)
assert normalize_spaces(page.select_one('main .govuk-body').text) == (
'service-two-user@test.gov.uk'
)
assert not page.select("input#email_address") or page.select("input[type=email]")
def test_should_show_page_if_prefilled_user_is_already_a_team_member(
mocker,
client_request,
mock_get_template_folders,
fake_uuid,
active_user_with_permissions,
active_caseworking_user,
):
mocker.patch('app.models.user.user_api_client.get_user', side_effect=[
# First call is to get the current user
active_user_with_permissions,
# Second call gets the user to invite
active_caseworking_user,
])
page = client_request.get(
'main.invite_user',
service_id=SERVICE_ONE_ID,
user_id=fake_uuid,
)
assert normalize_spaces(page.select_one('title').text).startswith(
'This person is already a team member'
)
assert normalize_spaces(page.select_one('h1').text) == (
'This person is already a team member'
)
assert normalize_spaces(page.select_one('main .govuk-body').text) == (
'Test User is already member of service one.'
)
assert not page.select("form")
def test_should_show_page_if_prefilled_user_is_already_invited(
mocker,
client_request,
mock_get_template_folders,
fake_uuid,
active_user_with_permissions,
active_user_with_permission_to_other_service,
mock_get_invites_for_service,
):
active_user_with_permission_to_other_service['email_address'] = (
'user_1@testnotify.gov.uk'
)
mocker.patch('app.models.user.user_api_client.get_user', side_effect=[
# First call is to get the current user
active_user_with_permissions,
# Second call gets the user to invite
active_user_with_permission_to_other_service,
])
page = client_request.get(
'main.invite_user',
service_id=SERVICE_ONE_ID,
user_id=fake_uuid,
)
assert normalize_spaces(page.select_one('title').text).startswith(
'This person has already received an invite'
)
assert normalize_spaces(page.select_one('h1').text) == (
'This person has already received an invite'
)
assert normalize_spaces(page.select_one('main .govuk-body').text) == (
'Service Two User has not accepted their invitation to '
'service one yet. You do not need to do anything.'
)
assert not page.select("form")
def test_should_403_if_trying_to_prefill_email_address_for_user_with_no_organisation(
mocker,
client_request,
service_one,
mock_get_template_folders,
fake_uuid,
active_user_with_permissions,
active_user_with_permission_to_other_service,
mock_get_invites_for_service,
mock_get_no_organisation_by_domain,
):
service_one['organisation'] = ORGANISATION_ID
mocker.patch('app.models.user.user_api_client.get_user', side_effect=[
# First call is to get the current user
active_user_with_permissions,
# Second call gets the user to invite
active_user_with_permission_to_other_service,
])
client_request.get(
'main.invite_user',
service_id=SERVICE_ONE_ID,
user_id=fake_uuid,
_expected_status=403,
)
def test_should_403_if_trying_to_prefill_email_address_for_user_from_other_organisation(
mocker,
client_request,
service_one,
mock_get_template_folders,
fake_uuid,
active_user_with_permissions,
active_user_with_permission_to_other_service,
mock_get_invites_for_service,
mock_get_organisation_by_domain,
):
service_one['organisation'] = ORGANISATION_TWO_ID
mocker.patch('app.models.user.user_api_client.get_user', side_effect=[
# First call is to get the current user
active_user_with_permissions,
# Second call gets the user to invite
active_user_with_permission_to_other_service,
])
client_request.get(
'main.invite_user',
service_id=SERVICE_ONE_ID,
user_id=fake_uuid,
_expected_status=403,
)
def test_should_show_folder_permission_form_if_service_has_folder_permissions_enabled(
client_request,
mocker,
@@ -877,6 +1039,48 @@ def test_invite_user(
[])
def test_invite_user_when_email_address_is_prefilled(
client_request,
service_one,
active_user_with_permissions,
active_user_with_permission_to_other_service,
fake_uuid,
mocker,
sample_invite,
mock_get_template_folders,
mock_get_invites_for_service,
mock_get_organisation_by_domain,
):
service_one['organisation'] = ORGANISATION_ID
mocker.patch('app.models.user.user_api_client.get_user', side_effect=[
# First call is to get the current user
active_user_with_permissions,
# Second call gets the user to invite
active_user_with_permission_to_other_service,
])
mocker.patch('app.invite_api_client.create_invite', return_value=sample_invite)
client_request.post(
'main.invite_user',
service_id=SERVICE_ONE_ID,
user_id=fake_uuid,
_data={
# No posted email address
'permissions_field': [
'send_messages',
],
},
)
app.invite_api_client.create_invite.assert_called_once_with(
active_user_with_permissions['id'],
SERVICE_ONE_ID,
active_user_with_permission_to_other_service['email_address'],
{'send_messages'},
'sms_auth',
[],
)
@pytest.mark.parametrize('auth_type', [
('sms_auth'),
('email_auth')