Coverage for app/backend/src/tests/test_resources.py: 100%

73 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-19 00:32 +0000

1import pytest 

2from google.protobuf import empty_pb2 

3from sqlalchemy import select 

4 

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 

9 

10 

11@pytest.fixture(autouse=True) 

12def _(testconfig): 

13 pass 

14 

15 

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() 

22 

23 

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 

32 

33 

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 

41 

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 

47 

48 

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 

56 

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 

62 

63 

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 

72 

73 

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} 

78 

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 

93 

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" 

100 

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" 

107 

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"