Skip to content

Commit d61bfd3

Browse files
Added auditlog class and license file (#17)
* Added auditlog class and license file * Added test case for invalid inputs * commented the idiom It Allows You to Execute Code When the File Runs as a Script, but Not When It’s Imported as a Module
1 parent 603ba2c commit d61bfd3

File tree

12 files changed

+664
-5
lines changed

12 files changed

+664
-5
lines changed

.talismanrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,11 @@ fileignoreconfig:
201201
- filename: README.md
202202
checksum: 45ea945d901c8a47144e8eb7106e49b1d121400cf47cbc7e5134f62a236b8da5
203203
version: ""
204+
fileignoreconfig:
205+
- filename: tests/mock/auditlogs/test_auditlogs_mock.py
206+
checksum: 6be6e351200624863cbfc443d3215242f03b0b06adc06649d119bee80b7608a3
207+
- filename: tests/api/auditlogs/test_auditlogs_api.py
208+
checksum: ff37d395fe03a822775e73a1c867cfcbbe14a75e37e4cb8a6d09f19fd790da7e
209+
- filename: tests/unit/auditlogs/test_auditlog_unit.py
210+
checksum: 1b8b24cc7f7921d209b983a99c0305d5fb27c1fe8e3fc74f1d525a7327eba830
211+
version: ""

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2012 - 2023 Contentstack. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""This class takes a base URL as an argument when it's initialized,
2+
which is the endpoint for the RESTFUL API that we'll be interacting with.
3+
The create(), read(), update(), and delete() methods each correspond to
4+
the CRUD operations that can be performed on the API """
5+
6+
import json
7+
from ..common import Parameter
8+
from urllib.parse import quote
9+
from .._errors import ArgumentException
10+
11+
class Auditlog(Parameter):
12+
"""
13+
This class takes a base URL as an argument when it's initialized,
14+
which is the endpoint for the RESTFUL API that
15+
we'll be interacting with. The create(), read(), update(), and delete()
16+
methods each correspond to the CRUD
17+
operations that can be performed on the API """
18+
19+
def __init__(self, client, log_item_uid: str):
20+
self.client = client
21+
self.log_item_uid = log_item_uid
22+
super().__init__(self.client)
23+
24+
self.path = "audit-logs"
25+
26+
def find(self):
27+
"""
28+
The "Get audit log" request is used to retrieve the audit log of a stack.
29+
:return: Json, with auditlog details.
30+
31+
-------------------------------
32+
[Example:]
33+
34+
>>> from contentstack_management import contentstack
35+
>>> client = contentstack.client(authtoken='your_authtoken')
36+
>>> result = client.stack("api_key").auditlog().find().json()
37+
38+
-------------------------------
39+
"""
40+
return self.client.get(self.path, headers = self.client.headers)
41+
42+
43+
44+
def fetch(self):
45+
"""
46+
The "Get audit log item" request is used to retrieve a specific item from the audit log of a stack.
47+
:return: Json, with auditlog details.
48+
-------------------------------
49+
[Example:]
50+
51+
>>> from contentstack_management import contentstack
52+
>>> client = contentstack.client(authtoken='your_authtoken')
53+
>>> result = client.stack('api_key').auditlog('log_item_uid').fetch().json()
54+
55+
-------------------------------
56+
"""
57+
self.validate_uid()
58+
url = f"{self.path}/{self.log_item_uid}"
59+
return self.client.get(url, headers = self.client.headers)
60+
61+
def validate_uid(self):
62+
if self.log_item_uid is None or '':
63+
raise ArgumentException('Log item Uid is required')
64+
65+

contentstack_management/stack/stack.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from ..workflows.workflows import Workflows
1111
from ..metadata.metadata import Metadata
1212
from ..roles.roles import Roles
13+
from ..auditlogs.auditlog import Auditlog
14+
1315

1416

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

332334
def roles(self, roles_uid: str = None):
333-
return Roles(self.client, roles_uid)
335+
return Roles(self.client, roles_uid)
336+
337+
def auditlog(self, log_item_uid: str = None):
338+
return Auditlog(self.client, log_item_uid)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
python-dotenv~=1.0.0
22
setuptools==68.0.0
33
requests~=2.31.0
4-
pylint
4+
pylint
5+
bson>=0.5.9

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
long_description_content_type="text/markdown",
1515
url="https://github.com/contentstack/contentstack-management-python",
1616
author="Sunil",
17-
author_email="sunil,lakshman@contentstack.com",
17+
author_email="sunil.lakshman@contentstack.com",
1818
license="MIT",
1919
classifiers=[
2020
"License :: OSI Approved :: MIT License",
2121
"Programming Language :: Python :: 3.10",
2222
"Operating System :: OS Independent",
2323
],
24-
install_requires=["bson >= 0.5.10"],
24+
install_requires=["bson >= 0.5.9", "requests >= 2.5.4"],
2525
extras_require={
2626
"dev": ["pytest>=7.0", "twine>=4.0.2", "dotenv>=0.0.5"],
2727
},
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import unittest
3+
4+
from dotenv import load_dotenv
5+
6+
from contentstack_management import contentstack
7+
from tests.cred import get_credentials
8+
9+
credentials = get_credentials()
10+
username = credentials["username"]
11+
password = credentials["password"]
12+
host = credentials["host"]
13+
api_key = credentials["api_key"]
14+
log_item_uid = credentials["log_item_uid"]
15+
16+
17+
class auditlogesApiTests(unittest.TestCase):
18+
19+
def setUp(self):
20+
self.client = contentstack.ContentstackClient(host=host)
21+
self.client.login(username, password)
22+
23+
def test_get_all_auditloges(self):
24+
response = self.client.stack(api_key).auditlog().find()
25+
self.assertEqual(response.status_code, 200)
26+
27+
def test_get_a_auditlog(self):
28+
response = self.client.stack(api_key).auditlog(log_item_uid).fetch()
29+
self.assertEqual(response.status_code, 200)
30+
31+
def test_get_all_auditloges_with_params(self):
32+
query = self.client.stack(api_key).auditlog()
33+
query.add_param("include_branch", True)
34+
response = query.find()
35+
self.assertEqual(response.status_code, 200)
36+
37+
def test_get_a_auditlog_with_params(self):
38+
query = self.client.stack(api_key).auditlog(log_item_uid)
39+
query.add_param("include_branch", True)
40+
response = query.fetch()
41+
self.assertEqual(response.status_code, 200)
42+
43+
if __name__ == '__main__':
44+
unittest.main()

tests/cred.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
default_rule_uid = "rule_uid" #Default rule uid
3030
default_metadata_uid = "metadata_uid" #Default metadata uid
3131
default_role_uid = "roles_uid" #Default roles uid
32+
default_log_item_uid = "log_item_uid" #Default roles uid
3233

3334

3435
def get_credentials():
@@ -63,7 +64,8 @@ def get_credentials():
6364
"workflow_uid": os.getenv("WORKFLOW_UID", default_workflow_uid),
6465
"rule_uid": os.getenv("RULE_UID", default_rule_uid),
6566
"metadata_uid": os.getenv("METADATA_UID", default_metadata_uid),
66-
"role_uid": os.getenv("ROLE_UID", default_role_uid)
67+
"role_uid": os.getenv("ROLE_UID", default_role_uid),
68+
"log_item_uid": os.getenv("LOG_ITEM_UID", default_log_item_uid)
6769

6870
}
6971
return credentials
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from dotenv import load_dotenv
6+
7+
from contentstack_management import contentstack
8+
from tests.cred import get_credentials
9+
10+
credentials = get_credentials()
11+
username = credentials["username"]
12+
password = credentials["password"]
13+
api_key = credentials["api_key"]
14+
host = credentials["host"]
15+
log_item_uid = credentials["log_item_uid"]
16+
17+
18+
class AuditlogMockTests(unittest.TestCase):
19+
20+
def setUp(self):
21+
22+
self.client = contentstack.ContentstackClient(host = host)
23+
self.client.login(username, password)
24+
25+
def read_file(self, file_name):
26+
file_path= f"tests/resources/mock_auditlogs/{file_name}"
27+
infile = open(file_path, 'r')
28+
data = infile.read()
29+
infile.close()
30+
return data
31+
32+
def test_mock_get_all_auditlogs(self):
33+
response = self.client.stack(api_key).auditlog().find().json()
34+
read_mock_auditlogs_data = self.read_file("find.json")
35+
mock_auditlogs_data = json.loads(read_mock_auditlogs_data)
36+
self.assertEqual(mock_auditlogs_data.keys(), response.keys())
37+
38+
def test_mock_get_a_auditlog(self):
39+
response = self.client.stack(api_key).auditlog(log_item_uid).fetch().json()
40+
read_mock_auditlogs_data = self.read_file("fetch.json")
41+
mock_auditlogs_data = json.loads(read_mock_auditlogs_data)
42+
self.assertEqual(mock_auditlogs_data.keys(), response.keys())
43+
44+
45+
if __name__ == '__main__':
46+
unittest.main()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"log": {
3+
"_id": "id",
4+
"uid": "log_uid",
5+
"stack": "stack_uid",
6+
"created_at": "2023-08-17T21:53:29.817Z",
7+
"created_by": "user_uid",
8+
"module": "global_field",
9+
"event_type": "update",
10+
"request_id": "request_id",
11+
"metadata": {
12+
"title": "First",
13+
"uid": "first",
14+
"version": 6
15+
},
16+
"remote_addr": "ip_address",
17+
"request": {
18+
"global_field": {
19+
"title": "First",
20+
"description": "",
21+
"schema": [
22+
{
23+
"display_name": "Name",
24+
"uid": "name",
25+
"data_type": "text",
26+
"multiple": false,
27+
"mandatory": false,
28+
"unique": false,
29+
"non_localizable": false
30+
},
31+
{
32+
"data_type": "text",
33+
"display_name": "Rich text editor",
34+
"uid": "description",
35+
"multiple": false,
36+
"mandatory": false,
37+
"unique": false,
38+
"non_localizable": false
39+
}
40+
]
41+
}
42+
},
43+
"response": {
44+
"notice": "Global Field updated successfully.",
45+
"global_field": {
46+
"created_at": "2023-07-13T08:14:10.772Z",
47+
"updated_at": "2023-08-17T21:53:29.794Z",
48+
"title": "First",
49+
"uid": "first",
50+
"_version": 6,
51+
"inbuilt_class": false,
52+
"schema": [
53+
{
54+
"display_name": "Name",
55+
"uid": "name",
56+
"data_type": "text",
57+
"multiple": false,
58+
"mandatory": false,
59+
"unique": false,
60+
"non_localizable": false
61+
},
62+
{
63+
"data_type": "text",
64+
"display_name": "Rich text editor",
65+
"uid": "description",
66+
"multiple": false,
67+
"mandatory": false,
68+
"unique": false,
69+
"non_localizable": false
70+
}
71+
],
72+
"last_activity": {},
73+
"maintain_revisions": true,
74+
"description": "",
75+
"DEFAULT_ACL": null,
76+
"SYS_ACL": null,
77+
"field_rules": null
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)