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