Skip to content

Commit

Permalink
Test Improvements (#1688)
Browse files Browse the repository at this point in the history
* Test Improvements

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Sep 20, 2023
1 parent 286baeb commit 5e9e4b8
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 120 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"require-dev": {
"mockery/mockery": "^1.0",
"orchestra/testbench": "^7.0|^8.0",
"orchestra/testbench": "^7.31|^8.11",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3"
},
Expand All @@ -46,7 +46,9 @@
},
"autoload-dev": {
"psr-4": {
"Laravel\\Passport\\Tests\\": "tests/"
"Laravel\\Passport\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/"
}
},
"extra": {
Expand All @@ -62,6 +64,10 @@
"config": {
"sort-packages": true
},
"scripts": {
"post-autoload-dump": "@prepare",
"prepare": "@php vendor/bin/testbench package:discover --ansi"
},
"minimum-stability": "dev",
"prefer-stable": true
}
5 changes: 5 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
providers:
- Laravel\Passport\PassportServiceProvider

migrations: true

83 changes: 27 additions & 56 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,26 @@

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Laravel\Passport\Client;
use Laravel\Passport\Database\Factories\ClientFactory;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Passport;
use Laravel\Passport\PersonalAccessTokenFactory;
use Laravel\Passport\Token;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Workbench\Database\Factories\UserFactory;

class AccessTokenControllerTest extends PassportTestCase
{
protected function setUp(): void
{
parent::setUp();

Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->dateTime('created_at');
$table->dateTime('updated_at');
});
}

protected function tearDown(): void
{
Schema::dropIfExists('users');

parent::tearDown();
}

protected function getUserClass()
{
return User::class;
}
use WithLaravelMigrations;

public function testGettingAccessTokenWithClientCredentialsGrant()
{
$this->withoutExceptionHandling();

$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make('foobar123');
$user->save();
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make('foobar123'),
]);

/** @var Client $client */
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->getKey()]);
Expand Down Expand Up @@ -87,10 +63,10 @@ public function testGettingAccessTokenWithClientCredentialsGrant()

public function testGettingAccessTokenWithClientCredentialsGrantInvalidClientSecret()
{
$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make('foobar123');
$user->save();
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make('foobar123'),
]);

/** @var Client $client */
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->getKey()]);
Expand Down Expand Up @@ -131,10 +107,10 @@ public function testGettingAccessTokenWithPasswordGrant()
$this->withoutExceptionHandling();

$password = 'foobar123';
$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make($password);
$user->save();
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make($password),
]);

/** @var Client $client */
$client = ClientFactory::new()->asPasswordClient()->create(['user_id' => $user->getKey()]);
Expand Down Expand Up @@ -178,10 +154,10 @@ public function testGettingAccessTokenWithPasswordGrant()
public function testGettingAccessTokenWithPasswordGrantWithInvalidPassword()
{
$password = 'foobar123';
$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make($password);
$user->save();
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make($password),
]);

/** @var Client $client */
$client = ClientFactory::new()->asPasswordClient()->create(['user_id' => $user->getKey()]);
Expand Down Expand Up @@ -221,10 +197,10 @@ public function testGettingAccessTokenWithPasswordGrantWithInvalidPassword()
public function testGettingAccessTokenWithPasswordGrantWithInvalidClientSecret()
{
$password = 'foobar123';
$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make($password);
$user->save();
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make($password),
]);

/** @var Client $client */
$client = ClientFactory::new()->asPasswordClient()->create(['user_id' => $user->getKey()]);
Expand Down Expand Up @@ -268,10 +244,10 @@ public function testGettingCustomResponseType()
$this->withoutExceptionHandling();
Passport::$authorizationServerResponseType = new IdTokenResponse('foo_bar_open_id_token');

$user = new User();
$user->email = '[email protected]';
$user->password = $this->app->make(Hasher::class)->make('foobar123');
$user->save();
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make('foobar123'),
]);

/** @var Client $client */
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->getKey()]);
Expand All @@ -294,11 +270,6 @@ public function testGettingCustomResponseType()
}
}

class User extends \Illuminate\Foundation\Auth\User
{
use HasApiTokens;
}

class IdTokenResponse extends \League\OAuth2\Server\ResponseTypes\BearerTokenResponse
{
/**
Expand Down
20 changes: 6 additions & 14 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Laravel\Passport\Tests\Feature;

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Http\Middleware\CheckForAnyScope;
use Laravel\Passport\Http\Middleware\CheckScopes;
use Laravel\Passport\Passport;
use Workbench\App\Models\User;

class ActingAsTest extends PassportTestCase
{
Expand All @@ -23,7 +22,7 @@ public function testActingAsWhenTheRouteIsProtectedByAuthMiddleware()
return 'bar';
})->middleware('auth:api');

Passport::actingAs(new PassportUser());
Passport::actingAs(new User());

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -41,7 +40,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware()
return 'bar';
})->middleware(CheckScopes::class.':admin,footest');

Passport::actingAs(new PassportUser(), ['admin', 'footest']);
Passport::actingAs(new User(), ['admin', 'footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -59,7 +58,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware(
return 'bar';
})->middleware(CheckForAnyScope::class.':admin,footest');

Passport::actingAs(new PassportUser(), ['footest']);
Passport::actingAs(new User(), ['footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -76,7 +75,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddlewareWithIn
return 'bar';
});

Passport::actingAs(new PassportUser(), ['foo', 'baz']);
Passport::actingAs(new User(), ['foo', 'baz']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -93,17 +92,10 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddlewareW
return 'bar';
});

Passport::actingAs(new PassportUser(), ['foo']);
Passport::actingAs(new User(), ['foo']);

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}
}

class PassportUser extends User
{
use HasApiTokens;

protected $table = 'users';
}
10 changes: 0 additions & 10 deletions tests/Feature/KeysCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

namespace Laravel\Passport\Tests\Feature;

use Mockery as m;

class KeysCommandTest extends PassportTestCase
{
protected function tearDown(): void
{
m::close();

@unlink(self::PUBLIC_KEY);
@unlink(self::PRIVATE_KEY);
}

public function testPrivateAndPublicKeysAreGenerated()
{
$this->assertFileExists(self::PUBLIC_KEY);
Expand Down
59 changes: 21 additions & 38 deletions tests/Feature/PassportTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,48 @@
namespace Laravel\Passport\Tests\Feature;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Laravel\Passport\Passport;
use Laravel\Passport\PassportServiceProvider;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\User;

abstract class PassportTestCase extends TestCase
{
use RefreshDatabase;
use LazilyRefreshDatabase, WithWorkbench;

const KEYS = __DIR__.'/../keys';
const PUBLIC_KEY = self::KEYS.'/oauth-public.key';
const PRIVATE_KEY = self::KEYS.'/oauth-private.key';

protected function setUp(): void
{
parent::setUp();
$this->afterApplicationCreated(function () {
Passport::loadKeysFrom(self::KEYS);

$this->artisan('migrate:fresh');
@unlink(self::PUBLIC_KEY);
@unlink(self::PRIVATE_KEY);

Passport::loadKeysFrom(self::KEYS);
$this->artisan('passport:keys');
});

@unlink(self::PUBLIC_KEY);
@unlink(self::PRIVATE_KEY);
$this->beforeApplicationDestroyed(function () {
@unlink(self::PUBLIC_KEY);
@unlink(self::PRIVATE_KEY);
});

$this->artisan('passport:keys');
parent::setUp();
}

protected function getEnvironmentSetUp($app)
protected function defineEnvironment($app)
{
$config = $app->make(Repository::class);

$config->set('auth.defaults.provider', 'users');

if (($userClass = $this->getUserClass()) !== null) {
$config->set('auth.providers.users.model', $userClass);
}

$config->set('auth.guards.api', ['driver' => 'passport', 'provider' => 'users']);

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
$config->set([
'auth.defaults.provider' => 'users',
'auth.providers.users.model' => User::class,
'auth.guards.api' => ['driver' => 'passport', 'provider' => 'users'],
'database.default' => 'testing',
]);
}

protected function getPackageProviders($app)
{
return [PassportServiceProvider::class];
}

/**
* Get the Eloquent user model class name.
*
* @return string|null
*/
protected function getUserClass()
{
}
}
42 changes: 42 additions & 0 deletions workbench/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Loading

0 comments on commit 5e9e4b8

Please sign in to comment.