Skip to content

Commit

Permalink
Add config option to disable mod API
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed Aug 25, 2022
1 parent 5f20c06 commit 0d7c3c4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public function getModpackBuild($modpackSlug, $buildName)

public function getMod($modSlug = null, $version = null)
{
if (config('solder.disable_mod_api')) {
return response()->json(['error' => 'Mod API has been disabled'], 404);
}

if (empty($modSlug)) {
$mods = Cache::remember('mods', now()->addMinutes(5), function () {
return Mod::pluck('pretty_name', 'name');
Expand Down
8 changes: 8 additions & 0 deletions config/solder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@
**/
'md5_file_timeout' => intval(env('SOLDER_MD5_FILE_TIMEOUT', '30')),

/**
* Disable mod API functionality?
*
* Setting this to true will disable the /mod endpoints in the API responses. You usually don't need to
* mess with this, but you might want to disable the mod API functionalities for privacy reasons.
*/
'disable_mod_api' => filter_var(env('SOLDER_DISABLE_MOD_API', 'false'), FILTER_VALIDATE_BOOLEAN),

];
47 changes: 43 additions & 4 deletions tests/Unit/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ public function test_modpack()

public function test_mod()
{
config()->set('solder.disable_mod_api', false);
$response = $this->get('api/mod');
$response->assertOk();
$response->assertJsonStructure(['mods']);

config()->set('solder.disable_mod_api', true);
$response = $this->get('api/mod');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod API has been disabled']);
}

public function test_invalid_modpack()
Expand Down Expand Up @@ -74,14 +80,22 @@ public function test_modpack_slug()

public function test_invalid_mod()
{
config()->set('solder.disable_mod_api', false);
$response = $this->get('api/mod/bob');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod does not exist']);

config()->set('solder.disable_mod_api', true);
$response = $this->get('api/mod/bob');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod API has been disabled']);
}

public function test_mod_slug()
{
$mod = Mod::find(1);

config()->set('solder.disable_mod_api', false);
$response = $this->get('api/mod/'.$mod->name);
$response->assertOk();
$response->assertJsonStructure([
Expand All @@ -92,6 +106,11 @@ public function test_mod_slug()
'link',
'versions',
]);

config()->set('solder.disable_mod_api', true);
$response = $this->get('api/mod/'.$mod->name);
$response->assertNotFound();
$response->assertJson(['error' => 'Mod API has been disabled']);
}

public function test_modpack_build()
Expand All @@ -113,27 +132,47 @@ public function test_mod_version()
{
$mod = Mod::find(1);
$modversion = $mod->versions->first();

config()->set('solder.disable_mod_api', false);
$response = $this->get('api/mod/'.$mod->name.'/'.$modversion->version);
$response->assertOk();
$response->assertJsonStructure([
'md5',
'filesize',
'url',
$response->assertJson([
'md5' => $modversion->md5,
'filesize' => $modversion->filesize,
'url' => $modversion->url,
]);

config()->set('solder.disable_mod_api', true);
$response = $this->get('api/mod/'.$mod->name.'/'.$modversion->version);
$response->assertNotFound();
$response->assertJson(['error' => 'Mod API has been disabled']);
}

public function test_modversion_with_invalid_mod()
{
config()->set('solder.disable_mod_api', false);
$response = $this->get('api/mod/foo/bar');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod does not exist']);

config()->set('solder.disable_mod_api', true);
$response = $this->get('api/mod/foo/bar');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod API has been disabled']);
}

public function test_invalid_modversion()
{
$mod = Mod::find(1);

config()->set('solder.disable_mod_api', false);
$response = $this->get('api/mod/'.$mod->name.'/invalid');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod version does not exist']);

config()->set('solder.disable_mod_api', true);
$response = $this->get('api/mod/'.$mod->name.'/invalid');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod API has been disabled']);
}
}

0 comments on commit 0d7c3c4

Please sign in to comment.