Skip to content

Commit

Permalink
[FLOWS-312] - Endpoint to update project config with has_vtex key (#271)
Browse files Browse the repository at this point in the history
* Endpoint to update project config with has_vtex key

* Add tests and run black
  • Loading branch information
lucaslinhares authored Feb 2, 2024
1 parent ec866b9 commit 0aa6978
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 40 deletions.
9 changes: 9 additions & 0 deletions weni/internal/orgs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ class Meta:
"is_multi_org",
"is_suspended",
]


class UpdateProjectSerializer(serializers.ModelSerializer):
project_uuid = serializers.CharField(read_only=True)
modified_by = weni_serializers.UserEmailRelatedField(required=False, write_only=True)

class Meta:
model = Project
fields = ["project_uuid", "modified_by"]
210 changes: 171 additions & 39 deletions weni/internal/orgs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.auth.models import User
from django.utils.http import urlencode
from django.urls import reverse
from rest_framework.test import APIClient

from weni.internal.models import Project
from temba.api.models import APIToken
Expand All @@ -31,36 +32,56 @@ def reverse(self, viewname, kwargs=None, query_params=None):
def request_get(self, **query_params):
url = self.reverse(self.get_url_namespace(), query_params=query_params)
url = url.replace("channel", "channel.json")
token = APIToken.get_or_create(self.org, self.admin, Group.objects.get(name="Administrators"))
token = APIToken.get_or_create(
self.org, self.admin, Group.objects.get(name="Administrators")
)

return self.client.get(f"{url}", HTTP_AUTHORIZATION=f"Token {token.key}")

def request_detail(self, uuid):
url = self.reverse(self.get_url_namespace(), kwargs={"project_uuid": uuid})
token = APIToken.get_or_create(self.org, self.admin, Group.objects.get(name="Administrators"))
token = APIToken.get_or_create(
self.org, self.admin, Group.objects.get(name="Administrators")
)

return self.client.get(f"{url}", HTTP_AUTHORIZATION=f"Token {token.key}")

def request_post(self, data):
url = reverse(self.get_url_namespace())
token = APIToken.get_or_create(self.org, self.admin, Group.objects.get(name="Administrators"))
token = APIToken.get_or_create(
self.org, self.admin, Group.objects.get(name="Administrators")
)

return self.client.post(
url, HTTP_AUTHORIZATION=f"Token {token.key}", data=json.dumps(data), content_type="application/json"
url,
HTTP_AUTHORIZATION=f"Token {token.key}",
data=json.dumps(data),
content_type="application/json",
)

def request_delete(self, uuid, **query_params):
url = self.reverse(self.get_url_namespace(), kwargs={"project_uuid": uuid}, query_params=query_params)
token = APIToken.get_or_create(self.org, self.admin, Group.objects.get(name="Administrators"))
url = self.reverse(
self.get_url_namespace(),
kwargs={"project_uuid": uuid},
query_params=query_params,
)
token = APIToken.get_or_create(
self.org, self.admin, Group.objects.get(name="Administrators")
)

return self.client.delete(f"{url}", HTTP_AUTHORIZATION=f"Token {token.key}")

def request_patch(self, uuid, data):
url = self.reverse(self.get_url_namespace(), kwargs={"project_uuid": uuid})
token = APIToken.get_or_create(self.org, self.admin, Group.objects.get(name="Administrators"))
token = APIToken.get_or_create(
self.org, self.admin, Group.objects.get(name="Administrators")
)

return self.client.patch(
url, HTTP_AUTHORIZATION=f"Token {token.key}", data=json.dumps(data), content_type="application/json"
url,
HTTP_AUTHORIZATION=f"Token {token.key}",
data=json.dumps(data),
content_type="application/json",
)

@abstractmethod
Expand All @@ -74,14 +95,24 @@ class OrgListTest(TembaTest, TembaRequestMixin):
WRONG_EMAIL = "wrong@email.com"

def setUp(self):
User.objects.create_user(username="testuser", password="123", email="test@weni.ai")
User.objects.create_user(username="weniuser", password="123", email="wene@user.com")
User.objects.create_user(
username="testuser", password="123", email="test@weni.ai"
)
User.objects.create_user(
username="weniuser", password="123", email="wene@user.com"
)

user = User.objects.get(username="testuser")

Project.objects.create(name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(
name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user
)

super().setUp()

Expand Down Expand Up @@ -145,19 +176,35 @@ class OrgCreateTest(TembaTest, TembaRequestMixin):
WRONG_EMAIL = "wrong@email.com"

def setUp(self):
User.objects.create_user(username="testuser", password="123", email="test@weni.ai")
User.objects.create_user(username="weniuser", password="123", email="wene@user.com")
User.objects.create_user(
username="testuser", password="123", email="test@weni.ai"
)
User.objects.create_user(
username="weniuser", password="123", email="wene@user.com"
)

user = User.objects.get(username="testuser")

Project.objects.create(
name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user, plan="infinity"
name="Tembinha",
timezone="Africa/Kigali",
created_by=user,
modified_by=user,
plan="infinity",
)
Project.objects.create(
name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user, plan="infinity"
name="Weni",
timezone="Africa/Kigali",
created_by=user,
modified_by=user,
plan="infinity",
)
Project.objects.create(
name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user, plan="infinity"
name="Test",
timezone="Africa/Kigali",
created_by=user,
modified_by=user,
plan="infinity",
)

super().setUp()
Expand All @@ -166,12 +213,18 @@ def setUp(self):
def test_create_org(self, mock):
org_name = "TestCreateOrg"
user = User.objects.get(username="weniuser")
response = self.request_post(data=dict(name=org_name, timezone="Wrong/Zone", user_email=user.email)).json()
response = self.request_post(
data=dict(name=org_name, timezone="Wrong/Zone", user_email=user.email)
).json()

self.assertEqual(response.get("timezone")[0], '"Wrong/Zone" is not a valid choice.')
self.assertEqual(
response.get("timezone")[0], '"Wrong/Zone" is not a valid choice.'
)

response = self.request_post(
data=dict(name=org_name, timezone="Africa/Kigali", user_email="newemail@email.com")
data=dict(
name=org_name, timezone="Africa/Kigali", user_email="newemail@email.com"
)
).json()

newuser_qs = User.objects.filter(email="newemail@email.com")
Expand All @@ -198,7 +251,9 @@ def test_create_org(self, mock):
self.assertEquals(administrator, newuser)

response = self.request_post(
data=dict(name="neworg", timezone="Africa/Kigali", user_email="newemail@email.com")
data=dict(
name="neworg", timezone="Africa/Kigali", user_email="newemail@email.com"
)
).json()

self.assertEqual(User.objects.filter(email="newemail@email.com").count(), 1)
Expand All @@ -213,14 +268,24 @@ class OrgRetrieveTest(TembaTest, TembaRequestMixin):
WRONG_EMAIL = "wrong@email.com"

def setUp(self):
User.objects.create_user(username="testuser", password="123", email="test@weni.ai")
User.objects.create_user(username="weniuser", password="123", email="wene@user.com")
User.objects.create_user(
username="testuser", password="123", email="test@weni.ai"
)
User.objects.create_user(
username="weniuser", password="123", email="wene@user.com"
)

user = User.objects.get(username="testuser")

Project.objects.create(name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(
name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user
)

super().setUp()

Expand Down Expand Up @@ -269,14 +334,24 @@ class OrgDestroyTest(TembaTest, TembaRequestMixin):
WRONG_EMAIL = "wrong@email.com"

def setUp(self):
User.objects.create_user(username="testuser", password="123", email="test@weni.ai")
User.objects.create_user(username="weniuser", password="123", email="wene@user.com")
User.objects.create_user(
username="testuser", password="123", email="test@weni.ai"
)
User.objects.create_user(
username="weniuser", password="123", email="wene@user.com"
)

user = User.objects.get(username="testuser")

Project.objects.create(name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(
name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user
)

super().setUp()

Expand Down Expand Up @@ -306,14 +381,24 @@ class OrgUpdateTest(TembaTest, TembaRequestMixin):
WRONG_EMAIL = "wrong@email.com"

def setUp(self):
User.objects.create_user(username="testuser", password="123", email="test@weni.ai")
User.objects.create_user(username="weniuser", password="123", email="wene@user.com")
User.objects.create_user(
username="testuser", password="123", email="test@weni.ai"
)
User.objects.create_user(
username="weniuser", password="123", email="wene@user.com"
)

user = User.objects.get(username="testuser")

Project.objects.create(name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user)
Project.objects.create(
name="Tembinha", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Weni", timezone="Africa/Kigali", created_by=user, modified_by=user
)
Project.objects.create(
name="Test", timezone="Africa/Kigali", created_by=user, modified_by=user
)

super().setUp()

Expand All @@ -322,7 +407,9 @@ def test_update_org(self):

permission_error_message = f"User: {self.user.id} has no permission to update Org: {project.project_uuid}"

response = self.request_patch(uuid=str(project.project_uuid), data=dict(modified_by=self.user.email)).json()
response = self.request_patch(
uuid=str(project.project_uuid), data=dict(modified_by=self.user.email)
).json()

self.assertEqual(response[0], permission_error_message)

Expand All @@ -345,7 +432,9 @@ def test_update_org(self):
"modified_by": self.user.email,
}

response = self.request_patch(uuid=str(project.project_uuid), data=update_fields).json()
response = self.request_patch(
uuid=str(project.project_uuid), data=update_fields
).json()

updated_org = Project.objects.get(pk=project.pk)

Expand Down Expand Up @@ -380,3 +469,46 @@ def test_update_org(self):

def get_url_namespace(self):
return "orgs-detail"


class ProjectUpdateVtexTest(TembaTest):
def setUp(self):
self.client = APIClient()
self.url = reverse("orgs-list")
super().setUp()

def test_update_vtex_post(self):
project = Project.objects.create(
name="Test",
timezone="Africa/Kigali",
created_by=self.user,
modified_by=self.user,
config={},
)

url = self.url + f"{project.project_uuid}/update-vtex/"
data = {"user_email": self.user.email}

response = self.client.post(url, data, format="json")

self.assertEqual(response.status_code, 200)
self.assertTrue(project.config.get("has_vtex", True))

def test_update_vtex_delete(self):
project = Project.objects.create(
name="Test",
timezone="Africa/Kigali",
created_by=self.user,
modified_by=self.user,
config={"has_vtex": True},
)

url = self.url + f"{project.project_uuid}/update-vtex/"
data = {"user_email": self.user.email}

response = self.client.delete(url, data, format="json")

self.assertEqual(response.status_code, 204)

project.refresh_from_db()
self.assertNotIn("has_vtex", project.config)
Loading

0 comments on commit 0aa6978

Please sign in to comment.