feat: add TextWidget
add TextWidget CRUD api add basic tests for API
This commit is contained in:
parent
13cfd2961f
commit
c66645cbdd
18 changed files with 536 additions and 1 deletions
92
tests/Unit/TextWidgetApiTest.php
Normal file
92
tests/Unit/TextWidgetApiTest.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
use App\Models\TextWidget;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
test('can list text widgets', function () {
|
||||
$user = User::factory()->create();
|
||||
TextWidget::factory()->count(3)->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson('/api/text-widgets');
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonCount(3);
|
||||
});
|
||||
|
||||
test('can create a text widget', function () {
|
||||
$admin = User::factory()->create(['email' => config('app.admin_email')]);
|
||||
|
||||
$this->actingAs($admin);
|
||||
|
||||
$data = [
|
||||
'title' => 'Sample Title',
|
||||
'content' => 'Sample Content',
|
||||
];
|
||||
|
||||
$response = $this->postJson('/api/text-widgets', $data);
|
||||
|
||||
$response->assertCreated()
|
||||
->assertJsonFragment($data);
|
||||
});
|
||||
|
||||
test('can show a text widget', function () {
|
||||
$user = User::factory()->create();
|
||||
$textWidget = TextWidget::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson("/api/text-widgets/{$textWidget->id}");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonFragment(['id' => $textWidget->id]);
|
||||
});
|
||||
|
||||
test('can update a text widget', function () {
|
||||
$user = User::factory()->create();
|
||||
$textWidget = TextWidget::factory()->create();
|
||||
$data = ['title' => 'Updated Title'];
|
||||
|
||||
$response = $this->actingAs($user)->putJson("/api/text-widgets/{$textWidget->id}", $data);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonFragment($data);
|
||||
});
|
||||
|
||||
test('can delete a text widget', function () {
|
||||
$user = User::factory()->create();
|
||||
$textWidget = TextWidget::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->deleteJson("/api/text-widgets/{$textWidget->id}");
|
||||
|
||||
$response->assertNoContent();
|
||||
|
||||
$this->assertDatabaseMissing('text_widgets', ['id' => $textWidget->id]);
|
||||
});
|
||||
|
||||
test('only admin can create text widgets', function () {
|
||||
$adminEmail = Config::get('app.admin_email');
|
||||
$user = User::factory()->create(['email' => $adminEmail]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson('/api/text-widgets', ['title' => 'Test', 'content' => 'Test content'])
|
||||
->assertCreated();
|
||||
|
||||
$nonAdmin = User::factory()->create(['email' => 'nonadmin@example.com']);
|
||||
|
||||
$this->actingAs($nonAdmin)
|
||||
->postJson('/api/text-widgets', ['title' => 'Test', 'content' => 'Test content'])
|
||||
->assertForbidden();
|
||||
});
|
||||
|
||||
test('authenticated users can read text widgets', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson('/api/text-widgets')
|
||||
->assertOk();
|
||||
|
||||
$this->getJson('/api/text-widgets')
|
||||
->assertOk();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue