Skip to content

Commit

Permalink
Throw error instead and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Feb 7, 2024
1 parent 4ef5cab commit a0d117e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
26 changes: 15 additions & 11 deletions app/Http/Controllers/BeatmapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class BeatmapsController extends Controller
const DEFAULT_API_INCLUDES = ['beatmapset.ratings', 'failtimes', 'max_combo'];
const DEFAULT_SCORE_INCLUDES = ['user', 'user.country', 'user.cover'];

public function __construct()
{
parent::__construct();

$this->middleware('require-scopes:public');
}

private static function assertSupporterOnlyOptions(?User $currentUser, string $type, array $mods): void
{
$isSupporter = $currentUser !== null && $currentUser->isSupporter();
Expand Down Expand Up @@ -68,13 +75,7 @@ private static function beatmapScores(string $id, ?string $scoreTransformerType,
'type:string',
], ['null_missing' => true]);

if ($params['mode'] !== null) {
$rulesetId = Beatmap::MODES[$params['mode']] ?? null;
if ($rulesetId === null) {
throw new InvariantException('invalid mode specified');
}
}
$rulesetId ??= $beatmap->playmode;
$rulesetId = static::getRulesetId($params['mode']) ?? $beatmap->playmode;
$mods = array_values(array_filter($params['mods'] ?? []));
$type = presence($params['type'], 'global');
$currentUser = \Auth::user();
Expand Down Expand Up @@ -114,11 +115,14 @@ private static function beatmapScores(string $id, ?string $scoreTransformerType,
return $results;
}

public function __construct()
private static function getRulesetId(?string $rulesetName): ?int
{
parent::__construct();
if ($rulesetName === null) {
return null;
}

$this->middleware('require-scopes:public');
return Ruleset::tryFromName($rulesetName)?->value
?? throw new InvariantException('invalid mode specified');
}

/**
Expand Down Expand Up @@ -445,7 +449,7 @@ public function userScore($beatmapId, $userId)
'mods:string[]',
]);

$rulesetId = Ruleset::tryFromName($params['mode'] ?? null)?->value ?? $beatmap->playmode;
$rulesetId = static::getRulesetId($params['mode'] ?? null) ?? $beatmap->playmode;
$mods = array_values(array_filter($params['mods'] ?? []));

$baseParams = ScoreSearchParams::fromArray([
Expand Down
18 changes: 18 additions & 0 deletions tests/Controllers/BeatmapsControllerSoloScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ public function testUserScore()
->assertJsonPath('score.id', static::$scores['legacy:userMods']->legacy_score_id);
}

/**
* @group RequiresScoreIndexer
*/
public function testUserScoreInvalidRulesetName()
{
$url = route('api.beatmaps.user.score', [
'beatmap' => static::$beatmap->getKey(),
'legacy_only' => 1,
'mode' => '_invalid',
'mods' => ['DT', 'HD'],
'user' => static::$user->getKey(),
]);
$this->actAsScopedUser(static::$user);
$this
->json('GET', $url)
->assertStatus(422);
}

/**
* @group RequiresScoreIndexer
*/
Expand Down

0 comments on commit a0d117e

Please sign in to comment.