示例#1
0
    def test_generate_charges(self, instance_charges_mock, mock_consul):
        """
        Make sure that we're able to generate charges for all of the appservers
        that belong to any fork under a given organization.
        The test here will pass if all forks with their AppServers are included
        in the billing data and each fork is associated with all charges that
        belong to its instances.
        :param instance_charges_mock: A mock for get_instance_charges function.
        """
        invoice_month = self._generate_invoice_date(2017)
        instance_charges = 22
        charges_mock = {
            'name': 'Mock AppServer',
            'billing_start': invoice_month,
            'billing_end': invoice_month,
            'days': 10,
            'charge': 31,
        }
        instance_charges_mock.return_value = ([charges_mock], instance_charges)
        watched_forks = {
            'fork1': [OpenEdXInstanceFactory() for _ in range(2)],
            'fork2': [OpenEdXInstanceFactory() for _ in range(1)],
        }

        appservers_charges, appservers_total = generate_charges(
            watched_forks, invoice_month)
        self.assertEqual(appservers_total, 3 * instance_charges)
        self.assertEqual(len(appservers_charges), 2)
        self.assertEqual(len(appservers_charges['fork1']['instances']), 2)
        self.assertEqual(len(appservers_charges['fork2']['instances']), 1)
        self.assertIn(str(charges_mock),
                      str(appservers_charges['fork1']['instances']))
        self.assertIn(str(charges_mock),
                      str(appservers_charges['fork2']['instances']))
示例#2
0
def report(request, organization, year, month):
    """
    Report view
    """
    try:
        invoice_start_date = timezone.make_aware(
            datetime(int(year), int(month), 1))
    except ValueError:
        return HttpResponseBadRequest(
            content='<h1>Bad Request</h1>'
            '<p>Error when processing given date, consider using parameters within range</p>'
        )

    organization = get_object_or_404(Organization, github_handle=organization)
    forks_instances = generate_watched_forks_instances(organization)
    billing_data, total = generate_charges(forks_instances, invoice_start_date)

    return render(request,
                  'report.html',
                  context={
                      'year': year,
                      'month': month,
                      'month_name': calendar.month_name[int(month)],
                      'organization': organization,
                      'billing_data': billing_data,
                      'total': total,
                  })