Michael Genson 1d1d61df77
fix: Missing Title and Metadata (#2770)
* add document title to server spa meta

* removed conflicting useMeta

* replaced head with useMeta

* formalized metadata injection

* small injection refactor

* added tests

* added missing global tag

* fixed setting tab title for logged-in users

* simplified metadata update

* remove duplicate tag and fix for foreign users

* add metadata for shared recipes

* added default recipe image

* fixed shared URL

---------

Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
2023-12-06 16:01:48 +01:00

71 lines
2.0 KiB
Python

from bs4 import BeautifulSoup
from mealie.routes.spa import MetaTag, inject_meta, inject_recipe_json
from tests import data as test_data
from tests.utils.factories import random_string
def test_spa_metadata_injection():
fp = test_data.html_mealie_recipe
with open(fp) as f:
soup = BeautifulSoup(f, "lxml")
assert soup.html and soup.html.head
tags = soup.find_all("meta")
assert tags
title_tag = None
for tag in tags:
if tag.get("data-hid") == "og:title":
title_tag = tag
break
assert title_tag and title_tag["content"]
new_title_tag = MetaTag(hid="og:title", property_name="og:title", content=random_string())
new_arbitrary_tag = MetaTag(hid=random_string(), property_name=random_string(), content=random_string())
new_html = inject_meta(str(soup), [new_title_tag, new_arbitrary_tag])
# verify changes were injected
soup = BeautifulSoup(new_html, "lxml")
assert soup.html and soup.html.head
tags = soup.find_all("meta")
assert tags
title_tag = None
for tag in tags:
if tag.get("data-hid") == "og:title":
title_tag = tag
break
assert title_tag and title_tag["content"] == new_title_tag.content
arbitrary_tag = None
for tag in tags:
if tag.get("data-hid") == new_arbitrary_tag.hid:
arbitrary_tag = tag
break
assert arbitrary_tag and arbitrary_tag["content"] == new_arbitrary_tag.content
def test_spa_recipe_json_injection():
recipe_name = random_string()
schema = {
"@context": "https://schema.org",
"@type": "Recipe",
"name": recipe_name,
}
fp = test_data.html_mealie_recipe
with open(fp) as f:
soup = BeautifulSoup(f, "lxml")
assert "https://schema.org" not in str(soup)
html = inject_recipe_json(str(soup), schema)
assert "@context" in html
assert "https://schema.org" in html
assert recipe_name in html