123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace Tests\Feature\Services;
- use Tests\FeatureTestCase;
- use Illuminate\Support\Facades\Crypt;
- use Illuminate\Support\Facades\DB;
- use App\Models\TwoFAccount;
- use App\Facades\Settings;
- /**
- * @covers \App\Services\SettingService
- */
- class SettingServiceTest extends FeatureTestCase
- {
- /**
- * App\Models\Group $groupOne, $groupTwo
- */
- protected $twofaccountOne, $twofaccountTwo;
- private const KEY = 'key';
- private const VALUE = 'value';
- private const SETTING_NAME = 'MySetting';
- private const SETTING_NAME_ALT = 'MySettingAlt';
- private const SETTING_VALUE_STRING = 'MyValue';
- private const SETTING_VALUE_TRUE_TRANSFORMED = '{{1}}';
- private const SETTING_VALUE_FALSE_TRANSFORMED = '{{}}';
- private const SETTING_VALUE_INT = 10;
- private const ACCOUNT = 'account';
- private const SERVICE = 'service';
- private const SECRET = 'A4GRFHVVRBGY7UIW';
- private const ALGORITHM_CUSTOM = 'sha256';
- private const DIGITS_CUSTOM = 7;
- private const PERIOD_CUSTOM = 40;
- private const IMAGE = 'https%3A%2F%2Fen.opensuse.org%2Fimages%2F4%2F44%2FButton-filled-colour.png';
- private const ICON = 'test.png';
- private const TOTP_FULL_CUSTOM_URI = 'otpauth://totp/'.self::SERVICE.':'.self::ACCOUNT.'?secret='.self::SECRET.'&issuer='.self::SERVICE.'&digits='.self::DIGITS_CUSTOM.'&period='.self::PERIOD_CUSTOM.'&algorithm='.self::ALGORITHM_CUSTOM.'&image='.self::IMAGE;
- /**
- * @test
- */
- public function setUp() : void
- {
- parent::setUp();
- $this->twofaccountOne = new TwoFAccount;
- $this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
- $this->twofaccountOne->service = self::SERVICE;
- $this->twofaccountOne->account = self::ACCOUNT;
- $this->twofaccountOne->icon = self::ICON;
- $this->twofaccountOne->otp_type = 'totp';
- $this->twofaccountOne->secret = self::SECRET;
- $this->twofaccountOne->digits = self::DIGITS_CUSTOM;
- $this->twofaccountOne->algorithm = self::ALGORITHM_CUSTOM;
- $this->twofaccountOne->period = self::PERIOD_CUSTOM;
- $this->twofaccountOne->counter = null;
- $this->twofaccountOne->save();
- $this->twofaccountTwo = new TwoFAccount;
- $this->twofaccountTwo->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
- $this->twofaccountTwo->service = self::SERVICE;
- $this->twofaccountTwo->account = self::ACCOUNT;
- $this->twofaccountTwo->icon = self::ICON;
- $this->twofaccountTwo->otp_type = 'totp';
- $this->twofaccountTwo->secret = self::SECRET;
- $this->twofaccountTwo->digits = self::DIGITS_CUSTOM;
- $this->twofaccountTwo->algorithm = self::ALGORITHM_CUSTOM;
- $this->twofaccountTwo->period = self::PERIOD_CUSTOM;
- $this->twofaccountTwo->counter = null;
- $this->twofaccountTwo->save();
- }
- /**
- * @test
- */
- public function test_get_string_setting_returns_correct_value()
- {
- Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
- $this->assertEquals(self::SETTING_VALUE_STRING, Settings::get(self::SETTING_NAME));
- }
- /**
- * @test
- */
- public function test_get_boolean_setting_returns_true()
- {
- Settings::set(self::SETTING_NAME, self::SETTING_VALUE_TRUE_TRANSFORMED);
- $this->assertEquals(true, Settings::get(self::SETTING_NAME));
- }
- /**
- * @test
- */
- public function test_get_boolean_setting_returns_false()
- {
- Settings::set(self::SETTING_NAME, self::SETTING_VALUE_FALSE_TRANSFORMED);
- $this->assertEquals(false, Settings::get(self::SETTING_NAME));
- }
- /**
- * @test
- */
- public function test_get_int_setting_returns_int()
- {
- Settings::set(self::SETTING_NAME, self::SETTING_VALUE_INT);
- $value = Settings::get(self::SETTING_NAME);
- $this->assertEquals(self::SETTING_VALUE_INT, $value);
- $this->assertIsInt($value);
- }
- /**
- * @test
- */
- public function test_all_returns_native_and_user_settings()
- {
- $native_options = config('2fauth.options');
- Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
- $all = Settings::all();
- $this->assertArrayHasKey(self::SETTING_NAME, $all);
- $this->assertEquals($all[self::SETTING_NAME], self::SETTING_VALUE_STRING);
- foreach ($native_options as $key => $val) {
- $this->assertArrayHasKey($key, $all);
- $this->assertEquals($all[$key], $val);
- }
-
- $this->assertArrayHasKey('lang', $all);
- }
- /**
- * @test
- */
- public function test_set_setting_persist_correct_value()
- {
- $value = Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
- $this->assertDatabaseHas('options', [
- self::KEY => self::SETTING_NAME,
- self::VALUE => self::SETTING_VALUE_STRING
- ]);
- }
-
- /**
- * @test
- */
- public function test_set_useEncryption_on_encrypts_all_accounts()
- {
- Settings::set('useEncryption', true);
- $twofaccounts = DB::table('twofaccounts')->get();
- $twofaccounts->each(function ($item, $key) {
- $this->assertEquals(self::ACCOUNT, Crypt::decryptString($item->account));
- $this->assertEquals(self::SECRET, Crypt::decryptString($item->secret));
- $this->assertEquals(self::TOTP_FULL_CUSTOM_URI, Crypt::decryptString($item->legacy_uri));
- });
- }
- /**
- * @test
- */
- public function test_set_useEncryption_on_twice_prevents_successive_encryption()
- {
- Settings::set('useEncryption', true);
- Settings::set('useEncryption', true);
- $twofaccounts = DB::table('twofaccounts')->get();
- $twofaccounts->each(function ($item, $key) {
- $this->assertEquals(self::ACCOUNT, Crypt::decryptString($item->account));
- $this->assertEquals(self::SECRET, Crypt::decryptString($item->secret));
- $this->assertEquals(self::TOTP_FULL_CUSTOM_URI, Crypt::decryptString($item->legacy_uri));
- });
- }
- /**
- * @test
- */
- public function test_set_useEncryption_off_decrypts_all_accounts()
- {
- Settings::set('useEncryption', true);
- Settings::set('useEncryption', false);
- $twofaccounts = DB::table('twofaccounts')->get();
- $twofaccounts->each(function ($item, $key) {
- $this->assertEquals(self::ACCOUNT, $item->account);
- $this->assertEquals(self::SECRET, $item->secret);
- $this->assertEquals(self::TOTP_FULL_CUSTOM_URI, $item->legacy_uri);
- });
- }
- /**
- * @test
- * @dataProvider provideUndecipherableData
- */
- public function test_set_useEncryption_off_returns_exception_when_data_are_undecipherable(array $data)
- {
- $this->expectException(\App\Exceptions\DbEncryptionException::class);
- Settings::set('useEncryption', true);
- $affected = DB::table('twofaccounts')
- ->where('id', $this->twofaccountOne->id)
- ->update($data);
- Settings::set('useEncryption', false);
- $twofaccount = TwoFAccount::find($this->twofaccountOne->id);
- }
- /**
- * Provide invalid data for validation test
- */
- public function provideUndecipherableData() : array
- {
- return [
- [[
- 'account' => 'undecipherableString'
- ]],
- [[
- 'secret' => 'undecipherableString'
- ]],
- [[
- 'legacy_uri' => 'undecipherableString'
- ]],
- ];
- }
- /**
- * @test
- */
- public function test_set_array_of_settings_persist_correct_values()
- {
- $value = Settings::set([
- self::SETTING_NAME => self::SETTING_VALUE_STRING,
- self::SETTING_NAME_ALT => self::SETTING_VALUE_INT,
- ]);
- $this->assertDatabaseHas('options', [
- self::KEY => self::SETTING_NAME,
- self::VALUE => self::SETTING_VALUE_STRING
- ]);
- $this->assertDatabaseHas('options', [
- self::KEY => self::SETTING_NAME_ALT,
- self::VALUE => self::SETTING_VALUE_INT
- ]);
- }
- /**
- * @test
- */
- public function test_set_true_setting_persist_transformed_boolean()
- {
- $value = Settings::set(self::SETTING_NAME, true);
- $this->assertDatabaseHas('options', [
- self::KEY => self::SETTING_NAME,
- self::VALUE => self::SETTING_VALUE_TRUE_TRANSFORMED
- ]);
- }
- /**
- * @test
- */
- public function test_set_false_setting_persist_transformed_boolean()
- {
- $value = Settings::set(self::SETTING_NAME, false);
- $this->assertDatabaseHas('options', [
- self::KEY => self::SETTING_NAME,
- self::VALUE => self::SETTING_VALUE_FALSE_TRANSFORMED
- ]);
- }
- /**
- * @test
- */
- public function test_del_remove_setting_from_db()
- {
- DB::table('options')->insert(
- [self::KEY => self::SETTING_NAME, self::VALUE => strval(self::SETTING_VALUE_STRING)]
- );
- $value = Settings::delete(self::SETTING_NAME);
- $this->assertDatabaseMissing('options', [
- self::KEY => self::SETTING_NAME,
- self::VALUE => self::SETTING_VALUE_STRING
- ]);
- }
- }
|