Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user mocking through Airlock using actingAs #51

Merged
merged 5 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ public function boot()
}
```

## Testing

While testing, the `Airlock::actingAs` method may be used to authenticate a user and specify which abilities are granted to their token:

```php
use App\User;
use Laravel\Airlock\Airlock;

public function test_task_list_can_be_retrieved()
{
Airlock::actingAs(
factory(User::class)->create(),
['view-tasks']
);

$response = $this->get('/api/task');

$response->assertOk();
}
```

If you would like to grant all abilities to the token, you should include `*` in your ability list:

```php
Airlock::actingAs(
factory(User::class)->create(),
['*']
);
```

## Contributing

Thank you for considering contributing to Airlock! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
Expand Down
40 changes: 40 additions & 0 deletions src/Airlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Airlock;

use Laravel\Airlock\HasApiTokens;
use Mockery;

class Airlock
{
Expand All @@ -20,6 +21,35 @@ class Airlock
*/
public static $runsMigrations = true;

/**
* Set the current user for the application with the given abilities.
*
* @param \Illuminate\Contracts\Auth\Authenticatable|\Laravel\Airlock\HasApiTokens $user
* @param array $abilities
* @param string $guard
* @return \Illuminate\Contracts\Auth\Authenticatable
*/
public static function actingAs($user, $abilities = [], $guard = 'airlock')
{
$token = Mockery::mock(self::personalAccessTokenModel())->shouldIgnoreMissing(false);

foreach ($abilities as $ability) {
$token->shouldReceive('can')->with($ability)->andReturn(true);
}

$user->withAccessToken($token);

if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
$user->wasRecentlyCreated = false;
}

app('auth')->guard($guard)->setUser($user);

app('auth')->shouldUse($guard);

return $user;
}

/**
* Set the personal access token model name.
*
Expand Down Expand Up @@ -52,4 +82,14 @@ public static function ignoreMigrations()

return new static;
}

/**
* Get the token model class name.
*
* @return string
*/
public static function personalAccessTokenModel()
{
return static::$personalAccessTokenModel;
}
}
73 changes: 73 additions & 0 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Laravel\Airlock\Tests\Feature;

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Foundation\Auth\User;
use Laravel\Airlock\Airlock;
use Laravel\Airlock\HasApiTokens;
use Orchestra\Testbench\TestCase;

class ActingAsTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

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

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
return 'bar';
})->middleware('auth:airlock');

Airlock::actingAs(new AirlockUser());

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

public function testActingAsWhenTheRouteIsProtectedUsingAbilities()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$this->withoutExceptionHandling();

/** @var Registrar $router */
$router = $this->app->make(Registrar::class);

$router->get('/foo', function () {
if (auth()->user()->tokenCan('baz')) {
return 'bar';
}

return response(403);
})->middleware('auth:airlock');

$user = new AirlockUser();
$user->createToken('test-token', ['baz'])->plainTextToken;

Airlock::actingAs($user);

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

class AirlockUser extends User
{
use HasApiTokens;
}