Skip to content

Commit

Permalink
Adds test to FlowsService.update_active_catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Oct 20, 2023
1 parent c6f9152 commit ea1098d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 12 deletions.
12 changes: 12 additions & 0 deletions marketplace/clients/flows/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ def update_config(self, data, flow_object_uuid):
json=payload,
)
return response

def update_catalogs(self, channel_uuid, fba_catalog_id):
data = {"channel": str(channel_uuid), "facebook_catalog_id": fba_catalog_id}
url = f"{self.base_url}/catalogs/update-catalog/"

response = self.make_request(
url,
method="POST",
headers=self.authentication_instance.headers,
json=data,
)
return response
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, *args, **kwargs):
pass

def enable_catalog(self, catalog):
return {"success": "True"}
return True, {"success": True}

def disable_catalog(self, catalog):
return {"success": "True"}
Expand All @@ -28,6 +28,14 @@ def get_connected_catalog(self, app):
return "0123456789"


class MockFailiedEnableCatalogFacebookService:
def __init__(self, *args, **kwargs):
pass

def enable_catalog(self, catalog):
return False, {"success": False}


class MockFlowsService:
def __init__(self, *args, **kwargs):
pass
Expand Down Expand Up @@ -138,7 +146,25 @@ def test_enable_catalog(self):
url, app_uuid=self.app.uuid, catalog_uuid=self.catalog.uuid
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json["success"], "True")

def test_failed_enable_catalog(self):
mock_facebook_service = MockFailiedEnableCatalogFacebookService()
patcher_fb_failure = patch.object(
self.view_class,
"fb_service",
PropertyMock(return_value=mock_facebook_service),
)
patcher_fb_failure.start()
self.addCleanup(patcher_fb_failure.stop)

url = reverse(
"catalog-enable",
kwargs={"app_uuid": self.app.uuid, "catalog_uuid": self.catalog.uuid},
)
response = self.request.post(
url, app_uuid=self.app.uuid, catalog_uuid=self.catalog.uuid
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


class CatalogDisableTestCase(MockServiceTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ def list(self, request, *args, **kwargs):
@action(detail=True, methods=["POST"])
def enable_catalog(self, request, *args, **kwargs):
catalog = self.get_object()
response = self.fb_service.enable_catalog(catalog)
success, response = self.fb_service.enable_catalog(catalog)
if not success:
return Response(status=status.HTTP_400_BAD_REQUEST, data=response)

self.flows_service.update_active_catalog(
catalog.app, catalog.facebook_catalog_id
)
return Response(response)
return Response(status=status.HTTP_200_OK)

@action(detail=True, methods=["POST"])
def disable_catalog(self, request, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ def get_app_facebook_credentials(self, app):

def enable_catalog(self, catalog):
waba_id = self.get_app_facebook_credentials(app=catalog.app).get("wa_waba_id")
return self.client.enable_catalog(
response = self.client.enable_catalog(
waba_id=waba_id, catalog_id=catalog.facebook_catalog_id
)
success = response.get("success") is True
return success, response

def disable_catalog(self, catalog):
waba_id = self.get_app_facebook_credentials(app=catalog.app).get("wa_waba_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ def update_treshold(self, app, treshold):
return self._update_flows_config(app, "treshold")

def update_active_catalog(self, app, fba_catalog_id):
app.config["catalog_id"] = fba_catalog_id
app.save()

return self._update_flows_config(app, "catalog_id")
return self.client.update_catalogs(app.flow_object_uuid, fba_catalog_id)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class MockClient:
def enable_catalog(self, waba_id, catalog_id):
return {"success": "true"}
return {"success": True}

def disable_catalog(self, waba_id, catalog_id):
return {"success": "true"}
Expand Down Expand Up @@ -89,8 +89,8 @@ def test_get_app_facebook_credentials(self):
self.assertEqual(credentials, expected_config)

def test_enable_catalog(self):
response = self.service.enable_catalog(self.catalog)
self.assertEqual(response, {"success": "true"})
success, _response = self.service.enable_catalog(self.catalog)
self.assertEqual(success, True)

def test_disable_catalog(self):
response = self.service.disable_catalog(self.catalog)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.test import TestCase
from django.contrib.auth import get_user_model
from rest_framework import status

from marketplace.core.types.channels.whatsapp_cloud.services.flows import (
FlowsService,
Expand All @@ -21,6 +22,15 @@
}


class MockResponse:
def __init__(self, status_code, json_data=None):
self.status_code = status_code
self.json_data = json_data

def json(self):
return self.json_data


class MockFlowsClient:
def detail_channel(self, flow_object_uuid):
return {
Expand All @@ -35,6 +45,9 @@ def detail_channel(self, flow_object_uuid):
def update_config(self, data, flow_object_uuid):
return None

def update_catalogs(self, flow_object_uuid, fba_catalog_id):
return MockResponse(status.HTTP_200_OK)


class TestFlowsService(TestCase):
def setUp(self):
Expand All @@ -54,3 +67,10 @@ def setUp(self):
def test_update_treshold(self):
response = self.service.update_treshold(self.app, 3.5)
self.assertEqual(response, True)

def test_update_active_catalog(self):
fake_facebook_catalog_id = "123456789"
response = self.service.update_active_catalog(
self.app, fake_facebook_catalog_id
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

0 comments on commit ea1098d

Please sign in to comment.