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 */