def user_with_orgs_and_services(num_orgs, num_services, platform_admin=False): return user_json( name="leo", organisations=["org{}".format(i) for i in range(1, num_orgs + 1)], services=["service{}".format(i) for i in range(1, num_services + 1)], platform_admin=platform_admin, )
def test_user_information_page_displays_if_there_are_failed_login_attempts( client, platform_admin_user, mocker): mocker.patch('app.user_api_client.get_user', side_effect=[ platform_admin_user, User(user_json(name="Apple Bloom", failed_login_count=2)) ], autospec=True) mocker.patch('app.user_api_client.get_organisations_and_services_for_user', return_value={ 'organisations': [], 'services_without_organisations': [ { "id": 1, "name": "Fresh Orchard Juice" }, { "id": 2, "name": "Nature Therapy" }, ] }, autospec=True) client.login(platform_admin_user) response = client.get(url_for('main.user_information', user_id=345)) assert response.status_code == 200 document = html.fromstring(response.get_data(as_text=True)) assert document.xpath( "//p/text()[normalize-space()='2 failed login attempts']")
def test_user_information_page_shows_unblocked_user( client, platform_admin_user, mocker ): mocker.patch('app.user_api_client.get_user', side_effect=[ platform_admin_user, user_json(name="Blocked Apple Bloom", services=[1, 2], blocked=True) ], autospec=True) mocker.patch( 'app.user_api_client.get_organisations_and_services_for_user', return_value={'organisations': [], 'services': [ {"id": 1, "name": "Fresh Orchard Juice", "restricted": True}, {"id": 2, "name": "Nature Therapy", "restricted": False}, ]}, autospec=True ) client.login(platform_admin_user) response = client.get(url_for('main.user_information', user_id=345)) assert response.status_code == 200 document = html.fromstring(response.get_data(as_text=True)) assert document.xpath("//h1/text()[normalize-space()='Blocked Apple Bloom']") assert document.xpath("//a/text()[normalize-space()='Unblock user']")
def user_with_orgs_and_services(num_orgs, num_services, platform_admin=False): return User(user_json( name='leo', organisations=['org{}'.format(i) for i in range(1, num_orgs + 1)], services=['service{}'.format(i) for i in range(1, num_services + 1)], platform_admin=platform_admin ))
def test_find_users_by_email_displays_users_found(client_request, platform_admin_user, mocker): client_request.login(platform_admin_user) mocker.patch( 'app.user_api_client.find_users_by_full_or_partial_email', return_value={"data": [user_json()]}, autospec=True, ) document = client_request.post('main.find_users_by_email', _data={"search": "twilight.sparkle"}, _expected_status=200) assert any(element.text.strip() == '*****@*****.**' for element in document.find_all( 'a', {'class': 'browse-list-link'}, href=True)) assert any( element.text.strip() == 'Test User' for element in document.find_all('p', {'class': 'browse-list-hint'})) assert document.find('a', { 'class': 'browse-list-link' }).text.strip() == '*****@*****.**' assert document.find('p', { 'class': 'browse-list-hint' }).text.strip() == 'Test User'
def test_find_users_by_email_displays_users_found(client_request, platform_admin_user, mocker): client_request.login(platform_admin_user) mocker.patch( "app.user_api_client.find_users_by_full_or_partial_email", return_value={"data": [user_json()]}, autospec=True, ) document = client_request.post( "main.find_users_by_email", _data={"search": "twilight.sparkle"}, _expected_status=200, ) assert any(element.text.strip() == "*****@*****.**" for element in document.find_all( "a", {"class": "browse-list-link"}, href=True)) assert any( element.text.strip() == "Test User" for element in document.find_all("p", {"class": "browse-list-hint"})) assert document.find("a", { "class": "browse-list-link" }).text.strip() == "*****@*****.**" assert document.find("p", { "class": "browse-list-hint" }).text.strip() == "Test User"
def test_should_show_updates_for_scheduled_job_as_json( logged_in_client, service_one, active_user_with_permissions, mock_get_notifications, mock_get_service_template, mock_get_service_data_retention, mocker, fake_uuid, ): mocker.patch('app.job_api_client.get_job', return_value={'data': job_json( service_one['id'], created_by=user_json(), job_id=fake_uuid, scheduled_for='2016-06-01T13:00:00+00:00', processing_started='2016-06-01T15:00:00+00:00', )}) response = logged_in_client.get(url_for('main.view_job_updates', service_id=service_one['id'], job_id=fake_uuid)) assert response.status_code == 200 content = response.json assert 'sending' in content['counts'] assert 'delivered' in content['counts'] assert 'failed' in content['counts'] assert 'Recipient' in content['notifications'] assert '07123456789' in content['notifications'] assert 'Status' in content['notifications'] assert 'Delivered' in content['notifications'] assert '12:01am' in content['notifications'] assert 'Sent by Test User on 1 June at 4:00pm' in content['status']
def test_find_users_by_email_displays_multiple_users( client_request, platform_admin_user, mocker ): client_request.login(platform_admin_user) mocker.patch( 'app.user_api_client.find_users_by_full_or_partial_email', return_value={"data": [user_json(name="Apple Jack"), user_json(name="Apple Bloom")]}, autospec=True, ) document = client_request.post('main.find_users_by_email', _data={"search": "apple"}, _expected_status=200) assert any( element.text.strip() == 'Apple Jack' for element in document.find_all('p', {'class': 'browse-list-hint'}) ) assert any( element.text.strip() == 'Apple Bloom' for element in document.find_all('p', {'class': 'browse-list-hint'}) )
def test_user_information_page_shows_information_about_user( client, platform_admin_user, mocker, fake_uuid, ): user_service_one = uuid.uuid4() user_service_two = uuid.uuid4() mocker.patch('app.user_api_client.get_user', side_effect=[ platform_admin_user, user_json(name="Apple Bloom", services=[user_service_one, user_service_two]) ], autospec=True) mocker.patch('app.user_api_client.get_organisations_and_services_for_user', return_value={ 'organisations': [], 'services': [ { "id": user_service_one, "name": "Fresh Orchard Juice", "restricted": True }, { "id": user_service_two, "name": "Nature Therapy", "restricted": False }, ] }, autospec=True) client.login(platform_admin_user) response = client.get(url_for('main.user_information', user_id=fake_uuid)) assert response.status_code == 200 document = html.fromstring(response.get_data(as_text=True)) assert document.xpath("//h1/text()[normalize-space()='Apple Bloom']") assert document.xpath("//p/text()[normalize-space()='*****@*****.**']") assert document.xpath("//p/text()[normalize-space()='+447700900986']") assert document.xpath("//h2/text()[normalize-space()='Live services']") assert document.xpath("//a/text()[normalize-space()='Nature Therapy']") assert document.xpath( "//h2/text()[normalize-space()='Trial mode services']") assert document.xpath( "//a/text()[normalize-space()='Fresh Orchard Juice']") assert document.xpath("//h2/text()[normalize-space()='Last login']") assert not document.xpath( "//p/text()[normalize-space()='0 failed login attempts']")
def test_user_information_page_shows_information_about_user( client, platform_admin_user, mocker): mocker.patch( "app.user_api_client.get_user", side_effect=[ platform_admin_user, user_json(name="Apple Bloom", services=[1, 2], blocked=False), ], autospec=True, ) mocker.patch( "app.user_api_client.get_organisations_and_services_for_user", return_value={ "organisations": [], "services": [ { "id": 1, "name": "Fresh Orchard Juice", "restricted": True }, { "id": 2, "name": "Nature Therapy", "restricted": False }, ], }, autospec=True, ) client.login(platform_admin_user) response = client.get(url_for("main.user_information", user_id=345)) assert response.status_code == 200 document = html.fromstring(response.get_data(as_text=True)) assert document.xpath("//h1/text()[normalize-space()='Apple Bloom']") assert document.xpath("//p/text()[normalize-space()='*****@*****.**']") assert document.xpath("//p/text()[normalize-space()='+16502532222']") assert document.xpath("//h2/text()[normalize-space()='Live services']") assert document.xpath("//a/text()[normalize-space()='Nature Therapy']") assert document.xpath("//h2/text()[normalize-space()='Trial services']") assert document.xpath( "//a/text()[normalize-space()='Fresh Orchard Juice']") assert document.xpath("//h2/text()[normalize-space()='Last sign-in']") assert not document.xpath( "//p/text()[normalize-space()='0 failed sign-in attempts']") assert document.xpath("//a/text()[normalize-space()='Block user']")
def test_view_broadcast_message_page( mocker, client_request, service_one, active_user_with_permissions, mock_get_broadcast_template, fake_uuid, extra_fields, expected_paragraphs, ): mocker.patch( 'app.broadcast_message_api_client.get_broadcast_message', return_value=broadcast_message_json( id_=fake_uuid, service_id=SERVICE_ONE_ID, template_id=fake_uuid, created_by_id=fake_uuid, approved_by_id=fake_uuid, starts_at='2020-02-20T20:20:20.000000', **extra_fields), ) mocker.patch('app.user_api_client.get_user', side_effect=[ active_user_with_permissions, user_json(name='Alice'), user_json(name='Bob'), user_json(name='Carol'), ]) service_one['permissions'] += ['broadcast'] page = client_request.get( '.view_broadcast_message', service_id=SERVICE_ONE_ID, broadcast_message_id=fake_uuid, ) assert [ normalize_spaces(p.text) for p in page.select('main p.govuk-body') ] == expected_paragraphs
def test_view_pending_broadcast( mocker, client_request, service_one, active_user_with_permissions, mock_get_broadcast_template, fake_uuid, ): mocker.patch( 'app.broadcast_message_api_client.get_broadcast_message', return_value=broadcast_message_json( id_=fake_uuid, service_id=SERVICE_ONE_ID, template_id=fake_uuid, created_by_id=fake_uuid, finishes_at=None, status='pending-approval', ), ) mocker.patch( 'app.user_api_client.get_user', side_effect=[ active_user_with_permissions, # Current user user_json(id_=uuid.uuid4()), # User who created broadcast ]) service_one['permissions'] += ['broadcast'] page = client_request.get( '.view_broadcast_message', service_id=SERVICE_ONE_ID, broadcast_message_id=fake_uuid, ) assert (normalize_spaces(page.select_one('.banner').text)) == ( 'Test User wants to broadcast Example template ' 'Start broadcasting now Reject this alert') form = page.select_one('form.banner') assert form['method'] == 'post' assert 'action' not in form assert form.select_one('button[type=submit]') link = form.select_one('a.govuk-link.govuk-link--destructive') assert link.text == 'Reject this alert' assert link['href'] == url_for( '.reject_broadcast_message', service_id=SERVICE_ONE_ID, broadcast_message_id=fake_uuid, )
def test_should_redirect_to_password_reset_sent_for_valid_email( client, fake_uuid, email_address, mocker, ): sample_user = user_json(email_address=email_address) mocker.patch('app.user_api_client.send_reset_password_url', return_value=None) response = client.post( url_for('.forgot_password'), data={'email_address': sample_user['email_address']}) assert response.status_code == 200 assert 'Click the link in the email to reset your password.' \ in response.get_data(as_text=True) app.user_api_client.send_reset_password_url.assert_called_once_with(sample_user['email_address'])
def test_forgot_password_sends_next_link_with_reset_password_email_request( client, fake_uuid, mocker, ): sample_user = user_json(email_address='*****@*****.**') mocker.patch('app.user_api_client.send_reset_password_url', return_value=None) response = client.post( url_for('.forgot_password') + f"?next=/services/{SERVICE_ONE_ID}/templates", data={'email_address': sample_user['email_address']}) assert response.status_code == 200 app.user_api_client.send_reset_password_url.assert_called_once_with( sample_user['email_address'], next_string=f'/services/{SERVICE_ONE_ID}/templates')
def test_user_information_page_does_not_show_archive_link_for_inactive_users( mocker, client, platform_admin_user, mock_get_organisations_and_services_for_user, ): inactive_user = user_json(state='inactive') mocker.patch('app.user_api_client.get_user', side_effect=[platform_admin_user, inactive_user], autospec=True) client.login(platform_admin_user) response = client.get( url_for('main.user_information', user_id=inactive_user['id']) ) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') archive_url = url_for('main.archive_user', user_id=inactive_user['id']) assert not page.find('a', {'href': archive_url})
def test_user_information_page_does_not_show_archive_link_for_inactive_users( mocker, client, platform_admin_user, mock_get_organisations_and_services_for_user, ): inactive_user = user_json(state="inactive", blocked=False) mocker.patch( "app.user_api_client.get_user", side_effect=[platform_admin_user, inactive_user], autospec=True, ) client.login(platform_admin_user) response = client.get( url_for("main.user_information", user_id=inactive_user["id"])) page = BeautifulSoup(response.data.decode("utf-8"), "html.parser") archive_url = url_for("main.archive_user", user_id=inactive_user["id"]) assert not page.find("a", {"href": archive_url})
def test_user_information_page_shows_information_about_user( client, platform_admin_user, mocker): mocker.patch('app.user_api_client.get_user', side_effect=[ platform_admin_user, User(user_json(name="Apple Bloom", services=[1, 2])) ], autospec=True) mocker.patch('app.user_api_client.get_organisations_and_services_for_user', return_value={ 'organisations': [], 'services_without_organisations': [ { "id": 1, "name": "Fresh Orchard Juice" }, { "id": 2, "name": "Nature Therapy" }, ] }, autospec=True) client.login(platform_admin_user) response = client.get(url_for('main.user_information', user_id=345)) assert response.status_code == 200 document = html.fromstring(response.get_data(as_text=True)) assert document.xpath("//h1/text()[normalize-space()='Apple Bloom']") assert document.xpath("//p/text()[normalize-space()='*****@*****.**']") assert document.xpath("//p/text()[normalize-space()='+447700900986']") assert document.xpath("//h2/text()[normalize-space()='Services']") assert document.xpath( "//a/text()[normalize-space()='Fresh Orchard Juice']") assert document.xpath("//a/text()[normalize-space()='Nature Therapy']") assert document.xpath("//h2/text()[normalize-space()='Last login']") assert not document.xpath( "//p/text()[normalize-space()='0 failed login attempts']")
def test_user_information_page_displays_if_there_are_failed_login_attempts( client, platform_admin_user, mocker): mocker.patch( "app.user_api_client.get_user", side_effect=[ platform_admin_user, user_json(name="Apple Bloom", failed_login_count=2, blocked=False), ], autospec=True, ) mocker.patch( "app.user_api_client.get_organisations_and_services_for_user", return_value={ "organisations": [], "services": [ { "id": 1, "name": "Fresh Orchard Juice", "restricted": True }, { "id": 2, "name": "Nature Therapy", "restricted": True }, ], }, autospec=True, ) client.login(platform_admin_user) response = client.get(url_for("main.user_information", user_id=345)) assert response.status_code == 200 document = html.fromstring(response.get_data(as_text=True)) assert document.xpath( "//p/text()[normalize-space()='2 failed sign-in attempts']")