-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTest.php
More file actions
202 lines (167 loc) · 6.31 KB
/
Copy pathProjectTest.php
File metadata and controls
202 lines (167 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Enums\ProjectStatus;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class ProjectTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
#[Test]
public function it_requires_authentication(): void
{
$this->getJson(route('api.v1.projects.index'))->assertUnauthorized();
}
#[Test]
public function it_lists_only_the_projects_of_the_authenticated_user(): void
{
Project::factory(3)->for($this->user)->create();
Project::factory(2)->create();
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.index'))
->assertOk()
->assertJsonCount(3, 'data')
->assertJsonPath('meta.total', 3);
}
#[Test]
public function it_paginates_and_reports_the_page_meta(): void
{
Project::factory(7)->for($this->user)->create();
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.index', ['per_page' => 3]))
->assertOk()
->assertJsonCount(3, 'data')
->assertJsonPath('meta.per_page', 3)
->assertJsonPath('meta.total', 7)
->assertJsonPath('meta.last_page', 3);
}
#[Test]
public function it_filters_projects_by_status(): void
{
Project::factory(2)->active()->for($this->user)->create();
Project::factory(3)->archived()->for($this->user)->create();
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.index', ['status' => ProjectStatus::Archived->value]))
->assertOk()
->assertJsonCount(3, 'data')
->assertJsonPath('data.0.status', ProjectStatus::Archived->value);
}
#[Test]
public function it_rejects_an_unknown_status_filter(): void
{
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.index', ['status' => 'flying']))
->assertUnprocessable()
->assertJsonValidationErrors('status');
}
#[Test]
public function it_creates_a_project_owned_by_the_authenticated_user(): void
{
$this->actingAs($this->user, 'sanctum')
->postJson(route('api.v1.projects.store'), [
'name' => 'Assessment API',
'description' => 'Task management system',
])
->assertCreated()
->assertJsonPath('data.name', 'Assessment API')
->assertJsonPath('data.status', ProjectStatus::Active->value)
->assertJsonPath('data.tasks_count', 0);
$this->assertDatabaseHas('projects', [
'name' => 'Assessment API',
'user_id' => $this->user->id,
]);
}
#[Test]
public function it_ignores_a_user_id_sent_by_the_client(): void
{
$victim = User::factory()->create();
$this->actingAs($this->user, 'sanctum')
->postJson(route('api.v1.projects.store'), [
'name' => 'Hijack attempt',
'user_id' => $victim->id,
])
->assertCreated();
$this->assertDatabaseHas('projects', [
'name' => 'Hijack attempt',
'user_id' => $this->user->id,
]);
}
#[Test]
public function it_validates_the_payload(): void
{
$this->actingAs($this->user, 'sanctum')
->postJson(route('api.v1.projects.store'), ['description' => 'no name'])
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
#[Test]
public function it_shows_a_project_with_its_task_count(): void
{
$project = Project::factory()->for($this->user)->create();
Task::factory(4)->for($project)->create();
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.show', $project))
->assertOk()
->assertJsonPath('data.id', $project->id)
->assertJsonPath('data.tasks_count', 4);
}
#[Test]
public function it_updates_only_the_provided_fields(): void
{
$project = Project::factory()->active()->for($this->user)->create(['name' => 'Original']);
$this->actingAs($this->user, 'sanctum')
->patchJson(route('api.v1.projects.update', $project), [
'status' => ProjectStatus::Completed->value,
])
->assertOk()
->assertJsonPath('data.name', 'Original')
->assertJsonPath('data.status', ProjectStatus::Completed->value);
}
#[Test]
public function it_soft_deletes_a_project_together_with_its_tasks(): void
{
$project = Project::factory()->for($this->user)->create();
$task = Task::factory()->for($project)->create();
$this->actingAs($this->user, 'sanctum')
->deleteJson(route('api.v1.projects.destroy', $project))
->assertOk()
->assertJsonPath('message', 'Project deleted successfully.');
$this->assertSoftDeleted($project);
$this->assertSoftDeleted($task);
}
#[Test]
public function it_hides_a_deleted_project_from_later_requests(): void
{
$project = Project::factory()->for($this->user)->create();
$project->delete();
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.show', $project))
->assertNotFound()
->assertJsonPath('message', 'Resource not found.');
}
#[Test]
public function it_forbids_touching_a_project_of_another_user(): void
{
$project = Project::factory()->create();
$this->actingAs($this->user, 'sanctum')
->getJson(route('api.v1.projects.show', $project))
->assertForbidden();
$this->actingAs($this->user, 'sanctum')
->patchJson(route('api.v1.projects.update', $project), ['name' => 'Taken over'])
->assertForbidden();
$this->actingAs($this->user, 'sanctum')
->deleteJson(route('api.v1.projects.destroy', $project))
->assertForbidden();
$this->assertNotSoftDeleted($project);
}
}