Skip to content

Commit

Permalink
fix: nickname regex validation not working (#3430)
Browse files Browse the repository at this point in the history
* test: regex validation for nickname on registration
* fix: nickname regex validation not working
* test: regex validation works with valid inputs
  • Loading branch information
SychO9 authored May 20, 2022
1 parent 5d427fd commit 0740345
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AddNicknameValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __invoke($flarumValidator, Validator $validator)
function ($attribute, $value, $fail) {
$regex = $this->settings->get('flarum-nicknames.regex');
if ($regex && ! preg_match_all("/$regex/", $value)) {
$this->translator->trans('flarum-nicknames.api.invalid_nickname_message');
$fail($this->translator->trans('flarum-nicknames.api.invalid_nickname_message'));
}
},
'min:'.$this->settings->get('flarum-nicknames.min'),
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/api/RegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,48 @@ public function cant_register_with_nickname_if_not_allowed()

$this->assertEquals(403, $response->getStatusCode());
}

/**
* @test
*/
public function cant_register_with_nickname_if_invalid_regex()
{
$this->setting('flarum-nicknames.set_on_registration', true);
$this->setting('flarum-nicknames.regex', '^[A-z]+$');

$response = $this->send(
$this->request('POST', '/register', [
'json' => [
'nickname' => '007',
'username' => 'test',
'password' => 'too-obscure',
'email' => 'test@machine.local',
]
])
);

$this->assertEquals(422, $response->getStatusCode());
}

/**
* @test
*/
public function can_register_with_nickname_if_valid_regex()
{
$this->setting('flarum-nicknames.set_on_registration', true);
$this->setting('flarum-nicknames.regex', '^[A-z]+$');

$response = $this->send(
$this->request('POST', '/register', [
'json' => [
'nickname' => 'Acme',
'username' => 'test',
'password' => 'too-obscure',
'email' => 'test@machine.local',
]
])
);

$this->assertEquals(201, $response->getStatusCode());
}
}

0 comments on commit 0740345

Please sign in to comment.