Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,11 @@ fileignoreconfig:
- filename: tests/unit/locales/test_locale_unit.py
checksum: 7d272795c81f4ab17a9e47a4c208fd6cb40f86c52e304aa528b05cef16e86125
version: ""
fileignoreconfig:
- filename: tests/mock/taxonomies/test_taxonomy_mock.py
checksum: fab3e50d4f82a206b8d0250e0e48825def4505ff92d45f4883c230428fa350d9
- filename: tests/api/taxonomies/test_taxonomy_api.py
checksum: c02ddccee423f2f50e0428a3d151f35128a51c3a4a8b11e87a4f0042c83f3fa9
- filename: tests/unit/taxonomies/test_taxonomy_unit.py
checksum: 5dd2f73f683c293f71fd9002148747a7167a85981d6fb90e491cafddf7b96169
version: ""
2 changes: 2 additions & 0 deletions contentstack_management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .common import Parameter
from ._errors import ArgumentException
from .locale.locale import Locale
from .taxonomies.taxonomy import Taxonomy

__all__ = (
"ContentstackClient",
Expand All @@ -45,6 +46,7 @@
"Environment",
"Entry",
"Locale",
"Taxonomy",
)

__title__ = 'contentstack-management-python'
Expand Down
7 changes: 6 additions & 1 deletion contentstack_management/stack/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..auditlogs.auditlog import Auditlog
from ..environments.environment import Environment
from ..locale.locale import Locale
from ..taxonomies.taxonomy import Taxonomy



Expand Down Expand Up @@ -343,4 +344,8 @@ def environments(self, environment_name: str = None):
return Environment(self.client, environment_name)

def locale(self, locale_code: str = None):
return Locale(self.client, locale_code)
return Locale(self.client, locale_code)

def taxonomy(self, taxonomy_uid: str = None):
return Taxonomy(self.client, taxonomy_uid)

151 changes: 151 additions & 0 deletions contentstack_management/taxonomies/taxonomy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""This class takes a base URL as an argument when it's initialized,
which is the endpoint for the RESTFUL API that we'll be interacting with.
The create(), read(), update(), and delete() methods each correspond to
the CRUD operations that can be performed on the API """

import json
from ..common import Parameter
from urllib.parse import quote
from .._errors import ArgumentException

class Taxonomy(Parameter):
"""
This class takes a base URL as an argument when it's initialized,
which is the endpoint for the RESTFUL API that
we'll be interacting with. The create(), read(), update(), and delete()
methods each correspond to the CRUD
operations that can be performed on the API """

def __init__(self, client, taxonomy_uid: str):
self.client = client
self.taxonomy_uid = taxonomy_uid
super().__init__(self.client)

self.path = "taxonomies"

def find(self):
"""
This call fetches the list of all taxonomies available for a stack.
:return: Json, with taxonomy details.

-------------------------------
[Example:]

>>> from contentstack_management import contentstack
>>> client = contentstack.client(authtoken='your_authtoken')
>>> result = client.stack("api_key").taxonomy().find().json()

-------------------------------
"""
return self.client.get(self.path, headers = self.client.headers)



def fetch(self, taxonomy_uid: str = None):
"""
The "Get a taxonomy" call returns information about a specific taxonomy available on the stack.
:return: Json, with taxonomy details.
-------------------------------
[Example:]

>>> from contentstack_management import contentstack
>>> client = contentstack.client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').fetch('taxonomy_uid').json()

-------------------------------
"""
if taxonomy_uid is not None and taxonomy_uid != '':
self.taxonomy_uid = taxonomy_uid

self.validate_taxonomy_uid()
url = f"{self.path}/{self.taxonomy_uid}"
return self.client.get(url, headers = self.client.headers)


def create(self, data: dict):
"""
This call lets you add a new taxonomy to your stack.

:param data: The `data` parameter is the payload that you want to send in the request body. It
should be a dictionary or a JSON serializable object that you want to send as the request body
:return: Json, with taxonomy details.

-------------------------------
[Example:]
>>> data ={
>>> "taxonomy": {
>>> "uid": "taxonomy12345",
>>> "name": "Taxonomy 12345",
>>> "description": "Description for Taxonomy 1"
>>> }
>>> }
>>> from contentstack_management import contentstack
>>> client = contentstack.client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy().create(data).json()

-------------------------------
"""

data = json.dumps(data)
return self.client.post(self.path, headers = self.client.headers, data=data)

def update(self, data: dict, taxonomy_uid: str = None):
"""
The "Update taxonomy" call will let you update the details

:param data: The `data` parameter is the data that you want to update. It should be a dictionary
or an object that can be serialized to JSON
:return: Json, with updated taxonomy details.
-------------------------------
[Example:]
>>> data ={
>>> "taxonomy": {
>>> "name": "Taxonomy 12345",
>>> "description": "Description updated for Taxonomy 12345"
>>> }
>>> }
>>> from contentstack_management import contentstack
>>> client = contentstack.client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy("taxonomy_uid").update(data).json()

-------------------------------
"""

if taxonomy_uid is not None and taxonomy_uid != '':
self.taxonomy_uid = taxonomy_uid

self.validate_taxonomy_uid()
url = f"{self.path}/{self.taxonomy_uid}"
data = json.dumps(data)
return self.client.put(url, headers = self.client.headers, data=data)


def delete(self, taxonomy_uid: str = None):
"""
The "Delete taxonomy" call deletes an existing taxonomy from your stack.
:return: The delete() method returns the status code and message as a response.

-------------------------------
[Example:]

>>> from contentstack_management import contentstack
>>> client = contentstack.client(authtoken='your_authtoken')
>>> result = result = client.stack('api_key').taxonomy('taxonomy_uid').delete('taxonomy_uid').json()

-------------------------------
"""

if taxonomy_uid is not None and taxonomy_uid != '':
self.taxonomy_uid = taxonomy_uid

self.validate_taxonomy_uid()
url = f"{self.path}/{self.taxonomy_uid}"
return self.client.delete(url, headers = self.client.headers)


def validate_taxonomy_uid(self):
if self.taxonomy_uid is None or '':
raise ArgumentException('Taxonomy Uid is required')



57 changes: 57 additions & 0 deletions tests/api/taxonomies/test_taxonomy_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import unittest
from dotenv import load_dotenv
from contentstack_management import contentstack
from tests.cred import get_credentials

credentials = get_credentials()
username = credentials["username"]
password = credentials["password"]
host = credentials["host"]
api_key = credentials["api_key"]
taxonomy_uid = credentials["taxonomy_uid"]



class TaxonomyApiTests(unittest.TestCase):

def setUp(self):
self.client = contentstack.ContentstackClient(host=host)
self.client.login(username, password)

def test_get_all_taxonomy(self):
response = self.client.stack(api_key).taxonomy().find()
self.assertEqual(response.status_code, 200)

def test_get_a_taxonomy(self):
response = self.client.stack(api_key).taxonomy(taxonomy_uid).fetch()
self.assertEqual(response.status_code, 200)

def test_create(self):
data = {
"taxonomy": {
"uid": "taxonomy_1",
"name": "Taxonomy 1",
"description": "Description for Taxonomy 1"
}
}
response = self.client.stack(api_key).taxonomy().create(data)
self.assertEqual(response.status_code, 201)

def test_update_taxonomy(self):
data = {
"taxonomy": {
"name": "Taxonomy 1",
"description": "Description updated for Taxonomy 1"
}
}
response = self.client.stack(api_key).taxonomy(taxonomy_uid).update(data)
self.assertEqual(response.status_code, 200)


def test_delete_taxonomy(self):
response = self.client.stack(api_key).taxonomy(taxonomy_uid).delete()
self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion tests/api/webhooks/test_webhooks_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@



class WebhooksUnitTests(unittest.TestCase):
class WebhooksApiTests(unittest.TestCase):

def setUp(self):
self.client = contentstack.ContentstackClient(host=host)
Expand Down
2 changes: 2 additions & 0 deletions tests/cred.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
default_log_item_uid = "log_item_uid" #Default roles uid
default_environments_name = "environments_name" #Default environment name
default_locale_code = "locale_code" #Default locale code
default_taxonomy_uid = "taxonomy_uid" #Default taxonomy code


def get_credentials():
Expand Down Expand Up @@ -70,6 +71,7 @@ def get_credentials():
"log_item_uid": os.getenv("LOG_ITEM_UID", default_log_item_uid),
"environments_name": os.getenv("ENVIRONMENT_NAME", default_environments_name),
"locale_code": os.getenv("LOCALE_CODE", default_locale_code),
"taxonomy_uid": os.getenv("TAXONOMY_UID", default_taxonomy_uid),

}
return credentials
75 changes: 75 additions & 0 deletions tests/mock/taxonomies/test_taxonomy_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import json
import os
import unittest

from dotenv import load_dotenv
from contentstack_management import contentstack
from tests.cred import get_credentials

credentials = get_credentials()
username = credentials["username"]
password = credentials["password"]
api_key = credentials["api_key"]
host = credentials["host"]
taxonomy_uid = credentials["taxonomy_uid"]


class TaxonomyMockTests(unittest.TestCase):

def setUp(self):

self.client = contentstack.ContentstackClient(host = host)
self.client.login(username, password)

def read_file(self, file_name):
file_path= f"tests/resources/mock_taxonomy/{file_name}"
infile = open(file_path, 'r')
data = infile.read()
infile.close()
return data


def test_get_all_taxonomy(self):
response = self.client.stack(api_key).taxonomy().find().json()
read_mock_taxonomy_data = self.read_file("find.json")
mock_taxonomy_data = json.loads(read_mock_taxonomy_data)
self.assertEqual(mock_taxonomy_data.keys(), response.keys())

def test_get_a_taxonomy(self):
response = self.client.stack(api_key).taxonomy(taxonomy_uid).fetch().json()
read_mock_taxonomy_data = self.read_file("fetch.json")
mock_taxonomy_data = json.loads(read_mock_taxonomy_data)
self.assertEqual(mock_taxonomy_data.keys(), response.keys())

def test_create(self):
data = {
"taxonomy": {
"uid": "taxonomy12345",
"name": "Taxonomy 12345",
"description": "Description for Taxonomy 1"
}
}

response = self.client.stack(api_key).taxonomy().create(data).json()
read_mock_taxonomy_data = self.read_file("fetch.json")
mock_taxonomy_data = json.loads(read_mock_taxonomy_data)
self.assertEqual(mock_taxonomy_data.keys(), response.keys())

def test_update_taxonomy(self):
data = {
"taxonomy": {
"name": "Taxonomy 12345",
"description": "Description updated for Taxonomy 12345"
}
}
response = self.client.stack(api_key).taxonomy(taxonomy_uid).update(data).json()
read_mock_taxonomy_data = self.read_file("fetch.json")
mock_taxonomy_data = json.loads(read_mock_taxonomy_data)
self.assertEqual(mock_taxonomy_data.keys(), response.keys())

def test_delete_taxonomy(self):
response = self.client.stack(api_key).taxonomy(taxonomy_uid).delete().json()
read_mock_taxonomy_data = self.read_file("fetch.json")
mock_taxonomy_data = json.loads(read_mock_taxonomy_data)
self.assertEqual(mock_taxonomy_data['notice'], response['notice'])

11 changes: 11 additions & 0 deletions tests/resources/mock_taxonomy/fetch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"taxonomy": {
"uid": "taxonomy12345",
"name": "Taxonomy 12345",
"description": "Description updated for Taxonomy 12345",
"created_at": "2023-09-06T16:32:54.819Z",
"created_by": "user_uid",
"updated_at": "2023-09-07T07:43:42.692Z",
"updated_by": "user_uid"
}
}
Loading