123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- <?php
- namespace Tests\Feature\Models;
- use App\Models\TwoFAccount;
- use Tests\FeatureTestCase;
- use Tests\Classes\OtpTestData;
- /**
- * @covers \App\Models\TwoFAccount
- */
- class TwoFAccountModelTest extends FeatureTestCase
- {
- /**
- * App\Models\TwoFAccount $customTotpTwofaccount
- */
- protected $customTotpTwofaccount;
- /**
- * App\Models\TwoFAccount $customTotpTwofaccount
- */
- protected $customHotpTwofaccount;
- /**
- * @test
- */
- public function setUp() : void
- {
- parent::setUp();
- // $this->twofaccountService = $this->app->make('App\Services\TwoFAccountService');
- $this->customTotpTwofaccount = new TwoFAccount;
- $this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
- $this->customTotpTwofaccount->service = OtpTestData::SERVICE;
- $this->customTotpTwofaccount->account = OtpTestData::ACCOUNT;
- $this->customTotpTwofaccount->icon = OtpTestData::ICON;
- $this->customTotpTwofaccount->otp_type = 'totp';
- $this->customTotpTwofaccount->secret = OtpTestData::SECRET;
- $this->customTotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
- $this->customTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
- $this->customTotpTwofaccount->period = OtpTestData::PERIOD_CUSTOM;
- $this->customTotpTwofaccount->counter = null;
- $this->customTotpTwofaccount->save();
- $this->customHotpTwofaccount = new TwoFAccount;
- $this->customHotpTwofaccount->legacy_uri = OtpTestData::HOTP_FULL_CUSTOM_URI;
- $this->customHotpTwofaccount->service = OtpTestData::SERVICE;
- $this->customHotpTwofaccount->account = OtpTestData::ACCOUNT;
- $this->customHotpTwofaccount->icon = OtpTestData::ICON;
- $this->customHotpTwofaccount->otp_type = 'hotp';
- $this->customHotpTwofaccount->secret = OtpTestData::SECRET;
- $this->customHotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
- $this->customHotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
- $this->customHotpTwofaccount->period = null;
- $this->customHotpTwofaccount->counter = OtpTestData::COUNTER_CUSTOM;
- $this->customHotpTwofaccount->save();
- // $this->group = new Group;
- // $this->group->name = 'MyGroup';
- // $this->group->save();
- }
- /**
- * @test
- */
- public function test_fill_with_custom_totp_uri_returns_correct_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI);
- $this->assertEquals('totp', $twofaccount->otp_type);
- $this->assertEquals(OtpTestData::TOTP_FULL_CUSTOM_URI, $twofaccount->legacy_uri);
- $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
- $this->assertEquals(OtpTestData::PERIOD_CUSTOM, $twofaccount->period);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
- $this->assertStringEndsWith('.png',$twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_fill_with_basic_totp_uri_returns_default_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI(OtpTestData::TOTP_SHORT_URI);
- $this->assertEquals('totp', $twofaccount->otp_type);
- $this->assertEquals(OtpTestData::TOTP_SHORT_URI, $twofaccount->legacy_uri);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(null, $twofaccount->service);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
- $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
- $this->assertEquals(null, $twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_fill_with_custom_hotp_uri_returns_correct_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI);
- $this->assertEquals('hotp', $twofaccount->otp_type);
- $this->assertEquals(OtpTestData::HOTP_FULL_CUSTOM_URI, $twofaccount->legacy_uri);
- $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
- $this->assertEquals(null, $twofaccount->period);
- $this->assertEquals(OtpTestData::COUNTER_CUSTOM, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
- $this->assertStringEndsWith('.png',$twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_fill_with_basic_hotp_uri_returns_default_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI(OtpTestData::HOTP_SHORT_URI);
- $this->assertEquals('hotp', $twofaccount->otp_type);
- $this->assertEquals(OtpTestData::HOTP_SHORT_URI, $twofaccount->legacy_uri);
- $this->assertEquals(null, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
- $this->assertEquals(null, $twofaccount->period);
- $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
- $this->assertEquals(null, $twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_filled_with_uri_persists_correct_values_to_db()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI(OtpTestData::TOTP_SHORT_URI);
- $twofaccount->save();
- $this->assertDatabaseHas('twofaccounts', [
- 'otp_type' => 'totp',
- 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
- 'service' => null,
- 'account' => OtpTestData::ACCOUNT,
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'icon' => null,
- ]);
- }
- /**
- * @test
- */
- public function test_fill_with_invalid_uri_returns_ValidationException()
- {
- $this->expectException(\Illuminate\Validation\ValidationException::class);
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI(OtpTestData::INVALID_OTPAUTH_URI);
- }
- /**
- * @test
- */
- public function test_fill_with_uri_without_label_returns_ValidationException()
- {
- $this->expectException(\Illuminate\Validation\ValidationException::class);
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithURI('otpauth://totp/?secret='.OtpTestData::SECRET);
- }
- /**
- * @test
- */
- public function test_create_custom_totp_from_parameters_returns_correct_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP);
- $this->assertEquals('totp', $twofaccount->otp_type);
- $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
- $this->assertEquals(OtpTestData::PERIOD_CUSTOM, $twofaccount->period);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
- $this->assertStringEndsWith('.png',$twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_create_basic_totp_from_parameters_returns_correct_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
- $this->assertEquals('totp', $twofaccount->otp_type);
- $this->assertEquals(null, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
- $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
- $this->assertEquals(null, $twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_create_custom_hotp_from_parameters_returns_correct_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP);
- $this->assertEquals('hotp', $twofaccount->otp_type);
- $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
- $this->assertEquals(null, $twofaccount->period);
- $this->assertEquals(OtpTestData::COUNTER_CUSTOM, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
- $this->assertStringEndsWith('.png',$twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_create_basic_hotp_from_parameters_returns_correct_value()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP);
- $this->assertEquals('hotp', $twofaccount->otp_type);
- $this->assertEquals(null, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
- $this->assertEquals(null, $twofaccount->period);
- $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
- $this->assertEquals(null, $twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_create_from_parameters_persists_correct_values_to_db()
- {
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
- $twofaccount->save();
- $this->assertDatabaseHas('twofaccounts', [
- 'otp_type' => 'totp',
- 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
- 'service' => null,
- 'account' => OtpTestData::ACCOUNT,
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'icon' => null,
- ]);
- }
-
- /**
- * @test
- */
- public function test_create_from_unsupported_parameters_returns_unsupportedOtpTypeException()
- {
- $this->expectException(\App\Exceptions\UnsupportedOtpTypeException::class);
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_PARAMETERS_FOR_UNSUPPORTED_OTP_TYPE);
- }
-
- /**
- * @test
- */
- public function test_create_from_invalid_parameters_type_returns_InvalidOtpParameterException()
- {
- $this->expectException(\App\Exceptions\InvalidOtpParameterException::class);
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters([
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'digits' => 'notsupported',
- ]);
- }
-
- /**
- * @test
- */
- public function test_create_from_invalid_parameters_returns_InvalidOtpParameterException()
- {
- $this->expectException(\App\Exceptions\InvalidOtpParameterException::class);
- $twofaccount = new TwoFAccount;
- $twofaccount->fillWithOtpParameters([
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'algorithm' => 'notsupported',
- ]);
- }
- /**
- * @test
- */
- public function test_update_totp_returns_updated_model()
- {
- $twofaccount = $this->customTotpTwofaccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
- $this->assertEquals('totp', $twofaccount->otp_type);
- $this->assertEquals(null, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
- $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(null, $twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_update_hotp_returns_updated_model()
- {
- $twofaccount = $this->customTotpTwofaccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP);
- $this->assertEquals('hotp', $twofaccount->otp_type);
- $this->assertEquals(null, $twofaccount->service);
- $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
- $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
- $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
- $this->assertEquals(null, $twofaccount->period);
- $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
- $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
- $this->assertEquals(null, $twofaccount->counter);
- $this->assertEquals(null, $twofaccount->icon);
- }
- /**
- * @test
- */
- public function test_update_totp_persists_updated_model()
- {
- $twofaccount = $this->customTotpTwofaccount;
- $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
- $twofaccount->save();
- $this->assertDatabaseHas('twofaccounts', [
- 'otp_type' => 'totp',
- 'service' => null,
- 'account' => OtpTestData::ACCOUNT,
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'icon' => null,
- ]);
- }
- /**
- * @test
- */
- public function test_getOTP_for_totp_returns_the_same_password()
- {
- $twofaccount = new TwoFAccount;
- $otp_from_model = $this->customTotpTwofaccount->getOTP();
- $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI)->getOTP();
- if ($otp_from_model->generated_at === $otp_from_uri->generated_at) {
- $this->assertEquals($otp_from_model, $otp_from_uri);
- }
- $otp_from_model = $this->customTotpTwofaccount->getOTP();
- $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)->getOTP();
- if ($otp_from_model->generated_at === $otp_from_parameters->generated_at) {
- $this->assertEquals($otp_from_model, $otp_from_parameters);
- }
- }
- /**
- * @test
- */
- public function test_getOTP_for_hotp_returns_the_same_password()
- {
- $twofaccount = new TwoFAccount;
- $otp_from_model = $this->customHotpTwofaccount->getOTP();
- $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI)->getOTP();
- $this->assertEquals($otp_from_model, $otp_from_uri);
- $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)->getOTP();
- $this->assertEquals($otp_from_model, $otp_from_parameters);
- }
- /**
- * @test
- */
- public function test_getOTP_for_totp_with_invalid_secret_returns_InvalidSecretException()
- {
- $twofaccount = new TwoFAccount;
- $this->expectException(\App\Exceptions\InvalidSecretException::class);
- $otp_from_uri = $twofaccount->fillWithURI('otpauth://totp/'.OtpTestData::ACCOUNT.'?secret=0')->getOTP();
- }
- /**
- * @test
- */
- public function test_getOTP_for_totp_with_undecipherable_secret_returns_UndecipherableException()
- {
- $twofaccount = new TwoFAccount;
- $this->expectException(\App\Exceptions\UndecipherableException::class);
- $otp_from_uri = $twofaccount->fillWithOtpParameters([
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => __('errors.indecipherable'),
- ])->getOTP();
- }
- /**
- * @test
- */
- public function test_getURI_for_custom_totp_model_returns_uri()
- {
- $uri = $this->customTotpTwofaccount->getURI();
-
- $this->assertStringContainsString('otpauth://totp/', $uri);
- $this->assertStringContainsString(OtpTestData::SERVICE, $uri);
- $this->assertStringContainsString(OtpTestData::ACCOUNT, $uri);
- $this->assertStringContainsString('secret='.OtpTestData::SECRET, $uri);
- $this->assertStringContainsString('digits='.OtpTestData::DIGITS_CUSTOM, $uri);
- $this->assertStringContainsString('period='.OtpTestData::PERIOD_CUSTOM, $uri);
- $this->assertStringContainsString('algorithm='.OtpTestData::ALGORITHM_CUSTOM, $uri);
- }
- /**
- * @test
- */
- public function test_getURI_for_custom_hotp_model_returns_uri()
- {
- $uri = $this->customHotpTwofaccount->getURI();
-
- $this->assertStringContainsString('otpauth://hotp/', $uri);
- $this->assertStringContainsString(OtpTestData::SERVICE, $uri);
- $this->assertStringContainsString(OtpTestData::ACCOUNT, $uri);
- $this->assertStringContainsString('secret='.OtpTestData::SECRET, $uri);
- $this->assertStringContainsString('digits='.OtpTestData::DIGITS_CUSTOM, $uri);
- $this->assertStringContainsString('counter='.OtpTestData::COUNTER_CUSTOM, $uri);
- $this->assertStringContainsString('algorithm='.OtpTestData::ALGORITHM_CUSTOM, $uri);
- }
- }
|