def test_serve_spa_from_cache(mocker, settings, preferences, no_api_auth): preferences["instance__name"] = 'Best Funkwhale "pod"' request = mocker.Mock(path="/", META={}) get_spa_html = mocker.patch.object( middleware, "get_spa_html", return_value="<html><head><title>Funkwhale</title></head></html>", ) mocker.patch.object( middleware, "get_default_head_tags", return_value=[ { "tag": "meta", "property": "og:title", "content": "default title" }, { "tag": "meta", "property": "og:site_name", "content": "default site name" }, ], ) get_request_head_tags = mocker.patch.object( middleware, "get_request_head_tags", return_value=[ { "tag": "meta", "property": "og:title", "content": "custom title" }, { "tag": "meta", "property": "og:description", "content": "custom description", }, ], ) response = middleware.serve_spa(request) assert response.status_code == 200 expected = [ "<html><head>" "<title>{}</title>".format(html.escape(preferences["instance__name"])), '<meta content="custom title" property="og:title" />', '<meta content="custom description" property="og:description" />', '<meta content="default site name" property="og:site_name" />', "</head></html>", ] get_spa_html.assert_called_once_with(settings.FUNKWHALE_SPA_HTML_ROOT) get_request_head_tags.assert_called_once_with(request) assert response.content == "\n".join(expected).encode()
def test_serve_spa_sets_csrf_token(mocker, no_api_auth): request = mocker.Mock(path="/", META={}) get_token = mocker.patch.object(middleware.csrf, "get_token", return_value="test") mocker.patch.object( middleware, "get_spa_html", return_value="<html><head></head><body></body></html>", ) mocker.patch.object(middleware, "get_default_head_tags", return_value=[]) mocker.patch.object(middleware, "get_request_head_tags", return_value=[]) response = middleware.serve_spa(request) assert response.status_code == 200 get_token.assert_called_once_with(request) assert response.cookies["csrftoken"].value == get_token.return_value
def test_rewrite_manifest_json_url_rewrite_disabled(mocker, settings): settings.FUNKWHALE_SPA_REWRITE_MANIFEST = False settings.FUNKWHALE_SPA_REWRITE_MANIFEST_URL = "custom_url" spa_html = "<html><head><link href=/manifest.json rel=manifest></head></html>" request = mocker.Mock(path="/") mocker.patch.object(middleware, "get_spa_html", return_value=spa_html) mocker.patch.object( middleware, "get_default_head_tags", return_value=[], ) response = middleware.serve_spa(request) assert response.status_code == 200 expected_html = ( "<html><head><link href=/manifest.json rel=manifest>\n\n</head></html>" ) assert response.content == expected_html.encode()
def test_rewrite_manifest_json_url(link, new_url, expected, mocker, settings): settings.FUNKWHALE_SPA_REWRITE_MANIFEST = True settings.FUNKWHALE_SPA_REWRITE_MANIFEST_URL = new_url spa_html = "<html><head><link rel=before>{}<link rel=after></head></html>".format( link) request = mocker.Mock(path="/") mocker.patch.object(middleware, "get_spa_html", return_value=spa_html) mocker.patch.object( middleware, "get_default_head_tags", return_value=[], ) response = middleware.serve_spa(request) assert response.status_code == 200 expected_html = "<html><head><link rel=before>{}<link rel=after>\n\n</head></html>".format( expected) assert response.content == expected_html.encode()
def test_rewrite_manifest_json_url_rewrite_default_url(mocker, settings): settings.FUNKWHALE_SPA_REWRITE_MANIFEST = True settings.FUNKWHALE_SPA_REWRITE_MANIFEST_URL = None spa_html = "<html><head><link href=/manifest.json rel=manifest></head></html>" expected_url = federation_utils.full_url( reverse("api:v1:instance:spa-manifest")) request = mocker.Mock(path="/") mocker.patch.object(middleware, "get_spa_html", return_value=spa_html) mocker.patch.object( middleware, "get_default_head_tags", return_value=[], ) response = middleware.serve_spa(request) assert response.status_code == 200 expected_html = '<html><head><link rel=manifest href="{}">\n\n</head></html>'.format( expected_url) assert response.content == expected_html.encode()
def test_serve_spa_includes_custom_css(mocker, no_api_auth): request = mocker.Mock(path="/") mocker.patch.object( middleware, "get_spa_html", return_value="<html><head></head><body></body></html>", ) mocker.patch.object(middleware, "get_default_head_tags", return_value=[]) mocker.patch.object(middleware, "get_request_head_tags", return_value=[]) get_custom_css = mocker.patch.object( middleware, "get_custom_css", return_value="body { background: black; }") response = middleware.serve_spa(request) assert response.status_code == 200 expected = [ "<html><head>\n\n</head><body>", "<style>body { background: black; }</style>", "</body></html>", ] get_custom_css.assert_called_once_with() assert response.content == "\n".join(expected).encode()