Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Enums\User\Setup;
use App\Http\Controllers\Controller;
use App\Rules\Timezone;
use App\Models\Language;
use App\Models\User;
use App\Models\Workspace;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function store(Request $request): RedirectResponse
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
'password' => ['required', Rules\Password::defaults()],
'timezone' => ['nullable', 'string', 'timezone'],
'timezone' => ['nullable', 'string', new Timezone],
]);

// Check if registering via invite link (redirect contains /invites/)
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/UpdateWorkspaceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use App\Rules\Timezone;
use Illuminate\Foundation\Http\FormRequest;

class UpdateWorkspaceRequest extends FormRequest
Expand All @@ -15,7 +16,7 @@ public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'timezone' => ['required', 'string', 'timezone:all'],
'timezone' => ['required', 'string', new Timezone],
];
}

Expand Down
23 changes: 23 additions & 0 deletions app/Rules/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Timezone implements ValidationRule
{
/**
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
new \DateTimeZone($value);
} catch (\Exception) {
$fail('The :attribute must be a valid timezone.');
}
}
}
40 changes: 40 additions & 0 deletions tests/Feature/Auth/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@
expect($user->email_verified_at)->toBeNull();
});

test('new users can register with deprecated timezone Asia/Calcutta', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'Asia/Calcutta',
]);

$user = User::where('email', 'test@example.com')->first();

expect($user)->not->toBeNull();
expect($user->workspaces->first()->timezone)->toBe('Asia/Calcutta');
});

test('new users can register with deprecated timezone US/Eastern', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'US/Eastern',
]);

$user = User::where('email', 'test@example.com')->first();

expect($user)->not->toBeNull();
expect($user->workspaces->first()->timezone)->toBe('US/Eastern');
});

test('new users cannot register with invalid timezone', function () {
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'Invalid/Timezone',
]);

$response->assertSessionHasErrors('timezone');
expect(User::where('email', 'test@example.com')->exists())->toBeFalse();
});

test('new users registering via invite have verified email automatically', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Rules/TimezoneRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use App\Rules\Timezone;
use Illuminate\Support\Facades\Validator;

test('accepts valid timezone', function () {
$validator = Validator::make(['tz' => 'America/Sao_Paulo'], ['tz' => new Timezone]);

expect($validator->passes())->toBeTrue();
});

test('accepts UTC', function () {
$validator = Validator::make(['tz' => 'UTC'], ['tz' => new Timezone]);

expect($validator->passes())->toBeTrue();
});

test('accepts deprecated timezone Asia/Calcutta', function () {
$validator = Validator::make(['tz' => 'Asia/Calcutta'], ['tz' => new Timezone]);

expect($validator->passes())->toBeTrue();
});

test('accepts deprecated timezone US/Eastern', function () {
$validator = Validator::make(['tz' => 'US/Eastern'], ['tz' => new Timezone]);

expect($validator->passes())->toBeTrue();
});

test('rejects invalid timezone', function () {
$validator = Validator::make(['tz' => 'Invalid/Timezone'], ['tz' => new Timezone]);

expect($validator->passes())->toBeFalse();
});

test('rejects random string', function () {
$validator = Validator::make(['tz' => 'blabla'], ['tz' => new Timezone]);

expect($validator->passes())->toBeFalse();
});

test('accepts Europe/London', function () {
$validator = Validator::make(['tz' => 'Europe/London'], ['tz' => new Timezone]);

expect($validator->passes())->toBeTrue();
});
Loading