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 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 bill_projects(cls, dry_run=False): ut = UserTransactions() for stat in cls._get_unbilled_statistics(): for data in cls._get_raw_data(stat.id): for priced_usage in PricedUsageBase.get_meter_class( stat.meter).get_priced_stats( data, (stat.from_ts, stat.until_ts)): if not dry_run: ut.consume_user_money( stat.project_id, priced_usage['price'], cls._get_transaction_message(priced_usage)) stat.billed = True stat.save() cls._print_info(stat.project_id, priced_usage, dry_run)
def test_action_queue_delay(self): Threshold.objects.create(balance=-50, actions=pickle.dumps( ['stop_project_instances']), up=False, down=True) 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 + 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() UserTransactions().consume_user_money(1, 60, 'some consumption') ActionQueueProcessor.process() ActionQueueProcessor.process() ActionQueueProcessor.process() ActionQueueProcessor.process()
def bill_projects(cls, dry_run=False): ut = UserTransactions() for stat in cls._get_unbilled_statistics(): for data in cls._get_raw_data(stat.id): for priced_usage in PricedUsageBase.get_meter_class( stat.meter).get_priced_stats( data, (stat.from_ts, stat.until_ts)): if not dry_run: ut.consume_user_money( stat.project_id, priced_usage['price'], cls._get_transaction_message( priced_usage)) stat.billed = True stat.save() cls._print_info(stat.project_id, priced_usage, dry_run)
def test_only_keep_last_threshold_downwards(self): Threshold.objects.create(balance=-2, actions=pickle.dumps(['send_notification']), up=False, down=True) Threshold.objects.create(balance=-3, actions=pickle.dumps(['send_notification']), up=False, down=True) Threshold.objects.create(balance=-1, actions=pickle.dumps(['send_notification']), up=False, down=True) ThresholdActionBase.pass_event(passed_limit=decimal.Decimal(-3), project_id=1, current_balance=decimal.Decimal('-5')) self.mox.ReplayAll() ut = UserTransactions() ut.consume_user_money(1, 5, 'some consumption')
def test_only_keep_last_threshold_downwards(self): Threshold.objects.create( balance=-2, actions=pickle.dumps(['send_notification']), up=False, down=True) Threshold.objects.create( balance=-3, actions=pickle.dumps(['send_notification']), up=False, down=True) Threshold.objects.create( balance=-1, actions=pickle.dumps(['send_notification']), up=False, down=True) ThresholdActionBase.pass_event( passed_limit=decimal.Decimal(-3), project_id=1, current_balance=decimal.Decimal('-5')) self.mox.ReplayAll() ut = UserTransactions() ut.consume_user_money(1, 5, 'some consumption')
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_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_instance_stopper(self): Threshold.objects.create(balance=-50, actions=pickle.dumps( ['stop_project_instances']), up=False, down=True) 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() StopProjectInstancesThresholdAction._get_due_datetime().AndReturn( timezone.now()) self.mox.ReplayAll() UserTransactions().consume_user_money(1, 60, 'some consumption') ActionQueueProcessor.process()
def handle(self, request, data): try: if not self.is_valid(): raise django_forms.ValidationError( 'form aint valid') # fix this prepaid_reservation = PrePaidReservation.objects.get( pk=data['prepaid_reservation_id']) if prepaid_reservation.upfront_price > balance(request)['balance']: raise django_forms.ValidationError( ('You have insufficient Balance' ' to purchase this reservation')) # redirect maybe? UserTransactions().consume_user_money( request.user.tenant_id, prepaid_reservation.upfront_price, u'Purchased {} reservation for {} days'.format( api.nova.flavor_get(request, prepaid_reservation.instance_type), prepaid_reservation.length)) assigned_reservation = AssignedReservation.objects.create( tenant_id=request.user.tenant_id, start=datetime.now(), end=datetime.now() + timedelta(days=prepaid_reservation.length), prepaid_reservation=prepaid_reservation) assigned_reservation.save() return True except ObjectDoesNotExist: self.api_error(u'Invalid Reservation') except IntegrityError as e: self.api_error(e.message) except django_forms.ValidationError as e: self.api_error(e.messages[0]) except Exception: exceptions.handle(request, ignore=True)
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)