1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
<?php
namespace Illuminate\Tests\Integration\Console;
use Illuminate\Encryption\Encrypter;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\File;
use Mockery as m;
use Orchestra\Testbench\TestCase;
class EnvironmentDecryptCommandTest extends TestCase
{
protected $filesystem;
protected function setUp(): void
{
parent::setUp();
$this->filesystem = m::spy(Filesystem::class);
$this->filesystem->shouldReceive('put')
->andReturn(true);
File::swap($this->filesystem);
}
public function testItFailsWithInvalidCipherFails()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false);
$this->artisan('env:decrypt', ['--cipher' => 'invalid', '--key' => 'abcdefghijklmnop'])
->expectsOutputToContain('Unsupported cipher')
->assertExitCode(1);
}
public function testItFailsUsingCipherWithInvalidKey()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false);
$this->artisan('env:decrypt', ['--cipher' => 'aes-128-cbc', '--key' => 'invalid'])
->expectsOutputToContain('incorrect key length')
->assertExitCode(1);
}
public function testItFailsWhenEncryptionFileCannotBeFound()
{
$this->filesystem->shouldReceive('exists')->andReturn(true);
$this->artisan('env:decrypt', ['--key' => 'secret-key'])
->expectsOutputToContain('Environment file already exists.')
->assertExitCode(1);
}
public function testItFailsWhenEnvironmentFileExists()
{
$this->filesystem->shouldReceive('exists')->andReturn(false);
$this->artisan('env:decrypt', ['--key' => 'secret-key'])
->expectsOutputToContain('Encrypted environment file not found.')
->assertExitCode(1);
}
public function testItGeneratesTheEnvironmentFileWithGeneratedKey()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter($key = Encrypter::generateKey('AES-256-CBC'), 'AES-256-CBC'))
->encrypt('APP_NAME=Laravel')
);
$this->artisan('env:decrypt', ['--force' => true, '--key' => 'base64:'.base64_encode($key)])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env'), 'APP_NAME=Laravel');
}
public function testItGeneratesTheEnvironmentFileWithUserProvidedKey()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnop', 'aes-128-gcm'))
->encrypt('APP_NAME="Laravel Two"')
);
$this->artisan('env:decrypt', ['--cipher' => 'aes-128-gcm', '--key' => 'abcdefghijklmnop'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env'), 'APP_NAME="Laravel Two"');
}
public function testItGeneratesTheEnvironmentFileWithKeyFromEnvironment()
{
$_SERVER['LARAVEL_ENV_ENCRYPTION_KEY'] = 'ponmlkjihgfedcbaponmlkjihgfedcba';
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('ponmlkjihgfedcbaponmlkjihgfedcba', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Three"')
);
$this->artisan('env:decrypt')
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env'), 'APP_NAME="Laravel Three"');
unset($_SERVER['LARAVEL_ENV_ENCRYPTION_KEY']);
}
public function testItGeneratesTheEnvironmentFileWhenForcing()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnop', 'aes-128-gcm'))
->encrypt('APP_NAME="Laravel Two"')
);
$this->artisan('env:decrypt', ['--force' => true, '--key' => 'abcdefghijklmnop', '--cipher' => 'aes-128-gcm'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env'), 'APP_NAME="Laravel Two"');
}
public function testItDecryptsMultiLineEnvironmentCorrectly()
{
$contents = <<<'Text'
APP_NAME=Laravel
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Text;
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnop', 'aes-128-gcm'))
->encrypt($contents)
);
$this->artisan('env:decrypt', ['--force' => true, '--key' => 'abcdefghijklmnop', '--cipher' => 'aes-128-gcm'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env'), $contents);
}
public function testItWritesTheEnvironmentFileCustomFilename()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--filename' => '.env'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env'), 'APP_NAME="Laravel Two"');
}
public function testItWritesTheEnvironmentFileCustomPath()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--path' => '/tmp'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with('/tmp'.DIRECTORY_SEPARATOR.'.env.production', 'APP_NAME="Laravel Two"');
}
public function testItWritesTheEnvironmentFileCustomPathAndFilename()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--filename' => '.env', '--path' => '/tmp'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);
$this->filesystem->shouldHaveReceived('put')
->with('/tmp'.DIRECTORY_SEPARATOR.'.env', 'APP_NAME="Laravel Two"');
}
public function testItCannotOverwriteEncryptedFiles()
{
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnop', '--filename' => '.env.production.encrypted'])
->expectsOutputToContain('Invalid filename.')
->assertExitCode(1);
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnop', '--filename' => '.env.staging.encrypted'])
->expectsOutputToContain('Invalid filename.')
->assertExitCode(1);
}
}
|