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 @@ -201,3 +201,11 @@ fileignoreconfig:
- filename: README.md
checksum: 45ea945d901c8a47144e8eb7106e49b1d121400cf47cbc7e5134f62a236b8da5
version: ""
fileignoreconfig:
- filename: tests/mock/auditlogs/test_auditlogs_mock.py
checksum: 6be6e351200624863cbfc443d3215242f03b0b06adc06649d119bee80b7608a3
- filename: tests/api/auditlogs/test_auditlogs_api.py
checksum: ff37d395fe03a822775e73a1c867cfcbbe14a75e37e4cb8a6d09f19fd790da7e
- filename: tests/unit/auditlogs/test_auditlog_unit.py
checksum: 1b8b24cc7f7921d209b983a99c0305d5fb27c1fe8e3fc74f1d525a7327eba830
version: ""
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2012 - 2023 Contentstack. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions contentstack_management/auditlogs/auditlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""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 Auditlog(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, log_item_uid: str):
self.client = client
self.log_item_uid = log_item_uid
super().__init__(self.client)

self.path = "audit-logs"

def find(self):
"""
The "Get audit log" request is used to retrieve the audit log of a stack.
:return: Json, with auditlog details.

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

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

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



def fetch(self):
"""
The "Get audit log item" request is used to retrieve a specific item from the audit log of a stack.
:return: Json, with auditlog details.
-------------------------------
[Example:]

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

-------------------------------
"""
self.validate_uid()
url = f"{self.path}/{self.log_item_uid}"
return self.client.get(url, headers = self.client.headers)

def validate_uid(self):
if self.log_item_uid is None or '':
raise ArgumentException('Log item Uid is required')


7 changes: 6 additions & 1 deletion contentstack_management/stack/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from ..workflows.workflows import Workflows
from ..metadata.metadata import Metadata
from ..roles.roles import Roles
from ..auditlogs.auditlog import Auditlog



class Stack(Parameter):
Expand Down Expand Up @@ -330,4 +332,7 @@ def metadata(self, metadata_uid: str = None):
return Metadata(self.client, metadata_uid)

def roles(self, roles_uid: str = None):
return Roles(self.client, roles_uid)
return Roles(self.client, roles_uid)

def auditlog(self, log_item_uid: str = None):
return Auditlog(self.client, log_item_uid)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
python-dotenv~=1.0.0
setuptools==68.0.0
requests~=2.31.0
pylint
pylint
bson>=0.5.9
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
long_description_content_type="text/markdown",
url="https://github.com/contentstack/contentstack-management-python",
author="Sunil",
author_email="sunil,lakshman@contentstack.com",
author_email="sunil.lakshman@contentstack.com",
license="MIT",
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.10",
"Operating System :: OS Independent",
],
install_requires=["bson >= 0.5.10"],
install_requires=["bson >= 0.5.9", "requests >= 2.5.4"],
extras_require={
"dev": ["pytest>=7.0", "twine>=4.0.2", "dotenv>=0.0.5"],
},
Expand Down
44 changes: 44 additions & 0 deletions tests/api/auditlogs/test_auditlogs_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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"]
log_item_uid = credentials["log_item_uid"]


class auditlogesApiTests(unittest.TestCase):

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

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

def test_get_a_auditlog(self):
response = self.client.stack(api_key).auditlog(log_item_uid).fetch()
self.assertEqual(response.status_code, 200)

def test_get_all_auditloges_with_params(self):
query = self.client.stack(api_key).auditlog()
query.add_param("include_branch", True)
response = query.find()
self.assertEqual(response.status_code, 200)

def test_get_a_auditlog_with_params(self):
query = self.client.stack(api_key).auditlog(log_item_uid)
query.add_param("include_branch", True)
response = query.fetch()
self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
unittest.main()
4 changes: 3 additions & 1 deletion tests/cred.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
default_rule_uid = "rule_uid" #Default rule uid
default_metadata_uid = "metadata_uid" #Default metadata uid
default_role_uid = "roles_uid" #Default roles uid
default_log_item_uid = "log_item_uid" #Default roles uid


def get_credentials():
Expand Down Expand Up @@ -63,7 +64,8 @@ def get_credentials():
"workflow_uid": os.getenv("WORKFLOW_UID", default_workflow_uid),
"rule_uid": os.getenv("RULE_UID", default_rule_uid),
"metadata_uid": os.getenv("METADATA_UID", default_metadata_uid),
"role_uid": os.getenv("ROLE_UID", default_role_uid)
"role_uid": os.getenv("ROLE_UID", default_role_uid),
"log_item_uid": os.getenv("LOG_ITEM_UID", default_log_item_uid)

}
return credentials
46 changes: 46 additions & 0 deletions tests/mock/auditlogs/test_auditlogs_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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"]
log_item_uid = credentials["log_item_uid"]


class AuditlogMockTests(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_auditlogs/{file_name}"
infile = open(file_path, 'r')
data = infile.read()
infile.close()
return data

def test_mock_get_all_auditlogs(self):
response = self.client.stack(api_key).auditlog().find().json()
read_mock_auditlogs_data = self.read_file("find.json")
mock_auditlogs_data = json.loads(read_mock_auditlogs_data)
self.assertEqual(mock_auditlogs_data.keys(), response.keys())

def test_mock_get_a_auditlog(self):
response = self.client.stack(api_key).auditlog(log_item_uid).fetch().json()
read_mock_auditlogs_data = self.read_file("fetch.json")
mock_auditlogs_data = json.loads(read_mock_auditlogs_data)
self.assertEqual(mock_auditlogs_data.keys(), response.keys())


if __name__ == '__main__':
unittest.main()
81 changes: 81 additions & 0 deletions tests/resources/mock_auditlogs/fetch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"log": {
"_id": "id",
"uid": "log_uid",
"stack": "stack_uid",
"created_at": "2023-08-17T21:53:29.817Z",
"created_by": "user_uid",
"module": "global_field",
"event_type": "update",
"request_id": "request_id",
"metadata": {
"title": "First",
"uid": "first",
"version": 6
},
"remote_addr": "ip_address",
"request": {
"global_field": {
"title": "First",
"description": "",
"schema": [
{
"display_name": "Name",
"uid": "name",
"data_type": "text",
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
},
{
"data_type": "text",
"display_name": "Rich text editor",
"uid": "description",
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
}
]
}
},
"response": {
"notice": "Global Field updated successfully.",
"global_field": {
"created_at": "2023-07-13T08:14:10.772Z",
"updated_at": "2023-08-17T21:53:29.794Z",
"title": "First",
"uid": "first",
"_version": 6,
"inbuilt_class": false,
"schema": [
{
"display_name": "Name",
"uid": "name",
"data_type": "text",
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
},
{
"data_type": "text",
"display_name": "Rich text editor",
"uid": "description",
"multiple": false,
"mandatory": false,
"unique": false,
"non_localizable": false
}
],
"last_activity": {},
"maintain_revisions": true,
"description": "",
"DEFAULT_ACL": null,
"SYS_ACL": null,
"field_rules": null
}
}
}
}
Loading