From 2a93c388306bdbd665cf950f6de79fa657c89deb Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 30 Aug 2023 16:40:28 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Don=E2=80=99t=20cast=20as=20boolean,=20vali?= =?UTF-8?q?date=20as=20boolean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: snipe --- app/Models/User.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 0d49b977c4..f54d99188e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -69,15 +69,12 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo ]; protected $casts = [ - 'activated' => 'boolean', 'manager_id' => 'integer', 'location_id' => 'integer', 'company_id' => 'integer', - 'vip' => 'boolean', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', - 'autoassign_licenses' => 'boolean', ]; /** @@ -103,6 +100,9 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo 'state' => 'min:2|max:191|nullable', 'country' => 'min:2|max:191|nullable', 'zip' => 'max:10|nullable', + 'vip' => 'boolean', + 'remote' => 'boolean', + 'activated' => 'boolean', ]; /** From 07c3fe1fcedcd159eafb452075eeeeb6c55cfb04 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 30 Aug 2023 09:42:53 -0700 Subject: [PATCH 2/4] Update assertions to account for type change --- tests/Feature/Users/UpdateUserTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Users/UpdateUserTest.php b/tests/Feature/Users/UpdateUserTest.php index 9ddb323625..0066788f07 100644 --- a/tests/Feature/Users/UpdateUserTest.php +++ b/tests/Feature/Users/UpdateUserTest.php @@ -22,7 +22,7 @@ class UpdateUserTest extends TestCase 'activated' => 1, ]); - $this->assertTrue($user->refresh()->activated); + $this->assertEquals(1, $user->refresh()->activated); } public function testUsersCanBeDeactivated() @@ -39,7 +39,7 @@ class UpdateUserTest extends TestCase // 'activated' => 0, ]); - $this->assertFalse($user->refresh()->activated); + $this->assertEquals(0, $user->refresh()->activated); } public function testUsersUpdatingThemselvesDoNotDeactivateTheirAccount() @@ -56,6 +56,6 @@ class UpdateUserTest extends TestCase // 'activated' => 0, ]); - $this->assertTrue($admin->refresh()->activated); + $this->assertEquals(1, $admin->refresh()->activated); } } From fb455be40631ce73ebd6b61b71a1b9e33e3b9f3f Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 30 Aug 2023 19:13:38 +0100 Subject: [PATCH 3/4] Added tests Signed-off-by: snipe --- tests/Feature/Api/Users/UpdateUserApiTest.php | 68 +++++++++++++++++++ tests/Feature/Users/UpdateUserTest.php | 44 +++++++++--- 2 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 tests/Feature/Api/Users/UpdateUserApiTest.php diff --git a/tests/Feature/Api/Users/UpdateUserApiTest.php b/tests/Feature/Api/Users/UpdateUserApiTest.php new file mode 100644 index 0000000000..81c1154648 --- /dev/null +++ b/tests/Feature/Api/Users/UpdateUserApiTest.php @@ -0,0 +1,68 @@ +superuser()->create(); + $user = User::factory()->create(['activated' => 0]); + + $this->actingAsForApi($admin) + ->patch(route('api.users.update', $user), [ + 'activated' => 1, + ]); + + $this->assertEquals(1, $user->refresh()->activated); + } + + public function testApiUsersCanBeActivatedWithBooleanTrue() + { + $admin = User::factory()->superuser()->create(); + $user = User::factory()->create(['activated' => false]); + + $this->actingAsForApi($admin) + ->patch(route('api.users.update', $user), [ + 'activated' => true, + ]); + + $this->assertEquals(1, $user->refresh()->activated); + } + + public function testApiUsersCanBeDeactivatedWithNumber() + { + $admin = User::factory()->superuser()->create(); + $user = User::factory()->create(['activated' => true]); + + $this->actingAsForApi($admin) + ->patch(route('api.users.update', $user), [ + 'activated' => 0, + ]); + + $this->assertEquals(0, $user->refresh()->activated); + } + + public function testApiUsersCanBeDeactivatedWithBooleanFalse() + { + $admin = User::factory()->superuser()->create(); + $user = User::factory()->create(['activated' => true]); + + $this->actingAsForApi($admin) + ->patch(route('api.users.update', $user), [ + 'activated' => false, + ]); + + $this->assertEquals(0, $user->refresh()->activated); + } + +} diff --git a/tests/Feature/Users/UpdateUserTest.php b/tests/Feature/Users/UpdateUserTest.php index 0066788f07..92245059ef 100644 --- a/tests/Feature/Users/UpdateUserTest.php +++ b/tests/Feature/Users/UpdateUserTest.php @@ -10,10 +10,10 @@ class UpdateUserTest extends TestCase { use InteractsWithSettings; - public function testUsersCanBeActivated() + public function testUsersCanBeActivatedWithNumber() { $admin = User::factory()->superuser()->create(); - $user = User::factory()->create(['activated' => false]); + $user = User::factory()->create(['activated' => 0]); $this->actingAs($admin) ->put(route('users.update', $user), [ @@ -25,7 +25,22 @@ class UpdateUserTest extends TestCase $this->assertEquals(1, $user->refresh()->activated); } - public function testUsersCanBeDeactivated() + public function testUsersCanBeActivatedWithBooleanTrue() + { + $admin = User::factory()->superuser()->create(); + $user = User::factory()->create(['activated' => false]); + + $this->actingAs($admin) + ->put(route('users.update', $user), [ + 'first_name' => $user->first_name, + 'username' => $user->username, + 'activated' => true, + ]); + + $this->assertEquals(1, $user->refresh()->activated); + } + + public function testUsersCanBeDeactivatedWithNumber() { $admin = User::factory()->superuser()->create(); $user = User::factory()->create(['activated' => true]); @@ -34,9 +49,22 @@ class UpdateUserTest extends TestCase ->put(route('users.update', $user), [ 'first_name' => $user->first_name, 'username' => $user->username, - // checkboxes that are not checked are - // not included in the request payload - // 'activated' => 0, + 'activated' => 0, + ]); + + $this->assertEquals(0, $user->refresh()->activated); + } + + public function testUsersCanBeDeactivatedWithBooleanFalse() + { + $admin = User::factory()->superuser()->create(); + $user = User::factory()->create(['activated' => true]); + + $this->actingAs($admin) + ->put(route('users.update', $user), [ + 'first_name' => $user->first_name, + 'username' => $user->username, + 'activated' => false, ]); $this->assertEquals(0, $user->refresh()->activated); @@ -50,10 +78,6 @@ class UpdateUserTest extends TestCase ->put(route('users.update', $admin), [ 'first_name' => $admin->first_name, 'username' => $admin->username, - // checkboxes that are disabled are not - // included in the request payload - // even if they are checked - // 'activated' => 0, ]); $this->assertEquals(1, $admin->refresh()->activated); From 663faffcc1f6cbc7fe9f46e99a2d61b59f036d74 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 30 Aug 2023 19:13:55 +0100 Subject: [PATCH 4/4] Un-cast byod, validate as boolean Signed-off-by: snipe --- app/Models/Asset.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 6792cf2859..d95aec51ce 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -82,7 +82,6 @@ class Asset extends Depreciable 'location_id' => 'integer', 'rtd_company_id' => 'integer', 'supplier_id' => 'integer', - 'byod' => 'boolean', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', @@ -105,6 +104,7 @@ class Asset extends Depreciable 'purchase_cost' => 'numeric|nullable|gte:0', 'supplier_id' => 'exists:suppliers,id|nullable', 'asset_eol_date' => 'date|max:10|min:10|nullable', + 'byod' => 'boolean', ]; /**