216 lines
6.8 KiB
PHP
216 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Bookmark;
|
|
use App\Models\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\Models\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('Test Description', $bookmark->description);
|
|
$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,
|
|
'description' => $bookmark->description,
|
|
'url' => $bookmark->url,
|
|
// 'favicon' => $bookmark->favicon,
|
|
]
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/** @test */
|
|
public function bookmark_url_are_required()
|
|
{
|
|
$this->actingAs($user = factory(\App\Models\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_retrieved_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',
|
|
'description' => 'Test Description',
|
|
'url' => 'https://portal.bricooli.fr',
|
|
'favicon' => 'https://portal.bricooli.fr/img/logo.svg',
|
|
];
|
|
}
|
|
}
|