SettingServiceTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. namespace Tests\Feature\Services;
  3. use App\Facades\Settings;
  4. use App\Models\TwoFAccount;
  5. use App\Services\SettingService;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Crypt;
  8. use Illuminate\Support\Facades\DB;
  9. use PHPUnit\Framework\Attributes\CoversClass;
  10. use PHPUnit\Framework\Attributes\DataProvider;
  11. use Tests\FeatureTestCase;
  12. /**
  13. * SettingServiceTest test class
  14. */
  15. #[CoversClass(SettingService::class)]
  16. #[CoversClass(Settings::class)]
  17. class SettingServiceTest extends FeatureTestCase
  18. {
  19. /**
  20. * App\Models\Group $groupOne, $groupTwo
  21. */
  22. protected $twofaccountOne;
  23. protected $twofaccountTwo;
  24. private const KEY = 'key';
  25. private const VALUE = 'value';
  26. private const SETTING_NAME = 'MySetting';
  27. private const SETTING_NAME_ALT = 'MySettingAlt';
  28. private const SETTING_VALUE_STRING = 'MyValue';
  29. private const SETTING_VALUE_TRUE_TRANSFORMED = '{{1}}';
  30. private const SETTING_VALUE_FALSE_TRANSFORMED = '{{}}';
  31. private const SETTING_VALUE_INT = 10;
  32. private const SETTING_VALUE_FLOAT = 10.5;
  33. private const ACCOUNT = 'account';
  34. private const SERVICE = 'service';
  35. private const SECRET = 'A4GRFHVVRBGY7UIW';
  36. private const ALGORITHM_CUSTOM = 'sha256';
  37. private const DIGITS_CUSTOM = 7;
  38. private const PERIOD_CUSTOM = 40;
  39. private const IMAGE = 'https%3A%2F%2Fen.opensuse.org%2Fimages%2F4%2F44%2FButton-filled-colour.png';
  40. private const ICON = 'test.png';
  41. 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;
  42. /**
  43. * @test
  44. */
  45. public function setUp() : void
  46. {
  47. parent::setUp();
  48. $this->twofaccountOne = new TwoFAccount;
  49. $this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
  50. $this->twofaccountOne->service = self::SERVICE;
  51. $this->twofaccountOne->account = self::ACCOUNT;
  52. $this->twofaccountOne->icon = self::ICON;
  53. $this->twofaccountOne->otp_type = 'totp';
  54. $this->twofaccountOne->secret = self::SECRET;
  55. $this->twofaccountOne->digits = self::DIGITS_CUSTOM;
  56. $this->twofaccountOne->algorithm = self::ALGORITHM_CUSTOM;
  57. $this->twofaccountOne->period = self::PERIOD_CUSTOM;
  58. $this->twofaccountOne->counter = null;
  59. $this->twofaccountOne->save();
  60. $this->twofaccountTwo = new TwoFAccount;
  61. $this->twofaccountTwo->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
  62. $this->twofaccountTwo->service = self::SERVICE;
  63. $this->twofaccountTwo->account = self::ACCOUNT;
  64. $this->twofaccountTwo->icon = self::ICON;
  65. $this->twofaccountTwo->otp_type = 'totp';
  66. $this->twofaccountTwo->secret = self::SECRET;
  67. $this->twofaccountTwo->digits = self::DIGITS_CUSTOM;
  68. $this->twofaccountTwo->algorithm = self::ALGORITHM_CUSTOM;
  69. $this->twofaccountTwo->period = self::PERIOD_CUSTOM;
  70. $this->twofaccountTwo->counter = null;
  71. $this->twofaccountTwo->save();
  72. }
  73. /**
  74. * @test
  75. */
  76. public function test_get_string_setting_returns_correct_value()
  77. {
  78. Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
  79. $this->assertEquals(self::SETTING_VALUE_STRING, Settings::get(self::SETTING_NAME));
  80. }
  81. /**
  82. * @test
  83. */
  84. public function test_get_boolean_setting_returns_true()
  85. {
  86. Settings::set(self::SETTING_NAME, self::SETTING_VALUE_TRUE_TRANSFORMED);
  87. $this->assertEquals(true, Settings::get(self::SETTING_NAME));
  88. }
  89. /**
  90. * @test
  91. */
  92. public function test_get_boolean_setting_returns_false()
  93. {
  94. Settings::set(self::SETTING_NAME, self::SETTING_VALUE_FALSE_TRANSFORMED);
  95. $this->assertEquals(false, Settings::get(self::SETTING_NAME));
  96. }
  97. /**
  98. * @test
  99. */
  100. public function test_get_int_setting_returns_int()
  101. {
  102. Settings::set(self::SETTING_NAME, self::SETTING_VALUE_INT);
  103. $value = Settings::get(self::SETTING_NAME);
  104. $this->assertEquals(self::SETTING_VALUE_INT, $value);
  105. $this->assertIsInt($value);
  106. }
  107. /**
  108. * @test
  109. */
  110. public function test_get_float_setting_returns_float()
  111. {
  112. Settings::set(self::SETTING_NAME, self::SETTING_VALUE_FLOAT);
  113. $value = Settings::get(self::SETTING_NAME);
  114. $this->assertEquals(self::SETTING_VALUE_FLOAT, $value);
  115. $this->assertIsFloat($value);
  116. }
  117. /**
  118. * @test
  119. */
  120. public function test_all_returns_default_and_overloaded_settings()
  121. {
  122. $default_options = config('2fauth.settings');
  123. Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
  124. $all = Settings::all();
  125. $this->assertArrayHasKey(self::SETTING_NAME, $all);
  126. $this->assertEquals($all[self::SETTING_NAME], self::SETTING_VALUE_STRING);
  127. foreach ($default_options as $key => $val) {
  128. $this->assertArrayHasKey($key, $all);
  129. $this->assertEquals($all[$key], $val);
  130. }
  131. }
  132. /**
  133. * @test
  134. */
  135. public function test_set_setting_persist_correct_value_in_db_and_cache()
  136. {
  137. $value = Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
  138. $cached = Cache::get(SettingService::CACHE_ITEM_NAME); // returns a Collection
  139. $this->assertDatabaseHas('options', [
  140. self::KEY => self::SETTING_NAME,
  141. self::VALUE => self::SETTING_VALUE_STRING,
  142. ]);
  143. $this->assertEquals($cached->get(self::SETTING_NAME), self::SETTING_VALUE_STRING);
  144. }
  145. /**
  146. * @test
  147. */
  148. public function test_set_useEncryption_on_encrypts_all_accounts()
  149. {
  150. Settings::set('useEncryption', true);
  151. $twofaccounts = DB::table('twofaccounts')->get();
  152. $twofaccounts->each(function ($item, $key) {
  153. $this->assertEquals(self::ACCOUNT, Crypt::decryptString($item->account));
  154. $this->assertEquals(self::SECRET, Crypt::decryptString($item->secret));
  155. $this->assertEquals(self::TOTP_FULL_CUSTOM_URI, Crypt::decryptString($item->legacy_uri));
  156. });
  157. }
  158. /**
  159. * @test
  160. */
  161. public function test_set_useEncryption_on_twice_prevents_successive_encryption()
  162. {
  163. Settings::set('useEncryption', true);
  164. Settings::set('useEncryption', true);
  165. $twofaccounts = DB::table('twofaccounts')->get();
  166. $twofaccounts->each(function ($item, $key) {
  167. $this->assertEquals(self::ACCOUNT, Crypt::decryptString($item->account));
  168. $this->assertEquals(self::SECRET, Crypt::decryptString($item->secret));
  169. $this->assertEquals(self::TOTP_FULL_CUSTOM_URI, Crypt::decryptString($item->legacy_uri));
  170. });
  171. }
  172. /**
  173. * @test
  174. */
  175. public function test_set_useEncryption_off_decrypts_all_accounts()
  176. {
  177. Settings::set('useEncryption', true);
  178. Settings::set('useEncryption', false);
  179. $twofaccounts = DB::table('twofaccounts')->get();
  180. $twofaccounts->each(function ($item, $key) {
  181. $this->assertEquals(self::ACCOUNT, $item->account);
  182. $this->assertEquals(self::SECRET, $item->secret);
  183. $this->assertEquals(self::TOTP_FULL_CUSTOM_URI, $item->legacy_uri);
  184. });
  185. }
  186. /**
  187. * @test
  188. */
  189. #[DataProvider('provideUndecipherableData')]
  190. public function test_set_useEncryption_off_returns_exception_when_data_are_undecipherable(array $data)
  191. {
  192. $this->expectException(\App\Exceptions\DbEncryptionException::class);
  193. Settings::set('useEncryption', true);
  194. $affected = DB::table('twofaccounts')
  195. ->where('id', $this->twofaccountOne->id)
  196. ->update($data);
  197. Settings::set('useEncryption', false);
  198. $twofaccount = TwoFAccount::find($this->twofaccountOne->id);
  199. }
  200. /**
  201. * Provide invalid data for validation test
  202. */
  203. public static function provideUndecipherableData() : array
  204. {
  205. return [
  206. [[
  207. 'account' => 'undecipherableString',
  208. ]],
  209. [[
  210. 'secret' => 'undecipherableString',
  211. ]],
  212. [[
  213. 'legacy_uri' => 'undecipherableString',
  214. ]],
  215. ];
  216. }
  217. /**
  218. * @test
  219. */
  220. public function test_set_array_of_settings_persist_correct_values()
  221. {
  222. $value = Settings::set([
  223. self::SETTING_NAME => self::SETTING_VALUE_STRING,
  224. self::SETTING_NAME_ALT => self::SETTING_VALUE_INT,
  225. ]);
  226. $cached = Cache::get(SettingService::CACHE_ITEM_NAME); // returns a Collection
  227. $this->assertDatabaseHas('options', [
  228. self::KEY => self::SETTING_NAME,
  229. self::VALUE => self::SETTING_VALUE_STRING,
  230. ]);
  231. $this->assertDatabaseHas('options', [
  232. self::KEY => self::SETTING_NAME_ALT,
  233. self::VALUE => self::SETTING_VALUE_INT,
  234. ]);
  235. $this->assertEquals($cached->get(self::SETTING_NAME), self::SETTING_VALUE_STRING);
  236. $this->assertEquals($cached->get(self::SETTING_NAME_ALT), self::SETTING_VALUE_INT);
  237. }
  238. /**
  239. * @test
  240. */
  241. public function test_set_true_setting_persist_transformed_boolean()
  242. {
  243. $value = Settings::set(self::SETTING_NAME, true);
  244. $this->assertDatabaseHas('options', [
  245. self::KEY => self::SETTING_NAME,
  246. self::VALUE => self::SETTING_VALUE_TRUE_TRANSFORMED,
  247. ]);
  248. }
  249. /**
  250. * @test
  251. */
  252. public function test_set_false_setting_persist_transformed_boolean()
  253. {
  254. $value = Settings::set(self::SETTING_NAME, false);
  255. $this->assertDatabaseHas('options', [
  256. self::KEY => self::SETTING_NAME,
  257. self::VALUE => self::SETTING_VALUE_FALSE_TRANSFORMED,
  258. ]);
  259. }
  260. /**
  261. * @test
  262. */
  263. public function test_del_remove_setting_from_db_and_cache()
  264. {
  265. DB::table('options')->insert(
  266. [self::KEY => self::SETTING_NAME, self::VALUE => strval(self::SETTING_VALUE_STRING)]
  267. );
  268. Settings::delete(self::SETTING_NAME);
  269. $cached = Cache::get(SettingService::CACHE_ITEM_NAME); // returns a Collection
  270. $this->assertDatabaseMissing('options', [
  271. self::KEY => self::SETTING_NAME,
  272. self::VALUE => self::SETTING_VALUE_STRING,
  273. ]);
  274. $this->assertFalse($cached->has(self::SETTING_NAME));
  275. }
  276. /**
  277. * @test
  278. */
  279. public function test_isEdited_returns_true()
  280. {
  281. DB::table('options')->insert(
  282. [self::KEY => 'showOtpAsDot', self::VALUE => strval(self::SETTING_VALUE_TRUE_TRANSFORMED)]
  283. );
  284. $this->assertTrue(Settings::isEdited('showOtpAsDot'));
  285. }
  286. /**
  287. * @test
  288. */
  289. public function test_isEdited_returns_false()
  290. {
  291. DB::table('options')->where(self::KEY, 'showOtpAsDot')->delete();
  292. $this->assertFalse(Settings::isEdited('showOtpAsDot'));
  293. }
  294. /**
  295. * @test
  296. */
  297. public function test_cache_is_requested_at_instanciation()
  298. {
  299. Cache::shouldReceive('remember')
  300. ->andReturn(collect([]));
  301. $settingService = new SettingService();
  302. Cache::shouldHaveReceived('remember');
  303. }
  304. /**
  305. * @test
  306. */
  307. public function test_cache_is_updated_when_setting_is_set()
  308. {
  309. Cache::shouldReceive('remember', 'put')
  310. ->andReturn(collect([]), true);
  311. $settingService = new SettingService();
  312. $settingService->set(self::SETTING_NAME, self::SETTING_VALUE_STRING);
  313. Cache::shouldHaveReceived('put');
  314. }
  315. /**
  316. * @test
  317. */
  318. public function test_cache_is_updated_when_setting_is_deleted()
  319. {
  320. Cache::shouldReceive('remember', 'put')
  321. ->andReturn(collect([]), true);
  322. $settingService = new SettingService();
  323. $settingService->delete(self::SETTING_NAME);
  324. Cache::shouldHaveReceived('put');
  325. }
  326. }