-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1254829
commit 3f281e1
Showing
1 changed file
with
95 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,17 @@ | |
|
||
class UserTest extends TestCase | ||
{ | ||
public static function dataProviderForAttributeTwitter(): array | ||
{ | ||
return [ | ||
['@hello', 'hello'], | ||
['hello', 'hello'], | ||
['@', null], | ||
['', null], | ||
[null, null], | ||
]; | ||
} | ||
|
||
public static function dataProviderForUsernameChangeCost() | ||
{ | ||
return [ | ||
|
@@ -68,6 +79,30 @@ public static function dataProviderForUsernameChangeCostTypeLastChange() | |
]; | ||
} | ||
|
||
public static function dataProviderValidDiscordUsername(): array | ||
{ | ||
return [ | ||
['username', true], | ||
['user_name', true], | ||
['user.name', true], | ||
['user2name', true], | ||
['u_sernam.e1337', true], | ||
['username#', false], | ||
['u', false], | ||
['morethan32characterinthisusername', false], // 33 characters | ||
|
||
// old format | ||
['username#1337', true], | ||
['ユーザー名#1337', true], | ||
['username#1', false], | ||
['username#13bb', false], | ||
['username#abcd', false], | ||
['user@name#1337', false], | ||
['user#name#1337', false], | ||
['user:name#1337', false], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForAttributeTwitter | ||
*/ | ||
|
@@ -78,6 +113,66 @@ public function testAttributeTwitter($setValue, $getValue) | |
$this->assertSame($getValue, $user->user_twitter); | ||
} | ||
|
||
public function testEmailLoginDisabled() | ||
{ | ||
config_set('osu.user.allow_email_login', false); | ||
User::factory()->create([ | ||
'username' => 'test', | ||
'user_email' => '[email protected]', | ||
]); | ||
|
||
$this->assertNull(User::findForLogin('[email protected]')); | ||
} | ||
|
||
public function testEmailLoginEnabled() | ||
{ | ||
config_set('osu.user.allow_email_login', true); | ||
$user = User::factory()->create([ | ||
'username' => 'test', | ||
'user_email' => '[email protected]', | ||
]); | ||
|
||
$this->assertTrue($user->is(User::findForLogin('[email protected]'))); | ||
} | ||
|
||
public function testResetSessions(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
// create session | ||
$this->post(route('login'), ['username' => $user->username, 'password' => User::factory()::DEFAULT_PASSWORD]); | ||
// sanity check | ||
$this->assertNotEmpty(Store::ids($user->getKey())); | ||
|
||
// create token | ||
$token = Token::factory()->create(['user_id' => $user, 'revoked' => false]); | ||
$refreshToken = (new RefreshTokenFactory())->create(['access_token_id' => $token, 'revoked' => false]); | ||
|
||
$user->resetSessions(); | ||
|
||
$this->assertEmpty(Store::ids($user->getKey())); | ||
$this->assertTrue($token->fresh()->revoked); | ||
$this->assertTrue($refreshToken->fresh()->revoked); | ||
} | ||
|
||
public function testUsernameAvailableAtForDefaultGroup() | ||
{ | ||
config_set('osu.user.allowed_rename_groups', ['default']); | ||
$allowedAtUpTo = now()->addYears(5); | ||
$user = User::factory()->withGroup('default')->create(); | ||
|
||
$this->assertLessThanOrEqual($allowedAtUpTo, $user->getUsernameAvailableAt()); | ||
} | ||
|
||
public function testUsernameAvailableAtForNonDefaultGroup() | ||
{ | ||
config_set('osu.user.allowed_rename_groups', ['default']); | ||
$allowedAt = now()->addYears(10); | ||
$user = User::factory()->withGroup('gmt')->create(['group_id' => app('groups')->byIdentifier('default')]); | ||
|
||
$this->assertGreaterThanOrEqual($allowedAt, $user->getUsernameAvailableAt()); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForUsernameChangeCost | ||
*/ | ||
|
@@ -138,66 +233,6 @@ public function testUsernameChangeCostTypeLastChange(string $type, int $cost) | |
$this->assertSame($user->usernameChangeCost(), $cost); | ||
} | ||
|
||
public function testEmailLoginDisabled() | ||
{ | ||
config_set('osu.user.allow_email_login', false); | ||
User::factory()->create([ | ||
'username' => 'test', | ||
'user_email' => '[email protected]', | ||
]); | ||
|
||
$this->assertNull(User::findForLogin('[email protected]')); | ||
} | ||
|
||
public function testEmailLoginEnabled() | ||
{ | ||
config_set('osu.user.allow_email_login', true); | ||
$user = User::factory()->create([ | ||
'username' => 'test', | ||
'user_email' => '[email protected]', | ||
]); | ||
|
||
$this->assertTrue($user->is(User::findForLogin('[email protected]'))); | ||
} | ||
|
||
public function testUsernameAvailableAtForDefaultGroup() | ||
{ | ||
config_set('osu.user.allowed_rename_groups', ['default']); | ||
$allowedAtUpTo = now()->addYears(5); | ||
$user = User::factory()->withGroup('default')->create(); | ||
|
||
$this->assertLessThanOrEqual($allowedAtUpTo, $user->getUsernameAvailableAt()); | ||
} | ||
|
||
public function testUsernameAvailableAtForNonDefaultGroup() | ||
{ | ||
config_set('osu.user.allowed_rename_groups', ['default']); | ||
$allowedAt = now()->addYears(10); | ||
$user = User::factory()->withGroup('gmt')->create(['group_id' => app('groups')->byIdentifier('default')]); | ||
|
||
$this->assertGreaterThanOrEqual($allowedAt, $user->getUsernameAvailableAt()); | ||
} | ||
|
||
public function testResetSessions(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
// create session | ||
$this->post(route('login'), ['username' => $user->username, 'password' => User::factory()::DEFAULT_PASSWORD]); | ||
// sanity check | ||
$this->assertNotEmpty(Store::ids($user->getKey())); | ||
|
||
// create token | ||
$token = Token::factory()->create(['user_id' => $user, 'revoked' => false]); | ||
$refreshToken = (new RefreshTokenFactory())->create(['access_token_id' => $token, 'revoked' => false]); | ||
|
||
$user->resetSessions(); | ||
|
||
$this->assertEmpty(Store::ids($user->getKey())); | ||
$this->assertTrue($token->fresh()->revoked); | ||
$this->assertTrue($refreshToken->fresh()->revoked); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderValidDiscordUsername | ||
*/ | ||
|
@@ -212,39 +247,4 @@ public function testValidDiscordUsername(string $username, bool $valid) | |
$this->assertArrayHasKey('user_discord', $user->validationErrors()->all()); | ||
} | ||
} | ||
|
||
public static function dataProviderForAttributeTwitter(): array | ||
{ | ||
return [ | ||
['@hello', 'hello'], | ||
['hello', 'hello'], | ||
['@', null], | ||
['', null], | ||
[null, null], | ||
]; | ||
} | ||
|
||
public static function dataProviderValidDiscordUsername(): array | ||
{ | ||
return [ | ||
['username', true], | ||
['user_name', true], | ||
['user.name', true], | ||
['user2name', true], | ||
['u_sernam.e1337', true], | ||
['username#', false], | ||
['u', false], | ||
['morethan32characterinthisusername', false], // 33 characters | ||
|
||
// old format | ||
['username#1337', true], | ||
['ユーザー名#1337', true], | ||
['username#1', false], | ||
['username#13bb', false], | ||
['username#abcd', false], | ||
['user@name#1337', false], | ||
['user#name#1337', false], | ||
['user:name#1337', false], | ||
]; | ||
} | ||
} |