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

75 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-03 11:56 +0000

1from google.protobuf import empty_pb2 

2from sqlalchemy import select 

3 

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 

8 

9 

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

16 

17 

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 

26 

27 

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 

35 

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 

41 

42 

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 ("aka", "Akan (aka)") in languages_list 

48 assert ("eng", "English") in languages_list 

49 assert ("fat", "Akan (fat)") in languages_list 

50 assert ("fil", "Filipino (fil)") in languages_list 

51 assert ("fin", "Finnish") in languages_list 

52 assert ("swe", "Swedish") in languages_list 

53 assert ("tgl", "Filipino (tgl)") in languages_list 

54 assert ("twi", "Akan (twi)") 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 "postal_verified", 

90 "strong_verification", 

91 "swagster", 

92 } 

93 assert set(badges_dict.keys()) == expected_badge_ids 

94 

95 # Check that a specific badge has the correct properties 

96 founder = badges_dict["founder"] 

97 assert founder.id == "founder" 

98 assert founder.name == "Founder" 

99 assert founder.description == "This user is one of the two founders of Couchers.org" 

100 assert founder.color == "#e47701" 

101 

102 # Check another badge to ensure translations are working 

103 moderator = badges_dict["moderator"] 

104 assert moderator.id == "moderator" 

105 assert moderator.name == "Moderator" 

106 assert moderator.description == "This user is a moderator of Couchers.org" 

107 assert moderator.color == "#c74f5b" 

108 

109 # Check strong_verification badge 

110 strong_verification = badges_dict["strong_verification"] 

111 assert strong_verification.id == "strong_verification" 

112 assert strong_verification.name == "Strong Verification" 

113 assert ( 

114 strong_verification.description 

115 == "This user has verified their gender and date of birth with a biometric passport" 

116 ) 

117 assert strong_verification.color == "#1b8aa0"