forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ppy#11809 from notbakaneko/feature/username-change…
…-cost-update Adjust username change cost
- Loading branch information
Showing
4 changed files
with
212 additions
and
64 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
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\User; | ||
use App\Models\UsernameChangeHistory; | ||
use Carbon\Carbon; | ||
|
||
class UsernameChangeHistoryFactory extends Factory | ||
{ | ||
protected $model = UsernameChangeHistory::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'timestamp' => Carbon::now(), | ||
'type' => 'paid', | ||
'user_id' => User::factory(), | ||
|
||
// depend on user_id; the username will be incorrect when factorying multiple names at once, | ||
// so they should be handled separately if realistic name changes are wanted. | ||
'username' => fn (array $attr) => User::find($attr['user_id'])->username, | ||
'username_last' => fn (array $attr) => "{$attr['username']}_prev", | ||
]; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,11 +10,99 @@ | |
use App\Libraries\Session\Store; | ||
use App\Models\OAuth\Token; | ||
use App\Models\User; | ||
use App\Models\UsernameChangeHistory; | ||
use Carbon\CarbonImmutable; | ||
use Database\Factories\OAuth\RefreshTokenFactory; | ||
use Tests\TestCase; | ||
|
||
class UserTest extends TestCase | ||
{ | ||
public static function dataProviderForAttributeTwitter(): array | ||
{ | ||
return [ | ||
['@hello', 'hello'], | ||
['hello', 'hello'], | ||
['@', null], | ||
['', null], | ||
[null, null], | ||
]; | ||
} | ||
|
||
public static function dataProviderForUsernameChangeCost() | ||
{ | ||
return [ | ||
[0, 0], | ||
[1, 8], | ||
[2, 16], | ||
[3, 32], | ||
[4, 64], | ||
[5, 100], | ||
[6, 100], | ||
[10, 100], | ||
]; | ||
} | ||
|
||
public static function dataProviderForUsernameChangeCostLastChange() | ||
{ | ||
// assume there are 6 changes (max tier + 1) | ||
return [ | ||
[0, 100], | ||
[1, 64], | ||
[2, 32], | ||
[3, 16], | ||
[4, 8], | ||
[5, 8], | ||
[6, 8], | ||
[10, 8], | ||
]; | ||
} | ||
|
||
public static function dataProviderForUsernameChangeCostType() | ||
{ | ||
return [ | ||
['admin', 0], | ||
['inactive', 0], | ||
['paid', 8], | ||
['revert', 0], | ||
['support', 8], | ||
]; | ||
} | ||
|
||
public static function dataProviderForUsernameChangeCostTypeLastChange() | ||
{ | ||
return [ | ||
['admin', 8], | ||
['inactive', 8], | ||
['paid', 32], | ||
['revert', 8], | ||
['support', 32], | ||
]; | ||
} | ||
|
||
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 | ||
*/ | ||
|
@@ -47,6 +135,26 @@ public function testEmailLoginEnabled() | |
$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']); | ||
|
@@ -65,24 +173,64 @@ public function testUsernameAvailableAtForNonDefaultGroup() | |
$this->assertGreaterThanOrEqual($allowedAt, $user->getUsernameAvailableAt()); | ||
} | ||
|
||
public function testResetSessions(): void | ||
/** | ||
* @dataProvider dataProviderForUsernameChangeCost | ||
*/ | ||
public function testUsernameChangeCost(int $changes, int $cost) | ||
{ | ||
$user = User::factory()->create(); | ||
$user = User::factory() | ||
->has(UsernameChangeHistory::factory()->count($changes)) | ||
->create(); | ||
|
||
// create session | ||
$this->post(route('login'), ['username' => $user->username, 'password' => User::factory()::DEFAULT_PASSWORD]); | ||
// sanity check | ||
$this->assertNotEmpty(Store::ids($user->getKey())); | ||
$this->assertSame($cost, $user->usernameChangeCost()); | ||
} | ||
|
||
// create token | ||
$token = Token::factory()->create(['user_id' => $user, 'revoked' => false]); | ||
$refreshToken = (new RefreshTokenFactory())->create(['access_token_id' => $token, 'revoked' => false]); | ||
/** | ||
* @dataProvider dataProviderForUsernameChangeCostLastChange | ||
*/ | ||
public function testUsernameChangeCostLastChange(int $years, int $cost) | ||
{ | ||
$this->travelTo(CarbonImmutable::now()->subYears($years)); | ||
|
||
$user->resetSessions(); | ||
$user = User::factory() | ||
->has(UsernameChangeHistory::factory()->count(6)) // 6 = max tier + 1 | ||
->create(); | ||
|
||
$this->assertEmpty(Store::ids($user->getKey())); | ||
$this->assertTrue($token->fresh()->revoked); | ||
$this->assertTrue($refreshToken->fresh()->revoked); | ||
$this->travelBack(); | ||
|
||
$this->assertSame($cost, $user->usernameChangeCost()); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderForUsernameChangeCostType | ||
*/ | ||
public function testUsernameChangeCostType(string $type, int $cost) | ||
{ | ||
$user = User::factory() | ||
->has(UsernameChangeHistory::factory()->state(['type' => $type])) | ||
->create(); | ||
|
||
$this->assertSame($cost, $user->usernameChangeCost()); | ||
} | ||
|
||
/** | ||
* This tests the correct last UsernameChangeHistory is used when applying the cost changes. | ||
* | ||
* @dataProvider dataProviderForUsernameChangeCostTypeLastChange | ||
*/ | ||
public function testUsernameChangeCostTypeLastChange(string $type, int $cost) | ||
{ | ||
$this->travelTo(CarbonImmutable::now()->subYears(1)); | ||
|
||
$user = User::factory() | ||
->has(UsernameChangeHistory::factory()->count(2)) | ||
->create(); | ||
|
||
$this->travelBack(); | ||
|
||
UsernameChangeHistory::factory()->state(['type' => $type, 'user_id' => $user])->create(); | ||
|
||
$this->assertSame($cost, $user->usernameChangeCost()); | ||
} | ||
|
||
/** | ||
|
@@ -99,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], | ||
]; | ||
} | ||
} |