test_profiles.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import pytest
  2. import sys
  3. import os
  4. from bs4 import BeautifulSoup
  5. import requests
  6. from fontbakery.designers_pb2 import DesignerInfoProto
  7. from google.protobuf import text_format
  8. # TODO this could potentially be a fontbakery profile
  9. @pytest.fixture
  10. def profile_dir():
  11. if len(sys.argv) != 3:
  12. print("Usage: python test_profiles fonts/catalog/designers/designerprofile")
  13. sys.exit(1)
  14. return sys.argv[-1]
  15. @pytest.fixture
  16. def proto_info(profile_dir):
  17. info_file = os.path.join(profile_dir, "info.pb")
  18. with open(info_file, "rb") as info:
  19. text = info.read()
  20. return load_info_proto(text)
  21. @pytest.fixture
  22. def bio(profile_dir):
  23. bio_file = os.path.join(profile_dir, "bio.html")
  24. with open(bio_file) as html:
  25. return BeautifulSoup(html.read(), features="lxml")
  26. def load_info_proto(text_data):
  27. message = DesignerInfoProto()
  28. text_format.Merge(text_data, message)
  29. return message
  30. def test_profile_dir_exists(profile_dir):
  31. assert os.path.exists(profile_dir)
  32. def test_profile_filenames_are_acscii(profile_dir):
  33. failed = []
  34. for filename in os.listdir(profile_dir) + [profile_dir]:
  35. try:
  36. filename.encode("ascii")
  37. except:
  38. failed.append(filename)
  39. assert not failed, f"filenames {failed} must be ascii"
  40. def test_profile_dir_has_bio(profile_dir):
  41. assert "bio.html" in os.listdir(profile_dir), "bio.html is missing"
  42. def test_profile_dir_has_info(profile_dir):
  43. assert "info.pb" in os.listdir(profile_dir), "info.pb is missing"
  44. def test_profile_has_correct_img(profile_dir):
  45. assert not any(f for f in os.listdir(profile_dir) if f.endswith((".jpg", ".jpeg")))
  46. assert any(
  47. f for f in os.listdir(profile_dir) if f.endswith(".png")
  48. ), "Profile is missing png image"
  49. def test_profile_info_image_link_is_correct(profile_dir, proto_info):
  50. img_path = proto_info.avatar.file_name
  51. assert img_path in os.listdir(profile_dir), "info.pb: image path is incorrect"
  52. def test_info_link(proto_info):
  53. link = proto_info.link
  54. assert (
  55. "plus.google" not in link
  56. ), "Google+ links are no longer supported. Please replace."
  57. def test_info_link_works(proto_info):
  58. link = proto_info.link
  59. if "instagram.com" in link or not link:
  60. return
  61. assert requests.get(link).status_code == 200, "info.pb: link is not producing a 200 status code"
  62. def test_bio_links_work(bio):
  63. urls = bio.find_all("a", href=True)
  64. urls = [u["href"] for u in urls]
  65. for url in urls:
  66. if "instagram.com" in url: # these have a habit of raise a 4xx status code
  67. continue
  68. assert (
  69. requests.get(url).status_code == 200
  70. ), f"{url} is not producing a 200 status code"