Skip to content

Commit

Permalink
Episode 31
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Apr 27, 2017
1 parent a7aee70 commit 8a388cc
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Http/Controllers/RepliesController.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Reply;
use App\Thread;

class RepliesController extends Controller
Expand Down Expand Up @@ -32,4 +33,19 @@ public function store($channelId, Thread $thread)

return back()->with('flash', 'Your reply has been left.');
}

/**
* Delete the given reply.
*
* @param Reply $reply
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Reply $reply)
{
$this->authorize('update', $reply);

$reply->delete();

return back();
}
}
24 changes: 24 additions & 0 deletions app/Policies/ReplyPolicy.php
@@ -0,0 +1,24 @@
<?php

namespace App\Policies;

use App\Reply;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class ReplyPolicy
{
use HandlesAuthorization;

/**
* Determine if the authenticated user has permission to update a reply.
*
* @param User $user
* @param Reply $reply
* @return bool
*/
public function update(User $user, Reply $reply)
{
return $reply->user_id == $user->id;
}
}
1 change: 1 addition & 0 deletions app/Providers/AuthServiceProvider.php
Expand Up @@ -14,6 +14,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
'App\Thread' => 'App\Policies\ThreadPolicy',
'App\Reply' => 'App\Policies\ReplyPolicy',
];

/**
Expand Down
11 changes: 11 additions & 0 deletions resources/views/threads/reply.blade.php
Expand Up @@ -23,4 +23,15 @@
{{ $reply->body }}
</div>

@can ('update', $reply)
<div class="panel-footer">
<form method="POST" action="/replies/{{ $reply->id }}">
{{ csrf_field() }}
{{ method_field('DELETE') }}

<button type="submit" class="btn btn-danger btn-xs">Delete</button>
</form>
</div>
@endcan

</div>
1 change: 1 addition & 0 deletions routes/web.php
Expand Up @@ -25,6 +25,7 @@
Route::post('threads', 'ThreadsController@store');
Route::get('threads/{channel}', 'ThreadsController@index');
Route::post('/threads/{channel}/{thread}/replies', 'RepliesController@store');
Route::delete('/replies/{reply}', 'RepliesController@destroy');
Route::post('/replies/{reply}/favorites', 'FavoritesController@store');

Route::get('/profiles/{user}', 'ProfilesController@show')->name('profile');
26 changes: 26 additions & 0 deletions tests/Feature/ParticipateInThreadsTest.php
Expand Up @@ -42,4 +42,30 @@ function a_reply_requires_a_body()
$this->post($thread->path() . '/replies', $reply->toArray())
->assertSessionHasErrors('body');
}

/** @test */
function unauthorized_users_cannot_delete_replies()
{
$this->withExceptionHandling();

$reply = create('App\Reply');

$this->delete("/replies/{$reply->id}")
->assertRedirect('login');

$this->signIn()
->delete("/replies/{$reply->id}")
->assertStatus(403);
}

/** @test */
function authorized_users_can_delete_replies()
{
$this->signIn();
$reply = create('App\Reply', ['user_id' => auth()->id()]);

$this->delete("/replies/{$reply->id}")->assertStatus(302);

$this->assertDatabaseMissing('replies', ['id' => $reply->id]);
}
}

1 comment on commit 8a388cc

@payafterwork
Copy link

@payafterwork payafterwork commented on 8a388cc Jun 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting
"Expected status code 403 but received 404.
Failed asserting that false is true."
and
"Expected status code 302 but received 404.
Failed asserting that false is true."
for those 2 tests on writing the exact code.
But works fine in browser and deletes in the database as well, only these test fail.
Can you please help me know as to why that is?

Please sign in to comment.