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
|
<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Appointment;
use App\Models\Company;
use App\Models\Student;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AppointmentApiTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_appointments()
{
// Getting all the appointments is something only admins can do
$admin = Admin::factory()->create();
Appointment::factory()->count(10)->create();
$response = $this->actingAs($admin)->getJson("/api/appointments");
$response->assertStatus(200);
}
public function test_index_should_return_false_for_non_admin()
{
// Students and companies should not be able to access the index endpoint
$company = Company::factory()->create();
$response = $this->actingAs($company)->getJson('/api/appointments');
$response->assertStatus(403)
->assertJsonFragment(['message' => 'This action is unauthorized.']);
}
public function test_show_returns_appointment()
{
$student = Student::factory()->create();
$appointment = Appointment::factory()->create();
$response = $this->actingAs($student)->getJson("/api/appointments/{$appointment->id}");
$response->assertStatus(200)
->assertJsonFragment(['id' => $appointment->id]);
}
public function test_store_creates_appointment()
{
$student = Student::factory()->create();
$company = Company::factory()->create();
$data = [
'student_id' => $student->id,
'company_id' => $company->id,
'time_start' => "15:00",
'time_end' => "15:30"
];
$response = $this->actingAs($student)->postJson('/api/appointments', $data);
$response->assertStatus(201)
->assertJsonFragment(['student_id' => $student->id]);
$this->assertDatabaseHas('appointments', $data);
}
public function test_store_prevents_double_booking()
{
$student = Student::factory()->create();
$company = Company::factory()->create();
$time_start = '18:00';
$time_end = '18:30';
Appointment::factory()->create([
'student_id' => $student->id,
'company_id' => $company->id,
'time_start' => "18:00",
'time_end' => "18:30"
]);
$response = $this->actingAs($student)->postJson('/api/appointments', [
'student_id' => $student->id,
'company_id' => $company->id,
'time_start' => $time_start,
'time_end' => $time_end
]);
$response->assertStatus(400)
->assertJsonFragment(['message' => 'That time slot is already being used']);
}
public function test_show_returns_404_for_missing()
{
$student = Student::factory()->create();
$response = $this->actingAs($student)->getJson('/api/appointments/9999');
$response->assertStatus(404)
->assertJsonFragment(['message' => 'Appointment not found']);
}
public function test_update_changes_time_student()
{
$student = Student::factory()->create();
$appointment = Appointment::factory()->create([
'student_id' => $student->id,
'time_start' => '12:00',
'time_end' => '12:30',
]);
$response = $this->actingAs($student)->putJson("/api/appointments/{$appointment->id}", [
'time_start' => '13:00',
'time_end' => '13:30',
]);
$response->assertStatus(200)
->assertJsonFragment(['time_start' => '13:00', 'time_end' => '13:30']);
$this->assertDatabaseHas('appointments', [
'time_start' => '13:00',
'time_end' => '13:30',
]);
}
public function test_update_changes_time_company()
{
$company = Company::factory()->create();
$appointment = Appointment::factory()->create([
'company_id' => $company->id,
'time_start' => '12:00',
'time_end' => '12:30',
]);
$response = $this->actingAs($company)->putJson("/api/appointments/{$appointment->id}", [
'time_start' => '13:00',
'time_end' => '13:30',
]);
$response->assertStatus(200)
->assertJsonFragment(['time_start' => '13:00', 'time_end' => '13:30']);
$this->assertDatabaseHas('appointments', [
'time_start' => '13:00',
'time_end' => '13:30',
]);
}
public function test_update_changes_time_admin()
{
$admin = Admin::factory()->create();
$appointment = Appointment::factory()->create([
'time_start' => '12:00',
'time_end' => '12:30',
]);
$response = $this->actingAs($admin)->putJson("/api/appointments/{$appointment->id}", [
'time_start' => '13:00',
'time_end' => '13:30',
]);
$response->assertStatus(200)
->assertJsonFragment(['time_start' => '13:00', 'time_end' => '13:30']);
$this->assertDatabaseHas('appointments', [
'time_start' => '13:00',
'time_end' => '13:30',
]);
}
public function test_destroy_deletes_appointment_admin()
{
$admin = Admin::factory()->create();
$appointment = Appointment::factory()->create();
$response = $this->actingAs($admin)->deleteJson("/api/appointments/{$appointment->id}");
$response->assertStatus(200)
->assertJsonFragment(['message' => 'Appointment deleted successfully']);
$this->assertDatabaseMissing('appointments', ['id' => $appointment->id]);
}
public function test_destroy_deletes_appointment_student()
{
$student = Student::factory()->create();
$appointment = Appointment::factory()->create([
'student_id' => $student->id,
]);
$response = $this->actingAs($student)->deleteJson("/api/appointments/{$appointment->id}");
$response->assertStatus(200)
->assertJsonFragment(['message' => 'Appointment deleted successfully']);
$this->assertDatabaseMissing('appointments', ['id' => $appointment->id]);
}
public function test_destroy_returns_404_for_missing()
{
$student = Student::factory()->create();
$response = $this->actingAs($student)->deleteJson('/api/appointments/9999');
$response->assertStatus(404)
->assertJsonFragment(['message' => 'Appointment not found']);
}
}
|