From 3e8015ed50d853fcf1391897817936a18d5389dd Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 16 Jan 2025 08:38:51 -0800 Subject: [PATCH 1/3] get rid of some noise in status updates --- .ds.baseline | 6 +++--- app/notify_client/billing_api_client.py | 12 ++++++++++++ app/notify_client/service_api_client.py | 14 +++++++++++++- tests/app/main/views/test_index.py | 4 +--- tests/app/main/views/test_register.py | 5 ++--- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/.ds.baseline b/.ds.baseline index df4be33a60..0ded707f2e 100644 --- a/.ds.baseline +++ b/.ds.baseline @@ -555,7 +555,7 @@ "filename": "tests/app/main/views/test_register.py", "hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8", "is_verified": false, - "line_number": 200, + "line_number": 199, "is_secret": false }, { @@ -563,7 +563,7 @@ "filename": "tests/app/main/views/test_register.py", "hashed_secret": "bb5b7caa27d005d38039e3797c3ddb9bcd22c3c8", "is_verified": false, - "line_number": 273, + "line_number": 272, "is_secret": false } ], @@ -684,5 +684,5 @@ } ] }, - "generated_at": "2025-01-13T20:16:58Z" + "generated_at": "2025-01-16T16:38:48Z" } diff --git a/app/notify_client/billing_api_client.py b/app/notify_client/billing_api_client.py index a6226f14ae..b1ffc19f08 100644 --- a/app/notify_client/billing_api_client.py +++ b/app/notify_client/billing_api_client.py @@ -1,3 +1,6 @@ +import json + +from app.extensions import redis_client from app.notify_client import NotifyAdminAPIClient @@ -15,10 +18,19 @@ def get_annual_usage_for_service(self, service_id, year=None): ) def get_free_sms_fragment_limit_for_year(self, service_id, year=None): + frag_limit = redis_client.get(f"free-sms-fragment-limit-{service_id}-{year}") + if frag_limit is not None: + return json.loads(frag_limit.decode("utf-8")) result = self.get( "/service/{0}/billing/free-sms-fragment-limit".format(service_id), params=dict(financial_year_start=year), ) + + redis_client.set( + f"free-sms-fragment-limit-{service_id}-{year}", + json.dumps(result["free_sms_fragment_limit"]), + ex=30, + ) return result["free_sms_fragment_limit"] def create_or_update_free_sms_fragment_limit( diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 3a0655d9e9..0229fee3dd 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -1,3 +1,4 @@ +import json from datetime import datetime, timezone from app.extensions import redis_client @@ -517,7 +518,18 @@ def get_notification_count(self, service_id): return int(count) def get_global_notification_count(self, service_id): - return self.get("/service/{}/notification-count".format(service_id)) + notification_count = redis_client.get(f"notification-count-{service_id}") + if notification_count is not None: + return json.loads(notification_count.decode("utf-8")) + + notification_count = self.get( + "/service/{}/notification-count".format(service_id) + ) + redis_client.set( + f"notification-count-{service_id}", json.dumps(notification_count), ex=30 + ) + + return notification_count def get_service_invite_data(self, redis_key): """ diff --git a/tests/app/main/views/test_index.py b/tests/app/main/views/test_index.py index db166d21ed..eaa9134250 100644 --- a/tests/app/main/views/test_index.py +++ b/tests/app/main/views/test_index.py @@ -122,9 +122,7 @@ def test_static_pages(client_request, mock_get_organization_by_domain, view, moc session["user_id"] = None request( _expected_status=302, - _expected_redirect="/sign-in?next={}".format( - url_for("main.{}".format(view)) - ), + _expected_redirect="/sign-in?next={}".format(url_for("main.{}".format(view))), ) diff --git a/tests/app/main/views/test_register.py b/tests/app/main/views/test_register.py index 952aa82114..a55307a2bd 100644 --- a/tests/app/main/views/test_register.py +++ b/tests/app/main/views/test_register.py @@ -144,9 +144,8 @@ def test_should_return_200_when_email_is_not_gov_uk( _expected_status=200, ) - assert ( - "Enter a public sector email address." - in normalize_spaces(page.select_one(".usa-error-message").text) + assert "Enter a public sector email address." in normalize_spaces( + page.select_one(".usa-error-message").text ) From 6f7202eb30820226cb6ac1cd5ce59c07e49e5b0d Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 16 Jan 2025 09:02:48 -0800 Subject: [PATCH 2/3] fix test --- app/main/views/index.py | 8 +------- tests/app/notify_client/test_billing_client.py | 8 ++++++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/main/views/index.py b/app/main/views/index.py index e05e72e870..8b63d2bd83 100644 --- a/app/main/views/index.py +++ b/app/main/views/index.py @@ -32,11 +32,7 @@ def check_feature_flags(): @main.route("/test/feature-flags") def test_feature_flags(): return jsonify( - { - "FEATURE_ABOUT_PAGE_ENABLED": current_app.config[ - "FEATURE_ABOUT_PAGE_ENABLED" - ] - } + {"FEATURE_ABOUT_PAGE_ENABLED": current_app.config["FEATURE_ABOUT_PAGE_ENABLED"]} ) @@ -235,7 +231,6 @@ def contact(): return render_template( "views/contact.html", navigation_links=about_notify_nav(), - ) @@ -268,7 +263,6 @@ def join_notify(): return render_template( "views/join-notify.html", navigation_links=about_notify_nav(), - ) diff --git a/tests/app/notify_client/test_billing_client.py b/tests/app/notify_client/test_billing_client.py index 9bb113efac..65898fec25 100644 --- a/tests/app/notify_client/test_billing_client.py +++ b/tests/app/notify_client/test_billing_client.py @@ -11,6 +11,9 @@ def test_get_free_sms_fragment_limit_for_year_correct_endpoint(mocker, api_user_ client = BillingAPIClient() mock_get = mocker.patch("app.notify_client.billing_api_client.BillingAPIClient.get") + mocker.patch( + "app.notify_client.billing_api_client.redis_client.get", return_value=None + ) client.get_free_sms_fragment_limit_for_year(service_id, year=1999) mock_get.assert_called_once_with( @@ -28,6 +31,11 @@ def test_post_free_sms_fragment_limit_for_current_year_endpoint( ) client = BillingAPIClient() + mocker.patch( + "app.notify_client.billing_api_client.redis_client.get", return_value=None + ) + + mocker.patch("app.notify_client.billing_api_client.redis_client.set") client.create_or_update_free_sms_fragment_limit( service_id=service_id, free_sms_fragment_limit=1111 ) From 1c77f8d60f0c3199ed8f31f20d549bfc69dbe73d Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 16 Jan 2025 09:27:32 -0800 Subject: [PATCH 3/3] fix test --- tests/app/notify_client/test_billing_client.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/app/notify_client/test_billing_client.py b/tests/app/notify_client/test_billing_client.py index 65898fec25..c102645d28 100644 --- a/tests/app/notify_client/test_billing_client.py +++ b/tests/app/notify_client/test_billing_client.py @@ -10,11 +10,16 @@ def test_get_free_sms_fragment_limit_for_year_correct_endpoint(mocker, api_user_ expected_url = "/service/{}/billing/free-sms-fragment-limit".format(service_id) client = BillingAPIClient() - mock_get = mocker.patch("app.notify_client.billing_api_client.BillingAPIClient.get") + mock_get = mocker.patch( + "app.notify_client.billing_api_client.BillingAPIClient.get", + return_value={"free_sms_fragment_limit": 0}, + ) mocker.patch( "app.notify_client.billing_api_client.redis_client.get", return_value=None ) + mocker.patch("app.notify_client.billing_api_client.redis_client.set") + client.get_free_sms_fragment_limit_for_year(service_id, year=1999) mock_get.assert_called_once_with( expected_url, params={"financial_year_start": 1999}