From bbfce93f6981cc71fb4bac6df7c8a87f417716e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Thu, 24 Oct 2024 00:07:29 +0200 Subject: [PATCH] refactor(API): store app_version in Price & Proof source field (#533) --- open_prices/api/prices/tests.py | 28 +++++++++++++++++----------- open_prices/api/prices/views.py | 3 ++- open_prices/api/proofs/tests.py | 28 +++++++++++++++++----------- open_prices/api/proofs/views.py | 3 ++- open_prices/api/utils.py | 8 ++++++++ 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/open_prices/api/prices/tests.py b/open_prices/api/prices/tests.py index e5a34e43..1cf008b2 100644 --- a/open_prices/api/prices/tests.py +++ b/open_prices/api/prices/tests.py @@ -456,17 +456,23 @@ def test_price_create_with_location_id(self): self.assertEqual(response.status_code, 201) def test_price_create_with_app_name(self): - for app_name in ["", "test app"]: - response = self.client.post( - self.url + f"?app_name={app_name}", - self.data, - headers={"Authorization": f"Bearer {self.user_session.token}"}, - content_type="application/json", - ) - self.assertEqual(response.status_code, 201) - self.assertTrue("source" not in response.data) - p = Price.objects.last() - self.assertEqual(p.source, app_name) + for params, result in [ + ("?", "API"), + ("?app_name=", ""), + ("?app_name=test app&app_version=", "test app"), + ("?app_name=mobile&app_version=1.0", "mobile (1.0)"), + ]: + with self.subTest(INPUT_OUPUT=(params, result)): + response = self.client.post( + self.url + params, + self.data, + headers={"Authorization": f"Bearer {self.user_session.token}"}, + content_type="application/json", + ) + self.assertEqual(response.status_code, 201) + self.assertTrue("source" not in response.data) + p = Price.objects.last() + self.assertEqual(p.source, result) class PriceUpdateApiTest(TestCase): diff --git a/open_prices/api/prices/views.py b/open_prices/api/prices/views.py index ccc83435..3bf7280e 100644 --- a/open_prices/api/prices/views.py +++ b/open_prices/api/prices/views.py @@ -13,6 +13,7 @@ PriceStatsSerializer, PriceUpdateSerializer, ) +from open_prices.api.utils import get_source_from_request from open_prices.common.authentication import CustomAuthentication from open_prices.prices.models import Price @@ -59,7 +60,7 @@ def create(self, request: Request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # get source - self.source = self.request.GET.get("app_name", "API") + self.source = get_source_from_request(self.request) # save price = self.perform_create(serializer) # return full price diff --git a/open_prices/api/proofs/tests.py b/open_prices/api/proofs/tests.py index 02f507f7..cd0ff83d 100644 --- a/open_prices/api/proofs/tests.py +++ b/open_prices/api/proofs/tests.py @@ -240,17 +240,23 @@ def test_proof_create_with_location_id(self): self.assertEqual(response.status_code, 201) def test_proof_create_with_app_name(self): - for app_name in ["", "test app"]: - # with empty app_name - response = self.client.post( - self.url + f"?app_name={app_name}", - self.data, - headers={"Authorization": f"Bearer {self.user_session.token}"}, - ) - self.assertEqual(response.status_code, 201) - self.assertTrue("source" not in response.data) - p = Proof.objects.last() - self.assertEqual(p.source, app_name) + for params, result in [ + ("?", "API"), + ("?app_name=", ""), + ("?app_name=test app&app_version=", "test app"), + ("?app_name=mobile&app_version=1.0", "mobile (1.0)"), + ]: + with self.subTest(INPUT_OUPUT=(params, result)): + # with empty app_name + response = self.client.post( + self.url + params, + self.data, + headers={"Authorization": f"Bearer {self.user_session.token}"}, + ) + self.assertEqual(response.status_code, 201) + self.assertTrue("source" not in response.data) + p = Proof.objects.last() + self.assertEqual(p.source, result) class ProofUpdateApiTest(TestCase): diff --git a/open_prices/api/proofs/views.py b/open_prices/api/proofs/views.py index 9b3a2442..b84ce0be 100644 --- a/open_prices/api/proofs/views.py +++ b/open_prices/api/proofs/views.py @@ -14,6 +14,7 @@ ProofUpdateSerializer, ProofUploadSerializer, ) +from open_prices.api.utils import get_source_from_request from open_prices.common.authentication import CustomAuthentication from open_prices.proofs.models import Proof from open_prices.proofs.utils import store_file @@ -88,7 +89,7 @@ def upload(self, request: Request) -> Response: serializer = ProofCreateSerializer(data=proof_create_data) serializer.is_valid(raise_exception=True) # get source - self.source = self.request.GET.get("app_name", "API") + self.source = get_source_from_request(self.request) # save proof = serializer.save( owner=self.request.user.user_id, diff --git a/open_prices/api/utils.py b/open_prices/api/utils.py index f22cc476..6a272ba9 100644 --- a/open_prices/api/utils.py +++ b/open_prices/api/utils.py @@ -6,3 +6,11 @@ def get_object_or_drf_404(model, **kwargs): return model.objects.get(**kwargs) except model.DoesNotExist: raise Http404(f"No {model._meta.verbose_name} matches the given query.") + + +def get_source_from_request(request): + app_name = request.GET.get("app_name", "API") + app_version = request.GET.get("app_version", "") + if app_version: + return f"{app_name} ({app_version})" + return app_name