2021-03-11 20:26:44 +00:00
|
|
|
|
import uuid
|
|
|
|
|
|
|
2018-02-19 17:14:01 +00:00
|
|
|
|
import pytest
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from flask import current_app, json
|
|
|
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
from notifications_utils.url_safe_token import generate_token
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.models import INVITE_PENDING, Notification
|
2021-08-04 15:12:09 +01:00
|
|
|
|
from tests import create_admin_authorization_header
|
2018-02-19 17:14:01 +00:00
|
|
|
|
from tests.app.db import create_invited_org_user
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"platform_admin, expected_invited_by",
|
|
|
|
|
|
((True, "The GOV.UK Notify team"), (False, "Test User")),
|
|
|
|
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"extra_args, expected_start_of_invite_url",
|
|
|
|
|
|
[
|
|
|
|
|
|
({}, "http://localhost:6012/organization-invitation/"),
|
|
|
|
|
|
(
|
|
|
|
|
|
{"invite_link_host": "https://www.example.com"},
|
|
|
|
|
|
"https://www.example.com/organization-invitation/",
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2018-02-19 17:14:01 +00:00
|
|
|
|
def test_create_invited_org_user(
|
|
|
|
|
|
admin_request,
|
2023-07-10 11:06:29 -07:00
|
|
|
|
sample_organization,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
sample_user,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
org_invite_email_template,
|
|
|
|
|
|
extra_args,
|
|
|
|
|
|
expected_start_of_invite_url,
|
2020-03-05 11:26:08 +00:00
|
|
|
|
platform_admin,
|
|
|
|
|
|
expected_invited_by,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
|
|
|
|
|
email_address = "invited_user@example.com"
|
2020-03-05 11:26:08 +00:00
|
|
|
|
sample_user.platform_admin = platform_admin
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
data = dict(
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization=str(sample_organization.id),
|
2018-02-19 17:14:01 +00:00
|
|
|
|
email_address=email_address,
|
|
|
|
|
|
invited_by=str(sample_user.id),
|
|
|
|
|
|
**extra_args
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.invite_user_to_org",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=sample_organization.id,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
_data=data,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=201,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"]["organization"] == str(sample_organization.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"]
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
notification = Notification.query.first()
|
|
|
|
|
|
|
|
|
|
|
|
assert notification.reply_to_text == sample_user.email_address
|
|
|
|
|
|
|
|
|
|
|
|
assert len(notification.personalisation.keys()) == 3
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert notification.personalisation["organization_name"] == "sample organization"
|
|
|
|
|
|
assert notification.personalisation["user_name"] == expected_invited_by
|
|
|
|
|
|
assert notification.personalisation["url"].startswith(expected_start_of_invite_url)
|
|
|
|
|
|
assert len(notification.personalisation["url"]) > len(expected_start_of_invite_url)
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked.assert_called_once_with(
|
|
|
|
|
|
[(str(notification.id))], queue="notify-internal-tasks"
|
|
|
|
|
|
)
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_create_invited_user_invalid_email(
|
|
|
|
|
|
admin_request, sample_organization, sample_user, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
|
|
|
|
|
email_address = "notanemail"
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"service": str(sample_organization.id),
|
|
|
|
|
|
"email_address": email_address,
|
|
|
|
|
|
"invited_by": str(sample_user.id),
|
2018-02-19 17:14:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.invite_user_to_org",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=sample_organization.id,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
_data=data,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=400,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
json_resp["errors"][0]["message"] == "email_address Not a valid email address"
|
|
|
|
|
|
)
|
2018-02-19 17:14:01 +00:00
|
|
|
|
assert mocked.call_count == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_get_all_invited_users_by_service(
|
|
|
|
|
|
admin_request, sample_organization, sample_user
|
|
|
|
|
|
):
|
2018-02-19 17:14:01 +00:00
|
|
|
|
for i in range(5):
|
|
|
|
|
|
create_invited_org_user(
|
2023-07-10 11:06:29 -07:00
|
|
|
|
sample_organization,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
sample_user,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
email_address="invited_user_{}@service.gov.uk".format(i),
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.get_invited_org_users_by_organization",
|
|
|
|
|
|
organization_id=sample_organization.id,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert len(json_resp["data"]) == 5
|
|
|
|
|
|
for invite in json_resp["data"]:
|
|
|
|
|
|
assert invite["organization"] == str(sample_organization.id)
|
|
|
|
|
|
assert invite["invited_by"] == str(sample_user.id)
|
|
|
|
|
|
assert invite["id"]
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_get_invited_users_by_service_with_no_invites(
|
|
|
|
|
|
admin_request, sample_organization
|
|
|
|
|
|
):
|
2018-02-19 17:14:01 +00:00
|
|
|
|
json_resp = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.get_invited_org_users_by_organization",
|
|
|
|
|
|
organization_id=sample_organization.id,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert len(json_resp["data"]) == 0
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def test_get_invited_user_by_organization(admin_request, sample_invited_org_user):
|
2020-08-17 11:59:55 +01:00
|
|
|
|
json_resp = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.get_invited_org_user_by_organization",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=sample_invited_org_user.organization.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
invited_org_user_id=sample_invited_org_user.id,
|
2020-08-17 11:59:55 +01:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"]["email_address"] == sample_invited_org_user.email_address
|
2020-08-17 11:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def test_get_invited_user_by_organization_when_user_does_not_belong_to_the_org(
|
2020-08-17 11:59:55 +01:00
|
|
|
|
admin_request,
|
|
|
|
|
|
sample_invited_org_user,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
):
|
|
|
|
|
|
json_resp = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.get_invited_org_user_by_organization",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=fake_uuid,
|
2020-08-17 11:59:55 +01:00
|
|
|
|
invited_org_user_id=sample_invited_org_user.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=404,
|
2020-08-17 11:59:55 +01:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
2020-08-17 11:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_org_invited_user_set_status_to_cancelled(
|
|
|
|
|
|
admin_request, sample_invited_org_user
|
|
|
|
|
|
):
|
|
|
|
|
|
data = {"status": "cancelled"}
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.update_org_invite_status",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=sample_invited_org_user.organization_id,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
invited_org_user_id=sample_invited_org_user.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_data=data,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"]["status"] == "cancelled"
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_org_invited_user_for_wrong_service_returns_404(
|
|
|
|
|
|
admin_request, sample_invited_org_user, fake_uuid
|
|
|
|
|
|
):
|
|
|
|
|
|
data = {"status": "cancelled"}
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.update_org_invite_status",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=fake_uuid,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
invited_org_user_id=sample_invited_org_user.id,
|
|
|
|
|
|
_data=data,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=404,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["message"] == "No result found"
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_org_invited_user_for_invalid_data_returns_400(
|
|
|
|
|
|
admin_request, sample_invited_org_user
|
|
|
|
|
|
):
|
|
|
|
|
|
data = {"status": "garbage"}
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.update_org_invite_status",
|
2023-07-10 11:06:29 -07:00
|
|
|
|
organization_id=sample_invited_org_user.organization_id,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
invited_org_user_id=sample_invited_org_user.id,
|
|
|
|
|
|
_data=data,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=400,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert len(json_resp["errors"]) == 1
|
|
|
|
|
|
assert (
|
|
|
|
|
|
json_resp["errors"][0]["message"]
|
|
|
|
|
|
== "status garbage is not one of [pending, accepted, cancelled]"
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"endpoint_format_str",
|
|
|
|
|
|
[
|
|
|
|
|
|
"/invite/organization/{}",
|
|
|
|
|
|
"/invite/organization/check/{}",
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_validate_invitation_token_returns_200_when_token_valid(
|
|
|
|
|
|
client, sample_invited_org_user, endpoint_format_str
|
|
|
|
|
|
):
|
|
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(sample_invited_org_user.id),
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
)
|
2021-03-11 20:47:24 +00:00
|
|
|
|
|
|
|
|
|
|
url = endpoint_format_str.format(token)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response = client.get(
|
|
|
|
|
|
url, headers=[("Content-Type", "application/json"), auth_header]
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"] == sample_invited_org_user.serialize()
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_invitation_token_for_expired_token_returns_400(client):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
with freeze_time("2016-01-01T12:00:00"):
|
|
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(uuid.uuid4()),
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
)
|
|
|
|
|
|
url = "/invite/organization/{}".format(token)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response = client.get(
|
|
|
|
|
|
url, headers=[("Content-Type", "application/json"), auth_header]
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == {
|
|
|
|
|
|
"invitation": "Your invitation to GOV.UK Notify has expired. "
|
|
|
|
|
|
"Please ask the person that invited you to send you another one"
|
|
|
|
|
|
}
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_invitation_token_returns_400_when_invited_user_does_not_exist(client):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(uuid.uuid4()),
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
)
|
|
|
|
|
|
url = "/invite/organization/{}".format(token)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response = client.get(
|
|
|
|
|
|
url, headers=[("Content-Type", "application/json"), auth_header]
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == "No result found"
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_invitation_token_returns_400_when_token_is_malformed(client):
|
|
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(uuid.uuid4()),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
2021-03-11 20:26:44 +00:00
|
|
|
|
)[:-2]
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
url = "/invite/organization/{}".format(token)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response = client.get(
|
|
|
|
|
|
url, headers=[("Content-Type", "application/json"), auth_header]
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == {
|
|
|
|
|
|
"invitation": "Something’s wrong with this link. Make sure you’ve copied the whole thing."
|
2021-03-11 20:26:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_invited_org_user(admin_request, sample_invited_org_user):
|
|
|
|
|
|
json_resp = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.get_invited_org_user",
|
|
|
|
|
|
invited_org_user_id=sample_invited_org_user.id,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert json_resp["data"]["id"] == str(sample_invited_org_user.id)
|
|
|
|
|
|
assert json_resp["data"]["email_address"] == sample_invited_org_user.email_address
|
|
|
|
|
|
assert json_resp["data"]["organization"] == str(
|
|
|
|
|
|
sample_invited_org_user.organization_id
|
2021-03-11 20:26:44 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_get_invited_org_user_404s_if_invite_doesnt_exist(
|
|
|
|
|
|
admin_request, sample_invited_org_user, fake_uuid
|
|
|
|
|
|
):
|
2021-03-11 20:26:44 +00:00
|
|
|
|
json_resp = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"organization_invite.get_invited_org_user",
|
2021-03-11 20:26:44 +00:00
|
|
|
|
invited_org_user_id=fake_uuid,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=404,
|
2021-03-11 20:26:44 +00:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|