Coverage for src/tests/test_api.py: 100%
705 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-12-20 18:03 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-12-20 18:03 +0000
1from datetime import timedelta
3import grpc
4import pytest
5from google.protobuf import empty_pb2, wrappers_pb2
7from couchers import errors
8from couchers.db import session_scope
9from couchers.jobs.handlers import update_badges
10from couchers.materialized_views import refresh_materialized_views_rapid
11from couchers.models import FriendRelationship, FriendStatus
12from couchers.resources import get_badge_dict
13from couchers.sql import couchers_select as select
14from couchers.utils import create_coordinate, to_aware_datetime
15from proto import api_pb2, jail_pb2, notifications_pb2
16from tests.test_fixtures import ( # noqa
17 api_session,
18 blocking_session,
19 db,
20 email_fields,
21 generate_user,
22 make_friends,
23 make_user_block,
24 make_user_invisible,
25 mock_notification_email,
26 notifications_session,
27 push_collector,
28 real_api_session,
29 real_jail_session,
30 testconfig,
31)
34@pytest.fixture(autouse=True)
35def _(testconfig):
36 pass
39def test_ping(db):
40 user, token = generate_user()
42 with real_api_session(token) as api:
43 res = api.Ping(api_pb2.PingReq())
45 assert res.user.user_id == user.id
46 assert res.user.username == user.username
47 assert res.user.name == user.name
48 assert res.user.city == user.city
49 assert res.user.hometown == user.hometown
50 assert res.user.verification == 0.0
51 assert res.user.community_standing == user.community_standing
52 assert res.user.num_references == 0
53 assert res.user.gender == user.gender
54 assert res.user.pronouns == user.pronouns
55 assert res.user.age == user.age
57 assert (res.user.lat, res.user.lng) == (user.coordinates or (0, 0))
59 # the joined time is fuzzed
60 # but shouldn't be before actual joined time, or more than one hour behind
61 assert user.joined - timedelta(hours=1) <= to_aware_datetime(res.user.joined) <= user.joined
62 # same for last_active
63 assert user.last_active - timedelta(hours=1) <= to_aware_datetime(res.user.last_active) <= user.last_active
65 assert res.user.hosting_status == api_pb2.HOSTING_STATUS_CANT_HOST
66 assert res.user.meetup_status == api_pb2.MEETUP_STATUS_OPEN_TO_MEETUP
68 assert res.user.occupation == user.occupation
69 assert res.user.education == user.education
70 assert res.user.about_me == user.about_me
71 assert res.user.things_i_like == user.things_i_like
72 assert {language_ability.code for language_ability in res.user.language_abilities} == {"fin", "fra"}
73 assert res.user.about_place == user.about_place
74 assert res.user.regions_visited == ["FIN", "REU", "CHE"] # Tests alphabetization by region name
75 assert res.user.regions_lived == ["EST", "FRA", "ESP"] # Ditto
76 assert res.user.additional_information == user.additional_information
78 assert res.user.friends == api_pb2.User.FriendshipStatus.NA
79 assert not res.user.HasField("pending_friend_request")
82def test_coords(db):
83 # make them have not added a location
84 user1, token1 = generate_user(geom=None, geom_radius=None)
85 user2, token2 = generate_user()
87 with api_session(token2) as api:
88 res = api.Ping(api_pb2.PingReq())
89 assert res.user.city == user2.city
90 lat, lng = user2.coordinates or (0, 0)
91 assert res.user.lat == lat
92 assert res.user.lng == lng
93 assert res.user.radius == user2.geom_radius
95 with api_session(token2) as api:
96 res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
97 assert res.city == user1.city
98 assert res.lat == 0.0
99 assert res.lng == 0.0
100 assert res.radius == 0.0
102 # Check coordinate wrapping
103 user3, token3 = generate_user(geom=create_coordinate(40.0, -180.5))
104 user4, token4 = generate_user(geom=create_coordinate(40.0, 20.0))
105 user5, token5 = generate_user(geom=create_coordinate(90.5, 20.0))
107 with api_session(token3) as api:
108 res = api.GetUser(api_pb2.GetUserReq(user=user3.username))
109 assert res.lat == 40.0
110 assert res.lng == 179.5
112 with api_session(token4) as api:
113 res = api.GetUser(api_pb2.GetUserReq(user=user4.username))
114 assert res.lat == 40.0
115 assert res.lng == 20.0
117 # PostGIS does not wrap longitude for latitude overflow
118 with api_session(token5) as api:
119 res = api.GetUser(api_pb2.GetUserReq(user=user5.username))
120 assert res.lat == 89.5
121 assert res.lng == 20.0
123 with real_jail_session(token1) as jail:
124 res = jail.JailInfo(empty_pb2.Empty())
125 assert res.jailed
126 assert res.has_not_added_location
128 res = jail.SetLocation(
129 jail_pb2.SetLocationReq(
130 city="New York City",
131 lat=40.7812,
132 lng=-73.9647,
133 radius=250,
134 )
135 )
137 assert not res.jailed
138 assert not res.has_not_added_location
140 res = jail.JailInfo(empty_pb2.Empty())
141 assert not res.jailed
142 assert not res.has_not_added_location
144 with api_session(token2) as api:
145 res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
146 assert res.city == "New York City"
147 assert res.lat == 40.7812
148 assert res.lng == -73.9647
149 assert res.radius == 250
152def test_get_user(db):
153 user1, token1 = generate_user()
154 user2, token2 = generate_user()
156 with api_session(token1) as api:
157 res = api.GetUser(api_pb2.GetUserReq(user=user2.username))
158 assert res.user_id == user2.id
159 assert res.username == user2.username
160 assert res.name == user2.name
162 with api_session(token1) as api:
163 res = api.GetUser(api_pb2.GetUserReq(user=str(user2.id)))
164 assert res.user_id == user2.id
165 assert res.username == user2.username
166 assert res.name == user2.name
169def test_lite_coords(db):
170 # make them have not added a location
171 user1, token1 = generate_user(geom=None, geom_radius=None)
172 user2, token2 = generate_user()
174 refresh_materialized_views_rapid(None)
176 with api_session(token2) as api:
177 res = api.Ping(api_pb2.PingReq())
178 assert res.user.city == user2.city
179 lat, lng = user2.coordinates or (0, 0)
180 assert res.user.lat == lat
181 assert res.user.lng == lng
182 assert res.user.radius == user2.geom_radius
184 with api_session(token2) as api:
185 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=user1.username))
186 assert res.city == user1.city
187 assert res.lat == 0.0
188 assert res.lng == 0.0
189 assert res.radius == 0.0
191 # Check coordinate wrapping
192 user3, token3 = generate_user(geom=create_coordinate(40.0, -180.5))
193 user4, token4 = generate_user(geom=create_coordinate(40.0, 20.0))
194 user5, token5 = generate_user(geom=create_coordinate(90.5, 20.0))
196 refresh_materialized_views_rapid(None)
198 with api_session(token3) as api:
199 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=user3.username))
200 assert res.lat == 40.0
201 assert res.lng == 179.5
203 with api_session(token4) as api:
204 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=user4.username))
205 assert res.lat == 40.0
206 assert res.lng == 20.0
208 # PostGIS does not wrap longitude for latitude overflow
209 with api_session(token5) as api:
210 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=user5.username))
211 assert res.lat == 89.5
212 assert res.lng == 20.0
214 with real_jail_session(token1) as jail:
215 res = jail.JailInfo(empty_pb2.Empty())
216 assert res.jailed
217 assert res.has_not_added_location
219 res = jail.SetLocation(
220 jail_pb2.SetLocationReq(
221 city="New York City",
222 lat=40.7812,
223 lng=-73.9647,
224 radius=250,
225 )
226 )
228 assert not res.jailed
229 assert not res.has_not_added_location
231 res = jail.JailInfo(empty_pb2.Empty())
232 assert not res.jailed
233 assert not res.has_not_added_location
235 refresh_materialized_views_rapid(None)
237 with api_session(token2) as api:
238 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=user1.username))
239 assert res.city == "New York City"
240 assert res.lat == 40.7812
241 assert res.lng == -73.9647
242 assert res.radius == 250
245def test_lite_get_user(db):
246 user1, token1 = generate_user()
247 user2, token2 = generate_user()
249 refresh_materialized_views_rapid(None)
251 with api_session(token1) as api:
252 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=user2.username))
253 assert res.user_id == user2.id
254 assert res.username == user2.username
255 assert res.name == user2.name
257 with api_session(token1) as api:
258 res = api.GetLiteUser(api_pb2.GetLiteUserReq(user=str(user2.id)))
259 assert res.user_id == user2.id
260 assert res.username == user2.username
261 assert res.name == user2.name
264def test_GetLiteUsers(db):
265 user1, token1 = generate_user()
266 user2, _ = generate_user()
267 user3, _ = generate_user()
268 user4, _ = generate_user()
269 user5, _ = generate_user()
270 user6, _ = generate_user()
272 make_user_block(user4, user1)
274 refresh_materialized_views_rapid(None)
276 with api_session(token1) as api:
277 res = api.GetLiteUsers(
278 api_pb2.GetLiteUsersReq(
279 users=[
280 user1.username,
281 str(user1.id),
282 "nonexistent",
283 str(user2.id),
284 "9994",
285 user6.username,
286 str(user5.id),
287 "notreal",
288 user4.username,
289 ]
290 )
291 )
293 assert len(res.responses) == 9
294 assert res.responses[0].query == user1.username
295 assert res.responses[0].user.user_id == user1.id
297 assert res.responses[1].query == str(user1.id)
298 assert res.responses[1].user.user_id == user1.id
300 assert res.responses[2].query == "nonexistent"
301 assert res.responses[2].not_found
303 assert res.responses[3].query == str(user2.id)
304 assert res.responses[3].user.user_id == user2.id
306 assert res.responses[4].query == "9994"
307 assert res.responses[4].not_found
309 assert res.responses[5].query == user6.username
310 assert res.responses[5].user.user_id == user6.id
312 assert res.responses[6].query == str(user5.id)
313 assert res.responses[6].user.user_id == user5.id
315 assert res.responses[7].query == "notreal"
316 assert res.responses[7].not_found
318 # blocked
319 assert res.responses[8].query == user4.username
320 assert res.responses[8].not_found
322 with api_session(token1) as api:
323 with pytest.raises(grpc.RpcError) as e:
324 api.GetLiteUsers(api_pb2.GetLiteUsersReq(users=201 * [user1.username]))
325 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
326 assert e.value.details() == errors.REQUESTED_TOO_MANY_USERS
329def test_update_profile(db):
330 user, token = generate_user()
332 with api_session(token) as api:
333 with pytest.raises(grpc.RpcError) as e:
334 api.UpdateProfile(api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(value=" ")))
335 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
336 assert e.value.details() == errors.INVALID_NAME
338 with pytest.raises(grpc.RpcError) as e:
339 api.UpdateProfile(
340 api_pb2.UpdateProfileReq(lat=wrappers_pb2.DoubleValue(value=0), lng=wrappers_pb2.DoubleValue(value=0))
341 )
342 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
343 assert e.value.details() == errors.INVALID_COORDINATE
345 with pytest.raises(grpc.RpcError) as e:
346 api.UpdateProfile(
347 api_pb2.UpdateProfileReq(regions_visited=api_pb2.RepeatedStringValue(value=["United States"]))
348 )
349 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
350 assert e.value.details() == errors.INVALID_REGION
352 with pytest.raises(grpc.RpcError) as e:
353 api.UpdateProfile(
354 api_pb2.UpdateProfileReq(regions_lived=api_pb2.RepeatedStringValue(value=["United Kingdom"]))
355 )
356 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
357 assert e.value.details() == errors.INVALID_REGION
359 api.UpdateProfile(
360 api_pb2.UpdateProfileReq(
361 name=wrappers_pb2.StringValue(value="New name"),
362 city=wrappers_pb2.StringValue(value="Timbuktu"),
363 hometown=api_pb2.NullableStringValue(value="Walla Walla"),
364 lat=wrappers_pb2.DoubleValue(value=0.01),
365 lng=wrappers_pb2.DoubleValue(value=-2),
366 radius=wrappers_pb2.DoubleValue(value=321),
367 pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"),
368 occupation=api_pb2.NullableStringValue(value="Testing"),
369 education=api_pb2.NullableStringValue(value="Couchers U"),
370 about_me=api_pb2.NullableStringValue(value="I rule"),
371 things_i_like=api_pb2.NullableStringValue(value="Couchers"),
372 about_place=api_pb2.NullableStringValue(value="My place"),
373 hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
374 meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
375 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
376 value=[
377 api_pb2.LanguageAbility(
378 code="eng",
379 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
380 )
381 ],
382 ),
383 regions_visited=api_pb2.RepeatedStringValue(value=["CXR", "FIN"]),
384 regions_lived=api_pb2.RepeatedStringValue(value=["USA", "EST"]),
385 additional_information=api_pb2.NullableStringValue(value="I <3 Couchers"),
386 )
387 )
389 user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
390 assert user_details.name == "New name"
391 assert user_details.city == "Timbuktu"
392 assert user_details.hometown == "Walla Walla"
393 assert user_details.pronouns == "Ro, Robo, Robots"
394 assert user_details.education == "Couchers U"
395 assert user_details.things_i_like == "Couchers"
396 assert user_details.lat == 0.01
397 assert user_details.lng == -2
398 assert user_details.radius == 321
399 assert user_details.occupation == "Testing"
400 assert user_details.about_me == "I rule"
401 assert user_details.about_place == "My place"
402 assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
403 assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
404 assert user_details.language_abilities[0].code == "eng"
405 assert user_details.language_abilities[0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT
406 assert user_details.additional_information == "I <3 Couchers"
407 assert user_details.regions_visited == ["CXR", "FIN"]
408 assert user_details.regions_lived == ["EST", "USA"]
410 # Test unset values
411 api.UpdateProfile(
412 api_pb2.UpdateProfileReq(
413 hometown=api_pb2.NullableStringValue(is_null=True),
414 radius=wrappers_pb2.DoubleValue(value=0),
415 pronouns=api_pb2.NullableStringValue(is_null=True),
416 occupation=api_pb2.NullableStringValue(is_null=True),
417 education=api_pb2.NullableStringValue(is_null=True),
418 about_me=api_pb2.NullableStringValue(is_null=True),
419 things_i_like=api_pb2.NullableStringValue(is_null=True),
420 about_place=api_pb2.NullableStringValue(is_null=True),
421 hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
422 meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
423 language_abilities=api_pb2.RepeatedLanguageAbilityValue(value=[]),
424 regions_visited=api_pb2.RepeatedStringValue(value=[]),
425 regions_lived=api_pb2.RepeatedStringValue(value=[]),
426 additional_information=api_pb2.NullableStringValue(is_null=True),
427 )
428 )
430 user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
431 assert not user_details.hometown
432 assert not user_details.radius
433 assert not user_details.pronouns
434 assert not user_details.occupation
435 assert not user_details.education
436 assert not user_details.about_me
437 assert not user_details.things_i_like
438 assert not user_details.about_place
439 assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
440 assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
441 assert not user_details.language_abilities
442 assert not user_details.regions_visited
443 assert not user_details.regions_lived
444 assert not user_details.additional_information
447def test_update_profile_do_not_email(db):
448 user, token = generate_user()
450 with notifications_session(token) as notifications:
451 notifications.SetNotificationSettings(notifications_pb2.SetNotificationSettingsReq(enable_do_not_email=True))
453 with api_session(token) as api:
454 with pytest.raises(grpc.RpcError) as e:
455 api.UpdateProfile(api_pb2.UpdateProfileReq(hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST))
456 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
457 assert e.value.details() == errors.DO_NOT_EMAIL_CANNOT_HOST
459 with pytest.raises(grpc.RpcError) as e:
460 api.UpdateProfile(api_pb2.UpdateProfileReq(meetup_status=api_pb2.MEETUP_STATUS_OPEN_TO_MEETUP))
461 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
462 assert e.value.details() == errors.DO_NOT_EMAIL_CANNOT_MEET
465def test_language_abilities(db):
466 user, token = generate_user()
468 with api_session(token) as api:
469 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
470 assert len(res.language_abilities) == 2
472 # can't add non-existent languages
473 with pytest.raises(grpc.RpcError) as e:
474 res = api.UpdateProfile(
475 api_pb2.UpdateProfileReq(
476 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
477 value=[
478 api_pb2.LanguageAbility(
479 code="QQQ",
480 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
481 )
482 ],
483 ),
484 )
485 )
486 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
487 assert e.value.details() == errors.INVALID_LANGUAGE
489 # can't have multiple languages of the same type
490 with pytest.raises(Exception) as e:
491 res = api.UpdateProfile(
492 api_pb2.UpdateProfileReq(
493 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
494 value=[
495 api_pb2.LanguageAbility(
496 code="eng",
497 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
498 ),
499 api_pb2.LanguageAbility(
500 code="eng",
501 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
502 ),
503 ],
504 ),
505 )
506 )
507 assert "violates unique constraint" in str(e.value)
509 # nothing changed
510 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
511 assert len(res.language_abilities) == 2
513 # now actually add a value
514 api.UpdateProfile(
515 api_pb2.UpdateProfileReq(
516 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
517 value=[
518 api_pb2.LanguageAbility(
519 code="eng",
520 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
521 )
522 ],
523 ),
524 )
525 )
527 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
528 assert len(res.language_abilities) == 1
529 assert res.language_abilities[0].code == "eng"
530 assert res.language_abilities[0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT
532 # change the value to a new one
533 api.UpdateProfile(
534 api_pb2.UpdateProfileReq(
535 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
536 value=[
537 api_pb2.LanguageAbility(
538 code="fin",
539 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER,
540 )
541 ],
542 ),
543 )
544 )
546 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
547 assert len(res.language_abilities) == 1
548 assert res.language_abilities[0].code == "fin"
549 assert res.language_abilities[0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER
551 # should be able to set to same value still
552 api.UpdateProfile(
553 api_pb2.UpdateProfileReq(
554 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
555 value=[
556 api_pb2.LanguageAbility(
557 code="fin",
558 fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER,
559 )
560 ],
561 ),
562 )
563 )
565 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
566 assert len(res.language_abilities) == 1
567 assert res.language_abilities[0].code == "fin"
568 assert res.language_abilities[0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER
570 # don't change it
571 api.UpdateProfile(api_pb2.UpdateProfileReq())
573 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
574 assert len(res.language_abilities) == 1
575 assert res.language_abilities[0].code == "fin"
576 assert res.language_abilities[0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER
578 # remove value
579 api.UpdateProfile(
580 api_pb2.UpdateProfileReq(
581 language_abilities=api_pb2.RepeatedLanguageAbilityValue(
582 value=[],
583 ),
584 )
585 )
587 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
588 assert len(res.language_abilities) == 0
591def test_pending_friend_request_count(db):
592 user1, token1 = generate_user()
593 user2, token2 = generate_user()
594 user3, token3 = generate_user()
596 with api_session(token2) as api:
597 res = api.Ping(api_pb2.PingReq())
598 assert res.pending_friend_request_count == 0
600 with api_session(token1) as api:
601 res = api.Ping(api_pb2.PingReq())
602 assert res.pending_friend_request_count == 0
603 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
604 res = api.Ping(api_pb2.PingReq())
605 assert res.pending_friend_request_count == 0
607 with api_session(token2) as api:
608 res = api.Ping(api_pb2.PingReq())
609 assert res.pending_friend_request_count == 1
611 with api_session(token2) as api:
612 # check it's there
613 res = api.ListFriendRequests(empty_pb2.Empty())
614 assert len(res.sent) == 0
615 assert len(res.received) == 1
617 assert res.received[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
618 assert res.received[0].user_id == user1.id
620 fr_id = res.received[0].friend_request_id
622 # accept it
623 api.RespondFriendRequest(api_pb2.RespondFriendRequestReq(friend_request_id=fr_id, accept=True))
625 res = api.Ping(api_pb2.PingReq())
626 assert res.pending_friend_request_count == 0
629def test_friend_request_flow(db, push_collector):
630 user1, token1 = generate_user(complete_profile=True)
631 user2, token2 = generate_user(complete_profile=True)
632 user3, token3 = generate_user()
634 # send friend request from user1 to user2
635 with mock_notification_email() as mock:
636 with api_session(token1) as api:
637 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
639 push_collector.assert_user_has_single_matching(
640 user2.id,
641 title=f"{user1.name} wants to be your friend",
642 body=f"You've received a friend request from {user1.name}",
643 )
645 mock.assert_called_once()
646 e = email_fields(mock)
647 assert e.recipient == user2.email
648 assert e.subject == f"[TEST] {user1.name} wants to be your friend on Couchers.org!"
649 assert user2.name in e.plain
650 assert user2.name in e.html
651 assert user1.name in e.plain
652 assert user1.name in e.html
653 assert "http://localhost:5001/img/thumbnail/" not in e.plain
654 assert "http://localhost:5001/img/thumbnail/" in e.html
655 assert "http://localhost:3000/connections/friends/" in e.plain
656 assert "http://localhost:3000/connections/friends/" in e.html
658 with session_scope() as session:
659 friend_request_id = (
660 session.execute(
661 select(FriendRelationship).where(
662 FriendRelationship.from_user_id == user1.id and FriendRelationship.to_user_id == user2.id
663 )
664 ).scalar_one_or_none()
665 ).id
667 with api_session(token1) as api:
668 # check it went through
669 res = api.ListFriendRequests(empty_pb2.Empty())
670 assert len(res.sent) == 1
671 assert len(res.received) == 0
673 assert res.sent[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
674 assert res.sent[0].user_id == user2.id
675 assert res.sent[0].friend_request_id == friend_request_id
677 with api_session(token2) as api:
678 # check it's there
679 res = api.ListFriendRequests(empty_pb2.Empty())
680 assert len(res.sent) == 0
681 assert len(res.received) == 1
683 assert res.received[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
684 assert res.received[0].user_id == user1.id
686 fr_id = res.received[0].friend_request_id
688 # accept it
689 with mock_notification_email() as mock:
690 api.RespondFriendRequest(api_pb2.RespondFriendRequestReq(friend_request_id=fr_id, accept=True))
692 # check it's gone
693 res = api.ListFriendRequests(empty_pb2.Empty())
694 assert len(res.sent) == 0
695 assert len(res.received) == 0
697 # check we're friends now
698 res = api.ListFriends(empty_pb2.Empty())
699 assert len(res.user_ids) == 1
700 assert res.user_ids[0] == user1.id
702 push_collector.assert_user_has_count(user2.id, 1)
703 push_collector.assert_user_push_matches_fields(
704 user1.id,
705 title=f"{user2.name} accepted your friend request!",
706 body=f"{user2.name} has accepted your friend request",
707 )
709 mock.assert_called_once()
710 e = email_fields(mock)
711 assert e.recipient == user1.email
712 assert e.subject == f"[TEST] {user2.name} accepted your friend request!"
713 assert user1.name in e.plain
714 assert user1.name in e.html
715 assert user2.name in e.plain
716 assert user2.name in e.html
717 assert "http://localhost:5001/img/thumbnail/" not in e.plain
718 assert "http://localhost:5001/img/thumbnail/" in e.html
719 assert f"http://localhost:3000/user/{user2.username}" in e.plain
720 assert f"http://localhost:3000/user/{user2.username}" in e.html
722 with api_session(token1) as api:
723 # check it's gone
724 res = api.ListFriendRequests(empty_pb2.Empty())
725 assert len(res.sent) == 0
726 assert len(res.received) == 0
728 # check we're friends now
729 res = api.ListFriends(empty_pb2.Empty())
730 assert len(res.user_ids) == 1
731 assert res.user_ids[0] == user2.id
734def test_cant_friend_request_twice(db):
735 user1, token1 = generate_user()
736 user2, token2 = generate_user()
738 # send friend request from user1 to user2
739 with api_session(token1) as api:
740 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
742 with pytest.raises(grpc.RpcError) as e:
743 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
744 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
745 assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
748def test_cant_friend_request_pending(db):
749 user1, token1 = generate_user()
750 user2, token2 = generate_user()
752 # send friend request from user1 to user2
753 with api_session(token1) as api:
754 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
756 with api_session(token2) as api:
757 with pytest.raises(grpc.RpcError) as e:
758 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user1.id))
759 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
760 assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
763def test_cant_friend_request_already_friends(db):
764 user1, token1 = generate_user()
765 user2, token2 = generate_user()
766 make_friends(user1, user2)
768 with api_session(token1) as api:
769 with pytest.raises(grpc.RpcError) as e:
770 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
771 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
772 assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
774 with api_session(token2) as api:
775 with pytest.raises(grpc.RpcError) as e:
776 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user1.id))
777 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
778 assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
781def test_ListFriends(db):
782 user1, token1 = generate_user()
783 user2, token2 = generate_user()
784 user3, token3 = generate_user()
786 # send friend request from user1 to user2 and user3
787 with api_session(token1) as api:
788 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
789 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user3.id))
791 with api_session(token3) as api:
792 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
794 with api_session(token2) as api:
795 res = api.ListFriendRequests(empty_pb2.Empty())
796 assert len(res.received) == 2
798 # order is an implementation detail
799 user1_req = [req for req in res.received if req.user_id == user1.id][0]
800 user3_req = [req for req in res.received if req.user_id == user3.id][0]
802 assert user1_req.state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
803 assert user1_req.user_id == user1.id
804 api.RespondFriendRequest(
805 api_pb2.RespondFriendRequestReq(friend_request_id=user1_req.friend_request_id, accept=True)
806 )
808 assert user3_req.state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
809 assert user3_req.user_id == user3.id
810 api.RespondFriendRequest(
811 api_pb2.RespondFriendRequestReq(friend_request_id=user3_req.friend_request_id, accept=True)
812 )
814 # check we now have two friends
815 res = api.ListFriends(empty_pb2.Empty())
816 assert len(res.user_ids) == 2
817 assert user1.id in res.user_ids
818 assert user3.id in res.user_ids
820 with api_session(token3) as api:
821 res = api.ListFriends(empty_pb2.Empty())
822 assert len(res.user_ids) == 1
823 assert user2.id in res.user_ids
825 res = api.ListFriendRequests(empty_pb2.Empty())
826 assert len(res.received) == 1
827 assert res.received[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
828 assert res.received[0].user_id == user1.id
829 fr_id = res.received[0].friend_request_id
830 api.RespondFriendRequest(api_pb2.RespondFriendRequestReq(friend_request_id=fr_id, accept=True))
832 res = api.ListFriends(empty_pb2.Empty())
833 assert len(res.user_ids) == 2
834 assert user1.id in res.user_ids
835 assert user2.id in res.user_ids
837 with api_session(token1) as api:
838 res = api.ListFriends(empty_pb2.Empty())
839 assert len(res.user_ids) == 2
840 assert user2.id in res.user_ids
841 assert user3.id in res.user_ids
844def test_ListMutualFriends(db):
845 user1, token1 = generate_user()
846 user2, token2 = generate_user()
847 user3, token3 = generate_user()
848 user4, token4 = generate_user()
849 user5, token5 = generate_user()
851 # arrange friends like this: 1<->2, 1<->3, 1<->4, 1<->5, 3<->2, 3<->4,
852 # so 1 and 2 should have mutual friend 3 only
853 make_friends(user1, user2)
854 make_friends(user1, user3)
855 make_friends(user1, user4)
856 make_friends(user1, user5)
857 make_friends(user3, user2)
858 make_friends(user3, user4)
860 with api_session(token1) as api:
861 mutual_friends = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user2.id)).mutual_friends
862 assert len(mutual_friends) == 1
863 assert mutual_friends[0].user_id == user3.id
865 # and other way around same
866 with api_session(token2) as api:
867 mutual_friends = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user1.id)).mutual_friends
868 assert len(mutual_friends) == 1
869 assert mutual_friends[0].user_id == user3.id
871 # Check pending request doesn't have effect
872 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user5.id))
874 mutual_friends = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user1.id)).mutual_friends
875 assert len(mutual_friends) == 1
876 assert mutual_friends[0].user_id == user3.id
878 # both ways
879 with api_session(token1) as api:
880 mutual_friends = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user2.id)).mutual_friends
881 assert len(mutual_friends) == 1
882 assert mutual_friends[0].user_id == user3.id
885def test_mutual_friends_self(db):
886 user1, token1 = generate_user()
887 user2, token2 = generate_user()
888 user3, token3 = generate_user()
889 user4, token4 = generate_user()
891 make_friends(user1, user2)
892 make_friends(user2, user3)
893 make_friends(user1, user4)
895 with api_session(token1) as api:
896 res = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user1.id))
897 assert len(res.mutual_friends) == 0
899 with api_session(token2) as api:
900 res = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user2.id))
901 assert len(res.mutual_friends) == 0
903 with api_session(token3) as api:
904 res = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user3.id))
905 assert len(res.mutual_friends) == 0
907 with api_session(token4) as api:
908 res = api.ListMutualFriends(api_pb2.ListMutualFriendsReq(user_id=user4.id))
909 assert len(res.mutual_friends) == 0
912def test_CancelFriendRequest(db):
913 user1, token1 = generate_user()
914 user2, token2 = generate_user()
916 with api_session(token1) as api:
917 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
919 res = api.ListFriendRequests(empty_pb2.Empty())
920 assert res.sent[0].user_id == user2.id
921 fr_id = res.sent[0].friend_request_id
923 api.CancelFriendRequest(api_pb2.CancelFriendRequestReq(friend_request_id=fr_id))
925 # check it's gone
926 res = api.ListFriendRequests(empty_pb2.Empty())
927 assert len(res.sent) == 0
928 assert len(res.received) == 0
930 # check not friends
931 res = api.ListFriends(empty_pb2.Empty())
932 assert len(res.user_ids) == 0
934 with api_session(token2) as api:
935 # check it's gone
936 res = api.ListFriendRequests(empty_pb2.Empty())
937 assert len(res.sent) == 0
938 assert len(res.received) == 0
940 # check we're not friends
941 res = api.ListFriends(empty_pb2.Empty())
942 assert len(res.user_ids) == 0
944 with api_session(token1) as api:
945 # check we can send another friend req
946 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
948 res = api.ListFriendRequests(empty_pb2.Empty())
949 assert res.sent[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
950 assert res.sent[0].user_id == user2.id
953def test_accept_friend_request(db):
954 user1, token1 = generate_user()
955 user2, token2 = generate_user()
957 with session_scope() as session:
958 friend_request = FriendRelationship(from_user_id=user1.id, to_user_id=user2.id, status=FriendStatus.pending)
959 session.add(friend_request)
960 session.commit()
961 friend_request_id = friend_request.id
963 with api_session(token2) as api:
964 # check request pending
965 res = api.ListFriendRequests(empty_pb2.Empty())
966 assert len(res.received) == 1
967 assert res.received[0].user_id == user1.id
969 api.RespondFriendRequest(api_pb2.RespondFriendRequestReq(friend_request_id=friend_request_id, accept=True))
971 # check request is gone
972 res = api.ListFriendRequests(empty_pb2.Empty())
973 assert len(res.sent) == 0
974 assert len(res.received) == 0
976 # check now friends
977 res = api.ListFriends(empty_pb2.Empty())
978 assert len(res.user_ids) == 1
979 assert res.user_ids[0] == user1.id
981 with api_session(token1) as api:
982 # check request gone
983 res = api.ListFriendRequests(empty_pb2.Empty())
984 assert len(res.sent) == 0
985 assert len(res.received) == 0
987 # check now friends
988 res = api.ListFriends(empty_pb2.Empty())
989 assert len(res.user_ids) == 1
990 assert res.user_ids[0] == user2.id
993def test_reject_friend_request(db):
994 user1, token1 = generate_user()
995 user2, token2 = generate_user()
997 with api_session(token1) as api:
998 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
1000 res = api.ListFriendRequests(empty_pb2.Empty())
1001 assert res.sent[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
1002 assert res.sent[0].user_id == user2.id
1004 with api_session(token2) as api:
1005 res = api.ListFriendRequests(empty_pb2.Empty())
1006 assert res.received[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
1007 assert res.received[0].user_id == user1.id
1009 fr_id = res.received[0].friend_request_id
1011 # reject it
1012 api.RespondFriendRequest(api_pb2.RespondFriendRequestReq(friend_request_id=fr_id, accept=False))
1014 # check it's gone
1015 res = api.ListFriendRequests(empty_pb2.Empty())
1016 assert len(res.sent) == 0
1017 assert len(res.received) == 0
1019 # check not friends
1020 res = api.ListFriends(empty_pb2.Empty())
1021 assert len(res.user_ids) == 0
1023 with api_session(token1) as api:
1024 # check it's gone
1025 res = api.ListFriendRequests(empty_pb2.Empty())
1026 assert len(res.sent) == 0
1027 assert len(res.received) == 0
1029 # check we're not friends
1030 res = api.ListFriends(empty_pb2.Empty())
1031 assert len(res.user_ids) == 0
1033 # check we can send another friend req
1034 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
1036 res = api.ListFriendRequests(empty_pb2.Empty())
1037 assert res.sent[0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
1038 assert res.sent[0].user_id == user2.id
1041def test_hosting_preferences(db):
1042 user1, token1 = generate_user()
1043 user2, token2 = generate_user()
1045 with api_session(token1) as api:
1046 res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
1047 assert not res.HasField("max_guests")
1048 assert not res.HasField("last_minute")
1049 assert not res.HasField("has_pets")
1050 assert not res.HasField("accepts_pets")
1051 assert not res.HasField("pet_details")
1052 assert not res.HasField("has_kids")
1053 assert not res.HasField("accepts_kids")
1054 assert not res.HasField("kid_details")
1055 assert not res.HasField("has_housemates")
1056 assert not res.HasField("housemate_details")
1057 assert not res.HasField("wheelchair_accessible")
1058 assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN
1059 assert not res.HasField("smokes_at_home")
1060 assert not res.HasField("drinking_allowed")
1061 assert not res.HasField("drinks_at_home")
1062 assert not res.HasField("other_host_info")
1063 assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN
1064 assert not res.HasField("sleeping_details")
1065 assert not res.HasField("area")
1066 assert not res.HasField("house_rules")
1067 assert not res.HasField("parking")
1068 assert res.parking_details == api_pb2.PARKING_DETAILS_UNKNOWN
1069 assert not res.HasField("camping_ok")
1071 api.UpdateProfile(
1072 api_pb2.UpdateProfileReq(
1073 max_guests=api_pb2.NullableUInt32Value(value=3),
1074 last_minute=api_pb2.NullableBoolValue(value=True),
1075 has_pets=api_pb2.NullableBoolValue(value=False),
1076 accepts_pets=api_pb2.NullableBoolValue(value=True),
1077 pet_details=api_pb2.NullableStringValue(value="I love dogs"),
1078 has_kids=api_pb2.NullableBoolValue(value=False),
1079 accepts_kids=api_pb2.NullableBoolValue(value=True),
1080 kid_details=api_pb2.NullableStringValue(value="I hate kids"),
1081 has_housemates=api_pb2.NullableBoolValue(value=False),
1082 housemate_details=api_pb2.NullableStringValue(value="I have 7 housemates"),
1083 wheelchair_accessible=api_pb2.NullableBoolValue(value=True),
1084 smoking_allowed=api_pb2.SMOKING_LOCATION_WINDOW,
1085 area=api_pb2.NullableStringValue(value="area!"),
1086 smokes_at_home=api_pb2.NullableBoolValue(value=False),
1087 drinking_allowed=api_pb2.NullableBoolValue(value=True),
1088 drinks_at_home=api_pb2.NullableBoolValue(value=False),
1089 other_host_info=api_pb2.NullableStringValue(value="I'm pretty swell"),
1090 sleeping_arrangement=api_pb2.SLEEPING_ARRANGEMENT_COMMON,
1091 sleeping_details=api_pb2.NullableStringValue(value="Couch in living room"),
1092 house_rules=api_pb2.NullableStringValue(value="RULES!"),
1093 parking=api_pb2.NullableBoolValue(value=True),
1094 parking_details=api_pb2.PARKING_DETAILS_PAID_ONSITE,
1095 camping_ok=api_pb2.NullableBoolValue(value=False),
1096 )
1097 )
1099 # Use a second user to view the hosting preferences just to check
1100 # that it is public information.
1101 with api_session(token2) as api:
1102 res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
1103 assert res.max_guests.value == 3
1104 assert res.last_minute.value
1105 assert not res.has_pets.value
1106 assert res.accepts_pets.value
1107 assert res.pet_details.value == "I love dogs"
1108 assert not res.has_kids.value
1109 assert res.accepts_kids.value
1110 assert res.kid_details.value == "I hate kids"
1111 assert not res.has_housemates.value
1112 assert res.housemate_details.value == "I have 7 housemates"
1113 assert res.wheelchair_accessible.value
1114 assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_WINDOW
1115 assert not res.smokes_at_home.value
1116 assert res.drinking_allowed.value
1117 assert not res.drinks_at_home.value
1118 assert res.other_host_info.value == "I'm pretty swell"
1119 assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_COMMON
1120 assert res.sleeping_details.value == "Couch in living room"
1121 assert res.area.value == "area!"
1122 assert res.house_rules.value == "RULES!"
1123 assert res.parking.value
1124 assert res.parking_details == api_pb2.PARKING_DETAILS_PAID_ONSITE
1125 assert not res.camping_ok.value
1127 # test unsetting
1128 with api_session(token1) as api:
1129 api.UpdateProfile(
1130 api_pb2.UpdateProfileReq(
1131 max_guests=api_pb2.NullableUInt32Value(is_null=True),
1132 last_minute=api_pb2.NullableBoolValue(is_null=True),
1133 has_pets=api_pb2.NullableBoolValue(is_null=True),
1134 accepts_pets=api_pb2.NullableBoolValue(is_null=True),
1135 pet_details=api_pb2.NullableStringValue(is_null=True),
1136 has_kids=api_pb2.NullableBoolValue(is_null=True),
1137 accepts_kids=api_pb2.NullableBoolValue(is_null=True),
1138 kid_details=api_pb2.NullableStringValue(is_null=True),
1139 has_housemates=api_pb2.NullableBoolValue(is_null=True),
1140 housemate_details=api_pb2.NullableStringValue(is_null=True),
1141 wheelchair_accessible=api_pb2.NullableBoolValue(is_null=True),
1142 smoking_allowed=api_pb2.SMOKING_LOCATION_UNKNOWN,
1143 area=api_pb2.NullableStringValue(is_null=True),
1144 smokes_at_home=api_pb2.NullableBoolValue(is_null=True),
1145 drinking_allowed=api_pb2.NullableBoolValue(is_null=True),
1146 drinks_at_home=api_pb2.NullableBoolValue(is_null=True),
1147 other_host_info=api_pb2.NullableStringValue(is_null=True),
1148 sleeping_arrangement=api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN,
1149 sleeping_details=api_pb2.NullableStringValue(is_null=True),
1150 house_rules=api_pb2.NullableStringValue(is_null=True),
1151 parking=api_pb2.NullableBoolValue(is_null=True),
1152 parking_details=api_pb2.PARKING_DETAILS_UNKNOWN,
1153 camping_ok=api_pb2.NullableBoolValue(is_null=True),
1154 )
1155 )
1157 res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
1158 assert not res.HasField("max_guests")
1159 assert not res.HasField("last_minute")
1160 assert not res.HasField("has_pets")
1161 assert not res.HasField("accepts_pets")
1162 assert not res.HasField("pet_details")
1163 assert not res.HasField("has_kids")
1164 assert not res.HasField("accepts_kids")
1165 assert not res.HasField("kid_details")
1166 assert not res.HasField("has_housemates")
1167 assert not res.HasField("housemate_details")
1168 assert not res.HasField("wheelchair_accessible")
1169 assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN
1170 assert not res.HasField("smokes_at_home")
1171 assert not res.HasField("drinking_allowed")
1172 assert not res.HasField("drinks_at_home")
1173 assert not res.HasField("other_host_info")
1174 assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN
1175 assert not res.HasField("sleeping_details")
1176 assert not res.HasField("area")
1177 assert not res.HasField("house_rules")
1178 assert not res.HasField("parking")
1179 assert res.parking_details == api_pb2.PARKING_DETAILS_UNKNOWN
1180 assert not res.HasField("camping_ok")
1183def test_badges(db):
1184 user1, _ = generate_user()
1185 user2, _ = generate_user()
1186 user3, _ = generate_user()
1187 user4, token = generate_user()
1189 update_badges(empty_pb2.Empty())
1191 founder_badge = get_badge_dict()["founder"]
1192 board_member_badge = get_badge_dict()["board_member"]
1194 with api_session(token) as api:
1195 assert api.GetUser(api_pb2.GetUserReq(user=user1.username)).badges == ["founder", "board_member"]
1196 assert api.GetUser(api_pb2.GetUserReq(user=user2.username)).badges == ["founder", "board_member"]
1197 assert api.GetUser(api_pb2.GetUserReq(user=user3.username)).badges == []
1199 assert api.ListBadgeUsers(api_pb2.ListBadgeUsersReq(badge_id=founder_badge["id"])).user_ids == [1, 2]
1200 res = api.ListBadgeUsers(api_pb2.ListBadgeUsersReq(badge_id=board_member_badge["id"], page_size=1))
1201 assert res.user_ids == [1]
1202 res2 = api.ListBadgeUsers(
1203 api_pb2.ListBadgeUsersReq(badge_id=board_member_badge["id"], page_token=res.next_page_token)
1204 )
1205 assert res2.user_ids == [2]