Files
notifications-admin/tests/app/main/views/test_index.py

160 lines
4.9 KiB
Python
Raw Normal View History

2024-10-24 17:58:23 -04:00
import os
from functools import partial
2019-10-31 10:53:45 +00:00
import pytest
2024-10-25 10:57:34 -04:00
from bs4 import BeautifulSoup
from flask import url_for
2024-10-25 10:57:34 -04:00
from freezegun import freeze_time
def test_non_logged_in_user_can_see_homepage(
client_request, mock_get_service_and_organization_counts, mocker
):
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
client_request.logout()
page = client_request.get("main.index", _test_page_title=False)
assert page.h1.text.strip() == (
"Reach people where they are with government-powered text messages"
)
assert (
page.select_one(
"a.usa-button.login-button.login-button--primary.margin-right-2"
).text
== "Sign in with \n"
)
assert page.select_one("meta[name=description]") is not None
# This area is hidden for the pilot
# assert normalize_spaces(page.select_one('#whos-using-notify').text) == (
# 'Whos using Notify.gov ' # Hiding this next area for the pilot
# # Hiding this next area for the pilot
# # 'See the list of services and organizations. '
# 'There are 111 Organizations and 9,999 Services using Notify.'
# )
assert page.select_one("#whos-using-notify a") is None
def test_logged_in_user_redirects_to_choose_account(
client_request,
api_user_active,
mock_get_user,
mock_get_user_by_email,
mock_login,
):
client_request.get(
"main.index",
_expected_status=302,
)
# Modify expected URL to include the query parameter
client_request.get(
"main.sign_in",
_expected_status=302,
2024-10-25 13:16:07 -07:00
_expected_redirect=url_for("main.show_accounts_or_dashboard"),
2024-10-25 10:57:34 -04:00
)
def test_robots(client_request):
client_request.get_url("/robots.txt", _expected_status=404)
2024-10-25 11:29:07 -04:00
@pytest.mark.parametrize(
("endpoint", "kwargs"),
[
("sign_in", {}),
("register", {}),
pytest.param("index", {}, marks=pytest.mark.xfail(raises=AssertionError)),
],
)
@freeze_time("2012-12-12 12:12") # So we dont go out of business hours
def test_hiding_pages_from_search_engines(
client_request, mock_get_service_and_organization_counts, endpoint, kwargs, mocker
):
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
client_request.logout()
response = client_request.get_response(f"main.{endpoint}", **kwargs)
assert "X-Robots-Tag" in response.headers
assert response.headers["X-Robots-Tag"] == "noindex"
page = BeautifulSoup(response.data.decode("utf-8"), "html.parser")
assert page.select_one("meta[name=robots]")["content"] == "noindex"
@pytest.mark.parametrize(
"view",
[
"privacy",
"pricing",
"roadmap",
"features",
"documentation",
2024-10-24 17:03:45 -07:00
"best_practices",
2024-10-23 10:09:47 -07:00
"clear_goals",
"rules_and_regulations",
"establish_trust",
"write_for_action",
"multiple_languages",
"benchmark_performance",
"security",
"message_status",
"features_sms",
"how_to_pay",
"get_started",
"guidance_index",
"create_and_send_messages",
"edit_and_format_messages",
"send_files_by_email",
"billing_details",
],
)
2024-07-11 09:38:32 -07:00
def test_static_pages(client_request, mock_get_organization_by_domain, view, mocker):
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
2024-10-24 17:58:23 -04:00
# Function to check if a view is feature-flagged and should return 404 when disabled
def is_feature_flagged(view):
2024-10-25 11:35:56 -04:00
2024-10-24 17:03:45 -07:00
feature_best_practices_enabled = (
os.getenv("FEATURE_BEST_PRACTICES_ENABLED", "false").lower() == "true"
)
2024-10-24 17:58:23 -04:00
feature_flagged_views = [
2024-10-24 17:03:45 -07:00
"best_practices",
2024-10-24 17:58:23 -04:00
"clear_goals",
"rules_and_regulations",
"establish_trust",
"write_for_action",
"multiple_languages",
"benchmark_performance",
]
2024-10-24 17:03:45 -07:00
return not feature_best_practices_enabled and view in feature_flagged_views
2024-10-24 17:58:23 -04:00
request = partial(client_request.get, "main.{}".format(view))
2024-10-24 17:58:23 -04:00
# If the guidance feature is disabled, expect a 404 for feature-flagged views
if is_feature_flagged(view):
page = request(_expected_status=404)
else:
# Check the page loads when user is signed in
page = request()
assert page.select_one("meta[name=description]")
# Check it still works when they dont have a recent service
with client_request.session_transaction() as session:
session["service_id"] = None
request()
# Check it redirects to the login screen when they sign out
client_request.logout()
with client_request.session_transaction() as session:
session["service_id"] = None
session["user_id"] = None
request(
_expected_status=302,
_expected_redirect="/sign-in?next={}".format(
url_for("main.{}".format(view))
),
)