SettingServiceTest.php 12 KB

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