11defmodule Tilex.PostControllerTest do
22 use TilexWeb.ConnCase , async: true
3- alias Tilex.Factory
3+
44 alias Tilex.Auth
5+ alias Tilex.Blog.Post
6+ alias Tilex.Factory
7+ alias Tilex.Repo
58
69 test "lists all entries on index" , % { conn: conn } do
710 conn = get ( conn , Routes . post_path ( conn , :index ) )
@@ -55,7 +58,7 @@ defmodule Tilex.PostControllerTest do
5558 post ( conn , Routes . post_path ( conn , :create , params ) )
5659
5760 til =
58- Tilex.Blog. Post
61+ Post
5962 |> Tilex.Repo . all ( )
6063 |> List . first ( )
6164
@@ -79,7 +82,7 @@ defmodule Tilex.PostControllerTest do
7982 post ( conn , Routes . post_path ( conn , :create , params ) )
8083
8184 til =
82- Tilex.Blog. Post
85+ Post
8386 |> Tilex.Repo . all ( )
8487 |> List . first ( )
8588
@@ -102,7 +105,7 @@ defmodule Tilex.PostControllerTest do
102105 post ( conn , Routes . post_path ( conn , :create , params ) )
103106
104107 til =
105- Tilex.Blog. Post
108+ Post
106109 |> Tilex.Repo . all ( )
107110 |> List . first ( )
108111
@@ -133,7 +136,7 @@ defmodule Tilex.PostControllerTest do
133136 put ( conn , Routes . post_path ( conn , :update , til . slug , params ) )
134137
135138 til =
136- Tilex.Blog. Post
139+ Post
137140 |> Tilex.Repo . get ( til . id )
138141
139142 assert til . title == "New Title"
@@ -162,7 +165,7 @@ defmodule Tilex.PostControllerTest do
162165 put ( conn , Routes . post_path ( conn , :update , til . slug , params ) )
163166
164167 til =
165- Tilex.Blog. Post
168+ Post
166169 |> Tilex.Repo . get ( til . id )
167170
168171 assert til . title == "New Title"
@@ -190,18 +193,82 @@ defmodule Tilex.PostControllerTest do
190193 put ( conn , Routes . post_path ( conn , :update , til . slug , params ) )
191194
192195 til =
193- Tilex.Blog. Post
196+ Post
194197 |> Tilex.Repo . get ( til . id )
195198
196199 assert til . title == "New Title"
197200 assert til . max_likes == 1
198201 end
202+
203+ test "author can delete their own post" , % {
204+ conn: conn ,
205+ current_user: current_user
206+ } do
207+ post = Factory . insert! ( :post , developer: current_user )
208+
209+ conn = delete ( conn , Routes . post_path ( conn , :delete , post . slug ) )
210+
211+ assert redirected_to ( conn ) == Routes . developer_path ( conn , :show , current_user )
212+ assert get_flash ( conn , :info ) == "Post deleted successfully"
213+ assert Repo . all ( Post ) == [ ]
214+ end
215+
216+ test "author cannot delete another developer's post" , % {
217+ conn: conn
218+ } do
219+ other_user = Factory . insert! ( :developer , username: "other-dev" )
220+ post = Factory . insert! ( :post , developer: other_user )
221+ post_id = post . id
222+
223+ assert_raise Ecto.NoResultsError , fn ->
224+ delete ( conn , Routes . post_path ( conn , :delete , post . slug ) )
225+ end
226+
227+ assert [ % Post { id: ^ post_id } ] = Repo . all ( Post )
228+ end
199229 end
200230
201- defp authenticated_conn ( % { conn: conn } ) do
202- current_user = Factory . insert! ( :developer , email: "current@example.com" , username: "current" )
203- channel = Factory . insert! ( :channel , name: "git" )
231+ describe "when authenticated as admin" do
232+ setup :authenticated_admin_conn
204233
234+ test "admin can delete any post" , % {
235+ conn: conn ,
236+ current_user: _admin
237+ } do
238+ other_user = Factory . insert! ( :developer , username: "other-dev" )
239+ post = Factory . insert! ( :post , developer: other_user )
240+
241+ conn = delete ( conn , Routes . post_path ( conn , :delete , post . slug ) )
242+
243+ assert redirected_to ( conn ) == Routes . developer_path ( conn , :show , other_user )
244+ assert get_flash ( conn , :info ) == "Post deleted successfully"
245+ assert Repo . all ( Post ) == [ ]
246+ end
247+ end
248+
249+ test "unauthenticated user cannot delete post" , % { conn: conn } do
250+ post = Factory . insert! ( :post )
251+ post_id = post . id
252+
253+ conn = delete ( conn , Routes . post_path ( conn , :delete , post . slug ) )
254+
255+ assert html_response ( conn , 302 )
256+ assert get_flash ( conn , :info ) == "Authentication required"
257+ assert [ % Post { id: ^ post_id } ] = Repo . all ( Post )
258+ end
259+
260+ defp authenticated_conn ( context ) do
261+ Factory . insert! ( :developer , email: "current@example.com" , username: "current" )
262+ |> do_authenticated_conn ( context )
263+ end
264+
265+ defp authenticated_admin_conn ( context ) do
266+ Factory . insert! ( :developer , email: "admin@example.com" , username: "admin" , admin: true )
267+ |> do_authenticated_conn ( context )
268+ end
269+
270+ defp do_authenticated_conn ( current_user , % { conn: conn } ) do
271+ channel = Factory . insert! ( :channel , name: "git" )
205272 conn = Auth.Guardian.Plug . sign_in ( conn , current_user )
206273 { :ok , conn: conn , current_user: current_user , channel: channel }
207274 end
0 commit comments