Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email validation enhancement #650

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function store(Request $request): RedirectResponse
$request->validate([
'name' => ['required', 'string', 'max:255'],
'username' => ['required', 'string', 'min:4', 'max:50', 'unique:'.User::class, new Username],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class, new UnauthorizedEmailProviders()],
'email' => ['required', 'string', 'lowercase', 'email:rfc,dns', 'max:255', 'unique:'.User::class, new UnauthorizedEmailProviders()],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'terms' => ['required', 'accepted'],
'g-recaptcha-response' => app()->environment('production') ? ['required', new Recaptcha($request->ip())] : [],
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/UserUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function rules(): array
new Username($user),
],
'email' => [
'required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($user->id),
'required', 'string', 'lowercase', 'email:rfc,dns', 'max:255', Rule::unique(User::class)->ignore($user->id),
new UnauthorizedEmailProviders(),
],
'mail_preference_time' => [Rule::enum(UserMailPreference::class)],
Expand Down
36 changes: 30 additions & 6 deletions tests/Http/Profile/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
->patch('/profile', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'mail_preference_time' => 'daily',
'prefers_anonymous_questions' => false,
]);
Expand All @@ -49,7 +49,7 @@
$user->refresh();

$this->assertSame('Test User', $user->name);
$this->assertSame('test@example.com', $user->email);
$this->assertSame('test@laravel.com', $user->email);
$this->assertSame('testuser', $user->username);
$this->assertNull($user->email_verified_at);
$this->assertFalse($user->prefers_anonymous_questions);
Expand Down Expand Up @@ -78,6 +78,7 @@
test('username can be updated to uppercase', function () {
$user = User::factory()->create([
'username' => 'testuser',
'email' => '[email protected]',
msamgan marked this conversation as resolved.
Show resolved Hide resolved
]);

$response = $this
Expand Down Expand Up @@ -126,7 +127,9 @@
});

test('email verification status is unchanged when the email address is unchanged', function () {
$user = User::factory()->create();
$user = User::factory()->create([
'email' => '[email protected]',
msamgan marked this conversation as resolved.
Show resolved Hide resolved
]);

$response = $this
->actingAs($user)
Expand All @@ -147,13 +150,15 @@

test('email verification job sent & status reset when the email address is changed', function () {
Notification::fake();
$user = User::factory()->create();
$user = User::factory()->create([
'email' => '[email protected]',
msamgan marked this conversation as resolved.
Show resolved Hide resolved
]);

$this->actingAs($user)
->patch('/profile', [
'name' => $user->name,
'username' => 'valid_username',
'email' => 'new@email.address',
'email' => 'new@laravel.com',
'prefers_anonymous_questions' => false,
])
->assertSessionHasNoErrors();
Expand All @@ -165,7 +170,9 @@

test('only updates avatar if email changes & avatar not been uploaded', function () {
Queue::fake();
$user = User::factory()->create();
$user = User::factory()->create([
'email' => '[email protected]',
msamgan marked this conversation as resolved.
Show resolved Hide resolved
]);

$this->actingAs($user)
->patch('/profile', [
Expand Down Expand Up @@ -327,6 +334,7 @@
test('prefers_anonymous_questions can be updated', function () {
$user = User::factory()->create([
'prefers_anonymous_questions' => true,
'email' => '[email protected]',
msamgan marked this conversation as resolved.
Show resolved Hide resolved
]);

$response = $this
Expand Down Expand Up @@ -462,3 +470,19 @@
->and($user->is_uploaded_avatar)->toBeFalse()
->and(session('flash-message'))->toBe('Updating avatar using GitHub.');
});

test('reject profile information update with fake email', function () {
$user = User::factory()->create();

$response = $this
->actingAs($user)
->patch('/profile', [
'name' => 'Test User',
'username' => 'testuser',
'email' => '[email protected]',
'mail_preference_time' => 'daily',
'prefers_anonymous_questions' => false,
]);

$response->assertInvalid('email');
});
38 changes: 29 additions & 9 deletions tests/Http/Register/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'm@9v_.*.XCN',
'password_confirmation' => 'm@9v_.*.XCN',
'terms' => true,
Expand All @@ -40,7 +40,7 @@
$payload = [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'password',
'password_confirmation' => 'password',
];
Expand Down Expand Up @@ -85,7 +85,7 @@
$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'password',
'password_confirmation' => 'not-password',
]);
Expand All @@ -98,7 +98,7 @@
$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'password',
'password_confirmation' => 'not-password',
'terms' => false,
Expand All @@ -116,7 +116,7 @@
$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
Expand All @@ -127,13 +127,13 @@

test('email must be unique', function () {
User::factory()->create([
'email' => 'test@example.com',
'email' => 'test@laravel.com',
]);

$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser1',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
Expand All @@ -148,7 +148,7 @@
$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'pass',
'password_confirmation' => 'pass',
]);
Expand Down Expand Up @@ -299,11 +299,31 @@
$this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser1',
'email' => 'test@example.com',
'email' => 'test@laravel.com',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => true,
]);

expect(User::first()->prefers_anonymous_questions)->toBeTrue();
});

test('reject fake emails', function () {
Http::fake([
'https://www.google.com/recaptcha/api/siteverify' => Http::response([
'success' => true,
]),
]);

$response = $this->from('/register')->post('/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => '[email protected]',
'password' => 'm@9v_.*.XCN',
'password_confirmation' => 'm@9v_.*.XCN',
'terms' => true,
'g-recaptcha-response' => 'valid',
]);

$response->assertInvalid('email');
});