Merge pull request #743 from alphagov/stringify-cost-attr-on-v2-get-notification

Stringify the cost before passing to jsonify as it complains otherwise
This commit is contained in:
Paul Craig
2016-11-23 10:50:59 +00:00
committed by GitHub
3 changed files with 26 additions and 6 deletions

View File

@@ -557,7 +557,7 @@ class Notification(db.Model):
desc(ProviderRates.valid_from)
).limit(1).one()
return provider_rate.rate * self.billable_units
return float(provider_rate.rate * self.billable_units)
def completed_at(self):
if self.status in [

View File

@@ -1,9 +1,9 @@
import pytest
from datetime import datetime
from sqlalchemy.orm.exc import NoResultFound
from tests.app.conftest import sample_notification, sample_provider_rate
from app.models import (
Notification,
ServiceWhitelist,
MOBILE_TYPE, EMAIL_TYPE)
@@ -39,7 +39,7 @@ def test_should_not_build_service_whitelist_from_invalid_contact(recipient_type,
@pytest.mark.parametrize('provider, billable_units, expected_cost', [
('mmg', 1, 3.5),
('firetext', 2, 5),
('firetext', 2, 0.025),
('ses', 0, 0)
])
def test_calculate_cost_from_notification_billable_units(
@@ -47,7 +47,7 @@ def test_calculate_cost_from_notification_billable_units(
):
provider_rates = [
('mmg', datetime(2016, 7, 1), 1.5),
('firetext', datetime(2016, 7, 1), 2.5),
('firetext', datetime(2016, 7, 1), 0.0125),
('mmg', datetime.utcnow(), 3.5),
]
for provider_identifier, valid_from, rate in provider_rates:
@@ -61,3 +61,11 @@ def test_calculate_cost_from_notification_billable_units(
notification = sample_notification(notify_db, notify_db_session, billable_units=billable_units, sent_by=provider)
assert notification.cost() == expected_cost
def test_billable_units_without_provider_rates_entry_raises_exception(
notify_db, notify_db_session, sample_provider_rate
):
notification = sample_notification(notify_db, notify_db_session, sent_by='not_a_provider')
with pytest.raises(NoResultFound):
notification.cost()

View File

@@ -1,12 +1,24 @@
import json
import pytest
from app import DATETIME_FORMAT
from tests import create_authorization_header
from tests.app.conftest import sample_notification as create_sample_notification
def test_get_notification_by_id_returns_200(client, sample_notification):
@pytest.mark.parametrize('billable_units, provider', [
(1, 'mmg'),
(0, 'mmg'),
(1, None)
])
def test_get_notification_by_id_returns_200(
client, notify_db, notify_db_session, sample_provider_rate, billable_units, provider
):
sample_notification = create_sample_notification(
notify_db, notify_db_session, billable_units=billable_units, sent_by=provider
)
auth_header = create_authorization_header(service_id=sample_notification.service_id)
response = client.get(
path='/v2/notifications/{}'.format(sample_notification.id),
headers=[('Content-Type', 'application/json'), auth_header])