From 6c89f8161e3c872f7771b377b29c7b6b3b48cf72 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca <146922689+DavideIadeluca@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:49:17 +0100 Subject: [PATCH] [1.x] fix: change length of email field (#4117) * test: write failing tests for email length * style: formatting * style: formatting * fix: change length of email field * test: write test for email with too long local part * style: formatting * chore: remove unnecessary tests --- ..._18_000000_increase_email_field_length.php | 25 +++++++ .../integration/api/users/CreateTest.php | 65 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 framework/core/migrations/2024_11_18_000000_increase_email_field_length.php diff --git a/framework/core/migrations/2024_11_18_000000_increase_email_field_length.php b/framework/core/migrations/2024_11_18_000000_increase_email_field_length.php new file mode 100644 index 0000000000..f1325546b8 --- /dev/null +++ b/framework/core/migrations/2024_11_18_000000_increase_email_field_length.php @@ -0,0 +1,25 @@ + function (Builder $schema) { + $schema->table('users', function (Blueprint $table) { + $table->string('email', 254)->change(); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('users', function (Blueprint $table) { + $table->string('email', 150)->change(); + }); + } +]; diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 4e667c4f29..a3204c3bdc 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -139,6 +139,71 @@ public function admins_can_create_activated_users() $this->assertEquals(1, $user->is_email_confirmed); } + /** + * @test + */ + public function admin_can_create_user_with_longest_valid_email() + { + $localPart = str_repeat('a', 64); + $domain = str_repeat('a', 61).'.'.str_repeat('a', 60).'.'.str_repeat('a', 60).'.local'; + $email = $localPart.'@'.$domain; + + $response = $this->send( + $this->request( + 'POST', + '/api/users', + [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'username' => 'test', + 'password' => 'too-obscure', + 'email' => $email, + ], + ] + ], + ] + ) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + /** @var User $user */ + $user = User::where('username', 'test')->firstOrFail(); + + $this->assertEquals($email, $user->email); + } + + /** + * @test + */ + public function admin_cannot_create_user_with_invalid_email_length() + { + $email = str_repeat('a', 65).'@'.str_repeat('a', 256).'.local'; + + $response = $this->send( + $this->request( + 'POST', + '/api/users', + [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'username' => 'test', + 'password' => 'too-obscure', + 'email' => $email, + ], + ] + ], + ] + ) + ); + + $this->assertEquals(422, $response->getStatusCode()); + } + /** * @test */