Skip to content

A free open source IT asset/license management system

MOAR tests

snipe205cf3cf28be
diff --git a/tests/Feature/Groups/Api/UpdateGroupTest.php b/tests/Feature/Groups/Api/UpdateGroupTest.php
new file mode 100644
index 0000000000..0a1feb43a8
--- /dev/null
+++ b/tests/Feature/Groups/Api/UpdateGroupTest.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Tests\Feature\Groups\Api;
+
+use App\Models\Group;
+use App\Models\User;
+use Tests\TestCase;
+
+class UpdateGroupTest extends TestCase
+{
+ public function test_updating_group_requires_super_admin_permission()
+ {
+ $this->actingAsForApi(User::factory()->create())
+ ->putJson(route('api.groups.update', Group::factory()->create()))
+ ->assertForbidden();
+ }
+
+ public function test_can_update_group_with_permissions_array_payload()
+ {
+ $group = Group::factory()->create([
+ 'permissions' => json_encode(['admin' => '0']),
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->putJson(route('api.groups.update', $group), [
+ 'name' => 'Updated Group Name',
+ 'notes' => 'Updated Group Notes',
+ 'permissions' => [
+ 'admin' => '1',
+ 'reports.view' => '0',
+ ],
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $decoded = (array) $group->refresh()->decodePermissions();
+
+ $this->assertSame('Updated Group Name', $group->name);
+ $this->assertSame('Updated Group Notes', $group->notes);
+ $this->assertSame('1', (string) ($decoded['admin'] ?? null));
+ $this->assertSame('0', (string) ($decoded['reports.view'] ?? null));
+ }
+
+ public function test_can_update_group_with_permissions_json_string_payload()
+ {
+ $group = Group::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.groups.update', $group), [
+ 'permissions' => '{"admin":"1","reports.view":"0","invalid.permission":"1"}',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $decoded = (array) $group->refresh()->decodePermissions();
+
+ $this->assertArrayHasKey('admin', $decoded);
+ $this->assertArrayHasKey('reports.view', $decoded);
+ $this->assertArrayNotHasKey('invalid.permission', $decoded);
+ }
+
+ public function test_permissions_are_preserved_when_not_passed_on_update()
+ {
+ $group = Group::factory()->create([
+ 'permissions' => json_encode(['admin' => '1']),
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.groups.update', $group), [
+ 'name' => 'Rename Only',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $decoded = (array) $group->refresh()->decodePermissions();
+
+ $this->assertSame('Rename Only', $group->name);
+ $this->assertSame('1', (string) ($decoded['admin'] ?? null));
+ }
+}
diff --git a/tests/Feature/Groups/Ui/UpdateGroupTest.php b/tests/Feature/Groups/Ui/UpdateGroupTest.php
index 3f567822e7..3a3c12f216 100644
--- a/tests/Feature/Groups/Ui/UpdateGroupTest.php
+++ b/tests/Feature/Groups/Ui/UpdateGroupTest.php
@@ -32,4 +32,50 @@ class UpdateGroupTest extends TestCase
$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Group::where('name', 'Test Group Edited')->where('notes', 'Test Note Edited')->exists());
}
+
+ public function test_user_can_edit_group_permissions_and_invalid_permissions_are_dropped()
+ {
+ $group = Group::factory()->create(['name' => 'Permission Test Group']);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('groups.update', ['group' => $group]), [
+ 'name' => 'Permission Test Group Edited',
+ 'notes' => 'Permission test note',
+ 'permission' => [
+ 'admin' => '1',
+ 'reports.view' => '0',
+ 'invalid.permission' => '1',
+ ],
+ ])
+ ->assertStatus(302)
+ ->assertSessionHasNoErrors()
+ ->assertRedirect(route('groups.index'));
+
+ $decoded = (array) $group->refresh()->decodePermissions();
+
+ $this->assertArrayHasKey('admin', $decoded);
+ $this->assertArrayHasKey('reports.view', $decoded);
+ $this->assertArrayNotHasKey('invalid.permission', $decoded);
+ }
+
+ public function test_permissions_are_preserved_if_permission_payload_not_sent()
+ {
+ $group = Group::factory()->create([
+ 'permissions' => json_encode(['admin' => '1']),
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('groups.update', ['group' => $group]), [
+ 'name' => 'Group Name Only',
+ 'notes' => 'Updated notes',
+ ])
+ ->assertStatus(302)
+ ->assertSessionHasNoErrors()
+ ->assertRedirect(route('groups.index'));
+
+ $decoded = (array) $group->refresh()->decodePermissions();
+
+ $this->assertSame('Group Name Only', $group->name);
+ $this->assertSame('1', (string) ($decoded['admin'] ?? null));
+ }
}
diff --git a/tests/Feature/Users/Api/CreateUserTest.php b/tests/Feature/Users/Api/CreateUserTest.php
index b00ee0cae2..684574b0a9 100644
--- a/tests/Feature/Users/Api/CreateUserTest.php
+++ b/tests/Feature/Users/Api/CreateUserTest.php
@@ -122,4 +122,50 @@ class CreateUserTest extends TestCase
$user = User::where('username', 'testuser')->first();
Notification::assertSentTo($user, WelcomeNotification::class);
}
+
+ public function test_non_admin_cannot_grant_admin_or_superuser_permissions_when_creating_user_via_api()
+ {
+ $this->actingAsForApi(User::factory()->createUsers()->create())
+ ->postJson(route('api.users.store'), [
+ 'first_name' => 'Taylor',
+ 'last_name' => 'Tester',
+ 'username' => 'taylor-create-api',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'permissions' => '{"admin":"1","superuser":"1","users.view":"1"}',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $createdUser = User::where('username', 'taylor-create-api')->firstOrFail();
+ $decoded = (array) $createdUser->decodePermissions();
+
+ $this->assertArrayNotHasKey('admin', $decoded, 'Non-admin user should not be able to grant admin during create');
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Non-admin user should not be able to grant superuser during create');
+ $this->assertEquals(1, $decoded['users.view'] ?? null, 'Non-privileged permissions should still be createable');
+ }
+
+ public function test_admin_cannot_grant_superuser_permission_when_creating_user_via_api()
+ {
+ $this->actingAsForApi(User::factory()->admin()->createUsers()->create())
+ ->postJson(route('api.users.store'), [
+ 'first_name' => 'Alex',
+ 'last_name' => 'Admin',
+ 'username' => 'alex-create-api',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'permissions' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ ],
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $createdUser = User::where('username', 'alex-create-api')->firstOrFail();
+ $decoded = (array) $createdUser->decodePermissions();
+
+ $this->assertSame('1', (string) ($decoded['admin'] ?? null), 'Admin should be able to grant admin during create');
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Admin should not be able to grant superuser during create');
+ }
}
diff --git a/tests/Feature/Users/Ui/CreateUserTest.php b/tests/Feature/Users/Ui/CreateUserTest.php
index 4b6b553ae0..bf89d0ec54 100644
--- a/tests/Feature/Users/Ui/CreateUserTest.php
+++ b/tests/Feature/Users/Ui/CreateUserTest.php
@@ -94,4 +94,60 @@ class CreateUserTest extends TestCase
$this->followRedirects($response)->assertSee('Success');
}
+
+ public function test_non_admin_cannot_grant_admin_or_superuser_permissions_when_creating_user_via_ui()
+ {
+ $response = $this->actingAs(User::factory()->createUsers()->viewUsers()->create())
+ ->from(route('users.index'))
+ ->post(route('users.store'), [
+ 'first_name' => 'Taylor',
+ 'last_name' => 'Tester',
+ 'username' => 'taylor-create-ui',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'permission' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ 'users.view' => '1',
+ ],
+ ])
+ ->assertSessionHasNoErrors()
+ ->assertStatus(302)
+ ->assertRedirect(route('users.index'));
+
+ $createdUser = User::where('username', 'taylor-create-ui')->firstOrFail();
+ $decoded = (array) $createdUser->decodePermissions();
+
+ $this->assertArrayNotHasKey('admin', $decoded, 'Non-admin user should not be able to grant admin during create');
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Non-admin user should not be able to grant superuser during create');
+ $this->assertEquals(1, $decoded['users.view'] ?? null, 'Non-privileged permissions should still be createable');
+ $this->followRedirects($response)->assertSee('Success');
+ }
+
+ public function test_admin_cannot_grant_superuser_permission_when_creating_user_via_ui()
+ {
+ $response = $this->actingAs(User::factory()->admin()->createUsers()->viewUsers()->create())
+ ->from(route('users.index'))
+ ->post(route('users.store'), [
+ 'first_name' => 'Alex',
+ 'last_name' => 'Admin',
+ 'username' => 'alex-create-ui',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'permission' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ ],
+ ])
+ ->assertSessionHasNoErrors()
+ ->assertStatus(302)
+ ->assertRedirect(route('users.index'));
+
+ $createdUser = User::where('username', 'alex-create-ui')->firstOrFail();
+ $decoded = (array) $createdUser->decodePermissions();
+
+ $this->assertSame('1', (string) ($decoded['admin'] ?? null), 'Admin should be able to grant admin during create');
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Admin should not be able to grant superuser during create');
+ $this->followRedirects($response)->assertSee('Success');
+ }
}
diff --git a/tests/Feature/Users/Ui/UpdateUserTest.php b/tests/Feature/Users/Ui/UpdateUserTest.php
index 9001bace5c..b4e0c06285 100644
--- a/tests/Feature/Users/Ui/UpdateUserTest.php
+++ b/tests/Feature/Users/Ui/UpdateUserTest.php
@@ -251,6 +251,59 @@ class UpdateUserTest extends TestCase
$this->followRedirects($response)->assertSee('success');
}
+ public function test_edit_users_permission_cannot_escalate_empty_permissions_user_to_admin_or_superuser_via_ui()
+ {
+ $editingUser = User::factory()->editUsers()->create();
+ $targetUser = User::factory()->create([
+ 'permissions' => null,
+ ]);
+
+ $this->actingAs($editingUser)
+ ->put(route('users.update', $targetUser), [
+ 'first_name' => $targetUser->first_name,
+ 'username' => $targetUser->username,
+ 'permission' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ 'users.view' => '1',
+ ],
+ 'redirect_option' => 'index',
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $decoded = (array) $targetUser->refresh()->decodePermissions();
+
+ $this->assertArrayNotHasKey('admin', $decoded, 'Non-admin user should not be able to grant admin');
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Non-admin user should not be able to grant superuser');
+ $this->assertEquals(1, $decoded['users.view'] ?? null, 'Non-privileged permissions should still be updateable');
+ }
+
+ public function test_admin_cannot_escalate_empty_permissions_user_to_superuser_via_ui()
+ {
+ $adminUser = User::factory()->admin()->create();
+ $targetUser = User::factory()->create([
+ 'permissions' => null,
+ ]);
+
+ $this->actingAs($adminUser)
+ ->put(route('users.update', $targetUser), [
+ 'first_name' => $targetUser->first_name,
+ 'username' => $targetUser->username,
+ 'permission' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ ],
+ 'redirect_option' => 'index',
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $decoded = (array) $targetUser->refresh()->decodePermissions();
+
+ $this->assertArrayHasKey('admin', $decoded, 'Admin should be able to grant admin');
+ $this->assertSame('1', (string) $decoded['admin']);
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Admin should not be able to grant superuser');
+ }
+
/**
* This can occur if the user edit screen is open in one tab and
* the user is deleted in another before the edit form is submitted.