def test_low_balance_notification(self): Notifications.delete_all_notifications() threshold = Threshold.objects.create( balance=5, actions=pickle.dumps(['send_notification']), up=False, down=True) number_recipients = 1 KeystoneClient.get_client().AndReturn( get_stub_keystone_client(number_recipients)) self.mox.ReplayAll() ut = UserTransactions() ut.receive_user_payment(1, 'STRIPE', 6, 'tester paid') ut.consume_user_money(1, 4, 'some consumption') Notifications.send_all_notifications() self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, LowBalanceNotificationSender.subject) self.assertEqual(mail.outbox[0].from_email, LowBalanceNotificationSender.from_email) self.assertEqual(len(mail.outbox[0].to), number_recipients) self.assertEqual(PassedThreshold.objects.count(), 1) self.assertEqual(PassedThreshold.objects.all()[0].threshold, threshold)
def grant_user_promotion(self, user, amount, message): account = self.account_manager.get_user_account(user) account.credit(amount, self.account_manager.get_promotions_account(), message) Notifications.get_notification_sender('promotion_granted').add( project_id=user, promotion_amount=amount, new_balance=account.balance(), message=message) Notifications.send_all_notifications()
def grant_user_promotion(self, user, amount, message): account = self.account_manager.get_user_account(user) account.credit( amount, self.account_manager.get_promotions_account(), message) Notifications.get_notification_sender('promotion_granted').add( project_id=user, promotion_amount=amount, new_balance=account.balance(), message=message) Notifications.send_all_notifications()
def test_notification_sender_and_instance_stopper_combination(self): Notifications.delete_all_notifications() Threshold.objects.create(balance=0, actions=pickle.dumps(['send_notification']), up=False, down=True) Threshold.objects.create(balance=-50, actions=pickle.dumps([ 'send_notification', 'stop_project_instances' ]), up=False, down=True) KeystoneClient.get_client().AndReturn(get_stub_keystone_client(3)) ServerManager.list(search_opts={ 'all_tenants': True, 'tenant_id': my_tenant_id, 'status': 'ACTIVE' }).AndReturn([ instance for instance in instance_list if instance.tenant_id == my_tenant_id ]) instance_list[0].stop() instance_list[2].stop() timezone.now().AndReturn(self.real_now) # called by user transaction timezone.now().AndReturn(self.real_now) # called by user transaction timezone.now().AndReturn(self.real_now) # called by user transaction timezone.now().AndReturn(self.real_now + timezone.timedelta(seconds=2 * 24 * 60 * 60)) timezone.now().AndReturn(self.real_now + timezone.timedelta(seconds=3 * 24 * 60 * 60)) timezone.now().AndReturn(self.real_now + timezone.timedelta(seconds=5 * 24 * 60 * 60)) timezone.now().AndReturn(self.real_now + timezone.timedelta(seconds=6 * 24 * 60 * 60)) self.mox.ReplayAll() ut = UserTransactions() ut.receive_user_payment(1, 'STRIPE', 6, 'user paid something') #import pdb #pdb.set_trace() ut.consume_user_money(1, 100, 'lot of consumption') Notifications.send_all_notifications() self.assertEqual(len(mail.outbox), 1) self.assertEqual(PassedThreshold.objects.count(), 2) ActionQueueProcessor.process() ActionQueueProcessor.process() self.assertEqual(len(instance_list[0].stop._expected_calls_queue), 1) self.assertEqual(len(instance_list[2].stop._expected_calls_queue), 1) ActionQueueProcessor.process() self.assertEqual(len(instance_list[0].stop._expected_calls_queue), 0) self.assertEqual(len(instance_list[2].stop._expected_calls_queue), 0) ActionQueueProcessor.process()
def test_notification_sender_and_instance_stopper_combination(self): Notifications.delete_all_notifications() Threshold.objects.create( balance=0, actions=pickle.dumps(['send_notification']), up=False, down=True) Threshold.objects.create( balance=-50, actions=pickle.dumps(['send_notification', 'stop_project_instances']), up=False, down=True) KeystoneClient.get_client().AndReturn( get_stub_keystone_client(3)) ServerManager.list( search_opts={'all_tenants': True, 'tenant_id': my_tenant_id, 'status': 'ACTIVE'}).AndReturn( [instance for instance in instance_list if instance.tenant_id == my_tenant_id]) instance_list[0].stop() instance_list[2].stop() timezone.now().AndReturn(self.real_now) # called by user transaction timezone.now().AndReturn(self.real_now) # called by user transaction timezone.now().AndReturn(self.real_now) # called by user transaction timezone.now().AndReturn( self.real_now + timezone.timedelta(seconds=2 * 24 * 60 * 60)) timezone.now().AndReturn( self.real_now + timezone.timedelta(seconds=3 * 24 * 60 * 60)) timezone.now().AndReturn( self.real_now + timezone.timedelta(seconds=5 * 24 * 60 * 60)) timezone.now().AndReturn( self.real_now + timezone.timedelta(seconds=6 * 24 * 60 * 60)) self.mox.ReplayAll() ut = UserTransactions() ut.receive_user_payment(1, 'STRIPE', 6, 'user paid something') #import pdb #pdb.set_trace() ut.consume_user_money(1, 100, 'lot of consumption') Notifications.send_all_notifications() self.assertEqual(len(mail.outbox), 1) self.assertEqual(PassedThreshold.objects.count(), 2) ActionQueueProcessor.process() ActionQueueProcessor.process() self.assertEqual(len(instance_list[0].stop._expected_calls_queue), 1) self.assertEqual(len(instance_list[2].stop._expected_calls_queue), 1) ActionQueueProcessor.process() self.assertEqual(len(instance_list[0].stop._expected_calls_queue), 0) self.assertEqual(len(instance_list[2].stop._expected_calls_queue), 0) ActionQueueProcessor.process()
def test_notification_sending(self): Notifications.delete_all_notifications() number_recipients = 5 KeystoneClient.get_client().AndReturn( get_stub_keystone_client(number_recipients)) self.mox.ReplayAll() Notifications.get_notification_sender('low_balance').add( project_id=1, passed_limit=5, current_balance=4) Notifications.send_all_notifications() self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, LowBalanceNotificationSender.subject) self.assertEqual(mail.outbox[0].from_email, settings.DEFAULT_FROM_EMAIL) self.assertEqual(len(mail.outbox[0].to), number_recipients)
def test_low_balance_notification(self): Notifications.delete_all_notifications() threshold = Threshold.objects.create(balance=5, actions=pickle.dumps( ['send_notification']), up=False, down=True) number_recipients = 1 KeystoneClient.get_client().AndReturn( get_stub_keystone_client(number_recipients)) self.mox.ReplayAll() ut = UserTransactions() ut.receive_user_payment(1, 'STRIPE', 6, 'tester paid') ut.consume_user_money(1, 4, 'some consumption') Notifications.send_all_notifications() self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, LowBalanceNotificationSender.subject) self.assertEqual(mail.outbox[0].from_email, LowBalanceNotificationSender.from_email) self.assertEqual(len(mail.outbox[0].to), number_recipients) self.assertEqual(PassedThreshold.objects.count(), 1) self.assertEqual(PassedThreshold.objects.all()[0].threshold, threshold)
def handle(self, *args, **kwargs): StatisticsIndexBuilder().build(date=kwargs['date']) UnfetchedStatisticsFetcher.fetch() AccountingTransactor.bill_projects(dry_run=(not kwargs['imsure'])) Notifications.send_all_notifications()