Skip to content

Commit

Permalink
test: news policy
Browse files Browse the repository at this point in the history
  • Loading branch information
arthaud-proust committed Oct 27, 2023
1 parent 8e42888 commit 9ed315e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
10 changes: 2 additions & 8 deletions app/Policies/NewsPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function view(User $user, News $news): bool
{
if ($user->id === $news->user_id) {
return true;
}
}

return $user->pigeon
->newsInChest()
Expand Down Expand Up @@ -51,13 +51,7 @@ public function update(User $user, News $news): bool
*/
public function delete(User $user, News $news): bool
{

if ($user->id === $news->user_id) {
return true;
} else {
return false;
}

return $user->id === $news->user_id;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/News/NewsViewingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ public function test_can_view_when_logged_and_pigeon_got_the_news(): void
$response->assertStatus(Response::HTTP_OK);
$this->assertTrue($user->pigeon->news()->find($news)->message->is_read);
}

public function test_can_view_when_logged_and_author_of_the_news(): void
{
$user = User::factory()->create();
$news = News::factory(
[
'user_id' => $user->id,
]
)->create();

$response = $this->actingAs($user)->get(route('news.show', $news));

$response->assertStatus(Response::HTTP_OK);
}
}
23 changes: 21 additions & 2 deletions tests/Feature/Pigeon/PigeonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function test_a_pigeon_cannot_get_a_news_if_guest(): void

public function test_a_pigeon_cannot_get_a_news_if_travelling(): void
{

$user = User::factory()->create();
$user2 = User::factory()->create();
$news1 = News::factory(
Expand All @@ -61,7 +60,7 @@ public function test_a_pigeon_cannot_get_a_news_if_travelling(): void
$response->assertForbidden();
}

public function test_a_pigeon_can_get_a_news_if_not_travelling(): void
public function test_a_pigeon_cannot_get_a_news_without_user_coords(): void
{
$user = User::factory()->create();
$user2 = User::factory()->create();
Expand All @@ -75,6 +74,26 @@ public function test_a_pigeon_can_get_a_news_if_not_travelling(): void
->actingAs($user)
->post(route('pigeon.get-news', $news));

$response->assertSessionHasErrors(['lat', 'lng']);
}

public function test_a_pigeon_can_get_a_news_if_not_travelling(): void
{
$user = User::factory()->create();
$user2 = User::factory()->create();
$news = News::factory(
[
'user_id' => $user2->id,
]
)->create();

$response = $this
->actingAs($user)
->post(route('pigeon.get-news', $news), [
'lat' => 0,
'lng' => 0,
]);

$response->assertRedirect(route('news.index'));
$this->assertTrue($user->pigeon->isTravelling());
}
Expand Down

0 comments on commit 9ed315e

Please sign in to comment.