TwoFAccountModelTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Models\TwoFAccount;
  4. use App\Events\TwoFAccountDeleted;
  5. use Tests\ModelTestCase;
  6. use Illuminate\Support\Facades\Event;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Support\Facades\Crypt;
  9. /**
  10. * @covers \App\Models\TwoFAccount
  11. */
  12. class TwoFAccountModelTest extends ModelTestCase
  13. {
  14. /**
  15. * @test
  16. */
  17. public function test_model_configuration()
  18. {
  19. $this->runConfigurationAssertions(
  20. new TwoFAccount(),
  21. [
  22. 'service',
  23. 'account',
  24. 'otp_type',
  25. 'digits',
  26. 'secret',
  27. 'algorithm',
  28. 'counter',
  29. 'period',
  30. 'icon'
  31. ],
  32. [],
  33. ['*'],
  34. [],
  35. ['id' => 'int'],
  36. ['deleted' => TwoFAccountDeleted::class],
  37. ['created_at', 'updated_at'],
  38. \Illuminate\Database\Eloquent\Collection::class,
  39. 'twofaccounts',
  40. 'id',
  41. true
  42. );
  43. }
  44. /**
  45. * @test
  46. *
  47. * @dataProvider provideSensitiveAttributes
  48. */
  49. public function test_sensitive_attributes_are_stored_encrypted(string $attribute)
  50. {
  51. \Facades\App\Services\SettingService::shouldReceive('get')
  52. ->with('useEncryption')
  53. ->andReturn(true);
  54. $twofaccount = TwoFAccount::factory()->make([
  55. $attribute => 'string',
  56. ]);
  57. $this->assertEquals('string', Crypt::decryptString($twofaccount->getAttributes()[$attribute]));
  58. }
  59. /**
  60. * Provide attributes to test for encryption
  61. */
  62. public function provideSensitiveAttributes() : array
  63. {
  64. return [
  65. [
  66. 'legacy_uri'
  67. ],
  68. [
  69. 'secret'
  70. ],
  71. [
  72. 'account'
  73. ],
  74. ];
  75. }
  76. /**
  77. * @test
  78. *
  79. * @dataProvider provideSensitiveAttributes
  80. */
  81. public function test_sensitive_attributes_are_returned_clear(string $attribute)
  82. {
  83. \Facades\App\Services\SettingService::shouldReceive('get')
  84. ->with('useEncryption')
  85. ->andReturn(false);
  86. $twofaccount = TwoFAccount::factory()->make();
  87. $this->assertEquals($twofaccount->getAttributes()[$attribute], $twofaccount->$attribute);
  88. }
  89. /**
  90. * @test
  91. *
  92. * @dataProvider provideSensitiveAttributes
  93. */
  94. public function test_indecipherable_attributes_returns_masked_value(string $attribute)
  95. {
  96. \Facades\App\Services\SettingService::shouldReceive('get')
  97. ->with('useEncryption')
  98. ->andReturn(true);
  99. Crypt::shouldReceive('encryptString')
  100. ->andReturn('indecipherableString');
  101. $twofaccount = TwoFAccount::factory()->make();
  102. $this->assertEquals(__('errors.indecipherable'), $twofaccount->$attribute);
  103. }
  104. }