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