mirror of https://github.com/pixelfed/pixelfed
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.3 KiB
PHTML
67 lines
2.3 KiB
PHTML
|
4 weeks ago
|
<?php
|
||
|
|
|
||
|
4 weeks ago
|
use App\Rules\ValidUsername;
|
||
|
4 weeks ago
|
|
||
|
|
/**
|
||
|
4 weeks ago
|
* Run the ValidUsername rule and return the failure message, or null if it passed.
|
||
|
4 weeks ago
|
*/
|
||
|
|
function validateUsername(string $value): ?string
|
||
|
|
{
|
||
|
4 weeks ago
|
$rule = new ValidUsername;
|
||
|
4 weeks ago
|
$message = null;
|
||
|
|
$rule->validate('username', $value, function ($msg) use (&$message) {
|
||
|
|
$message = $msg;
|
||
|
|
});
|
||
|
|
|
||
|
|
return $message;
|
||
|
|
}
|
||
|
|
|
||
|
4 weeks ago
|
describe('ValidUsername', function () {
|
||
|
4 weeks ago
|
it('accepts a simple alphanumeric username', function () {
|
||
|
|
expect(validateUsername('dansup'))->toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('accepts a username with a single allowed separator', function () {
|
||
|
|
expect(validateUsername('dan_sup'))->toBeNull();
|
||
|
|
expect(validateUsername('dan.sup'))->toBeNull();
|
||
|
|
expect(validateUsername('dan-sup'))->toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('rejects usernames ending in a disallowed file extension', function (string $value) {
|
||
|
|
expect(validateUsername($value))->toBe('Username is invalid.');
|
||
|
|
})->with([
|
||
|
|
'user.php',
|
||
|
|
'user.js',
|
||
|
|
'user.css',
|
||
|
|
]);
|
||
|
|
|
||
|
|
it('rejects more than one separator', function () {
|
||
|
|
expect(validateUsername('a_b.c'))
|
||
|
|
->toBe('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('rejects usernames not starting with a letter or number', function () {
|
||
|
|
expect(validateUsername('_dansup'))
|
||
|
|
->toBe('Username is invalid. Must start with a letter or number.');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('rejects usernames not ending with a letter or number', function () {
|
||
|
|
expect(validateUsername('dansup_'))
|
||
|
|
->toBe('Username is invalid. Must end with a letter or number.');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('rejects usernames with disallowed characters', function () {
|
||
|
|
expect(validateUsername('dan$up'))
|
||
|
|
->toBe('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
|
||
|
|
});
|
||
|
|
|
||
|
|
// it('rejects all-numeric usernames (must contain at least one letter)', function () {
|
||
|
|
// expect(validateUsername('12345'))
|
||
|
|
// ->toBe('Username is invalid. Must contain at least one alphabetical character.');
|
||
|
|
// });
|
||
|
|
|
||
|
|
it('rejects restricted names', function () {
|
||
|
|
expect(validateUsername('admin'))->toBe('Username cannot be used.');
|
||
|
|
});
|
||
|
|
});
|