Skip to content

Commit 764fbd8

Browse files
committed
[Tests] Add test for rejecting client id that already exists
1 parent e7a808e commit 764fbd8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

tests/dummy/database/factories/VideoFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace Database\Factories;
2121

22+
use App\Models\User;
2223
use App\Models\Video;
2324
use Illuminate\Database\Eloquent\Factories\Factory;
2425

@@ -39,6 +40,7 @@ class VideoFactory extends Factory
3940
public function definition()
4041
{
4142
return [
43+
'owner_id' => User::factory(),
4244
'title' => $this->faker->words(3, true),
4345
'url' => $this->faker->url,
4446
];

tests/dummy/tests/Api/V1/Videos/CreateTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use App\Models\Tag;
2323
use App\Models\Video;
2424
use App\Tests\Api\V1\TestCase;
25-
use App\Models\User;
2625
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
2726
use LaravelJsonApi\Core\Document\ResourceObject;
2827

@@ -54,7 +53,7 @@ public function test(): void
5453

5554
$response = $this
5655
->withoutExceptionHandling()
57-
->actingAs($user = User::factory()->create())
56+
->actingAs($user = $video->owner)
5857
->jsonApi('videos')
5958
->withData($data)
6059
->post('/api/v1/videos');
@@ -96,7 +95,7 @@ public function testClientId(): void
9695

9796
$response = $this
9897
->withoutExceptionHandling()
99-
->actingAs(User::factory()->create())
98+
->actingAs($video->owner)
10099
->jsonApi('videos')
101100
->withData($data)
102101
->post('/api/v1/videos');
@@ -112,6 +111,29 @@ public function testClientId(): void
112111
$this->assertDatabaseCount('taggables', 2);
113112
}
114113

114+
public function testClientIdAlreadyExists(): void
115+
{
116+
$video = Video::factory()->make();
117+
$existing = Video::factory()->create();
118+
119+
$data = $this
120+
->serialize($video)
121+
->withId($id = $existing->getRouteKey());
122+
123+
$response = $this
124+
->actingAs($video->owner)
125+
->jsonApi('videos')
126+
->withData($data)
127+
->post('/api/v1/videos');
128+
129+
$response->assertExactErrorStatus([
130+
'detail' => "Resource {$id} already exists.",
131+
'source' => ['pointer' => '/data/id'],
132+
'status' => '409',
133+
'title' => 'Conflict',
134+
]);
135+
}
136+
115137
public function testInvalidClientId(): void
116138
{
117139
$video = Video::factory()->make();
@@ -121,7 +143,7 @@ public function testInvalidClientId(): void
121143
->withId('123456');
122144

123145
$response = $this
124-
->actingAs(User::factory()->create())
146+
->actingAs($video->owner)
125147
->jsonApi('videos')
126148
->withData($data)
127149
->post('/api/v1/videos');

0 commit comments

Comments
 (0)