Skip to content

Commit c37c22f

Browse files
committed
Add tests for create_post and edit_post
1 parent eea4118 commit c37c22f

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

tests/test_loading_api_wrapper.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,3 +1550,174 @@ def test_get_editorials_failure_page_too_high(self, mock_requests):
15501550
)
15511551

15521552
self.assertDictEqual(response, expected_response)
1553+
1554+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1555+
@patch("loading_api_wrapper.api.requests")
1556+
def test_edit_post_success(self, mock_requests, mock_authenticate):
1557+
status_code = 200
1558+
expected_response = {
1559+
"id": "000000000000000000000000",
1560+
"body": "updated message",
1561+
"postType": "regular",
1562+
"createdAt": "2022-01-01T00:00:00.000Z",
1563+
"updatedAt": "2022-01-02T00:00:00.000Z",
1564+
"parentId": "222222222222222222222222",
1565+
"userId": "111111111111111111111111",
1566+
"replies": 0,
1567+
"edits": 1,
1568+
"lastEdit": "2022-01-02T00:00:00.000Z",
1569+
}
1570+
1571+
mock_response = MagicMock()
1572+
mock_response.status_code = status_code
1573+
mock_response.json.return_value = expected_response
1574+
mock_requests.patch.return_value = mock_response
1575+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1576+
1577+
api = LoadingApiWrapper("test@email.com", "password")
1578+
response = api.edit_post(
1579+
post_id="000000000000000000000000",
1580+
message="updated message",
1581+
)
1582+
1583+
self.assertIsNotNone(api._cookies)
1584+
self.assertEqual(api._cookies, self.cookie_jar)
1585+
self.assertEqual(response.get("code"), 200)
1586+
self.assertDictEqual(response.get("data"), expected_response)
1587+
1588+
@patch("loading_api_wrapper.api.requests")
1589+
def test_edit_post_failure_no_auth_token(self, mock_requests):
1590+
status_code = 401
1591+
expected_response = {"code": status_code, "message": "No auth token"}
1592+
1593+
mock_response = MagicMock()
1594+
mock_response.status_code = status_code
1595+
mock_response.json.return_value = expected_response
1596+
mock_requests.patch.return_value = mock_response
1597+
1598+
api = LoadingApiWrapper()
1599+
response = api.edit_post(post_id="post_id_to_edit", message="updated message")
1600+
1601+
self.assertEqual(response, expected_response)
1602+
1603+
@patch("loading_api_wrapper.api.requests")
1604+
def test_edit_post_failure_post_does_not_exist(self, mock_requests):
1605+
status_code = 404
1606+
expected_response = {"code": status_code, "message": "Post does not exist"}
1607+
1608+
mock_response = MagicMock()
1609+
mock_response.status_code = status_code
1610+
mock_response.json.return_value = expected_response
1611+
mock_requests.patch.return_value = mock_response
1612+
1613+
api = LoadingApiWrapper()
1614+
response = api.edit_post(
1615+
post_id="non_existing_post_id", message="new updated message"
1616+
)
1617+
1618+
self.assertEqual(response, expected_response)
1619+
1620+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1621+
def test_edit_post_failure_empty_thread_id(self, mock_authenticate):
1622+
expected_response = {
1623+
"code": 400,
1624+
"message": '"message" is not allowed to be empty',
1625+
}
1626+
1627+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1628+
1629+
api = LoadingApiWrapper("test@email.com", "password")
1630+
response = api.edit_post(post_id="existing_post_id", message="")
1631+
1632+
self.assertIsNotNone(api._cookies)
1633+
self.assertEqual(api._cookies, self.cookie_jar)
1634+
self.assertEqual(response, expected_response)
1635+
1636+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1637+
def test_create_post_failure_empty_thread_id(self, mock_authenticate):
1638+
expected_response = {
1639+
"code": 400,
1640+
"message": '"thread_id" is not allowed to be empty',
1641+
}
1642+
1643+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1644+
1645+
api = LoadingApiWrapper("test@email.com", "password")
1646+
response = api.create_post(thread_id="", message="New message")
1647+
1648+
self.assertIsNotNone(api._cookies)
1649+
self.assertEqual(api._cookies, self.cookie_jar)
1650+
self.assertEqual(response, expected_response)
1651+
1652+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1653+
@patch("loading_api_wrapper.api.requests")
1654+
def test_create_post_failure_thread_id_does_not_exist(
1655+
self, mock_requests, mock_authenticate
1656+
):
1657+
status_code = 404
1658+
expected_response = {"code": status_code, "message": "Post does not exist"}
1659+
1660+
mock_response = MagicMock()
1661+
mock_response.status_code = status_code
1662+
mock_response.json.return_value = expected_response
1663+
mock_requests.post.return_value = mock_response
1664+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1665+
1666+
api = LoadingApiWrapper("test@email.com", "password")
1667+
response = api.create_post(
1668+
thread_id="non_existing_thread_id",
1669+
message="New message",
1670+
)
1671+
1672+
self.assertIsNotNone(api._cookies)
1673+
self.assertEqual(api._cookies, self.cookie_jar)
1674+
self.assertEqual(response, expected_response)
1675+
1676+
@patch("loading_api_wrapper.api.requests")
1677+
def test_create_post_failure_no_auth_token(self, mock_requests):
1678+
status_code = 401
1679+
expected_response = {"code": status_code, "message": "No auth token"}
1680+
1681+
mock_response = MagicMock()
1682+
mock_response.status_code = status_code
1683+
mock_response.json.return_value = expected_response
1684+
mock_requests.post.return_value = mock_response
1685+
1686+
api = LoadingApiWrapper()
1687+
response = api.create_post(
1688+
thread_id="existing_thread_id", message="New message"
1689+
)
1690+
1691+
self.assertEqual(response, expected_response)
1692+
1693+
@patch("loading_api_wrapper.api.LoadingApiWrapper._authenticate")
1694+
@patch("loading_api_wrapper.api.requests")
1695+
def test_create_post_success(self, mock_requests, mock_authenticate):
1696+
status_code = 201
1697+
expected_response = {
1698+
"id": "000000000000000000000000",
1699+
"body": "New message!",
1700+
"postType": "regular",
1701+
"createdAt": "2022-01-01T00:00:00.000Z",
1702+
"updatedAt": "2022-01-02T00:00:00.000Z",
1703+
"parentId": "111111111111111111111111",
1704+
"userId": "222222222222222222222222",
1705+
"replies": 0,
1706+
}
1707+
1708+
mock_response = MagicMock()
1709+
mock_response.status_code = status_code
1710+
mock_response.json.return_value = expected_response
1711+
mock_requests.post.return_value = mock_response
1712+
mock_authenticate.return_value = {"code": 200, "cookies": self.cookie_jar}
1713+
1714+
api = LoadingApiWrapper("test@email.com", "password")
1715+
response = api.create_post(
1716+
thread_id="111111111111111111111111",
1717+
message="New message!",
1718+
)
1719+
1720+
self.assertIsNotNone(api._cookies)
1721+
self.assertEqual(api._cookies, self.cookie_jar)
1722+
self.assertEqual(response.get("code"), 201)
1723+
self.assertEqual(response.get("data"), expected_response)

0 commit comments

Comments
 (0)