@@ -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