def test_process_send_message_notifications_seen(db): user1, token1 = generate_user() user2, token2 = generate_user() make_friends(user1, user2) process_send_message_notifications(empty_pb2.Empty()) # should find no jobs, since there's no messages with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 0 with conversations_session(token1) as c: group_chat_id = c.CreateGroupChat( conversations_pb2.CreateGroupChatReq( recipient_user_ids=[user2.id])).group_chat_id c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 2")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 3")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 4")) # user 2 now marks those messages as seen with conversations_session(token2) as c: m_id = c.GetGroupChat( conversations_pb2.GetGroupChatReq( group_chat_id=group_chat_id)).latest_message.message_id c.MarkLastSeenGroupChat( conversations_pb2.MarkLastSeenGroupChatReq( group_chat_id=group_chat_id, last_seen_message_id=m_id)) process_send_message_notifications(empty_pb2.Empty()) # no emails sent out with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 0 def now_30_min_in_future(): return now() + timedelta(minutes=30) # still shouldn't generate emails as user2 has seen all messages with patch("couchers.jobs.handlers.now", now_30_min_in_future): process_send_message_notifications(empty_pb2.Empty()) with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 0
def test_cant_friend_request_already_friends(db): user1, token1 = generate_user() user2, token2 = generate_user() make_friends(user1, user2) with api_session(token1) as api: with pytest.raises(grpc.RpcError) as e: api.SendFriendRequest( api_pb2.SendFriendRequestReq(user_id=user2.id)) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING with api_session(token2) as api: with pytest.raises(grpc.RpcError) as e: api.SendFriendRequest( api_pb2.SendFriendRequestReq(user_id=user1.id)) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
def test_mutual_friends_self(db): user1, token1 = generate_user() user2, token2 = generate_user() user3, token3 = generate_user() user4, token4 = generate_user() make_friends(user1, user2) make_friends(user2, user3) make_friends(user1, user4) with api_session(token1) as api: res = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user1.id)) assert len(res.mutual_friends) == 0 with api_session(token2) as api: res = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user2.id)) assert len(res.mutual_friends) == 0 with api_session(token3) as api: res = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user3.id)) assert len(res.mutual_friends) == 0 with api_session(token4) as api: res = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user4.id)) assert len(res.mutual_friends) == 0
def test_mutual_friends_self(db): user1, token1 = generate_user("user1") user2, token2 = generate_user("user2") user3, token3 = generate_user("user3") user4, token4 = generate_user("user4") make_friends(user1, user2) make_friends(user2, user3) make_friends(user1, user4) with api_session(token1) as api: res = api.GetUser(api_pb2.GetUserReq(user=user3.username)) assert len(res.mutual_friends) == 1 assert res.mutual_friends[0].user_id == user2.id with api_session(token3) as api: res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert len(res.mutual_friends) == 1 assert res.mutual_friends[0].user_id == user2.id with api_session(token1) as api: res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert len(res.mutual_friends) == 0 with api_session(token2) as api: res = api.GetUser(api_pb2.GetUserReq(user=user2.username)) assert len(res.mutual_friends) == 0 with api_session(token3) as api: res = api.GetUser(api_pb2.GetUserReq(user=user3.username)) assert len(res.mutual_friends) == 0 with api_session(token4) as api: res = api.GetUser(api_pb2.GetUserReq(user=user4.username)) assert len(res.mutual_friends) == 0
def test_select_dot_where_users_column_visible(db): user1, token1 = generate_user() user2, token2 = generate_user() user3, token3 = generate_user() user4, token4 = generate_user() user5, token5 = generate_user() make_friends(user1, user2) make_friends(user1, user3) make_friends(user1, user4) make_friends(user1, user5) make_user_invisible(user3.id) make_user_block(user1, user4) make_user_block(user5, user1) context = _FakeContext(user1.id) with session_scope() as session: assert (session.execute( select(func.count()).select_from( FriendRelationship).where_users_column_visible( context, FriendRelationship.to_user_id)).scalar_one() == 1)
def test_ListMutualFriends(db): user1, token1 = generate_user() user2, token2 = generate_user() user3, token3 = generate_user() user4, token4 = generate_user() user5, token5 = generate_user() # arrange friends like this: 1<->2, 1<->3, 1<->4, 1<->5, 3<->2, 3<->4, # so 1 and 2 should have mutual friend 3 only make_friends(user1, user2) make_friends(user1, user3) make_friends(user1, user4) make_friends(user1, user5) make_friends(user3, user2) make_friends(user3, user4) with api_session(token1) as api: mutual_friends = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user2.id)).mutual_friends assert len(mutual_friends) == 1 assert mutual_friends[0].user_id == user3.id # and other way around same with api_session(token2) as api: mutual_friends = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user1.id)).mutual_friends assert len(mutual_friends) == 1 assert mutual_friends[0].user_id == user3.id # Check pending request doesn't have effect api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user5.id)) mutual_friends = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user1.id)).mutual_friends assert len(mutual_friends) == 1 assert mutual_friends[0].user_id == user3.id # both ways with api_session(token1) as api: mutual_friends = api.ListMutualFriends( api_pb2.ListMutualFriendsReq(user_id=user2.id)).mutual_friends assert len(mutual_friends) == 1 assert mutual_friends[0].user_id == user3.id
def test_process_send_message_notifications_muted(db): user1, token1 = generate_user() user2, token2 = generate_user() user3, token3 = generate_user() make_friends(user1, user2) make_friends(user1, user3) make_friends(user2, user3) process_send_message_notifications(empty_pb2.Empty()) # should find no jobs, since there's no messages with session_scope() as session: assert (session.execute( select(func.count()).select_from(BackgroundJob).where( BackgroundJob.job_type == BackgroundJobType.send_email)).scalar_one() == 0) with conversations_session(token1) as c: group_chat_id = c.CreateGroupChat( conversations_pb2.CreateGroupChatReq( recipient_user_ids=[user2.id, user3.id])).group_chat_id with conversations_session(token3) as c: # mute it for user 3 c.MuteGroupChat( conversations_pb2.MuteGroupChatReq(group_chat_id=group_chat_id, forever=True)) with conversations_session(token1) as c: c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 2")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 3")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 4")) with conversations_session(token3) as c: group_chat_id = c.CreateGroupChat( conversations_pb2.CreateGroupChatReq( recipient_user_ids=[user2.id])).group_chat_id c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 5")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 6")) process_send_message_notifications(empty_pb2.Empty()) # no emails sent out with session_scope() as session: assert (session.execute( select(func.count()).select_from(BackgroundJob).where( BackgroundJob.job_type == BackgroundJobType.send_email)).scalar_one() == 0) # this should generate emails for both user2 and NOT user3 with patch("couchers.jobs.handlers.now", now_5_min_in_future): process_send_message_notifications(empty_pb2.Empty()) with session_scope() as session: assert (session.execute( select(func.count()).select_from(BackgroundJob).where( BackgroundJob.job_type == BackgroundJobType.send_email)).scalar_one() == 1) # delete them all session.execute( delete(BackgroundJob).execution_options(synchronize_session=False)) # shouldn't generate any more emails with patch("couchers.jobs.handlers.now", now_5_min_in_future): process_send_message_notifications(empty_pb2.Empty()) with session_scope() as session: assert (session.execute( select(func.count()).select_from(BackgroundJob).where( BackgroundJob.job_type == BackgroundJobType.send_email)).scalar_one() == 0)
def test_process_send_message_notifications_basic(db): user1, token1 = generate_user() user2, token2 = generate_user() user3, token3 = generate_user() make_friends(user1, user2) make_friends(user1, user3) make_friends(user2, user3) process_send_message_notifications(empty_pb2.Empty()) # should find no jobs, since there's no messages with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 0 with conversations_session(token1) as c: group_chat_id = c.CreateGroupChat( conversations_pb2.CreateGroupChatReq( recipient_user_ids=[user2.id, user3.id])).group_chat_id c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 2")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 3")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 4")) with conversations_session(token3) as c: group_chat_id = c.CreateGroupChat( conversations_pb2.CreateGroupChatReq( recipient_user_ids=[user2.id])).group_chat_id c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 5")) c.SendMessage( conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 6")) process_send_message_notifications(empty_pb2.Empty()) # no emails sent out with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 0 def now_30_min_in_future(): return now() + timedelta(minutes=30) # this should generate emails for both user2 and user3 with patch("couchers.jobs.handlers.now", now_30_min_in_future): process_send_message_notifications(empty_pb2.Empty()) with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 2 # delete them all session.query(BackgroundJob).delete(synchronize_session=False) # shouldn't generate any more emails with patch("couchers.jobs.handlers.now", now_30_min_in_future): process_send_message_notifications(empty_pb2.Empty()) with session_scope() as session: assert session.query(BackgroundJob).filter( BackgroundJob.job_type == BackgroundJobType.send_email).count() == 0