Skip to content

Commit 761df4a

Browse files
test: cover standardized JSON validation response for reset password request
1 parent 9796aa5 commit 761df4a

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

tests/Feature/Api/PasswordReset/ResetPasswordControllerTest.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,57 @@ public function update(Authenticatable $user, string $newPasswordRaw, bool $refr
302302
&& $event->guard === 'web'
303303
&& $event->remember === true;
304304
});
305-
});
305+
});
306+
307+
it('returns standardized DTO validation response for JSON reset password requests', function () {
308+
$response = $this->postJson(route('authkit.api.password.reset'), []);
309+
310+
$response->assertStatus(422)
311+
->assertJson([
312+
'ok' => false,
313+
'status' => 422,
314+
'message' => 'The given data was invalid.',
315+
])
316+
->assertJsonPath('flow.name', 'failed')
317+
->assertJsonPath('payload.fields.email.0', 'The E-mail field is required.')
318+
->assertJsonPath('payload.fields.token.0', 'The Reset token field is required.')
319+
->assertJsonPath('payload.fields.password.0', 'The New password field is required.')
320+
->assertJsonPath('payload.fields.password_confirmation.0', 'The Confirm password field is required.');
321+
322+
$errors = $response->json('errors');
323+
324+
expect($errors)->toBeArray()
325+
->and(count($errors))->toBe(4)
326+
->and($errors[0])->toHaveKeys(['code', 'message', 'field', 'meta']);
327+
328+
expect(collect($errors)->pluck('field')->all())
329+
->toContain('email', 'token', 'password', 'password_confirmation');
330+
331+
expect(collect($errors)->pluck('code')->unique()->values()->all())
332+
->toBe(['validation_error']);
333+
});
334+
335+
it('normalizes email before validation for JSON reset password requests', function () {
336+
$response = $this->postJson(route('authkit.api.password.reset'), [
337+
'email' => ' NOT-AN-EMAIL ',
338+
'token' => '',
339+
'password' => '',
340+
'password_confirmation' => '',
341+
]);
342+
343+
$response->assertStatus(422)
344+
->assertJson([
345+
'ok' => false,
346+
'status' => 422,
347+
'message' => 'The given data was invalid.',
348+
])
349+
->assertJsonPath('flow.name', 'failed');
350+
351+
expect($response->json('payload.fields.email'))->toBeArray();
352+
expect($response->json('payload.fields.token'))->toBeArray();
353+
expect($response->json('payload.fields.password'))->toBeArray();
354+
expect($response->json('payload.fields.password_confirmation'))->toBeArray();
355+
356+
expect(collect($response->json('errors'))->pluck('field')->all())
357+
->toContain('email', 'token', 'password', 'password_confirmation');
358+
});

0 commit comments

Comments
 (0)