def test_create_subscription_mf_units_log_with_invalid_date(self): subscription = SubscriptionFactory.create() metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={ 'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code }) response = self.client.patch(url, json.dumps({ "count": 150, "date": "2008-12-24", "update_type": "absolute" }), content_type='application/json') assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.data == {'detail': 'Date is out of bounds.'}
def test_create_subscription_mf_units_log_with_insufficient_data(self): subscription = SubscriptionFactory.create() metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={ 'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code }) data = {"count": 150, "date": "2008-12-24", "update_type": "absolute"} for field in data: data_copy = data.copy() data_copy.pop(field) response = self.client.patch(url, json.dumps(data_copy), content_type='application/json') assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.data == {field: ['This field is required.']}
def test_create_subscription_mf_units_log_with_sub_canceled_before(self): subscription = SubscriptionFactory.create( state=Subscription.STATES.CANCELED, start_date=datetime.date(2016, 1, 1), cancel_date=datetime.date(2016, 12, 31)) metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) url = reverse('mf-log-units', kwargs={ 'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code }) date = str(datetime.date.today()) response = self.client.patch(url, json.dumps({ "count": 150, "date": date, "update_type": "absolute" }), content_type='application/json') assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.data == {"detail": "Date is out of bounds."}
def test_get_metered_feature_list(self): MeteredFeatureFactory.create_batch(settings.API_PAGE_SIZE * 2) url = reverse('metered-feature-list') response = self.client.get(url) full_url = build_absolute_test_url(url) assert response.status_code == status.HTTP_200_OK assert response['link'] == \ ('<' + full_url + '?page=2>; rel="next", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"') response = self.client.get(url + '?page=2') assert response.status_code == status.HTTP_200_OK assert response['link'] == \ ('<' + full_url + '>; rel="prev", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"')
def test_create_subscription_mf_units_log_with_ended_sub(self): subscription = SubscriptionFactory.create(state=Subscription.STATES.ENDED) metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) url = reverse('mf-log-units', kwargs={'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code}) response = self.client.patch(url) assert response.status_code == status.HTTP_403_FORBIDDEN assert response.data == {'detail': 'Subscription is ended.'}
def test_create_subscription_mf_units_log_sub_canceled_at_end_of_month(self): subscription = SubscriptionFactory.create(state=Subscription.STATES.CANCELED, start_date=datetime.date(2022, 5, 2), cancel_date=datetime.date(2022, 5, 31)) metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code}) date = str(datetime.date.today()) response = self.client.patch(url, json.dumps({ "consumed_units": '150.0000', "date": date, "update_type": "absolute" }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '150.0000', 'annotation': None, 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A successive request response = self.client.patch(url, json.dumps({ "consumed_units": 29, "date": date, "update_type": "relative" }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '179.0000', 'annotation': None, 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', }
def test_create_subscription_mf_units_log_sub_canceled_at_end_of_month( self): subscription = SubscriptionFactory.create( state=Subscription.STATES.CANCELED, start_date=datetime.date(2016, 1, 1), cancel_date=datetime.date(2017, 1, 31)) metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={ 'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code }) date = str(datetime.date.today()) response = self.client.patch(url, json.dumps({ "count": 150, "date": date, "update_type": "absolute" }), content_type='application/json') assert response.status_code == status.HTTP_200_OK assert response.data == {'count': 150} response = self.client.patch(url, json.dumps({ "count": 29, "date": date, "update_type": "relative" }), content_type='application/json') assert response.status_code == status.HTTP_200_OK assert response.data == {'count': 179}
def test_create_subscription_mf_units_log_with_insufficient_data(self): subscription = SubscriptionFactory.create() metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code}) data = {} response = self.client.patch(url, json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.data == { "consumed_units": ['This field is required.'], 'date': ['This field is required.'], 'update_type': ['This field is required.']}
def test_create_subscription_mf_units_log_with_end_log(self): subscription = SubscriptionFactory.create( start_date=datetime.date(2022, 5, 2), ) metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code}) date = str(datetime.date.today()) response = self.client.patch(url, json.dumps({ "consumed_units": '150.0000', "date": date, "update_type": "absolute", }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '150.0000', 'annotation': None, 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A second relative patch request with end bucket response = self.client.patch(url, json.dumps({ "consumed_units": 29, "date": date, "update_type": "relative", "end_log": True, }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '179.0000', 'annotation': None, 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-15T00:00:00Z', } # A third patch request matching a new bucket in the same month response = self.client.patch(url, json.dumps({ "consumed_units": 50, "date": str(datetime.date.today() + datetime.timedelta(days=1)), "update_type": "absolute", "end_log": True, }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '50.0000', 'annotation': None, 'start_datetime': '2022-05-15T00:00:01Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A fourth GET request for all buckets response = self.client.get(url) assert response.status_code == status.HTTP_200_OK, response.data assert response.data == [ OrderedDict( [('consumed_units', '179.0000'), ('start_datetime', '2022-05-02T00:00:00Z'), ('end_datetime', '2022-05-15T00:00:00Z'), ('annotation', None)] ), OrderedDict( [('consumed_units', '50.0000'), ('start_datetime', '2022-05-15T00:00:01Z'), ('end_datetime', '2022-05-31T23:59:59Z'), ('annotation', None)] ), ]
def test_create_subscription_mf_units_log_with_annotation(self): subscription = SubscriptionFactory.create() metered_feature = MeteredFeatureFactory.create() subscription.plan.metered_features.add(metered_feature) subscription.activate() subscription.save() url = reverse('mf-log-units', kwargs={'subscription_pk': subscription.pk, 'customer_pk': subscription.customer.pk, 'mf_product_code': metered_feature.product_code}) date = str(datetime.date.today()) response = self.client.patch(url, json.dumps({ "consumed_units": '150.0000', "date": date, "update_type": "absolute", "annotation": "test", }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '150.0000', 'annotation': "test", 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A successive relative patch request response = self.client.patch(url, json.dumps({ "consumed_units": 29, "date": date, "update_type": "relative", "annotation": "test", }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '179.0000', 'annotation': "test", 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A third patch request on a different annotation response = self.client.patch(url, json.dumps({ "consumed_units": 42, "date": date, "update_type": "relative", "annotation": "different", }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '42.0000', 'annotation': "different", 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A fourth patch request with no annotation response = self.client.patch(url, json.dumps({ "consumed_units": 99, "date": date, "update_type": "absolute", }), content_type='application/json') assert response.status_code == status.HTTP_200_OK, response.data assert response.data == { "consumed_units": '99.0000', 'annotation': None, 'start_datetime': '2022-05-02T00:00:00Z', 'end_datetime': '2022-05-31T23:59:59Z', } # A fifth GET request for all buckets response = self.client.get(url) assert response.status_code == status.HTTP_200_OK, response.data assert response.data == [ OrderedDict([ ('consumed_units', '99.0000'), ('start_datetime', '2022-05-02T00:00:00Z'), ('end_datetime', '2022-05-31T23:59:59Z'), ('annotation', None) ]), OrderedDict([ ('consumed_units', '42.0000'), ('start_datetime', '2022-05-02T00:00:00Z'), ('end_datetime', '2022-05-31T23:59:59Z'), ('annotation', 'different') ]), OrderedDict([ ('consumed_units', '179.0000'), ('start_datetime', '2022-05-02T00:00:00Z'), ('end_datetime', '2022-05-31T23:59:59Z'), ('annotation', 'test') ]), ]