Skip to content

Commit b20812a

Browse files
committed
Add create thread with unit tests and fix edit_thread and update tests
1 parent 65410b2 commit b20812a

File tree

2 files changed

+164
-5
lines changed

2 files changed

+164
-5
lines changed

loading_api_wrapper/api.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"podcast",
1414
"conversation",
1515
]
16-
1716
EDITORIAL_SORT = ["title"]
1817

1918

@@ -274,8 +273,51 @@ def edit_post(self, post_id, message):
274273
# Handle any other unknown status code.
275274
return response.json()
276275

277-
def create_thread(self, title, text, category, post_type):
278-
pass
276+
def create_thread(self, title, message, category_name, post_type=None):
277+
if category_name not in ["games", "other"]:
278+
return {"code": 400, "message": "Invalid forum category"}
279+
280+
if post_type and post_type not in EDITORIAL_POST_TYPES:
281+
return {"code": 400, "message": "Invalid post_type"}
282+
283+
if not post_type:
284+
post_type = "regular"
285+
286+
url = f"{API_URL}/{API_VERSION}/posts/"
287+
headers = {
288+
"User-Agent": USER_AGENT,
289+
"content-type": "application/x-www-form-urlencoded",
290+
}
291+
data = {
292+
"category": category_name,
293+
"postType": post_type,
294+
"title": title,
295+
"body": message,
296+
}
297+
response = requests.post(url, headers=headers, data=data, cookies=self._cookies)
298+
299+
# Validation errors. Happens when title or message is empty. Possibly in other cases too.
300+
if response.status_code == 400:
301+
return response.json()
302+
303+
# No auth token.
304+
if response.status_code == 401:
305+
return response.json()
306+
307+
if response.status_code == 201:
308+
return {
309+
"code": response.status_code,
310+
"message": "Thread created",
311+
"data": response.json(),
312+
}
313+
314+
# Handle any other unknown status code.
315+
return response.json()
279316

280317
def edit_thread(self, thread_id, message):
281-
return self.edit_post(thread_id, message)
318+
thread_data = self.edit_post(thread_id, message)
319+
320+
if thread_data["code"] == 200:
321+
thread_data["message"] = "Thread updated"
322+
323+
return thread_data

tests/test_loading_api_wrapper.py

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ def test_edit_post_failure_post_does_not_exist(self, mock_requests):
16521652
self.assertEqual(response, expected_response)
16531653

16541654
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1655-
def test_edit_post_failure_empty_thread_id(self, mock_authenticate):
1655+
def test_edit_post_failure_empty_message(self, mock_authenticate):
16561656
expected_response = {
16571657
"code": 400,
16581658
"message": '"message" is not allowed to be empty',
@@ -1764,3 +1764,120 @@ def test_create_post_success(self, mock_requests, mock_authenticate):
17641764
self.assertEqual(api._cookies, self.cookie_jar)
17651765
self.assertEqual(response.get("code"), 201)
17661766
self.assertEqual(response.get("data"), expected_response)
1767+
1768+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1769+
@patch("loading_api_wrapper.api.requests")
1770+
def test_create_thread_success(self, mock_requests, mock_authenticate):
1771+
status_code = 201
1772+
expected_response = {
1773+
"id": "000000000000000000000000",
1774+
"body": "updated message",
1775+
"postType": "regular",
1776+
"createdAt": "2022-01-01T00:00:00.000Z",
1777+
"updatedAt": "2022-01-02T00:00:00.000Z",
1778+
"parentId": "222222222222222222222222",
1779+
"userId": "111111111111111111111111",
1780+
"replies": 0,
1781+
}
1782+
1783+
mock_response = MagicMock()
1784+
mock_response.status_code = status_code
1785+
mock_response.json.return_value = expected_response
1786+
mock_requests.post.return_value = mock_response
1787+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1788+
1789+
api = LoadingApiWrapper("test@email.com", "password")
1790+
response = api.create_thread(
1791+
title="Hello",
1792+
message="My message",
1793+
category_name="other",
1794+
)
1795+
1796+
self.assertIsNotNone(api._cookies)
1797+
self.assertEqual(api._cookies, self.cookie_jar)
1798+
self.assertEqual(response.get("code"), 201)
1799+
self.assertDictEqual(response.get("data"), expected_response)
1800+
1801+
def test_create_thread_failure_invalid_category(self):
1802+
expected_response = {"code": 400, "message": "Invalid forum category"}
1803+
1804+
api = LoadingApiWrapper()
1805+
response = api.create_thread(
1806+
title="Hello",
1807+
message="My message",
1808+
category_name="invalid_category",
1809+
)
1810+
1811+
self.assertEqual(response, expected_response)
1812+
1813+
def test_create_thread_failure_invalid_post_type(self):
1814+
expected_response = {"code": 400, "message": "Invalid post_type"}
1815+
1816+
api = LoadingApiWrapper()
1817+
response = api.create_thread(
1818+
title="Hello",
1819+
message="My message",
1820+
category_name="other",
1821+
post_type="invalid_post_type",
1822+
)
1823+
1824+
self.assertEqual(response, expected_response)
1825+
1826+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1827+
@patch("loading_api_wrapper.api.requests")
1828+
def test_create_thread_failure_empty_title_or_message(
1829+
self, mock_requests, mock_authenticate
1830+
):
1831+
status_code = 400
1832+
expected_response = {
1833+
"code": status_code,
1834+
"message": "Validation error",
1835+
"errors": [
1836+
{
1837+
"field": "title",
1838+
"location": "body",
1839+
"messages": ['"title" is not allowed to be empty'],
1840+
"types": ["any.empty"],
1841+
},
1842+
{
1843+
"field": "body",
1844+
"location": "body",
1845+
"messages": ['"body" is not allowed to be empty'],
1846+
"types": ["any.empty"],
1847+
},
1848+
],
1849+
}
1850+
1851+
mock_response = MagicMock()
1852+
mock_response.status_code = status_code
1853+
mock_response.json.return_value = expected_response
1854+
mock_requests.post.return_value = mock_response
1855+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1856+
1857+
api = LoadingApiWrapper("test@email.com", "password")
1858+
response = api.create_thread(
1859+
title="",
1860+
message="",
1861+
category_name="other",
1862+
)
1863+
1864+
self.assertEqual(response, expected_response)
1865+
1866+
@patch("loading_api_wrapper.api.requests")
1867+
def test_create_thread_failure_no_auth_token(self, mock_requests):
1868+
status_code = 401
1869+
expected_response = {"code": status_code, "message": "No auth token"}
1870+
1871+
mock_response = MagicMock()
1872+
mock_response.status_code = status_code
1873+
mock_response.json.return_value = expected_response
1874+
mock_requests.post.return_value = mock_response
1875+
1876+
api = LoadingApiWrapper()
1877+
response = api.create_thread(
1878+
title="Hello",
1879+
message="My message",
1880+
category_name="other",
1881+
)
1882+
1883+
self.assertEqual(response, expected_response)

0 commit comments

Comments
 (0)