Add test for GenerateJWTSecretCommand

This commit is contained in:
Phan An 2018-08-19 22:48:47 +02:00
parent f087dc71a2
commit a96bd3e326
3 changed files with 64 additions and 20 deletions

View file

@ -4,29 +4,21 @@ namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
use Jackiedo\DotenvEditor\DotenvEditor;
class GenerateJWTSecret extends Command
class GenerateJwtSecretCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'koel:generate-jwt-secret';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Set the JWTAuth secret key used to sign the tokens';
private $dotenvEditor;
public function __construct(DotenvEditor $dotenvEditor)
{
parent::__construct();
$this->dotenvEditor = $dotenvEditor;
}
/**
* Execute the console command.
*
* @throws \RuntimeException
*/
public function handle()
{
if (config('jwt.secret')) {
@ -36,6 +28,6 @@ class GenerateJWTSecret extends Command
}
$this->info('Generating JWT secret');
DotenvEditor::setKey('JWT_SECRET', Str::random(32))->save();
$this->dotenvEditor->setKey('JWT_SECRET', Str::random(32))->save();
}
}

View file

@ -15,7 +15,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
Commands\SyncMediaCommand::class,
Commands\Init::class,
Commands\GenerateJWTSecret::class,
Commands\GenerateJwtSecretCommand::class,
];
/**

View file

@ -0,0 +1,52 @@
<?php
namespace Tests\Integration\Commands;
use App\Console\Commands\GenerateJwtSecretCommand;
use App\Console\Kernel;
use Jackiedo\DotenvEditor\DotenvEditor;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;
class GenerateJwtSecretCommandTest extends TestCase
{
/** @var DotenvEditor|MockInterface */
private $dotenvEditor;
/** @var GenerateJwtSecretCommand */
private $command;
public function setUp()
{
parent::setUp();
$this->dotenvEditor = $this->mockIocDependency(DotenvEditor::class);
$this->command = app(GenerateJwtSecretCommand::class);
app(Kernel::class)->registerCommand($this->command);
}
public function testGenerateJwtSecret()
{
config(['jwt.secret' => false]);
$this->dotenvEditor
->shouldReceive('setKey')
->with('JWT_SECRET', Mockery::on(function ($key) {
return preg_match('/[a-f0-9]{32}$/i', $key);
}));
$this->artisan('koel:generate-jwt-secret');
}
public function testNotRegenerateJwtSecret()
{
config(['jwt.secret' => '12345678901234567890123456789012']);
$this->dotenvEditor
->shouldReceive('setKey')
->never();
$this->artisan('koel:generate-jwt-secret');
}
}