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_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