55use App \Models \Asset ;
66use App \Models \Company ;
77use App \Models \User ;
8+ use Error ;
89use Tests \TestCase ;
910
1011class UpdateUserTest extends TestCase
@@ -16,6 +17,15 @@ public function testPageRenders()
1617 ->assertOk ();
1718 }
1819
20+ public function testCannotViewEditPageForSoftDeletedUser ()
21+ {
22+ $ user = User::factory ()->trashed ()->create ();
23+
24+ $ this ->actingAs (User::factory ()->superuser ()->create ())
25+ ->get (route ('users.edit ' , $ user ->id ))
26+ ->assertRedirectToRoute ('users.show ' , $ user ->id );
27+ }
28+
1929 public function testUsersCanBeActivatedWithNumber ()
2030 {
2131 $ admin = User::factory ()->superuser ()->create ();
@@ -161,4 +171,40 @@ public function testMultiCompanyUserCanBeUpdatedIfHasAssetInSameCompany()
161171
162172 $ this ->followRedirects ($ response )->assertSee ('success ' );
163173 }
174+
175+ /**
176+ * This can occur if the user edit screen is open in one tab and
177+ * the user is deleted in another before the edit form is submitted.
178+ * @link https://app.shortcut.com/grokability/story/29166
179+ */
180+ public function testAttemptingToUpdateDeletedUserIsHandledGracefully ()
181+ {
182+ [$ companyA , $ companyB ] = Company::factory ()->count (2 )->create ();
183+ $ user = User::factory ()->for ($ companyA )->create ();
184+ Asset::factory ()->assignedToUser ($ user )->create ();
185+
186+ $ id = $ user ->id ;
187+
188+ $ user ->delete ();
189+
190+ $ response = $ this ->actingAs (User::factory ()->editUsers ()->create ())
191+ ->put (route ('users.update ' , $ id ), [
192+ 'first_name ' => 'test ' ,
193+ 'username ' => 'test ' ,
194+ 'company_id ' => $ companyB ->id ,
195+ ]);
196+
197+ $ this ->assertFalse ($ response ->exceptions ->contains (function ($ exception ) {
198+ // Avoid hard 500
199+ return $ exception instanceof Error;
200+ }));
201+
202+ // As of now, the user will be updated but not be restored
203+ $ this ->assertDatabaseHas ('users ' , [
204+ 'id ' => $ id ,
205+ 'first_name ' => 'test ' ,
206+ 'username ' => 'test ' ,
207+ 'company_id ' => $ companyB ->id ,
208+ ]);
209+ }
164210}
0 commit comments