PersonalAccessTokenTest refactoring

This commit is contained in:
Peter Papp
2021-07-18 10:20:47 +02:00
parent 4384e50f34
commit aeb5c8419a
21 changed files with 119 additions and 173 deletions

View File

@@ -6,7 +6,6 @@ use App\Models\Setting;
use App\Models\User;
use App\Notifications\ResetPassword;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Password;
use Laravel\Sanctum\Sanctum;
use Notification;
@@ -15,8 +14,6 @@ use Tests\TestCase;
class AuthTest extends TestCase
{
use DatabaseMigrations;
/**
* @test
*/

View File

@@ -0,0 +1,117 @@
<?php
namespace Tests\Feature\Accounts;
use App\Models\File;
use App\Models\Folder;
use App\Models\User;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class PersonalAccessTokenTest extends TestCase
{
/**
* @test
*/
public function it_create_user_token()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson('/api/user/token/create', [
'name' => 'token',
])->assertStatus(201);
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
'name' => 'token',
]);
}
/**
* @test
*/
public function it_revoke_user_token()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$user->createToken('token');
$token_id = $user->tokens()->first()->id;
$this->deleteJson("/api/user/token/revoke/$token_id")
->assertStatus(204);
$this->assertDatabaseMissing('personal_access_tokens', [
'id' => $token_id,
]);
}
/**
* @test
*/
public function it_get_user_tokens()
{
$user = User::factory(User::class)
->create();
$user->createToken('token');
$token = $user->tokens()->first();
$this
->actingAs($user)
->getJson('/api/user/tokens')
->assertStatus(200)
->assertJsonFragment([
'id' => $token->id,
'tokenable_type' => $token->tokenable_type,
'tokenable_id' => $user->id,
'name' => $token->name,
'abilities' => $token->abilities,
]);
}
/**
* @test
*/
public function it_use_user_token_in_public_api_request()
{
$user = User::factory(User::class)
->create();
$folder = Folder::factory(Folder::class)
->create([
'user_id' => $user->id,
]);
$file = File::factory(File::class)
->create([
'user_id' => $user->id,
'folder_id' => $folder->id,
]);
$token = $user->createToken('token')->plainTextToken;
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
]);
$this->assertDatabaseHas('folders', [
'id' => $folder->id,
'user_id' => $user->id,
]);
$this
->withToken($token)
->getJson("/api/browse/folders/$folder->id")
->assertOk()
->assertJsonFragment([
'id' => $file->id,
]);
}
}

View File

@@ -4,20 +4,15 @@ namespace Tests\Feature\Accounts;
use Storage;
use Notification;
use Tests\TestCase;
use App\Models\File;
use App\Models\User;
use App\Models\Folder;
use Laravel\Sanctum\Sanctum;
use App\Services\SetupService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\URL;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class UserAccountTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();
@@ -153,112 +148,6 @@ class UserAccountTest extends TestCase
]);
}
/**
* @test
*/
public function it_create_user_token()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson('/api/user/token/create', [
'name' => 'token',
])->assertStatus(201);
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
'name' => 'token',
]);
}
/**
* @test
*/
public function it_revoke_user_token()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$user->createToken('token');
$token_id = $user->tokens()->first()->id;
$this->deleteJson("/api/user/token/revoke/$token_id")
->assertStatus(204);
$this->assertDatabaseMissing('personal_access_tokens', [
'id' => $token_id,
]);
}
/**
* @test
*/
public function it_get_user_tokens()
{
$user = User::factory(User::class)
->create();
$user->createToken('token');
$token = $user->tokens()->first();
$this
->actingAs($user)
->getJson('/api/user/tokens')
->assertStatus(200)
->assertJsonFragment([
'id' => $token->id,
'tokenable_type' => $token->tokenable_type,
'tokenable_id' => $user->id,
'name' => $token->name,
'abilities' => $token->abilities,
]);
}
/**
* @test
*/
public function it_use_user_token_in_public_api_request()
{
$user = User::factory(User::class)
->create();
$folder = Folder::factory(Folder::class)
->create([
'user_id' => $user->id,
]);
$file = File::factory(File::class)
->create([
'user_id' => $user->id,
'folder_id' => $folder->id,
]);
$token = $user->createToken('token')->plainTextToken;
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
]);
$this->assertDatabaseHas('folders', [
'id' => $folder->id,
'user_id' => $user->id,
]);
$this
->withToken($token)
->getJson("/api/browse/folders/$folder->id")
->assertOk()
->assertJsonFragment([
'id' => $file->id,
]);
}
/**
* @test
*/

View File

@@ -11,7 +11,6 @@ use App\Models\Zip;
use App\Notifications\ResetPassword;
use App\Services\SetupService;
use DB;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Notification;
use Laravel\Sanctum\Sanctum;
@@ -20,8 +19,6 @@ use Tests\TestCase;
class AdminTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -6,14 +6,11 @@ use App\Models\Language;
use App\Models\Setting;
use App\Models\User;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class LanguageEditorTest extends TestCase
{
use DatabaseMigrations;
protected $setup;
public function __construct()

View File

@@ -9,15 +9,12 @@ use App\Models\Setting;
use App\Models\Share;
use App\Models\User;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Mail;
use ScssPhp\ScssPhp\Compiler;
use Tests\TestCase;
class AppTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -2,18 +2,14 @@
namespace Tests\Feature\App;
use App\Models\LanguageTranslation;
use App\Models\User;
use DB;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Str;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AppUpgradeTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -7,15 +7,12 @@ use App\Models\User;
use App\Models\Zip;
use App\Services\SchedulerService;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Storage;
use Tests\TestCase;
class SchedulerTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -3,16 +3,12 @@
namespace Tests\Feature\External;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Str;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class SubscriptionTest extends TestCase
{
use DatabaseMigrations;
private $user;
private $plan;

View File

@@ -6,7 +6,6 @@ use App\Models\File;
use App\Models\Folder;
use App\Models\Share;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Sanctum\Sanctum;

View File

@@ -6,8 +6,6 @@ use App\Models\File;
use App\Models\Folder;
use App\Models\User;
use App\Models\Zip;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Services\SetupService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
@@ -17,8 +15,6 @@ use Tests\TestCase;
class ContentAccessTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -8,7 +8,6 @@ use App\Models\Setting;
use App\Models\User;
use App\Models\Zip;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Laravel\Sanctum\Sanctum;
use Storage;
@@ -16,8 +15,6 @@ use Tests\TestCase;
class FileTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -7,7 +7,6 @@ use App\Models\Folder;
use App\Models\User;
use App\Models\Zip;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Laravel\Sanctum\Sanctum;
use Storage;
@@ -17,8 +16,6 @@ use Tests\TestCase;
class FolderTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -6,8 +6,6 @@ use App\Models\File;
use App\Models\Folder;
use App\Models\User;
use App\Services\SetupService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Laravel\Sanctum\Sanctum;
use Storage;
@@ -15,8 +13,6 @@ use Tests\TestCase;
class TrashTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -3,17 +3,13 @@
namespace Tests\Feature\Setup;
use App\Models\Language;
use App\Models\LanguageTranslation;
use App\Models\Setting;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Storage;
use Tests\TestCase;
class SetupServiceTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -4,7 +4,6 @@ namespace Tests\Feature\Setup;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
@@ -12,8 +11,6 @@ use Tests\TestCase;
class SetupWizardTest extends TestCase
{
use DatabaseMigrations;
/**
* CAVEAT:
*

View File

@@ -4,20 +4,14 @@ namespace Tests\Feature\Share;
use App\Models\File;
use App\Models\Folder;
use App\Models\Share;
use App\Models\User;
use App\Notifications\SharedSendViaEmail;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class UserShareTest extends TestCase
{
use DatabaseMigrations;
/**
* @test
*/

View File

@@ -7,17 +7,13 @@ use App\Models\Share;
use App\Models\User;
use App\Models\Zip;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Str;
use Storage;
use Tests\TestCase;
class VisitorAccessToItemsTest extends TestCase
{
use DatabaseMigrations;
private $setup;
public function __construct()

View File

@@ -7,8 +7,6 @@ use App\Models\Folder;
use App\Models\Share;
use App\Models\User;
use App\Models\Zip;
use Hash;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Services\SetupService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
@@ -17,8 +15,6 @@ use Tests\TestCase;
class VisitorBrowseTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -7,15 +7,12 @@ use App\Models\Folder;
use App\Models\Share;
use App\Models\User;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Storage;
use Tests\TestCase;
class VisitorManipulatingTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();

View File

@@ -3,6 +3,7 @@
namespace Tests;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
@@ -10,6 +11,7 @@ use Illuminate\Support\Facades\Storage;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use DatabaseMigrations;
public function setUp(): void
{