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

170 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-19 06:12 +0000

1from datetime import timedelta 

2from typing import Any 

3 

4import pytest 

5from google.protobuf import empty_pb2 

6from sqlalchemy import select, update 

7 

8from couchers.db import session_scope 

9from couchers.materialized_views import LiteUser, refresh_materialized_views, refresh_materialized_views_rapid 

10from couchers.models import ( 

11 HostingStatus, 

12 HostRequest, 

13 HostRequestStatus, 

14 Message, 

15 MessageType, 

16 ModerationState, 

17 ModerationVisibility, 

18 User, 

19) 

20from couchers.proto import account_pb2, api_pb2 

21from couchers.utils import now 

22from tests.fixtures.db import generate_user 

23from tests.fixtures.sessions import account_session 

24from tests.test_references import create_friend_reference, create_host_request 

25 

26LONG_ABOUT_PLACE = "My home is a cosy flat with a spare room and a very comfy couch." 

27 

28 

29def _generate_host(**kwargs: Any) -> tuple[Any, str]: 

30 fields: dict[str, Any] = dict(hosting_status=HostingStatus.can_host, about_place=LONG_ABOUT_PLACE) 

31 fields.update(kwargs) 

32 return generate_user(**fields) 

33 

34 

35def _get_account_info(token: str) -> account_pb2.GetAccountInfoRes: 

36 refresh_materialized_views(empty_pb2.Empty()) 

37 with account_session(token) as account: 

38 res: account_pb2.GetAccountInfoRes = account.GetAccountInfo(empty_pb2.Empty()) 

39 return res 

40 

41 

42def _assert_account_info(token: str, host_pool: int, has_newbie_hosting_history: bool, my_home_complete: bool) -> None: 

43 res = _get_account_info(token) 

44 assert (res.host_pool, res.has_newbie_hosting_history, res.my_home_complete) == ( 

45 host_pool, 

46 has_newbie_hosting_history, 

47 my_home_complete, 

48 ) 

49 

50 

51def _add_references(host_id: int, count: int) -> list[int]: 

52 author_ids = [] 

53 for _ in range(count): 

54 author, _ = generate_user() 

55 with session_scope() as session: 

56 create_friend_reference(session, author.id, host_id, timedelta(days=10)) 

57 author_ids.append(author.id) 

58 return author_ids 

59 

60 

61def _add_host_requests( 

62 host_id: int, 

63 count: int, 

64 response_time: timedelta | None, 

65 *, 

66 accepted: bool = False, 

67 status: HostRequestStatus = HostRequestStatus.confirmed, 

68 host_request_age: timedelta = timedelta(days=15), 

69 is_offer: bool = False, 

70) -> list[int]: 

71 surfer_ids = [] 

72 for _ in range(count): 

73 surfer, _ = generate_user() 

74 surfer_ids.append(surfer.id) 

75 with session_scope() as session: 

76 conversation_id = create_host_request( 

77 session, surfer.id, host_id, host_request_age=host_request_age, status=status, is_offer=is_offer 

78 ) 

79 session.execute( 

80 update(ModerationState) 

81 .where( 

82 ModerationState.id 

83 == select(HostRequest.moderation_state_id) 

84 .where(HostRequest.conversation_id == conversation_id) 

85 .scalar_subquery() 

86 ) 

87 .values(visibility=ModerationVisibility.visible) 

88 ) 

89 if response_time is not None: 

90 created = session.execute( 

91 select(Message.time) 

92 .where(Message.conversation_id == conversation_id) 

93 .where(Message.message_type == MessageType.chat_created) 

94 ).scalar_one() 

95 if accepted: 

96 response = Message( 

97 conversation_id=conversation_id, 

98 author_id=host_id, 

99 message_type=MessageType.host_request_status_changed, 

100 host_request_status_target=HostRequestStatus.accepted, 

101 ) 

102 else: 

103 response = Message( 

104 conversation_id=conversation_id, 

105 author_id=host_id, 

106 text="Thanks for your request!", 

107 message_type=MessageType.text, 

108 ) 

109 response.time = created + response_time 

110 session.add(response) 

111 return surfer_ids 

112 

113 

114def test_host_pool_newbie_without_requests(db): 

115 _, token = _generate_host() 

116 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

117 

118 

119@pytest.mark.parametrize( 

120 "kwargs, my_home_complete", 

121 [ 

122 ({"hosting_status": HostingStatus.maybe}, True), 

123 ({"hosting_status": HostingStatus.cant_host}, True), 

124 ({"about_place": None}, False), 

125 ({"about_place": "x" * 49}, False), 

126 ({"complete_profile": False}, True), 

127 ], 

128) 

129def test_host_pool_host_profile_not_ready(db, kwargs, my_home_complete): 

130 _, token = _generate_host(**kwargs) 

131 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, True, my_home_complete) 

132 

133 

134def test_host_pool_about_place_just_long_enough(db): 

135 _, token = _generate_host(about_place="x" * 50) 

136 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

137 

138 

139def test_host_pool_newbie_with_a_reference(db): 

140 host, token = _generate_host() 

141 _add_references(host.id, 1) 

142 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

143 

144 

145def test_host_pool_normal_with_two_references(db): 

146 host, token = _generate_host() 

147 _add_references(host.id, 2) 

148 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

149 

150 

151def test_host_pool_ignores_references_hidden_from_profile(db): 

152 host, token = _generate_host() 

153 author_ids = _add_references(host.id, 2) 

154 with session_scope() as session: 

155 session.execute(update(User).where(User.id == author_ids[0]).values(banned_at=now())) 

156 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

157 

158 

159def test_host_pool_ineligible_and_my_home_not_filled(db): 

160 host, token = _generate_host(about_place=None) 

161 _add_references(host.id, 2) 

162 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, False) 

163 

164 

165def test_host_pool_newbie_with_prompt_responses(db): 

166 host, token = _generate_host() 

167 _add_host_requests(host.id, 1, timedelta(hours=95), accepted=True) 

168 _add_host_requests(host.id, 4, timedelta(hours=95)) 

169 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

170 

171 

172def test_host_pool_normal_with_too_many_requests(db): 

173 host, token = _generate_host() 

174 _add_host_requests(host.id, 1, timedelta(hours=1), accepted=True) 

175 _add_host_requests(host.id, 5, timedelta(hours=1)) 

176 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

177 

178 

179def test_host_pool_normal_with_slow_responses(db): 

180 host, token = _generate_host() 

181 _add_host_requests(host.id, 1, timedelta(hours=97), accepted=True) 

182 _add_host_requests(host.id, 2, timedelta(hours=97)) 

183 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

184 

185 

186def test_host_pool_normal_with_unanswered_requests(db): 

187 host, token = _generate_host() 

188 _add_host_requests(host.id, 1, timedelta(hours=1), accepted=True) 

189 _add_host_requests(host.id, 1, timedelta(hours=1)) 

190 _add_host_requests(host.id, 2, None) 

191 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

192 

193 

194def test_host_pool_ignores_unanswered_requests_from_banned_users(db): 

195 host, token = _generate_host() 

196 _add_host_requests(host.id, 1, timedelta(hours=1), accepted=True) 

197 _add_host_requests(host.id, 1, timedelta(hours=1)) 

198 spammer_ids = _add_host_requests(host.id, 2, None) 

199 with session_scope() as session: 

200 session.execute(update(User).where(User.id.in_(spammer_ids)).values(banned_at=now())) 

201 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

202 

203 

204def test_host_pool_ignores_requests_younger_than_96_hours(db): 

205 host, token = _generate_host() 

206 _add_host_requests(host.id, 1, timedelta(hours=1), accepted=True) 

207 _add_host_requests(host.id, 5, timedelta(hours=1), host_request_age=timedelta(days=-2)) 

208 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

209 

210 

211def test_host_pool_newbie_with_two_unaccepted_requests(db): 

212 host, token = _generate_host() 

213 _add_host_requests(host.id, 2, timedelta(hours=1)) 

214 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

215 

216 

217def test_host_pool_normal_without_acceptance_in_three_requests(db): 

218 host, token = _generate_host() 

219 _add_host_requests(host.id, 3, timedelta(hours=1)) 

220 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

221 

222 

223def test_host_pool_newbie_with_acceptance_after_three_requests(db): 

224 host, token = _generate_host() 

225 _add_host_requests(host.id, 3, timedelta(hours=1)) 

226 _add_host_requests(host.id, 1, timedelta(hours=1), accepted=True) 

227 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

228 

229 

230def test_host_pool_acceptance_after_168_hours_does_not_count(db): 

231 host, token = _generate_host() 

232 _add_host_requests(host.id, 2, timedelta(hours=1)) 

233 _add_host_requests(host.id, 1, timedelta(hours=170), accepted=True) 

234 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

235 

236 

237def test_host_pool_acceptance_counts_after_cancellation(db): 

238 host, token = _generate_host() 

239 _add_host_requests(host.id, 1, timedelta(hours=1), accepted=True, status=HostRequestStatus.cancelled) 

240 _add_host_requests(host.id, 2, timedelta(hours=1)) 

241 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

242 

243 

244def test_host_pool_ignores_unaccepted_requests_from_banned_users(db): 

245 host, token = _generate_host() 

246 _add_host_requests(host.id, 2, timedelta(hours=1)) 

247 spammer_ids = _add_host_requests(host.id, 2, None) 

248 with session_scope() as session: 

249 session.execute(update(User).where(User.id.in_(spammer_ids)).values(banned_at=now())) 

250 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

251 

252 

253def test_host_pool_recent_request_does_not_count_yet(db): 

254 host, token = _generate_host() 

255 _add_host_requests(host.id, 2, timedelta(hours=1)) 

256 _add_host_requests(host.id, 1, None, status=HostRequestStatus.pending, host_request_age=timedelta(days=-2)) 

257 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

258 

259 

260def test_host_pool_unanswered_third_request_loses_eligibility(db): 

261 host, token = _generate_host() 

262 _add_host_requests(host.id, 2, timedelta(hours=1)) 

263 _add_host_requests(host.id, 1, None, status=HostRequestStatus.pending) 

264 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, False, True) 

265 

266 

267def test_host_pool_ignores_offers_on_public_trips(db): 

268 host, token = _generate_host() 

269 _add_host_requests(host.id, 3, timedelta(hours=1), is_offer=True) 

270 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

271 

272 

273def test_host_pool_in_account_info_is_live(db): 

274 host, token = _generate_host() 

275 _assert_account_info(token, api_pb2.HOST_POOL_NEWBIE, True, True) 

276 

277 with session_scope() as session: 

278 session.execute(update(User).where(User.id == host.id).values(hosting_status=HostingStatus.cant_host)) 

279 

280 _assert_account_info(token, api_pb2.HOST_POOL_NORMAL, True, True) 

281 

282 

283def test_has_completed_my_home_in_lite_users(db): 

284 filled, _ = _generate_host(about_place="x" * 50) 

285 too_short, _ = _generate_host(about_place="x" * 49) 

286 empty, _ = _generate_host(about_place=None) 

287 

288 refresh_materialized_views_rapid(empty_pb2.Empty()) 

289 

290 with session_scope() as session: 

291 has_completed_my_home = dict( 

292 session.execute( 

293 select(LiteUser.id, LiteUser.has_completed_my_home).where( 

294 LiteUser.id.in_([filled.id, too_short.id, empty.id]) 

295 ) 

296 ).all() 

297 ) 

298 assert has_completed_my_home == {filled.id: True, too_short.id: False, empty.id: False}