def test_old_invoices_are_marked_as_created(self): # previous year with freeze_time('2016-11-01'): invoice1 = factories.InvoiceFactory() # previous month with freeze_time('2017-01-15'): invoice2 = factories.InvoiceFactory() with freeze_time('2017-02-4'): tasks.create_monthly_invoices() invoice1.refresh_from_db() self.assertEqual( invoice1.state, models.Invoice.States.CREATED, 'Invoice for previous year is not marked as CREATED', ) invoice2.refresh_from_db() self.assertEqual( invoice2.state, models.Invoice.States.CREATED, 'Invoice for previous month is not marked as CREATED', )
def test_if_customer_has_a_fixed_price_payment_profile_then_invoice_are_created_as_paid( self, ): with freeze_time('2020-01-01'): invoice = self.create_invoice() with freeze_time('2020-02-01'): tasks.create_monthly_invoices() invoice.refresh_from_db() self.assertEqual(invoice.state, models.Invoice.States.PAID)
def test_create_monthly_invoices(self): self.resource.set_state_ok() self.resource.save() create_monthly_invoices() invoice = invoices_models.Invoice.objects.get( customer=self.resource.project.customer, year=2020, month=12) self.assertEqual( invoice.items.filter(resource_id=self.resource.id, ).count(), 1, )
def test_invoice_item_is_not_created_for_pending_resource_in_new_month( self): with freeze_time('2017-01-15'): fixture = fixtures.InvoiceFixture() fixture.invoice_item with freeze_time('2017-02-01'): tasks.create_monthly_invoices() self.assertEqual(models.InvoiceItem.objects.count(), 1)
def test_invoice_item_is_created_for_created_resource_in_new_month(self): with freeze_time('2017-01-15'): fixture = fixtures.InvoiceFixture() fixture.resource.set_state_ok() fixture.resource.save() with freeze_time('2017-02-01'): tasks.create_monthly_invoices() self.assertEqual(models.InvoiceItem.objects.count(), 2)
def test_recurring_usage(self): self.fixture.offering_component_ram.delete() with freeze_time('2018-01-15'): self._create_usage(usage=10, recurring=True) with freeze_time('2018-02-01'): create_monthly_invoices() invoice = invoices_models.Invoice.objects.get( customer=self.fixture.customer, month=2, year=2018 ) self.assertEqual(marketplace_models.ComponentUsage.objects.count(), 2) self.assertEqual(invoice.price, self.fixture.plan_component_cpu.price * 10)
def test_invoice_created_if_trial_period_disabled_or_ended(self, args): skip_trial, accounting_started, invoice_exists = args if accounting_started: accounting_start_date = timezone.now() - timedelta(days=30) else: accounting_start_date = timezone.now() + timedelta(days=30) with override_waldur_core_settings( ENABLE_ACCOUNTING_START_DATE=skip_trial): customer = structure_factories.CustomerFactory() customer.accounting_start_date = accounting_start_date customer.save() tasks.create_monthly_invoices() self.assertEqual( invoice_exists, models.Invoice.objects.filter(customer=customer).exists(), )
def setUp(self): super(CostsStatsTest, self).setUp() self.url = factories.OfferingFactory.get_url(self.offering, action='costs') self.plan = factories.PlanFactory( offering=self.offering, unit=UnitPriceMixin.Units.PER_DAY, ) self.plan_component = factories.PlanComponentFactory( plan=self.plan, component=self.offering_component, amount=10) self.resource = factories.ResourceFactory( offering=self.offering, state=models.Resource.States.OK, plan=self.plan, limits={'cpu': 1}, ) invoices_tasks.create_monthly_invoices()
def test_offering_costs_stats(self): with freeze_time('2020-01-01'): url = factories.OfferingFactory.get_url(self.offering, action='costs') plan = factories.PlanFactory( offering=self.offering, unit=UnitPriceMixin.Units.PER_DAY, ) plan_component = factories.PlanComponentFactory( plan=plan, component=self.offering_component, amount=10) factories.ResourceFactory( offering=self.offering, state=models.Resource.States.OK, plan=plan, limits={'cpu': 1}, ) invoices_tasks.create_monthly_invoices() with freeze_time('2020-03-01'): self.client.force_authenticate(self.fixture.staff) result = self.client.get(url, { 'start': '2020-01', 'end': '2020-02' }) self.assertEqual(result.status_code, status.HTTP_200_OK) self.assertEqual(len(result.data), 2) self.assertEqual( result.data[0], { 'tax': 0, 'total': plan_component.price * 31, 'price': plan_component.price * 31, 'price_current': plan_component.price * 31, 'period': '2020-01', }, )
def test_create_monthly_invoices(self): create_monthly_invoices() invoice = invoices_models.Invoice.objects.get( customer=self.resource.project.customer, year=2020, month=12) self.assertEqual(1, invoice.items.filter(resource=self.resource).count())
def test_when_monthly_invoice_is_created_for_provisioned_resource_invoice_item_is_not_created( self, ): with freeze_time('2020-12-01'): create_monthly_invoices() items = self.get_invoice_items(year=2020, month=12) self.assertEqual(items.count(), 0)
def test_invoice_stats(self): tasks.create_monthly_invoices() invoice = models.Invoice.objects.get(customer=self.customer, year=2019, month=1) url = factories.InvoiceFactory.get_url(invoice=invoice, action='stats') self.client.force_authenticate( structure_factories.UserFactory(is_staff=True)) result = self.client.get(url) self.assertEqual(len(result.data), 3) self.assertEqual( {d['uuid'] for d in result.data}, { self.offering.uuid.hex, self.marketplace_support_offering.uuid.hex, self.offering_2.uuid.hex, }, ) self.assertEqual( list( filter(lambda x: x['uuid'] == self.offering.uuid.hex, result.data))[0], { 'uuid': self.offering.uuid.hex, 'offering_name': self.offering.name, 'aggregated_cost': sum([ item.total for item in models.InvoiceItem.objects.filter( invoice=invoice, resource_id__in=[ self.resource_1.id, self.resource_2.id ], ) ]), 'service_category_title': self.offering.category.title, 'service_provider_name': self.offering.customer.name, 'service_provider_uuid': self.provider.uuid.hex, }, ) self.assertEqual( list( filter(lambda x: x['uuid'] == self.offering_2.uuid.hex, result.data))[0], { 'uuid': self.offering_2.uuid.hex, 'offering_name': self.offering_2.name, 'aggregated_cost': sum([ item.total for item in models.InvoiceItem.objects.filter( invoice=invoice, resource_id__in=[self.resource_3.id], ) ]), 'service_category_title': self.offering_2.category.title, 'service_provider_name': self.offering_2.customer.name, 'service_provider_uuid': self.provider_2.uuid.hex, }, ) self.assertEqual( list( filter( lambda x: x['uuid'] == self.marketplace_support_offering. uuid.hex, result.data, ))[0], { 'uuid': self.marketplace_support_offering.uuid.hex, 'offering_name': self.marketplace_support_offering.name, 'aggregated_cost': models.InvoiceItem.objects.get( invoice=invoice, resource_id=self.resource_4.id, ).price, 'service_category_title': self.marketplace_support_offering.category.title, 'service_provider_name': self.offering.customer.name, 'service_provider_uuid': self.provider.uuid.hex, }, )
def create_invoice(self): tasks.create_monthly_invoices() invoice = models.Invoice.objects.get( year='2020', month='01', customer=self.profile.organization) self.assertEqual(invoice.state, models.Invoice.States.PENDING) return invoice
def _create_item(self): invoices_tasks.create_monthly_invoices() invoice = invoices_models.Invoice.objects.get( year=2020, month=3, customer=self.resource.project.customer) return invoice.items.get(object_id=self.resource.id)
def _create_usage(self, mock_executors): order = marketplace_factories.OrderFactory( project=self.fixture.project, created_by=self.fixture.owner) order_item = marketplace_factories.OrderItemFactory( order=order, offering=self.offering, attributes={ 'name': 'name', 'tenant_settings': openstack_tenant_factories. OpenStackTenantServiceSettingsFactory.get_url( self.fixture.openstack_tenant_service_settings), 'nodes': [{ 'subnet': openstack_tenant_factories.SubNetFactory.get_url( self.fixture.subnet), 'system_volume_size': 1024, 'memory': 1, 'cpu': 1, 'roles': ['controlplane', 'etcd', 'worker'], }], }, ) serialized_order = core_utils.serialize_instance(order_item.order) serialized_user = core_utils.serialize_instance(self.fixture.staff) marketplace_tasks.process_order(serialized_order, serialized_user) self.assertTrue( marketplace_models.Resource.objects.filter(name='name').exists()) self.assertTrue( rancher_models.Cluster.objects.filter(name='name').exists()) self.cluster = rancher_models.Cluster.objects.get(name='name') self.cluster.backend_id = 'cluster_backend_id' self.cluster.save() create_node_task = tasks.CreateNodeTask() create_node_task.execute( mock_executors.ClusterCreateExecutor.execute.mock_calls[0][1] [0].node_set.first(), user_id=mock_executors.ClusterCreateExecutor.execute.mock_calls[0] [2]['user'].id, ) self.assertTrue( self.cluster.node_set.filter(cluster=self.cluster).exists()) today = datetime.date.today() self.resource = marketplace_models.Resource.objects.get( scope=self.cluster) self.plan_period = marketplace_models.ResourcePlanPeriod.objects.create( start=today, end=None, resource=self.resource, plan=self.plan, ) invoices_tasks.create_monthly_invoices() tasks.pull_cluster_nodes(self.cluster.id) utils.update_cluster_nodes_states(self.cluster.id)