Add an assertion to test for encrypted values in your database.
composer require ohseesoftware/laravel-assert-encrypted
Say you have an encrypted value in your database:
User::create([
'name' => 'John Doe',
'secret' => encrypt('api-key')
]);
It's a bit hard to test the value of secret
in the database because there's randomness in encrypt()
. This means encrypt('value') !== encrypt('value')
.
To get around this, you can use the trait exposed in this package in your tests:
<?php
namespace Tests;
use OhSeeSoftware\LaravelAssertEncrypted\Traits\AssertEncrypted;
class SomeTest extends TestCase
{
use AssertEncrypted;
/** @test */
public function it_stores_users_secrets()
{
// Given
$user = factory(User::class)->create([
'secret' => encrypt('api-key')
]);
// Then
$this->assertEncrypted('users', ['id' => $user->id], [
'secret' => 'api-key'
]);
// assertEncrypted is an alias for assertEncryptedSerialized
// since encrypt by default serializes the passed value
}
}
If your values are not serialized before encryption, you can use the assertEncryptedUnserialized
assertion.
<?php
/** @test */
public function it_stores_users_secrets()
{
// Given
$user = factory(User::class)->create([
'secret' => encrypt('api-key', false)
]);
// Then
$this->assertEncryptedUnserialized('users', ['id' => $user->id], [
'secret' => 'api-key'
]);
}
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.