def test_notify_users_failed_notification(card_manager, pinpoint_client, user, TestCardTemplate): # add card with a notification in the immediate past now = pendulum.now('utc') card = card_manager.add_or_update_card( TestCardTemplate(user.id, title='t', action='a', notify_user_after=pendulum.duration()), now=now, ) assert card.notify_user_at == now # configure our mock to report a failed message send pinpoint_client.configure_mock(**{'send_user_apns.return_value': False}) # run notificiations, verify attempted send and correct DB changes upon failure cnts = card_manager.notify_users() assert cnts == (1, 0) assert pinpoint_client.mock_calls == [ call.send_user_apns(user.id, 'a', 't', body=None) ] org_item = card.item card.refresh_item() assert 'gsiK1PartitionKey' not in card.item assert 'gsiK1SortKey' not in card.item assert org_item.pop('gsiK1PartitionKey') assert org_item.pop('gsiK1SortKey') assert card.item == org_item
def test_notify_user(user, card): mocked_resp = {} card.pinpoint_client.configure_mock(**{'send_user_apns.return_value': mocked_resp}) assert card.pinpoint_client.mock_calls == [] resp = card.notify_user() assert resp is mocked_resp assert card.pinpoint_client.mock_calls == [ call.send_user_apns(user.id, 'https://action', 'card title', body=None) ]
def test_notify_users_only_usernames(card_manager, pinpoint_client, user, user2, user3, TestCardTemplate): # configure mock to claim all apns-sending attempts succeeded pinpoint_client.configure_mock(**{'send_user_apns.return_value': True}) # add one notification for each user in immediate past, verify they're there card1 = card_manager.add_or_update_card( TestCardTemplate(user.id, title='t1', action='a1', notify_user_after=pendulum.duration(seconds=-2)), ) card2 = card_manager.add_or_update_card( TestCardTemplate(user2.id, title='t2', action='a2', notify_user_after=pendulum.duration(seconds=-1)), ) card3 = card_manager.add_or_update_card( TestCardTemplate(user3.id, title='t3', action='a3', notify_user_after=pendulum.duration()), ) assert card1.refresh_item().notify_user_at assert card2.refresh_item().notify_user_at assert card3.refresh_item().notify_user_at # run notificiations for just two of the users, verify just those two sent pinpoint_client.reset_mock() cnts = card_manager.notify_users( only_usernames=[user.username, user3.username]) assert cnts == (2, 2) assert pinpoint_client.mock_calls == [ call.send_user_apns(user.id, 'a1', 't1', body=None), call.send_user_apns(user3.id, 'a3', 't3', body=None), ] assert card1.refresh_item().notify_user_at is None assert card2.refresh_item().notify_user_at assert card3.refresh_item().notify_user_at is None # re-add those cards for which we just sent notificaitons card_manager.dynamo.delete_card(card1.id) card_manager.dynamo.delete_card(card3.id) card1 = card_manager.add_or_update_card( TestCardTemplate(user.id, title='t1', action='a1', notify_user_after=pendulum.duration(seconds=-2))) card3 = card_manager.add_or_update_card( TestCardTemplate(user3.id, title='t3', action='a3', notify_user_after=pendulum.duration())) assert card1.refresh_item().notify_user_at assert card3.refresh_item().notify_user_at # run notificiations for just one of the user, verify just that one sent pinpoint_client.reset_mock() cnts = card_manager.notify_users(only_usernames=[user2.username]) assert cnts == (1, 1) assert pinpoint_client.mock_calls == [ call.send_user_apns(user2.id, 'a2', 't2', body=None), ] assert card1.refresh_item().notify_user_at assert card2.refresh_item().notify_user_at is None assert card3.refresh_item().notify_user_at # re-add a cards for which we just sent notificaitons card_manager.dynamo.delete_card(card2.id) card2 = card_manager.add_or_update_card( TestCardTemplate(user2.id, title='t2', action='a2', notify_user_after=pendulum.duration(seconds=-1))) assert card2.refresh_item().notify_user_at # run notificiations for no users, verify none sent pinpoint_client.reset_mock() cnts = card_manager.notify_users(only_usernames=[]) assert cnts == (0, 0) assert pinpoint_client.mock_calls == [] assert card1.refresh_item().notify_user_at assert card2.refresh_item().notify_user_at assert card3.refresh_item().notify_user_at # run notificiations for all users, verify all sent pinpoint_client.reset_mock() cnts = card_manager.notify_users() assert cnts == (3, 3) assert pinpoint_client.mock_calls == [ call.send_user_apns(user.id, 'a1', 't1', body=None), call.send_user_apns(user2.id, 'a2', 't2', body=None), call.send_user_apns(user3.id, 'a3', 't3', body=None), ] assert card1.refresh_item().notify_user_at is None assert card2.refresh_item().notify_user_at is None assert card3.refresh_item().notify_user_at is None
def test_notify_users(card_manager, pinpoint_client, user, user2, TestCardTemplate): # configure mock to claim all apns-sending attempts succeeded pinpoint_client.configure_mock(**{'send_user_apns.return_value': True}) now = pendulum.now('utc') # add a card with a notification in the far future card1 = card_manager.add_or_update_card( TestCardTemplate(user.id, title='t1', action='a1', notify_user_after=pendulum.duration(hours=1)), now=now, ) assert card1.notify_user_at == now + pendulum.duration(hours=1) # run notificiations, verify none sent and no db changes cnts = card_manager.notify_users() assert cnts == (0, 0) assert pinpoint_client.mock_calls == [] assert card1.item == card1.refresh_item().item # add another card with a notification in the immediate future card2 = card_manager.add_or_update_card( TestCardTemplate(user.id, title='t2', action='a2', notify_user_after=pendulum.duration(seconds=2)), now=now, ) assert card2.notify_user_at == now + pendulum.duration(seconds=2) # run notificiations, verify none sent and no db changes cnts = card_manager.notify_users() assert cnts == (0, 0) assert pinpoint_client.mock_calls == [] assert card1.item == card1.refresh_item().item assert card2.item == card2.refresh_item().item # add another card with a notification in the immediate past card3 = card_manager.add_or_update_card(TestCardTemplate( user.id, title='t3', action='a3', notify_user_after=pendulum.duration()), now=now) assert card3.notify_user_at == now # run notificiations, verify one sent cnts = card_manager.notify_users() assert cnts == (1, 1) assert pinpoint_client.mock_calls == [ call.send_user_apns(user.id, 'a3', 't3', body=None), ] assert card1.item == card1.refresh_item().item assert card2.item == card2.refresh_item().item assert card3.refresh_item().notify_user_at is None # two cards with a notification in past card4 = card_manager.add_or_update_card( TestCardTemplate(user2.id, title='t4', action='a4', notify_user_after=pendulum.duration(seconds=-1)), now=now, ) card5 = card_manager.add_or_update_card( TestCardTemplate(user.id, title='t5', action='a5', notify_user_after=pendulum.duration(hours=-1), sub_title='s'), now=now, ) assert card4.notify_user_at == now + pendulum.duration(seconds=-1) assert card5.notify_user_at == now + pendulum.duration(hours=-1) # run notificiations, verify both sent pinpoint_client.reset_mock() cnts = card_manager.notify_users() assert cnts == (2, 2) assert pinpoint_client.mock_calls == [ call.send_user_apns(user.id, 'a5', 't5', body='s'), call.send_user_apns(user2.id, 'a4', 't4', body=None), ] assert card1.item == card1.refresh_item().item assert card2.item == card2.refresh_item().item assert card4.refresh_item().notify_user_at is None assert card5.refresh_item().notify_user_at is None