Skip to content

A self-hosted media server and file management system

chore: code smells

aminnausin251d390a0668
diff --git a/app/Http/Requests/UserStoreRequest.php b/app/Http/Requests/UserStoreRequest.php
index 569c085b..709f9c83 100644
--- a/app/Http/Requests/UserStoreRequest.php
+++ b/app/Http/Requests/UserStoreRequest.php
@@ -36,7 +36,7 @@ class UserStoreRequest extends FormRequest {
if (preg_match('/[_-]{2}/', $value)) {
$fail('Username cannot contain consecutive hyphens or underscores.');
}
- if (preg_match('/^[_-]|[_-]$/', $value)) {
+ if (preg_match('/^[_-]/', $value) || preg_match('/[_-]$/', $value)) {
$fail('Username cannot start or end with a hyphen or underscore.');
}
},
diff --git a/tests/Feature/Auth/RegistrationTest.php b/tests/Feature/Auth/RegistrationTest.php
index 76891b04..84fc60fd 100644
--- a/tests/Feature/Auth/RegistrationTest.php
+++ b/tests/Feature/Auth/RegistrationTest.php
@@ -9,55 +9,59 @@ use Tests\TestCase;
class RegistrationTest extends TestCase {
use RefreshDatabase;
+ private const VALID_EMAIL = '[email protected]';
+
+ private const API_ENDPOINT = '/api/register';
+
private function validPayload(array $overrides = []): array {
return array_merge([
'name' => 'validuser',
- 'email' => '[email protected]',
+ 'email' => self::VALID_EMAIL,
'password' => 'Password1!',
'password_confirmation' => 'Password1!',
], $overrides);
}
public function test_user_can_register(): void {
- $response = $this->postJson('/api/register', $this->validPayload());
+ $response = $this->postJson(self::API_ENDPOINT, $this->validPayload());
$response->assertStatus(200)->assertJsonStructure(['user', 'token']);
- $this->assertDatabaseHas('users', ['email' => '[email protected]']);
+ $this->assertDatabaseHas('users', ['email' => self::VALID_EMAIL]);
}
public function test_name_must_contain_a_letter(): void {
- $this->postJson('/api/register', $this->validPayload(['name' => '1234']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => '1234']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
public function test_name_rejects_spaces(): void {
- $this->postJson('/api/register', $this->validPayload(['name' => 'valid user']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => 'valid user']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
public function test_name_rejects_consecutive_special_characters(): void {
- $this->postJson('/api/register', $this->validPayload(['name' => 'valid--user']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => 'valid--user']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
public function test_name_rejects_leading_special_characters(): void {
- $this->postJson('/api/register', $this->validPayload(['name' => '-validuser']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => '-validuser']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
public function test_name_rejects_trailing_special_characters(): void {
- $this->postJson('/api/register', $this->validPayload(['name' => 'validuser_']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => 'validuser_']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
public function test_name_rejects_reserved_words(): void {
foreach (['admin', 'profile', 'settings', 'login', 'register'] as $reserved) {
- $this->postJson('/api/register', $this->validPayload(['name' => $reserved]))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => $reserved]))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
@@ -66,7 +70,7 @@ class RegistrationTest extends TestCase {
public function test_name_is_unique(): void {
User::factory()->create(['name' => 'existinguser']);
- $this->postJson('/api/register', $this->validPayload(['name' => 'existinguser']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => 'existinguser']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
@@ -74,21 +78,21 @@ class RegistrationTest extends TestCase {
public function test_name_unique_check_is_case_insensitive(): void {
User::factory()->create(['name' => 'existinguser']);
- $this->postJson('/api/register', $this->validPayload(['name' => 'ExistingUser']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['name' => 'ExistingUser']))
->assertUnprocessable()
->assertJsonValidationErrors('name');
}
public function test_email_must_be_unique(): void {
- User::factory()->create(['email' => '[email protected]']);
+ User::factory()->create(['email' => self::VALID_EMAIL]);
- $this->postJson('/api/register', $this->validPayload())
+ $this->postJson(self::API_ENDPOINT, $this->validPayload())
->assertUnprocessable()
->assertJsonValidationErrors('email');
}
public function test_password_must_be_confirmed(): void {
- $this->postJson('/api/register', $this->validPayload(['password_confirmation' => 'wrong']))
+ $this->postJson(self::API_ENDPOINT, $this->validPayload(['password_confirmation' => 'wrong']))
->assertUnprocessable()
->assertJsonValidationErrors('password');
}