-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from fumeapp/ability-tests
Creates additional tests for session token abilities
- Loading branch information
Showing
8 changed files
with
191 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Fumeapp\Humble\Tests; | ||
|
||
use Fumeapp\Humble\Tests\Models\User; | ||
|
||
class HumbleTest extends TestCase | ||
{ | ||
public function test_session_token_can_be_created() | ||
{ | ||
$user = User::create([ | ||
'name' => 'John Doe', | ||
]); | ||
|
||
$newToken = $user->createToken('action'); | ||
$storedToken = $user->sessions()->first()->getKey(); | ||
|
||
$this->assertEquals($newToken, $storedToken); | ||
} | ||
|
||
public function test_session_token_can_be_created_with_abilities() | ||
{ | ||
$user = User::create([ | ||
'name' => 'John Doe', | ||
]); | ||
|
||
$abilities = ['create', 'update', 'delete']; | ||
|
||
$newToken = $user->createToken('action', $abilities); | ||
$storedToken = $user->sessions()->find($newToken); | ||
|
||
$this->assertEquals($storedToken->abilities, $abilities); | ||
} | ||
|
||
public function test_token_can_return_true_with_correct_abilities() | ||
{ | ||
$user = User::create([ | ||
'name' => 'John Doe', | ||
]); | ||
|
||
$abilities = ['create', 'update', 'delete']; | ||
|
||
$newToken = $user->createToken('action', $abilities); | ||
$storedToken = $user->sessions()->find($newToken); | ||
|
||
$this->assertTrue($storedToken->can('delete')); | ||
} | ||
|
||
public function test_token_can_return_false_if_it_does_not_have_correct_abilities() | ||
{ | ||
$user = User::create([ | ||
'name' => 'John Doe', | ||
]); | ||
|
||
$abilities = ['create', 'update']; | ||
|
||
$newToken = $user->createToken('action', $abilities); | ||
$storedToken = $user->sessions()->find($newToken); | ||
|
||
$this->assertFalse($storedToken->can('delete')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Fumeapp\Humble\Tests\Models; | ||
|
||
use Fumeapp\Humble\Models\Session; | ||
use Fumeapp\Humble\Traits\Humble; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
|
||
class User extends Authenticatable | ||
{ | ||
protected $guarded = []; | ||
|
||
use Humble; | ||
|
||
/** | ||
* Override the default session model since in testing we don't have the request object. | ||
*/ | ||
public function createToken(string $source, array $abilities = ['*']): string | ||
{ | ||
return $this->sessions()->create([ | ||
'token' => Session::hash(), | ||
'source' => $source, | ||
'abilities' => $abilities, | ||
'ip' => null, | ||
'location' => null, | ||
'agent' => null, | ||
]) | ||
->getKey(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Fumeapp\Humble\Tests; | ||
|
||
use Fumeapp\Humble\HumbleServiceProvider; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
abstract class TestCase extends Orchestra | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
HumbleServiceProvider::class, | ||
]; | ||
} | ||
|
||
public function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('database.default', 'testbench'); | ||
|
||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
$this->migrateDatabase(); | ||
} | ||
|
||
public function migrateDatabase() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('sessions', function (Blueprint $table) { | ||
$table->string('token', 64)->unique(); | ||
$table->bigInteger('user_id')->unsigned(); | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
$table->string('source')->nullable(); | ||
$table->json('abilities')->nullable(); | ||
$table->string('ip', 300)->nullable(); | ||
$table->string('agent')->nullable(); | ||
$table->string('location')->nullable(); | ||
$table->timestamps(); | ||
$table->primary('token'); | ||
}); | ||
|
||
Schema::create('attempts', function (Blueprint $table) { | ||
$table->string('token', 64)->unique(); | ||
$table->json('action')->nullable(); | ||
$table->bigInteger('user_id')->unsigned(); | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
$table->string('ip', 300)->nullable(); | ||
$table->string('agent')->nullable(); | ||
$table->timestamps(); | ||
$table->primary('token'); | ||
}); | ||
} | ||
} |