|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Sanctum\Tests; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Auth\User; |
| 6 | +use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract; |
| 7 | +use Laravel\Sanctum\HasApiTokens; |
| 8 | +use Laravel\Sanctum\PersonalAccessToken; |
| 9 | +use Laravel\Sanctum\SanctumServiceProvider; |
| 10 | +use Orchestra\Testbench\TestCase; |
| 11 | + |
| 12 | +class PruneExpiredTest extends TestCase |
| 13 | +{ |
| 14 | + protected function getEnvironmentSetUp($app) |
| 15 | + { |
| 16 | + $app['config']->set('database.default', 'testbench'); |
| 17 | + |
| 18 | + $app['config']->set('database.connections.testbench', [ |
| 19 | + 'driver' => 'sqlite', |
| 20 | + 'database' => ':memory:', |
| 21 | + 'prefix' => '', |
| 22 | + ]); |
| 23 | + } |
| 24 | + |
| 25 | + public function test_can_delete_expired_tokens_with_integer_expiration() |
| 26 | + { |
| 27 | + $this->loadLaravelMigrations(['--database' => 'testbench']); |
| 28 | + $this->artisan('migrate', ['--database' => 'testbench'])->run(); |
| 29 | + |
| 30 | + config(['sanctum.expiration' => 60]); |
| 31 | + |
| 32 | + $user = UserForPruneExpiredTest::forceCreate([ |
| 33 | + 'name' => 'Taylor Otwell', |
| 34 | + 'email' => 'taylor@laravel.com', |
| 35 | + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', |
| 36 | + ]); |
| 37 | + |
| 38 | + $token_1 = PersonalAccessToken::forceCreate([ |
| 39 | + 'tokenable_id' => $user->id, |
| 40 | + 'tokenable_type' => get_class($user), |
| 41 | + 'name' => 'Test_1', |
| 42 | + 'token' => hash('sha256', 'test_1'), |
| 43 | + 'created_at' => now()->subMinutes(181), |
| 44 | + ]); |
| 45 | + |
| 46 | + $token_2 = PersonalAccessToken::forceCreate([ |
| 47 | + 'tokenable_id' => $user->id, |
| 48 | + 'tokenable_type' => get_class($user), |
| 49 | + 'name' => 'Test_2', |
| 50 | + 'token' => hash('sha256', 'test_2'), |
| 51 | + 'created_at' => now()->subMinutes(179), |
| 52 | + ]); |
| 53 | + |
| 54 | + $token_3 = PersonalAccessToken::forceCreate([ |
| 55 | + 'tokenable_id' => $user->id, |
| 56 | + 'tokenable_type' => get_class($user), |
| 57 | + 'name' => 'Test_3', |
| 58 | + 'token' => hash('sha256', 'test_3'), |
| 59 | + 'created_at' => now()->subMinutes(121), |
| 60 | + ]); |
| 61 | + |
| 62 | + $this->artisan('sanctum:prune-expired --hours=2') |
| 63 | + ->expectsOutput('Tokens expired for more than 2 hours pruned successfully.'); |
| 64 | + |
| 65 | + $this->assertDatabaseMissing('personal_access_tokens', ['name' => 'Test_1']); |
| 66 | + $this->assertDatabaseHas('personal_access_tokens', ['name' => 'Test_2']); |
| 67 | + $this->assertDatabaseHas('personal_access_tokens', ['name' => 'Test_3']); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_cant_delete_expired_tokens_with_null_expiration() |
| 71 | + { |
| 72 | + $this->loadLaravelMigrations(['--database' => 'testbench']); |
| 73 | + $this->artisan('migrate', ['--database' => 'testbench'])->run(); |
| 74 | + |
| 75 | + config(['sanctum.expiration' => null]); |
| 76 | + |
| 77 | + $user = UserForPruneExpiredTest::forceCreate([ |
| 78 | + 'name' => 'Taylor Otwell', |
| 79 | + 'email' => 'taylor@laravel.com', |
| 80 | + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', |
| 81 | + ]); |
| 82 | + |
| 83 | + $token = PersonalAccessToken::forceCreate([ |
| 84 | + 'tokenable_id' => $user->id, |
| 85 | + 'tokenable_type' => get_class($user), |
| 86 | + 'name' => 'Test', |
| 87 | + 'token' => hash('sha256', 'test'), |
| 88 | + 'created_at' => now()->subMinutes(70), |
| 89 | + ]); |
| 90 | + |
| 91 | + $this->artisan('sanctum:prune-expired --hours=2') |
| 92 | + ->expectsOutput('Expiration value not specified in configuration file.'); |
| 93 | + |
| 94 | + $this->assertDatabaseHas('personal_access_tokens', ['name' => 'Test']); |
| 95 | + } |
| 96 | + |
| 97 | + protected function getPackageProviders($app) |
| 98 | + { |
| 99 | + return [SanctumServiceProvider::class]; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class UserForPruneExpiredTest extends User implements HasApiTokensContract |
| 104 | +{ |
| 105 | + use HasApiTokens; |
| 106 | + |
| 107 | + protected $table = 'users'; |
| 108 | +} |
0 commit comments