Skip to content

A free open source IT asset/license management system

Added tests

snipee6ead7c6faea
diff --git a/tests/Feature/Settings/OauthSettingsTest.php b/tests/Feature/Settings/OauthSettingsTest.php
new file mode 100644
index 0000000000..e585f5febb
--- /dev/null
+++ b/tests/Feature/Settings/OauthSettingsTest.php
@@ -0,0 +1,301 @@
+<?php
+
+namespace Tests\Feature\Settings;
+
+use App\Helpers\Helper;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+class OauthSettingsTest extends TestCase
+{
+ public function test_superuser_can_view_personal_access_tokens_table(): void
+ {
+ $superuser = User::factory()->superuser()->create();
+ $tokenOwner = User::factory()->create([
+ 'username' => 'token-owner', 'first_name' => 'Token',
+ 'last_name' => 'Owner',
+ 'display_name' => 'Token Owner Display',
+ ]);
+ $createdAt = now()->subHour();
+ $expiresAt = now()->addDay();
+
+ $personalClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'Personal Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 1,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $nonPersonalClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'OAuth Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 0,
+ 'password_client' => 1,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ [
+ 'id' => 'personal-token-1',
+ 'user_id' => $tokenOwner->id,
+ 'client_id' => $personalClientId,
+ 'name' => 'Personal Token One',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => $createdAt,
+ 'updated_at' => $createdAt,
+ 'expires_at' => $expiresAt,
+ ],
+ [
+ 'id' => 'oauth-token-1',
+ 'user_id' => $tokenOwner->id,
+ 'client_id' => $nonPersonalClientId,
+ 'name' => 'Non Personal Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ],
+ ]);
+
+ $this->actingAs($superuser)
+ ->get(route('settings.oauth.index'))
+ ->assertOk()
+ ->assertSee(trans('admin/settings/general.oauth_personal_access_tokens'))
+ ->assertSee('Personal Token One')
+ ->assertSee('Token Owner Display')
+ ->assertSee(route('users.show', $tokenOwner))
+ ->assertSee(Helper::getFormattedDateObject($createdAt, 'datetime', false))
+ ->assertSee(Helper::getFormattedDateObject($expiresAt, 'datetime', false))
+ ->assertDontSee('Non Personal Token');
+ }
+
+ public function test_permission_required_to_view_oauth_settings(): void
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('settings.oauth.index'))
+ ->assertForbidden();
+ }
+
+ public function test_soft_deleted_token_owner_display_name_is_shown_with_strikethrough(): void
+ {
+ $superuser = User::factory()->superuser()->create();
+ $tokenOwner = User::factory()->create([
+ 'username' => 'deleted-token-owner',
+ 'display_name' => 'Deleted Token User',
+ ]);
+ $tokenOwner->delete();
+
+ $personalClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'Personal Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 1,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ 'id' => 'soft-deleted-owner-token',
+ 'user_id' => $tokenOwner->id,
+ 'client_id' => $personalClientId,
+ 'name' => 'Soft Deleted Owner Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ]);
+
+ $this->actingAs($superuser)
+ ->get(route('settings.oauth.index'))
+ ->assertOk()
+ ->assertSee('<del>Deleted Token User</del>', false)
+ ->assertSee(route('users.show', $tokenOwner));
+ }
+
+ public function test_authorized_applications_show_and_link_client_creator_with_soft_deleted_strikethrough(): void
+ {
+ $superuser = User::factory()->superuser()->create();
+
+ $activeCreator = User::factory()->create([
+ 'display_name' => 'Active Client Creator',
+ 'username' => 'active-creator',
+ ]);
+
+ $softDeletedCreator = User::factory()->create([
+ 'display_name' => 'Deleted Client Creator',
+ 'username' => 'deleted-creator',
+ ]);
+ $softDeletedCreator->delete();
+
+ $activeClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => $activeCreator->id,
+ 'name' => 'Authorized Client Active',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 0,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $deletedClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => $softDeletedCreator->id,
+ 'name' => 'Authorized Client Deleted',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 0,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ [
+ 'id' => 'authorized-active-token',
+ 'user_id' => $superuser->id,
+ 'client_id' => $activeClientId,
+ 'name' => 'Authorized Active Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ],
+ [
+ 'id' => 'authorized-deleted-token',
+ 'user_id' => $superuser->id,
+ 'client_id' => $deletedClientId,
+ 'name' => 'Authorized Deleted Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ],
+ ]);
+
+ $this->actingAs($superuser)
+ ->get(route('settings.oauth.index'))
+ ->assertOk()
+ ->assertSee('Active Client Creator')
+ ->assertSee(route('users.show', $activeCreator))
+ ->assertSee('<del>Deleted Client Creator</del>', false)
+ ->assertSee(route('users.show', $softDeletedCreator))
+ ->assertSee('data-field="client_owner"', false)
+ ->assertSee('data-sortable="true"', false);
+ }
+
+ public function test_superuser_can_revoke_and_unrevoke_personal_access_token(): void
+ {
+ $superuser = User::factory()->superuser()->create();
+
+ $personalClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'Personal Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 1,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ 'id' => 'toggle-token-1',
+ 'user_id' => $superuser->id,
+ 'client_id' => $personalClientId,
+ 'name' => 'Toggle Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ]);
+
+ $this->actingAs($superuser)
+ ->post(route('settings.oauth.tokens.revoke', ['token' => 'toggle-token-1']))
+ ->assertRedirect(route('settings.oauth.index').'#personal-access-tokens');
+
+ $this->assertDatabaseHas('oauth_access_tokens', [
+ 'id' => 'toggle-token-1',
+ 'revoked' => 1,
+ ]);
+
+ $this->assertDatabaseHas('action_logs', [
+ 'item_type' => User::class,
+ 'item_id' => $superuser->id,
+ 'target_type' => User::class,
+ 'target_id' => $superuser->id,
+ 'created_by' => $superuser->id,
+ 'action_type' => 'token revoked',
+ ]);
+
+ $this->actingAs($superuser)
+ ->post(route('settings.oauth.tokens.unrevoke', ['token' => 'toggle-token-1']))
+ ->assertRedirect(route('settings.oauth.index').'#personal-access-tokens');
+
+ $this->assertDatabaseHas('oauth_access_tokens', [
+ 'id' => 'toggle-token-1',
+ 'revoked' => 0,
+ ]);
+
+ $this->assertDatabaseHas('action_logs', [
+ 'item_type' => User::class,
+ 'item_id' => $superuser->id,
+ 'target_type' => User::class,
+ 'target_id' => $superuser->id,
+ 'created_by' => $superuser->id,
+ 'action_type' => 'token unrevoked',
+ ]);
+ }
+
+ public function test_permission_required_to_toggle_oauth_token_state(): void
+ {
+ $personalClientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'Personal Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 1,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ 'id' => 'toggle-permission-token-1',
+ 'user_id' => User::factory()->create()->id,
+ 'client_id' => $personalClientId,
+ 'name' => 'Permission Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ]);
+
+ $this->actingAs(User::factory()->create())
+ ->post(route('settings.oauth.tokens.revoke', ['token' => 'toggle-permission-token-1']))
+ ->assertForbidden();
+ }
+}
diff --git a/tests/Feature/Users/Api/DeleteUsersTest.php b/tests/Feature/Users/Api/DeleteUsersTest.php
index fafa880b59..03d578ff39 100644
--- a/tests/Feature/Users/Api/DeleteUsersTest.php
+++ b/tests/Feature/Users/Api/DeleteUsersTest.php
@@ -6,6 +6,7 @@ use App\Models\Company;
use App\Models\LicenseSeat;
use App\Models\Location;
use App\Models\User;
+use Illuminate\Support\Facades\DB;
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
@@ -153,6 +154,102 @@ class DeleteUsersTest extends TestCase implements TestsFullMultipleCompaniesSupp
$this->assertSoftDeleted($user);
}
+ public function test_deleting_user_revokes_associated_passport_tokens()
+ {
+ $user = User::factory()->create();
+
+ $clientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'Test Personal Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 1,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ 'id' => 'soft-delete-token-'.$user->id,
+ 'user_id' => $user->id,
+ 'client_id' => $clientId,
+ 'name' => 'Soft Delete Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ]);
+
+ DB::table('oauth_refresh_tokens')->insert([
+ 'id' => 'soft-delete-refresh-'.$user->id,
+ 'access_token_id' => 'soft-delete-token-'.$user->id,
+ 'revoked' => 0,
+ 'expires_at' => now()->addDays(7),
+ ]);
+
+ $this->actingAsForApi(User::factory()->deleteUsers()->create())
+ ->deleteJson(route('api.users.destroy', $user))
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $this->assertSoftDeleted($user);
+ $this->assertDatabaseHas('oauth_access_tokens', [
+ 'id' => 'soft-delete-token-'.$user->id,
+ 'revoked' => 1,
+ ]);
+ $this->assertDatabaseHas('oauth_refresh_tokens', [
+ 'id' => 'soft-delete-refresh-'.$user->id,
+ 'revoked' => 1,
+ ]);
+ }
+
+ public function test_force_deleting_user_hard_deletes_associated_passport_tokens()
+ {
+ $user = User::factory()->create();
+
+ $clientId = DB::table('oauth_clients')->insertGetId([
+ 'user_id' => null,
+ 'name' => 'Test Personal Client',
+ 'secret' => 'secret',
+ 'redirect' => 'http://localhost/callback',
+ 'personal_access_client' => 1,
+ 'password_client' => 0,
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ DB::table('oauth_access_tokens')->insert([
+ 'id' => 'force-delete-token-'.$user->id,
+ 'user_id' => $user->id,
+ 'client_id' => $clientId,
+ 'name' => 'Force Delete Token',
+ 'scopes' => '[]',
+ 'revoked' => 0,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ 'expires_at' => now()->addDay(),
+ ]);
+
+ DB::table('oauth_refresh_tokens')->insert([
+ 'id' => 'force-delete-refresh-'.$user->id,
+ 'access_token_id' => 'force-delete-token-'.$user->id,
+ 'revoked' => 0,
+ 'expires_at' => now()->addDays(7),
+ ]);
+
+ $user->forceDelete();
+
+ $this->assertDatabaseMissing('oauth_access_tokens', [
+ 'id' => 'force-delete-token-'.$user->id,
+ ]);
+ $this->assertDatabaseMissing('oauth_refresh_tokens', [
+ 'id' => 'force-delete-refresh-'.$user->id,
+ ]);
+ }
+
public function test_admin_cannot_delete_super_user()
{
$superuser = User::factory()->superuser()->create();