@@ -105,6 +105,60 @@ def test_update_like_only_toggles_value_not_project_or_author(self):
105105 self .assertEqual (like .project , self .project )
106106 self .assertFalse (like .like )
107107
108+ def test_project_like_toggle_requires_authentication (self ):
109+ response = self .client .post (
110+ reverse ("api_project_like_toggle" , kwargs = {"project_id" : self .project .id }),
111+ data = json .dumps ({"like" : True }),
112+ content_type = "application/json" ,
113+ )
114+
115+ self .assertIn (response .status_code , {401 , 403 })
116+ self .assertFalse (Like .objects .exists ())
117+
118+ def test_project_like_toggle_creates_like_and_returns_count (self ):
119+ self .client .force_login (self .user )
120+
121+ response = self .client .post (
122+ reverse ("api_project_like_toggle" , kwargs = {"project_id" : self .project .id }),
123+ data = json .dumps ({"like" : True }),
124+ content_type = "application/json" ,
125+ )
126+
127+ self .assertEqual (response .status_code , 200 )
128+ self .assertEqual (response .json ()["like" ], True )
129+ self .assertEqual (response .json ()["like_count" ], 1 )
130+ self .assertTrue (Like .objects .get (author = self .user , project = self .project ).like )
131+
132+ def test_project_like_toggle_updates_existing_like_and_returns_count (self ):
133+ Like .objects .create (author = self .user , project = self .project , like = True )
134+ Like .objects .create (author = self .other_user , project = self .project , like = True )
135+ self .client .force_login (self .user )
136+
137+ response = self .client .post (
138+ reverse ("api_project_like_toggle" , kwargs = {"project_id" : self .project .id }),
139+ data = json .dumps ({"like" : False }),
140+ content_type = "application/json" ,
141+ )
142+
143+ self .assertEqual (response .status_code , 200 )
144+ self .assertEqual (response .json ()["like" ], False )
145+ self .assertEqual (response .json ()["like_count" ], 1 )
146+ self .assertFalse (Like .objects .get (author = self .user , project = self .project ).like )
147+
148+ def test_project_like_toggle_toggles_when_like_value_is_omitted (self ):
149+ Like .objects .create (author = self .user , project = self .project , like = True )
150+ self .client .force_login (self .user )
151+
152+ response = self .client .post (
153+ reverse ("api_project_like_toggle" , kwargs = {"project_id" : self .project .id }),
154+ data = json .dumps ({}),
155+ content_type = "application/json" ,
156+ )
157+
158+ self .assertEqual (response .status_code , 200 )
159+ self .assertEqual (response .json ()["like" ], False )
160+ self .assertEqual (response .json ()["like_count" ], 0 )
161+
108162
109163class SearchProjectsApiTests (TestCase ):
110164 def test_search_projects_filters_to_public_active_projects_and_limits_results (self ):
0 commit comments