Coverage for app/backend/src/tests/test_resources.py: 100%
69 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-19 15:47 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-19 15:47 +0000
1from google.protobuf import empty_pb2
2from sqlalchemy import select
4from couchers.db import session_scope
5from couchers.models import Language
6from couchers.resources import copy_resources_to_database
7from tests.fixtures.sessions import resources_session
10def test_GetTermsOfService():
11 # make sure it works and we get out a bunch of text
12 with resources_session() as api:
13 res = api.GetTermsOfService(empty_pb2.Empty()).terms_of_service
14 assert len(res) > 100
15 assert "couchers, inc." in res.lower()
18def test_GetCommunityGuidelines():
19 # make sure it works and we get out a bunch of text
20 with resources_session() as api:
21 res = api.GetCommunityGuidelines(empty_pb2.Empty()).community_guidelines
22 assert len(res) == 4
23 assert res[2].title == "Be safe and sensible"
24 assert "inappropriate content" in res[2].guideline
25 assert "stroke" in res[2].icon_svg
28def test_GetRegions(db):
29 with resources_session() as api:
30 regions = api.GetRegions(empty_pb2.Empty()).regions
31 regions_list = [(r.alpha3, r.name) for r in regions]
32 assert ("FIN", "Finland") in regions_list
33 assert ("SWE", "Sweden") in regions_list
34 assert ("???", "Nonexistent region") not in regions_list
36 with resources_session(locale="es") as api:
37 regions = api.GetRegions(empty_pb2.Empty()).regions
38 regions_list = [(r.alpha3, r.name) for r in regions]
39 assert ("FIN", "Finlandia") in regions_list
40 assert ("FIN", "Finland") not in regions_list
43def test_GetLanguages(db):
44 with resources_session() as api:
45 languages = api.GetLanguages(empty_pb2.Empty()).languages
46 languages_list = [(r.code, r.name) for r in languages]
47 assert ("fin", "Finnish") in languages_list
48 assert ("swe", "Swedish") in languages_list
49 assert ("???", "Nonexistent language") not in languages_list
51 with resources_session(locale="es") as api:
52 languages = api.GetLanguages(empty_pb2.Empty()).languages
53 languages_list = [(r.code, r.name) for r in languages]
54 assert ("swe", "Sueco") in languages_list
55 assert ("swe", "Swedish") not in languages_list
58def test_languages_resource_drops_deprecated_ajp(db):
59 # Load the real languages.json (not the hardcoded testing fixture) so a future bad code is caught.
60 # Mirrors test_add_dummy_data's pattern of calling copy_resources_to_database directly.
61 with session_scope() as session:
62 copy_resources_to_database(session)
63 codes = set(session.execute(select(Language.code)).scalars().all())
64 assert "ajp" not in codes # deprecated; ISO 639-3 CR 2022-006 merged it into apc
65 assert "apc" in codes
68def test_GetBadges(db):
69 with resources_session() as api:
70 badges = api.GetBadges(empty_pb2.Empty()).badges
71 badges_dict = {b.id: b for b in badges}
73 # Check that all expected badges are present
74 expected_badge_ids = {
75 "founder",
76 "board_member",
77 "past_board_member",
78 "moderator",
79 "volunteer",
80 "past_volunteer",
81 "donor",
82 "phone_verified",
83 "postal_verified",
84 "strong_verification",
85 "swagster",
86 }
87 assert set(badges_dict.keys()) == expected_badge_ids
89 # Check that a specific badge has the correct properties
90 founder = badges_dict["founder"]
91 assert founder.id == "founder"
92 assert founder.name == "Founder"
93 assert founder.description == "This user is one of the two founders of Couchers.org"
94 assert founder.color == "#e47701"
96 # Check another badge to ensure translations are working
97 moderator = badges_dict["moderator"]
98 assert moderator.id == "moderator"
99 assert moderator.name == "Moderator"
100 assert moderator.description == "This user is a moderator of Couchers.org"
101 assert moderator.color == "#c74f5b"
103 # Check strong_verification badge
104 strong_verification = badges_dict["strong_verification"]
105 assert strong_verification.id == "strong_verification"
106 assert strong_verification.name == "Strong Verification"
107 assert (
108 strong_verification.description
109 == "This user has verified their gender and date of birth with a biometric passport"
110 )
111 assert strong_verification.color == "#1b8aa0"