Skip to content

Commit

Permalink
Unit test to check impersonation with guards using different provider…
Browse files Browse the repository at this point in the history
…s & tables
  • Loading branch information
carsso committed Oct 3, 2020
1 parent 21c4563 commit 7df8f58
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
67 changes: 67 additions & 0 deletions migrations/2020_10_03_233352_create_other_users_table.php
@@ -0,0 +1,67 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class CreateOtherUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('other_users', function (Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('is_admin')->default(0)->index();
$table->boolean('can_be_impersonated')->default(1)->index();
$table->rememberToken();
$table->timestamps();
});

DB::table('other_users')->insert([
[
'name' => 'OtherAdmin',
'email' => 'otheradmin@test.rocks',
'password' => bcrypt('password'),
'is_admin' => 1,
'can_be_impersonated' => 1,
'created_at' => Carbon::now()->toDateTimeString(),
],
[
'name' => 'OtherUser',
'email' => 'otheruser@test.rocks',
'password' => bcrypt('password'),
'is_admin' => 0,
'can_be_impersonated' => 1,
'created_at' => Carbon::now()->toDateTimeString(),
],
[
'name' => 'OtherSuperAdmin',
'email' => 'othersuperadmin@test.rocks',
'password' => bcrypt('password'),
'is_admin' => 1,
'can_be_impersonated' => 0,
'created_at' => Carbon::now()->toDateTimeString(),
],
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('other_users');
}
}
25 changes: 25 additions & 0 deletions tests/ImpersonateManagerTest.php
Expand Up @@ -16,6 +16,8 @@ class ImpersonateManagerTest extends TestCase
protected $firstGuard;
/** @var string */
protected $secondGuard;
/** @var string */
protected $thirdGuard;

public function setUp() : void
{
Expand All @@ -24,6 +26,7 @@ public function setUp() : void
$this->manager = $this->app->make(ImpersonateManager::class);
$this->firstGuard = 'web';
$this->secondGuard = 'admin';
$this->thirdGuard = 'otheruser';
}

/** @test */
Expand Down Expand Up @@ -159,6 +162,28 @@ public function it_can_get_impersonator()
$this->assertEquals('Admin', $this->manager->getImpersonator()->name);
}

/** @test */
public function it_can_get_impersonator_with_guards_from_different_tables()
{
$this->app['auth']->guard($this->thirdGuard)->loginUsingId('otheradmin@test.rocks');
$this->assertTrue($this->app['auth']->guard($this->thirdGuard)->check());
$this->manager->take(
$this->app['auth']->guard($this->thirdGuard)->user(),
$this->manager->findUserById('user@test.rocks', $this->firstGuard),
$this->thirdGuard
);

# Impersonated user ("User" #2) is from table "users"
$this->assertEquals(2, $this->app['auth']->guard($this->thirdGuard)->user()->id);
$this->assertEquals('User', $this->app['auth']->guard($this->thirdGuard)->user()->name);
$this->assertEquals('users', $this->app['auth']->guard($this->thirdGuard)->user()->getTable());

# Impersonating user ("OtherAdmin" #1) is from table "other_users"
$this->assertEquals(1, $this->manager->getImpersonator()->id);
$this->assertEquals('OtherAdmin', $this->manager->getImpersonator()->name);
$this->assertEquals('other_users', $this->manager->getImpersonator()->getTable());
}

public function it_renames_the_remember_web_cookie_when_taking_and_reverts_the_change_when_leaving()
{
app('router')->get('/cookie', function () {
Expand Down
48 changes: 48 additions & 0 deletions tests/Stubs/Models/OtherUser.php
@@ -0,0 +1,48 @@
<?php

namespace Lab404\Tests\Stubs\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Lab404\Impersonate\Models\Impersonate;

class OtherUser extends Authenticatable
{
use Notifiable, Impersonate;

/**
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];

/**
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

/**
* @return bool
*/
public function canImpersonate()
{
return $this->attributes['is_admin'] == 1;
}

/*
* @return bool
*/
public function canBeImpersonated()
{
return $this->attributes['can_be_impersonated'] == 1;
}


public function getAuthIdentifierName()
{
return 'email';
}
}
11 changes: 11 additions & 0 deletions tests/TestCase.php
Expand Up @@ -4,6 +4,7 @@

use Lab404\Impersonate\ImpersonateServiceProvider;
use Lab404\Tests\Stubs\Models\User;
use Lab404\Tests\Stubs\Models\OtherUser;
use Orchestra\Database\ConsoleServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
Expand Down Expand Up @@ -50,6 +51,16 @@ protected function getEnvironmentSetUp($app)
'driver' => 'session',
'provider' => 'admins',
]);

// Setup a guard from another user table/model
$app['config']->set('auth.providers.otherusers', [
'driver' => 'eloquent',
'model' => OtherUser::class,
]);
$app['config']->set('auth.guards.otheruser', [
'driver' => 'session',
'provider' => 'otherusers',
]);
}

/**
Expand Down

0 comments on commit 7df8f58

Please sign in to comment.