UserModelTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. namespace Tests\Feature\Models;
  3. use App\Facades\IconStore;
  4. use App\Models\AuthLog;
  5. use App\Models\Dto\IconDto;
  6. use App\Models\Group;
  7. use App\Models\TwoFAccount;
  8. use App\Models\User;
  9. use App\Observers\UserObserver;
  10. use Database\Factories\AuthLogFactory;
  11. use Illuminate\Auth\Events\PasswordReset;
  12. use Illuminate\Support\Facades\Artisan;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Event;
  15. use Illuminate\Support\Facades\Password;
  16. use Illuminate\Support\Facades\Storage;
  17. use PHPUnit\Framework\Attributes\CoversClass;
  18. use PHPUnit\Framework\Attributes\Test;
  19. use Tests\Data\OtpTestData;
  20. use Tests\FeatureTestCase;
  21. /**
  22. * UserModelTest test class
  23. */
  24. #[CoversClass(User::class)]
  25. #[CoversClass(UserObserver::class)]
  26. class UserModelTest extends FeatureTestCase
  27. {
  28. #[Test]
  29. public function test_admin_scope_returns_only_admin()
  30. {
  31. User::factory()->count(4)->create();
  32. $firstAdmin = User::factory()->administrator()->create([
  33. 'name' => 'first',
  34. ]);
  35. $secondAdmin = User::factory()->administrator()->create([
  36. 'name' => 'secondAdmin',
  37. ]);
  38. $admins = User::admins()->get();
  39. $this->assertCount(2, $admins);
  40. $this->assertEquals($admins[0]->is_admin, true);
  41. $this->assertEquals($admins[1]->is_admin, true);
  42. $this->assertEquals($admins[0]->name, $firstAdmin->name);
  43. $this->assertEquals($admins[1]->name, $secondAdmin->name);
  44. }
  45. #[Test]
  46. public function test_isAdministrator_returns_correct_state()
  47. {
  48. $user = User::factory()->create();
  49. $admin = User::factory()->administrator()->create();
  50. $this->assertEquals($user->isAdministrator(), false);
  51. $this->assertEquals($admin->isAdministrator(), true);
  52. }
  53. #[Test]
  54. public function test_promoteToAdministrator_sets_administrator_status()
  55. {
  56. $user = User::factory()->create();
  57. $user->promoteToAdministrator();
  58. $this->assertEquals($user->isAdministrator(), true);
  59. }
  60. #[Test]
  61. public function test_promoteToAdministrator_demote_administrator_status()
  62. {
  63. $admin = User::factory()->administrator()->create();
  64. // We need another admin to prevent demoting event returning false
  65. // and blocking the demotion
  66. $another_admin = User::factory()->administrator()->create();
  67. $admin->promoteToAdministrator(false);
  68. $admin->save();
  69. $this->assertFalse($admin->isAdministrator());
  70. }
  71. #[Test]
  72. public function test_resetPassword_resets_password_with_success()
  73. {
  74. $user = User::factory()->create();
  75. $oldPassword = $user->password;
  76. $user->resetPassword();
  77. $this->assertNotEquals($user->password, $oldPassword);
  78. }
  79. #[Test]
  80. public function test_resetPassword_dispatch_event()
  81. {
  82. Event::fake();
  83. $user = User::factory()->create();
  84. Event::assertDispatched(
  85. PasswordReset::class,
  86. $user->resetPassword()
  87. );
  88. }
  89. #[Test]
  90. public function test_delete_removes_user_data()
  91. {
  92. Artisan::call('passport:install', [
  93. '--verbose' => 2,
  94. '--no-interaction' => 1,
  95. ]);
  96. $user = User::factory()->create();
  97. TwoFAccount::factory()->for($user)->create();
  98. AuthLog::factory()->for($user, 'authenticatable')->create();
  99. Group::factory()->for($user)->create();
  100. DB::table('webauthn_credentials')->insert([
  101. 'id' => '-VOLFKPY-_FuMI_sJ7gMllK76L3VoRUINj6lL_Z3qDg',
  102. 'authenticatable_type' => \App\Models\User::class,
  103. 'authenticatable_id' => $user->id,
  104. 'user_id' => 'e8af6f703f8042aa91c30cf72289aa07',
  105. 'counter' => 0,
  106. 'rp_id' => 'http://localhost',
  107. 'origin' => 'http://localhost',
  108. 'aaguid' => '00000000-0000-0000-0000-000000000000',
  109. 'attestation_format' => 'none',
  110. 'public_key' => 'eyJpdiI6Imp0U0NVeFNNbW45KzEvMXpad2p2SUE9PSIsInZhbHVlIjoic0VxZ2I1WnlHM2lJakhkWHVkK2kzMWtibk1IN2ZlaExGT01qOElXMDdRTjhnVlR0TDgwOHk1S0xQUy9BQ1JCWHRLNzRtenNsMml1dVQydWtERjFEU0h0bkJGT2RwUXE1M1JCcVpablE2Y2VGV2YvVEE2RGFIRUE5L0x1K0JIQXhLVE1aNVNmN3AxeHdjRUo2V0hwREZSRTJYaThNNnB1VnozMlVXZEVPajhBL3d3ODlkTVN3bW54RTEwSG0ybzRQZFFNNEFrVytUYThub2IvMFRtUlBZamoyZElWKzR1bStZQ1IwU3FXbkYvSm1FU2FlMTFXYUo0SG9kc1BDME9CNUNKeE9IelE5d2dmNFNJRXBKNUdlVzJ3VHUrQWJZRFluK0hib0xvVTdWQ0ZISjZmOWF3by83aVJES1dxbU9Zd1lhRTlLVmhZSUdlWmlBOUFtcTM2ZVBaRWNKNEFSQUhENk5EaC9hN3REdnVFbm16WkRxekRWOXd4cVcvZFdKa2tlWWJqZWlmZnZLS0F1VEVCZEZQcXJkTExiNWRyQmxsZWtaSDRlT3VVS0ZBSXFBRG1JMjRUMnBKRXZxOUFUa2xxMjg2TEplUzdscVo2UytoVU5SdXk1OE1lcFN6aU05ZkVXTkdIM2tKM3Q5bmx1TGtYb1F5bGxxQVR3K3BVUVlia1VybDFKRm9lZDViNzYraGJRdmtUb2FNTEVGZmZYZ3lYRDRiOUVjRnJpcTVvWVExOHJHSTJpMnVBZ3E0TmljbUlKUUtXY2lSWDh1dE5MVDNRUzVRSkQrTjVJUU8rSGhpeFhRRjJvSEdQYjBoVT0iLCJtYWMiOiI5MTdmNWRkZGE5OTEwNzQ3MjhkYWVhYjRlNjk0MWZlMmI5OTQ4YzlmZWI1M2I4OGVkMjE1MjMxNjUwOWRmZTU2IiwidGFnIjoiIn0=',
  111. 'updated_at' => now(),
  112. 'created_at' => now(),
  113. ]);
  114. $user->createToken('myToken', []);
  115. Password::broker('webauthn')->createToken($user);
  116. Password::broker()->createToken($user);
  117. $user->delete();
  118. $this->assertDatabaseMissing('twofaccounts', [
  119. 'user_id' => $user->id,
  120. ]);
  121. $this->assertDatabaseMissing('groups', [
  122. 'user_id' => $user->id,
  123. ]);
  124. $this->assertDatabaseMissing('webauthn_credentials', [
  125. 'authenticatable_id' => $user->id,
  126. ]);
  127. $this->assertDatabaseMissing(config('auth.passwords.webauthn.table'), [
  128. 'email' => $user->email,
  129. ]);
  130. $this->assertDatabaseMissing('oauth_access_tokens', [
  131. 'user_id' => $user->id,
  132. ]);
  133. $this->assertDatabaseMissing(config('auth.passwords.users.table'), [
  134. 'email' => $user->email,
  135. ]);
  136. $this->assertDatabaseMissing('auth_logs', [
  137. 'authenticatable_id' => $user->id,
  138. ]);
  139. }
  140. #[Test]
  141. public function test_delete_flushes_icons_of_user_twofaccounts()
  142. {
  143. Storage::fake('icons');
  144. $user = User::factory()->create();
  145. $twofaccount = TwoFAccount::factory()->for($user)->create([
  146. 'icon' => OtpTestData::ICON_PNG,
  147. ]);
  148. IconStore::store(OtpTestData::ICON_PNG, base64_decode(OtpTestData::ICON_PNG_DATA));
  149. Storage::disk('icons')->assertExists($twofaccount->icon);
  150. $user->delete();
  151. Storage::disk('icons')->assertMissing($twofaccount->icon);
  152. }
  153. #[Test]
  154. public function test_delete_does_not_delete_the_only_admin()
  155. {
  156. $admin = User::factory()->administrator()->create();
  157. $this->assertEquals(1, User::admins()->count());
  158. $isDeleted = $admin->delete();
  159. $this->assertFalse($isDeleted);
  160. }
  161. #[Test]
  162. public function test_getFromCredentialId_retreives_the_user()
  163. {
  164. $user = User::factory()->create();
  165. DB::table('webauthn_credentials')->insert([
  166. 'id' => '-VOLFKPY-_FuMI_sJ7gMllK76L3VoRUINj6lL_Z3qDg',
  167. 'authenticatable_type' => \App\Models\User::class,
  168. 'authenticatable_id' => $user->id,
  169. 'user_id' => 'e8af6f703f8042aa91c30cf72289aa07',
  170. 'counter' => 0,
  171. 'rp_id' => 'http://localhost',
  172. 'origin' => 'http://localhost',
  173. 'aaguid' => '00000000-0000-0000-0000-000000000000',
  174. 'attestation_format' => 'none',
  175. 'public_key' => 'eyJpdiI6Imp0U0NVeFNNbW45KzEvMXpad2p2SUE9PSIsInZhbHVlIjoic0VxZ2I1WnlHM2lJakhkWHVkK2kzMWtibk1IN2ZlaExGT01qOElXMDdRTjhnVlR0TDgwOHk1S0xQUy9BQ1JCWHRLNzRtenNsMml1dVQydWtERjFEU0h0bkJGT2RwUXE1M1JCcVpablE2Y2VGV2YvVEE2RGFIRUE5L0x1K0JIQXhLVE1aNVNmN3AxeHdjRUo2V0hwREZSRTJYaThNNnB1VnozMlVXZEVPajhBL3d3ODlkTVN3bW54RTEwSG0ybzRQZFFNNEFrVytUYThub2IvMFRtUlBZamoyZElWKzR1bStZQ1IwU3FXbkYvSm1FU2FlMTFXYUo0SG9kc1BDME9CNUNKeE9IelE5d2dmNFNJRXBKNUdlVzJ3VHUrQWJZRFluK0hib0xvVTdWQ0ZISjZmOWF3by83aVJES1dxbU9Zd1lhRTlLVmhZSUdlWmlBOUFtcTM2ZVBaRWNKNEFSQUhENk5EaC9hN3REdnVFbm16WkRxekRWOXd4cVcvZFdKa2tlWWJqZWlmZnZLS0F1VEVCZEZQcXJkTExiNWRyQmxsZWtaSDRlT3VVS0ZBSXFBRG1JMjRUMnBKRXZxOUFUa2xxMjg2TEplUzdscVo2UytoVU5SdXk1OE1lcFN6aU05ZkVXTkdIM2tKM3Q5bmx1TGtYb1F5bGxxQVR3K3BVUVlia1VybDFKRm9lZDViNzYraGJRdmtUb2FNTEVGZmZYZ3lYRDRiOUVjRnJpcTVvWVExOHJHSTJpMnVBZ3E0TmljbUlKUUtXY2lSWDh1dE5MVDNRUzVRSkQrTjVJUU8rSGhpeFhRRjJvSEdQYjBoVT0iLCJtYWMiOiI5MTdmNWRkZGE5OTEwNzQ3MjhkYWVhYjRlNjk0MWZlMmI5OTQ4YzlmZWI1M2I4OGVkMjE1MjMxNjUwOWRmZTU2IiwidGFnIjoiIn0=',
  176. 'updated_at' => now(),
  177. 'created_at' => now(),
  178. ]);
  179. $searched = User::getFromCredentialId('-VOLFKPY-_FuMI_sJ7gMllK76L3VoRUINj6lL_Z3qDg');
  180. $this->assertEquals($user->id, $searched->id);
  181. }
  182. #[Test]
  183. public function test_authentications_returns_user_auth_logs_sorted_by_latest_id()
  184. {
  185. $user = User::factory()->create();
  186. $tenDaysAgoAuthLog = AuthLog::factory()->daysAgo(10)->for($user, 'authenticatable')->create();
  187. $fiveDaysAgoAuthLog = AuthLog::factory()->daysAgo(5)->for($user, 'authenticatable')->create();
  188. $lastAuthLog = AuthLog::factory()->daysAgo(1)->for($user, 'authenticatable')->create();
  189. $authentications = $user->authentications()->get();
  190. $this->assertCount(3, $authentications);
  191. $this->assertEquals($lastAuthLog->id, $authentications[0]->id);
  192. $this->assertEquals($fiveDaysAgoAuthLog->id, $authentications[1]->id);
  193. $this->assertEquals($tenDaysAgoAuthLog->id, $authentications[2]->id);
  194. }
  195. #[Test]
  196. public function test_authentications_returns_user_auth_logs_only()
  197. {
  198. $user = User::factory()->create();
  199. $anotherUser = User::factory()->create();
  200. $userAuthLog = AuthLog::factory()->daysAgo(10)->for($user, 'authenticatable')->create();
  201. AuthLog::factory()->daysAgo(5)->for($anotherUser, 'authenticatable')->create();
  202. $authentications = $user->authentications()->get();
  203. $this->assertCount(1, $authentications);
  204. $this->assertEquals($userAuthLog->id, $authentications[0]->id);
  205. }
  206. #[Test]
  207. public function test_authenticationsByPeriod_returns_last_month_auth_logs()
  208. {
  209. $user = User::factory()->create();
  210. $twoMonthsAgoAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
  211. $duringLastMonthAuthLog = AuthLog::factory()->duringLastMonth()->for($user, 'authenticatable')->create();
  212. $authentications = $user->authenticationsByPeriod(1);
  213. $this->assertCount(1, $authentications);
  214. $this->assertEquals($duringLastMonthAuthLog->id, $authentications[0]->id);
  215. }
  216. #[Test]
  217. public function test_authenticationsByPeriod_returns_last_three_months_auth_logs()
  218. {
  219. $user = User::factory()->create();
  220. $sixMonthsAgoAuthLog = AuthLog::factory()->duringLastSixMonth()->for($user, 'authenticatable')->create();
  221. $threeMonthsAgoAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
  222. $duringLastMonthAuthLog = AuthLog::factory()->duringLastMonth()->for($user, 'authenticatable')->create();
  223. $authentications = $user->authenticationsByPeriod(3);
  224. $this->assertCount(2, $authentications);
  225. $this->assertEquals($duringLastMonthAuthLog->id, $authentications[0]->id);
  226. $this->assertEquals($threeMonthsAgoAuthLog->id, $authentications[1]->id);
  227. }
  228. #[Test]
  229. public function test_latestAuthentication_returns_user_latest_auth_logs()
  230. {
  231. $user = User::factory()->create();
  232. $twoMonthsAgoAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
  233. $duringLastMonthAuthLog = AuthLog::factory()->duringLastMonth()->for($user, 'authenticatable')->create();
  234. $authentications = $user->latestAuthentication()->get();
  235. $this->assertCount(1, $authentications);
  236. $this->assertEquals($duringLastMonthAuthLog->id, $authentications[0]->id);
  237. }
  238. #[Test]
  239. public function test_latestAuthentication_returns_user_latest_auth_logs_only()
  240. {
  241. $user = User::factory()->create();
  242. $anotherUser = User::factory()->create();
  243. $userAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
  244. $anotherUserAuthLog = AuthLog::factory()->duringLastMonth()->for($anotherUser, 'authenticatable')->create();
  245. $authentications = $user->latestAuthentication()->get();
  246. $this->assertCount(1, $authentications);
  247. $this->assertEquals($userAuthLog->id, $authentications[0]->id);
  248. }
  249. #[Test]
  250. public function test_lastLoginAt_returns_user_last_auth_date()
  251. {
  252. $user = User::factory()->create();
  253. $now = now();
  254. $tenDaysAgoAuthLog = AuthLog::factory()->daysAgo(10)->for($user, 'authenticatable')->create();
  255. $fiveDaysAgoAuthLog = AuthLog::factory()->daysAgo(5)->for($user, 'authenticatable')->create();
  256. $lastAuthLog = AuthLog::factory()->at($now)->for($user, 'authenticatable')->create();
  257. $lastLoginAt = $user->lastLoginAt();
  258. $this->assertEquals($lastLoginAt->startOfSecond(), $now->startOfSecond());
  259. }
  260. #[Test]
  261. public function test_lastLoginAt_returns_null_if_user_has_no_login()
  262. {
  263. $user = User::factory()->create();
  264. AuthLog::factory()->logoutOnly()->for($user, 'authenticatable')->create();
  265. $lastLoginAt = $user->lastLoginAt();
  266. $this->assertNull($lastLoginAt);
  267. }
  268. #[Test]
  269. public function test_lastSuccessfulLoginAt_returns_user_last_successful_login_date()
  270. {
  271. $user = User::factory()->create();
  272. $now = now();
  273. AuthLog::factory()->at($now)->for($user, 'authenticatable')->create();
  274. $lastSuccessfulLoginAt = $user->lastSuccessfulLoginAt();
  275. $this->assertEquals($lastSuccessfulLoginAt->startOfSecond(), $now->startOfSecond());
  276. }
  277. #[Test]
  278. public function test_lastSuccessfulLoginAt_returns_null_if_user_has_no_successful_login()
  279. {
  280. $user = User::factory()->create();
  281. $now = now();
  282. AuthLog::factory()->at($now)->failedLogin()->for($user, 'authenticatable')->create();
  283. $lastSuccessfulLoginAt = $user->lastSuccessfulLoginAt();
  284. $this->assertNull($lastSuccessfulLoginAt);
  285. }
  286. #[Test]
  287. public function test_lastLoginIp_returns_user_last_login_ip()
  288. {
  289. $user = User::factory()->create();
  290. AuthLog::factory()->for($user, 'authenticatable')->create();
  291. $lastLoginIp = $user->lastLoginIp();
  292. $this->assertEquals(AuthLogFactory::IP, $lastLoginIp);
  293. }
  294. #[Test]
  295. public function test_lastLoginIp_returns_null_if_user_has_no_auth_log()
  296. {
  297. $user = User::factory()->create();
  298. $lastLoginIp = $user->lastLoginIp();
  299. $this->assertNull($lastLoginIp);
  300. }
  301. #[Test]
  302. public function test_lastSuccessfulLoginIp_returns_user_last_successful_login_ip()
  303. {
  304. $user = User::factory()->create();
  305. AuthLog::factory()->for($user, 'authenticatable')->create();
  306. $lastSuccessfulLoginIp = $user->lastSuccessfulLoginIp();
  307. $this->assertEquals(AuthLogFactory::IP, $lastSuccessfulLoginIp);
  308. }
  309. #[Test]
  310. public function test_lastSuccessfulLoginIp_returns_null_if_user_has_no_successful_login()
  311. {
  312. $user = User::factory()->create();
  313. AuthLog::factory()->failedLogin()->for($user, 'authenticatable')->create();
  314. $lastSuccessfulLoginIp = $user->lastSuccessfulLoginIp();
  315. $this->assertNull($lastSuccessfulLoginIp);
  316. }
  317. #[Test]
  318. public function test_previousLoginAt_returns_user_last_auth_date()
  319. {
  320. $user = User::factory()->create();
  321. $now = now();
  322. $yesterday = now()->subDay();
  323. $yesterdayAuthLog = AuthLog::factory()->at($yesterday)->for($user, 'authenticatable')->create();
  324. $lastAuthLog = AuthLog::factory()->at($now)->for($user, 'authenticatable')->create();
  325. $previousLoginAt = $user->previousLoginAt();
  326. $this->assertEquals($previousLoginAt->startOfSecond(), $yesterday->startOfSecond());
  327. }
  328. #[Test]
  329. public function test_previousLoginAt_returns_null_if_user_has_no_auth_log()
  330. {
  331. $user = User::factory()->create();
  332. $previousLoginAt = $user->previousLoginAt();
  333. $this->assertNull($previousLoginAt);
  334. }
  335. #[Test]
  336. public function test_previousLoginIp_returns_user_last_auth_ip()
  337. {
  338. $user = User::factory()->create();
  339. $yesterday = now()->subDay();
  340. AuthLog::factory()->for($user, 'authenticatable')->create();
  341. AuthLog::factory()->at($yesterday)->for($user, 'authenticatable')->create();
  342. $previousLoginIp = $user->previousLoginIp();
  343. $this->assertEquals(AuthLogFactory::IP, $previousLoginIp);
  344. }
  345. #[Test]
  346. public function test_previousLoginIp_returns_null_if_user_has_no_auth_log()
  347. {
  348. $user = User::factory()->create();
  349. $previousLoginIp = $user->previousLoginIp();
  350. $this->assertNull($previousLoginIp);
  351. }
  352. }