Skip to content

Commit 5cc99a3

Browse files
authored
[tests] add sketch related tests to the API unit tests (#3312)
* [tests] add sketch related tests to the API unit tests Adding new unit tests to API resources_test to: * create a sketch * add a label to a Sketch * attempt to delete an acled Sketch * attempt to delete a non existent sketch * attempt to delete a protected sketch * Update resources_test.py * fix * black * attempt to fix tests
1 parent 06d83a2 commit 5cc99a3

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# limitations under the License.
1515
"""This is the setup file for the project. The standard setup rules apply:
1616
17-
python setup.py build
18-
sudo python setup.py install
17+
python setup.py build
18+
sudo python setup.py install
1919
"""
2020

2121
from __future__ import print_function

timesketch/api/v1/resources_test.py

+93
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,99 @@ def test_sketch_acl(self):
114114
response = self.client.get("/api/v1/sketches/2/")
115115
self.assert403(response)
116116

117+
def test_create_a_sketch(self):
118+
"""Authenticated request to create a sketch."""
119+
self.login()
120+
data = dict(
121+
name="test_create_a_sketch",
122+
description="test_create_a_sketch",
123+
)
124+
response = self.client.post(
125+
"/api/v1/sketches/",
126+
data=json.dumps(data, ensure_ascii=False),
127+
content_type="application/json",
128+
)
129+
self.assertEqual(HTTP_STATUS_CODE_CREATED, response.status_code)
130+
131+
# check the created sketch
132+
133+
response = self.client.get("/api/v1/sketches/")
134+
self.assertEqual(len(response.json["objects"]), 3)
135+
self.assertIn(b"test_create_a_sketch", response.data)
136+
self.assert200(response)
137+
138+
def test_append_label_to_sketch(self):
139+
"""Authenticated request to append a label to a sketch."""
140+
self.login()
141+
142+
data = dict(
143+
labels=["test_append_label_to_sketch"],
144+
label_action="add",
145+
)
146+
147+
response = self.client.post(
148+
"/api/v1/sketches/3/",
149+
data=json.dumps(data, ensure_ascii=False),
150+
content_type="application/json",
151+
)
152+
153+
self.assertEqual(response.status_code, HTTP_STATUS_CODE_CREATED)
154+
155+
# check the result in content
156+
response = self.client.get("/api/v1/sketches/3/")
157+
self.assertEqual(len(response.json["objects"]), 1)
158+
self.assertEqual(response.json["objects"][0]["name"], "Test 3")
159+
self.assertIn(
160+
"test_append_label_to_sketch", response.json["objects"][0]["label_string"]
161+
)
162+
self.assert200(response)
163+
164+
def test_sketch_delete_not_existant_sketch(self):
165+
"""Authenticated request to delete a sketch that does not exist."""
166+
self.login()
167+
response = self.client.delete("/api/v1/sketches/99/")
168+
self.assert404(response)
169+
170+
def test_sketch_delete_no_acl(self):
171+
"""Authenticated request to delete a sketch that the User has no read
172+
permission on.
173+
"""
174+
self.login()
175+
response = self.client.delete("/api/v1/sketches/2/")
176+
self.assert403(response)
177+
178+
def test_attempt_to_delete_protected_sketch(self):
179+
"""Authenticated request to delete a protected sketch."""
180+
self.login()
181+
data = dict(
182+
name="test_attempt_to_delete_protected_sketch",
183+
description="test_attempt_to_delete_protected_sketch",
184+
)
185+
response = self.client.post(
186+
"/api/v1/sketches/",
187+
data=json.dumps(data, ensure_ascii=False),
188+
content_type="application/json",
189+
)
190+
self.assertEqual(HTTP_STATUS_CODE_CREATED, response.status_code)
191+
data = dict(
192+
labels=["protected"],
193+
label_action="add",
194+
)
195+
response = self.client.post(
196+
"/api/v1/sketches/4/",
197+
data=json.dumps(data, ensure_ascii=False),
198+
content_type="application/json",
199+
)
200+
201+
self.assertEqual(
202+
response.json["objects"][0]["name"],
203+
"test_attempt_to_delete_protected_sketch",
204+
)
205+
self.assertIn("protected", response.json["objects"][0]["label_string"])
206+
207+
response = self.client.delete("/api/v1/sketches/4/")
208+
self.assert403(response)
209+
117210

118211
class ViewListResourceTest(BaseTest):
119212
"""Test ViewListResource."""

0 commit comments

Comments
 (0)