-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathIncidentTest.php
More file actions
381 lines (294 loc) · 11.5 KB
/
IncidentTest.php
File metadata and controls
381 lines (294 loc) · 11.5 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
use Cachet\Enums\IncidentStatusEnum;
use Cachet\Models\Incident;
use Cachet\Models\IncidentTemplate;
use Laravel\Sanctum\Sanctum;
use Workbench\App\User;
use function Pest\Laravel\deleteJson;
use function Pest\Laravel\getJson;
use function Pest\Laravel\postJson;
use function Pest\Laravel\putJson;
use function Pest\Laravel\withoutExceptionHandling;
it('can list incidents', function () {
Incident::factory(2)->create();
$response = getJson('/status/api/incidents');
$response->assertOk();
$response->assertJsonCount(2, 'data');
});
it('does not list more than 15 incidents by default', function () {
Incident::factory(20)->create();
$response = getJson('/status/api/incidents');
$response->assertOk();
$response->assertJsonCount(15, 'data');
});
it('can list more than 15 incidents', function () {
Incident::factory(20)->create();
$response = getJson('/status/api/incidents?per_page=18');
$response->assertOk();
$response->assertJsonCount(18, 'data');
});
it('sorts incidents by created at descending by default', function () {
$incidents = Incident::factory(5)->sequence(
['created_at' => '2020-01-01'],
['created_at' => '2021-01-01'],
['created_at' => '2022-01-01'],
['created_at' => '2023-01-01'],
['created_at' => '2023-02-01'],
)->create();
$response = getJson('/status/api/incidents');
$response->assertJsonPath('data.0.attributes.id', $incidents->last()->id);
$response->assertJsonPath('data.1.attributes.id', $incidents->skip(3)->first()->id);
$response->assertJsonPath('data.2.attributes.id', $incidents->skip(2)->first()->id);
$response->assertJsonPath('data.3.attributes.id', $incidents->skip(1)->first()->id);
$response->assertJsonPath('data.4.attributes.id', $incidents->first()->id);
});
it('can sort incidents by ID', function () {
Incident::factory(5)->create();
$response = getJson('/status/api/incidents?sort=id');
$response->assertJsonPath('data.0.attributes.id', 1);
$response->assertJsonPath('data.1.attributes.id', 2);
$response->assertJsonPath('data.2.attributes.id', 3);
$response->assertJsonPath('data.3.attributes.id', 4);
$response->assertJsonPath('data.4.attributes.id', 5);
});
it('can sort incidents by name', function () {
Incident::factory(5)->sequence(
['name' => 'c', 'created_at' => '2020-01-01'],
['name' => 'a', 'created_at' => '2021-01-01'],
['name' => 'b', 'created_at' => '2022-01-01'],
['name' => 'e', 'created_at' => '2023-01-01'],
['name' => 'd', 'created_at' => '2023-02-01'],
)->create();
$response = getJson('/status/api/incidents?sort=name');
$response->assertJsonPath('data.0.attributes.id', 2);
$response->assertJsonPath('data.1.attributes.id', 3);
$response->assertJsonPath('data.2.attributes.id', 1);
$response->assertJsonPath('data.3.attributes.id', 5);
$response->assertJsonPath('data.4.attributes.id', 4);
});
it('can sort incidents by status', function () {
Incident::factory(5)->sequence(
['status' => 3, 'created_at' => '2020-01-01'],
['status' => 1, 'created_at' => '2021-01-01'],
['status' => 2, 'created_at' => '2022-01-01'],
['status' => 1, 'created_at' => '2023-01-01'],
['status' => 4, 'created_at' => '2023-02-01'],
)->create();
$response = getJson('/status/api/incidents?sort=status');
$response->assertJsonPath('data.0.attributes.id', 2);
$response->assertJsonPath('data.1.attributes.id', 4);
$response->assertJsonPath('data.2.attributes.id', 3);
$response->assertJsonPath('data.3.attributes.id', 1);
$response->assertJsonPath('data.4.attributes.id', 5);
});
it('can filter incidents by name', function () {
Incident::factory(20)->create();
$incident = Incident::factory()->create([
'name' => 'Name to filter by.',
]);
$query = http_build_query([
'filter' => [
'name' => 'Name to filter by.',
],
]);
$response = getJson('/status/api/incidents?'.$query);
$response->assertJsonCount(1, 'data');
$response->assertJsonPath('data.0.attributes.id', $incident->id);
});
it('can filter incidents by status', function () {
Incident::factory(20)->create([
'status' => IncidentStatusEnum::investigating,
]);
$incident = Incident::factory()->create([
'status' => IncidentStatusEnum::identified,
]);
$query = http_build_query([
'filter' => [
'status' => IncidentStatusEnum::identified->value,
],
]);
$response = getJson('/status/api/incidents?'.$query);
$response->assertJsonCount(1, 'data');
$response->assertJsonPath('data.0.attributes.id', $incident->id);
})->todo('This test needs to be aware of the computed status.');
it('can filter incidents by occurred at date', function () {
Incident::factory(20)->create([
'occurred_at' => '2019-01-01 00:00:00',
]);
$incident = Incident::factory()->create([
'occurred_at' => '2025-01-01 00:00:00',
]);
withoutExceptionHandling();
dd(route('cachet.api.incidents.index', [
'filter' => [
'occurs_after' => '2024-12-31',
],
]));
$response = getJson(route('cachet.api.incidents.index', [
'filter' => [
'occurs_after' => '2024-12-31',
],
]))
->assertOk();
$response->assertJsonCount(1, 'data');
$response->assertJsonPath('data.0.attributes.id', $incident->id);
});
it('can get an incident', function () {
Incident::factory(5)->create();
$incident = Incident::factory()->create();
$response = getJson('/status/api/incidents/'.$incident->id);
$response->assertOk();
$response->assertJsonFragment([
'id' => $incident->id,
]);
});
it('can get an incident with updates', function () {
Incident::factory(5)->hasUpdates(2)->create();
$incident = Incident::factory()->hasUpdates(2)->create();
$response = getJson('/status/api/incidents/'.$incident->id.'?include=updates');
$response->assertOk();
$response->assertJsonFragment([
'id' => $incident->id,
]);
});
it('cannot create an incident if not authenticated', function () {
$response = postJson('/status/api/incidents', [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
]);
$response->assertUnauthorized();
});
it('cannot create an incident without the token ability', function () {
Sanctum::actingAs(User::factory()->create());
$response = postJson('/status/api/incidents', [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
]);
$response->assertForbidden();
});
it('can create an incident', function () {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);
$response = postJson('/status/api/incidents', $payload = [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
]);
$response->assertCreated();
$response->assertJson([
'data' => [
'attributes' => [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => [
'value' => 2,
],
],
],
]);
});
it('can create an incident with a template', function () {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);
$incidentTemplate = IncidentTemplate::factory()->twig()->create();
$response = postJson('/status/api/incidents', [
'name' => 'New Incident Occurred',
'template' => $incidentTemplate->slug,
'status' => 2,
]);
$response->assertCreated();
$response->assertJson([
'data' => [
'attributes' => [
'name' => 'New Incident Occurred',
'message' => "Hey,\n\nA new incident has been reported:\n\nName: New Incident Occurred\n",
'status' => [
'value' => 2,
],
],
],
]);
});
it('cannot create an incident with bad data', function (array $payload) {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);
$response = postJson('/status/api/incidents', $payload);
$response->assertUnprocessable();
$response->assertInvalid(array_keys($response->json('errors')));
})->with([
fn () => ['name' => null, 'message' => null],
fn () => ['name' => 'New Incident', 'message' => null, 'status' => 999],
fn () => ['name' => 'New Incident', 'template' => 123, 'status' => 999],
]);
it('cannot update an incident if not authenticated', function () {
$incident = Incident::factory()->create();
$response = putJson('/status/api/incidents/'.$incident->id, [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
]);
$response->assertUnauthorized();
});
it('cannot update an incident without the token ability', function () {
Sanctum::actingAs(User::factory()->create());
$incident = Incident::factory()->create();
$response = putJson('/status/api/incidents/'.$incident->id, [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
]);
$response->assertForbidden();
});
it('can update an incident', function () {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);
$incident = Incident::factory()->create();
$response = putJson('/status/api/incidents/'.$incident->id, [
'name' => 'New Incident Occurred',
'message' => 'Something went wrong.',
'status' => 2,
]);
$response->assertOk();
});
it('can update an incident while passing null data', function (array $payload) {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);
$incident = Incident::factory()->create();
$response = putJson('/status/api/incidents/'.$incident->id, $payload);
$response->assertJson([
'data' => [
'attributes' => [
'name' => 'Updated Incident',
'status' => [
'value' => 1,
],
],
],
]);
})->with([
fn () => ['name' => 'Updated Incident', 'status' => 1],
]);
it('cannot update an incident with bad data', function (array $payload) {
Sanctum::actingAs(User::factory()->create(), ['incidents.manage']);
$incident = Incident::factory()->create();
$response = putJson('/status/api/incidents/'.$incident->id, $payload);
$response->assertUnprocessable();
$response->assertInvalid(array_keys($response->json('errors')));
})->with([
fn () => ['name' => null, 'message' => null],
fn () => ['name' => 'New Incident', 'message' => null, 'status' => 999],
]);
it('cannot delete an incident if not authenticated', function () {
$incident = Incident::factory()->create();
$response = deleteJson('/status/api/incidents/'.$incident->id);
$response->assertUnauthorized();
});
it('cannot delete an incident without the token ability', function () {
Sanctum::actingAs(User::factory()->create());
$incident = Incident::factory()->create();
$response = deleteJson('/status/api/incidents/'.$incident->id);
$response->assertForbidden();
});
it('can delete an incident', function () {
Sanctum::actingAs(User::factory()->create(), ['incidents.delete']);
$incident = Incident::factory()->create();
$response = deleteJson('/status/api/incidents/'.$incident->id);
$response->assertNoContent();
});