add bookmark back

This commit is contained in:
2020-05-09 18:37:31 +02:00
parent 2f8e8ca378
commit a1b8962fe6
12 changed files with 520 additions and 0 deletions

View File

@@ -0,0 +1,212 @@
<?php
namespace Tests\Feature;
use App\Models\Bookmark;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class BookmarkTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function an_unauthenticated_user_can_add_a_bookmark()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory(\App\User::class)->create(), 'api');
$response = $this->post('/api/bookmarks', $this->data());
$bookmark = Bookmark::first();
$this->assertCount(1, Bookmark::all());
$this->assertEquals('Test Name', $bookmark->name);
$this->assertEquals('https://portal.bricooli.fr', $bookmark->url);
$this->assertEquals('https://portal.bricooli.fr/img/logo.svg', $bookmark->favicon);
$response->assertStatus(201);
$response->assertJson([
'data' => [
'type' => 'bookmark',
'bookmark_id' => $bookmark->id,
'attributes' => [
'data' => [
'name' => $bookmark->name,
'url' => $bookmark->url,
'favicon' => $bookmark->favicon,
]
],
],
]);
}
/** @test */
public function bookmark_url_are_required()
{
$this->actingAs($user = factory(\App\User::class)->create(), 'api');
$response = $this->post('/api/bookmarks', array_merge($this->data(), ['url' => '']));
$response->assertSessionHasErrors('url');
$this->assertCount(0, Bookmark::all());
}
/** @test */
public function a_bookmark_can_be_retrieved()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$bookmark = factory(Bookmark::class)->create(['user_id' => $user->id]);
$response = $this->get('/api/bookmarks/' . $bookmark->id );
$response->assertJson([
'data' => [
'bookmark_id' => $bookmark->id,
'attributes' => [
'data' => [
'name' => $bookmark->name,
'url' => $bookmark->url,
'favicon' => $bookmark->favicon,
'last_updated' => $bookmark->updated_at->diffForHumans(),
]
],
],
]);
}
/** @test */
public function a_user_can_retrueved_all_this_to_do_lists()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$bookmarkOne = factory(Bookmark::class)->create(['user_id' => $user->id]);
$bookmarkTwo = factory(Bookmark::class)->create(['user_id' => $user->id]);
$response = $this->get('/api/bookmarks');
$response->assertJson([
'data' => [
[
'data' => [
'bookmark_id' => $bookmarkOne->id,
'attributes' => [
'data' => [
'name' => $bookmarkOne->name,
'last_updated' => $bookmarkOne->updated_at->diffForHumans(),
]
],
]
],
[
'data' => [
'bookmark_id' => $bookmarkTwo->id,
'attributes' => [
'data' => [
'name' => $bookmarkTwo->name,
'last_updated' => $bookmarkTwo->updated_at->diffForHumans(),
]
],
],
]
],
'links' => [
'self' => url('/bookmarks'),
]
]);
}
/** @test */
public function only_owner_bookmark_can_retrieved_it()
{
$user = factory(User::class)->create();
$bookmark = factory(Bookmark::class)->create(['user_id' => $user->id]);
$this->actingAs($userAnother = factory(User::class)->create(), 'api');
$response = $this->get('/api/bookmarks/' . $bookmark->id );
$response->assertStatus(403);
}
/** @test */
public function a_to_bookmark_can_be_patch()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory(User::class)->create(), 'api');
$bookmark = factory(Bookmark::class)->create(['user_id' => $user->id]);
$response = $this->patch('/api/bookmarks/' . $bookmark->id, [
'name' => 'Bookmark Update',
'url' => 'https://portal.bricooli.fr',
]);
$bookmark = $bookmark->fresh();
$this->assertEquals('Bookmark Update', $bookmark->name);
$this->assertEquals('https://portal.bricooli.fr', $bookmark->url);
$response->assertStatus(200);
$response->assertJson([
'data' => [
'bookmark_id' => $bookmark->id,
'attributes' => [
'data' => [
'name' => 'Bookmark Update'
]
]
]
]);
}
/** @test */
public function only_the_owner_can_patch_the_bookmark()
{
$user = factory(User::class)->create();
$bookmark = factory(Bookmark::class)->create(['id' => 123, 'user_id' => $user->id]);
$this->actingAs($anotherUser = factory(User::class)->create(), 'api');
$this->patch('/api/bookmarks/'. $bookmark->id, ['url' => 'https://portal.bricooli.fr'])
->assertStatus(403);
}
/** @test */
public function a_bookmark_can_be_delete()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$bookmark = factory(Bookmark::class)->create(['user_id' => $user->id]);
$response = $this->delete('/api/bookmarks/' . $bookmark->id);
$toDoList = $bookmark->fresh();
$this->assertCount(0, Bookmark::all());
$response->assertStatus(204);
}
/** @test */
public function only_the_owner_can_delete_the_bookmark()
{
$user = factory(User::class)->create();
$bookmark = factory(Bookmark::class)->create();
$this->actingAs($anotherUser = factory(User::class)->create(), 'api');
$response = $this->delete('/api/bookmarks/' . $bookmark->id);
$response->assertStatus(403);
}
private function data()
{
return [
'name' => 'Test Name',
'url' => 'https://portal.bricooli.fr',
'favicon' => 'https://portal.bricooli.fr/img/logo.svg',
];
}
}