def test_only_keep_last_threshold_upwards_and_flags(self): Threshold.objects.create( balance=2, actions=pickle.dumps(['send_notification']), up=True, down=False) Threshold.objects.create( balance=3, actions=pickle.dumps(['send_notification']), up=True, down=False) Threshold.objects.create( balance=1, actions=pickle.dumps(['send_notification']), up=True, down=False) Threshold.objects.create( balance=4, actions=pickle.dumps(['send_notification']), up=False, down=True) KeystoneClient.get_client().AndReturn( get_stub_keystone_client(3)) ThresholdActionBase.pass_event( passed_limit=decimal.Decimal(3), project_id=1, current_balance=decimal.Decimal(5)) self.mox.ReplayAll() ut = UserTransactions() ut.grant_user_promotion(1, 5, 'granting promotion')
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 test_grant_user_promotion(self): Notifications.delete_all_notifications() KeystoneClient.get_client().AndReturn( get_stub_keystone_client(1)) self.mox.ReplayAll() self.user_transactions.grant_user_promotion( self.user_id, 100, 'free money!') self.assertEqual(len(mail.outbox), 1) self.assertEqual(self.promotions_account.balance(), 100) self.assertEqual(self.user_account.balance(), 100) self.assertEqual(mail.outbox[0].subject, PromotionGrantedNotificationSender.subject)
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_promotion_message(self): Notifications.delete_all_notifications() KeystoneClient.get_client().AndReturn( get_stub_keystone_client(1)) self.mox.ReplayAll() self.user_transactions.grant_user_promotion( self.user_id, 100, "free money!") self.assertEqual( self.transaction_history.get_user_account_transaction_history( self.user_id)[0]["description"], "free money!") self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, PromotionGrantedNotificationSender.subject)
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 test_only_keep_last_threshold_upwards_and_flags(self): Threshold.objects.create(balance=2, actions=pickle.dumps(['send_notification']), up=True, down=False) Threshold.objects.create(balance=3, actions=pickle.dumps(['send_notification']), up=True, down=False) Threshold.objects.create(balance=1, actions=pickle.dumps(['send_notification']), up=True, down=False) Threshold.objects.create(balance=4, actions=pickle.dumps(['send_notification']), up=False, down=True) KeystoneClient.get_client().AndReturn(get_stub_keystone_client(3)) ThresholdActionBase.pass_event(passed_limit=decimal.Decimal(3), project_id=1, current_balance=decimal.Decimal(5)) self.mox.ReplayAll() ut = UserTransactions() ut.grant_user_promotion(1, 5, 'granting promotion')
def _resolve_project_name(cls, id): try: return KeystoneClient.get_client().tenants.get( tenant_id=id).name except Exception: return ''
def _lookup_email_address(self, project_id): return [x.email for x in KeystoneClient.get_client().tenants.get( project_id).list_users()]
def set_password(user, password): KeystoneClient.get_client().users.update_password( User.objects.get_by_natural_key(user).keystone_id, password)
def email_confirmed(address): KeystoneClient.get_client().users.update( User.objects.get_by_natural_key(address).keystone_id, enabled=True)
def _lookup_email_address(self, project_id): return [ x.email for x in KeystoneClient.get_client().tenants.get( project_id).list_users() ]
def __init__(self): self.client = KeystoneClient.get_client()
def _list_ks_project_ids(self): if not self.project_ids: self.project_ids = [ x.id for x in KeystoneClient.get_client().tenants.list()] return self.project_ids
def _resolve_project_name(cls, id): try: return KeystoneClient.get_client().tenants.get(tenant_id=id).name except Exception: return ''