Skip to content

Commit

Permalink
Merge branch 'feature/vtex-apptype' of github.com:weni-ai/weni-integr…
Browse files Browse the repository at this point in the history
…ations-engine into develop
  • Loading branch information
elitonzky committed Nov 24, 2023
2 parents 56b6f79 + 49a8790 commit 91fc6dd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
18 changes: 14 additions & 4 deletions marketplace/core/types/ecommerce/vtex/serializers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from marketplace.core.serializers import AppTypeBaseSerializer
from marketplace.applications.models import App


class VtexDomainSerializer(serializers.Serializer):
domain = serializers.CharField(required=True)


class VtexSerializer(serializers.Serializer):
domain = serializers.CharField(required=True)
app_key = serializers.CharField(required=True)
app_token = serializers.CharField(required=True)
wpp_cloud_uuid = serializers.UUIDField(required=True)

def validate_wpp_cloud_uuid(self, value):
"""
Check that the wpp_cloud_uuid corresponds to an existing App with code 'wpp-cloud'.
"""
try:
App.objects.get(uuid=value, code="wpp-cloud")
except App.DoesNotExist:
raise ValidationError(
"The wpp_cloud_uuid does not correspond to a valid 'wpp-cloud' App."
)
return str(value)


class VtexAppSerializer(AppTypeBaseSerializer):
Expand Down
41 changes: 36 additions & 5 deletions marketplace/core/types/ecommerce/vtex/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def get_vtex_app_or_error(self, project_uuid):
def check_is_valid_credentials(self, credentials):
return True

def configure(self, app, credentials):
app.configured = True
def configure(self, app, credentials, wpp_cloud_uuid):
app.config["api_credentials"] = credentials.to_dict()
app.config["wpp_cloud_uuid"] = wpp_cloud_uuid
app.configured = True
app.save()
return app

Expand Down Expand Up @@ -57,15 +58,23 @@ class CreateVtexAppTestCase(SetUpService):

def setUp(self):
super().setUp()
project_uuid = str(uuid.uuid4())
self.project_uuid = str(uuid.uuid4())

self.wpp_cloud = App.objects.create(
code="wpp-cloud",
created_by=self.user,
project_uuid=self.project_uuid,
platform=App.PLATFORM_WENI_FLOWS,
)
self.body = {
"project_uuid": project_uuid,
"project_uuid": self.project_uuid,
"app_key": "valid-app-key",
"app_token": "valid-app-token",
"domain": "valid.domain.com",
"wpp_cloud_uuid": str(self.wpp_cloud.uuid),
}
self.user_authorization = self.user.authorizations.create(
project_uuid=project_uuid, role=ProjectAuthorization.ROLE_CONTRIBUTOR
project_uuid=self.project_uuid, role=ProjectAuthorization.ROLE_CONTRIBUTOR
)

def test_request_ok(self):
Expand All @@ -91,6 +100,28 @@ def test_create_app_without_permission(self):
response = self.request.post(self.url, self.body)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_create_app_without_valid_wpp_cloud_app(self):
not_wpp_cloud = App.objects.create(
code="wpp",
created_by=self.user,
project_uuid=self.project_uuid,
platform=App.PLATFORM_WENI_FLOWS,
)
body = {
"project_uuid": self.project_uuid,
"app_key": "valid-app-key",
"app_token": "valid-app-token",
"domain": "valid.domain.com",
"wpp_cloud_uuid": str(not_wpp_cloud.uuid),
}
response = self.request.post(self.url, body)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("wpp_cloud_uuid", response.data)
self.assertIn(
"does not correspond to a valid 'wpp-cloud' App",
str(response.data["wpp_cloud_uuid"]),
)

def test_create_app_configuration_exception(self):
original_configure = self.mock_service.configure
self.mock_service.configure = Mock(side_effect=Exception("Configuration error"))
Expand Down
3 changes: 2 additions & 1 deletion marketplace/core/types/ecommerce/vtex/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def create(self, request, *args, **kwargs):
app_token=validated_data.get("app_token"),
domain=validated_data.get("domain"),
)
wpp_cloud_uuid = validated_data["wpp_cloud_uuid"]
self.service.check_is_valid_credentials(credentials)
# Calls the create method of the base class to create the App object
super().create(request, *args, **kwargs)
app = self.get_app()
try:
updated_app = self.service.configure(app, credentials)
updated_app = self.service.configure(app, credentials, wpp_cloud_uuid)
return Response(
data=self.get_serializer(updated_app).data,
status=status.HTTP_201_CREATED,
Expand Down
19 changes: 5 additions & 14 deletions marketplace/services/vtex/generic_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,9 @@ def check_is_valid_credentials(self, credentials: APICredentials) -> bool:

return True

def configure(self, app, credentials: APICredentials) -> App:
updated_app = self._update_config(
app, key="api_credentials", data=credentials.to_dict()
)
updated_app.configured = True
updated_app.save()
return updated_app

# ================================
# Private Methods
# ================================

def _update_config(self, app, key, data):
app.config[key] = data
def configure(self, app, credentials: APICredentials, wpp_cloud_uuid) -> App:
app.config["api_credentials"] = credentials.to_dict()
app.config["wpp_cloud_uuid"] = wpp_cloud_uuid
app.configured = True
app.save()
return app

0 comments on commit 91fc6dd

Please sign in to comment.