From b54ea07dd9e16bd137cbf17c98f50c75408ac455 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 11:24:10 +0100 Subject: [PATCH 1/7] test: write failing tests for email length --- .../integration/api/users/CreateTest.php | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 4e667c4f29..1edefc91ce 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -139,6 +139,139 @@ public function admins_can_create_activated_users() $this->assertEquals(1, $user->is_email_confirmed); } + /** + * @test + */ + public function admin_can_create_user_with_longest_possible_local_part_email() + { + $email = str_repeat('a', 64) . '@machine.local'; + + $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_can_create_user_with_longest_valid_domain() + { + $email = 't@' . str_repeat('a', 63) . '.' . str_repeat('b', 63) . '.' . str_repeat('c', 63) . '.' . str_repeat('d', 58) . '.x'; + + $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_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 */ From c783206ac31140021c7595ecdb5e3ab55a35577a Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 11:33:34 +0100 Subject: [PATCH 2/7] style: formatting --- .../core/tests/integration/api/users/CreateTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 1edefc91ce..84b167e7cd 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -144,7 +144,7 @@ public function admins_can_create_activated_users() */ public function admin_can_create_user_with_longest_possible_local_part_email() { - $email = str_repeat('a', 64) . '@machine.local'; + $email = str_repeat('a', 64).'@machine.local'; $response = $this->send( $this->request( @@ -178,7 +178,7 @@ public function admin_can_create_user_with_longest_possible_local_part_email() */ public function admin_can_create_user_with_longest_valid_domain() { - $email = 't@' . str_repeat('a', 63) . '.' . str_repeat('b', 63) . '.' . str_repeat('c', 63) . '.' . str_repeat('d', 58) . '.x'; + $email = 't@'.str_repeat('a', 63).'.'.str_repeat('b', 63).'.'.str_repeat('c', 63).'.'. str_repeat('d', 58).'.x'; $response = $this->send( $this->request( @@ -213,8 +213,8 @@ public function admin_can_create_user_with_longest_valid_domain() 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; + $domain = str_repeat('a', 61).'.'.str_repeat('a', 60).'.'.str_repeat('a', 60).'.local'; + $email = $localPart.'@'.$domain; $response = $this->send( $this->request( @@ -248,7 +248,7 @@ public function admin_can_create_user_with_longest_valid_email() */ public function admin_cannot_create_user_with_invalid_email_length() { - $email = str_repeat('a', 65) . '@' . str_repeat('a', 256) . '.local'; + $email = str_repeat('a', 65).'@'.str_repeat('a', 256).'.local'; $response = $this->send( $this->request( From e04957b0ed8d14b0eed5e7b47d922d659356743d Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 11:35:21 +0100 Subject: [PATCH 3/7] style: formatting --- framework/core/tests/integration/api/users/CreateTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 84b167e7cd..48d1340ad5 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -178,7 +178,7 @@ public function admin_can_create_user_with_longest_possible_local_part_email() */ public function admin_can_create_user_with_longest_valid_domain() { - $email = 't@'.str_repeat('a', 63).'.'.str_repeat('b', 63).'.'.str_repeat('c', 63).'.'. str_repeat('d', 58).'.x'; + $email = 't@'.str_repeat('a', 63).'.'.str_repeat('b', 63).'.'.str_repeat('c', 63).'.'.str_repeat('d', 58).'.x'; $response = $this->send( $this->request( From e9d6b055f682ea8cbc58da7bc807055a6def5bda Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 11:36:38 +0100 Subject: [PATCH 4/7] fix: change length of email field --- ..._18_000000_increase_email_field_length.php | 25 +++++++++++++++++++ 1 file changed, 25 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(); + }); + } +]; From c828b0a6b867f756bfd08f624fd62945057d5f0d Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 11:55:07 +0100 Subject: [PATCH 5/7] test: write test for email with too long local part --- .../integration/api/users/CreateTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 48d1340ad5..98f988ce01 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -173,6 +173,35 @@ public function admin_can_create_user_with_longest_possible_local_part_email() $this->assertEquals($email, $user->email); } + /** + * @test + */ + public function admin_cannot_create_user_with_invalid_local_part_email() + { + $email = str_repeat('a', 65) . '@machine.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 */ From d180ca14ca345cd1371478b45ff8561f790838f8 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 12:00:34 +0100 Subject: [PATCH 6/7] style: formatting --- framework/core/tests/integration/api/users/CreateTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 98f988ce01..92a60a0471 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -178,7 +178,7 @@ public function admin_can_create_user_with_longest_possible_local_part_email() */ public function admin_cannot_create_user_with_invalid_local_part_email() { - $email = str_repeat('a', 65) . '@machine.local'; + $email = str_repeat('a', 65).'@machine.local'; $response = $this->send( $this->request( From 8261f2e77ebda7c276cda9bb3a6f580a44da6f19 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 18 Nov 2024 12:22:20 +0100 Subject: [PATCH 7/7] chore: remove unnecessary tests --- .../integration/api/users/CreateTest.php | 97 ------------------- 1 file changed, 97 deletions(-) diff --git a/framework/core/tests/integration/api/users/CreateTest.php b/framework/core/tests/integration/api/users/CreateTest.php index 92a60a0471..a3204c3bdc 100644 --- a/framework/core/tests/integration/api/users/CreateTest.php +++ b/framework/core/tests/integration/api/users/CreateTest.php @@ -139,103 +139,6 @@ public function admins_can_create_activated_users() $this->assertEquals(1, $user->is_email_confirmed); } - /** - * @test - */ - public function admin_can_create_user_with_longest_possible_local_part_email() - { - $email = str_repeat('a', 64).'@machine.local'; - - $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_local_part_email() - { - $email = str_repeat('a', 65).'@machine.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 - */ - public function admin_can_create_user_with_longest_valid_domain() - { - $email = 't@'.str_repeat('a', 63).'.'.str_repeat('b', 63).'.'.str_repeat('c', 63).'.'.str_repeat('d', 58).'.x'; - - $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 */