added socialite and some hacks via vendor sideload to add social login
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
use Mockery as M;
|
||||
use AdamWathan\EloquentOAuth\Authenticator;
|
||||
use AdamWathan\EloquentOAuth\OAuthIdentity;
|
||||
use SocialNorm\User as SocialNormUser;
|
||||
|
||||
class AuthenticatorTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
M::close();
|
||||
}
|
||||
|
||||
public function test_login_creates_new_user_if_no_matching_user_exists()
|
||||
{
|
||||
$providerAlias = 'provider';
|
||||
$auth = M::spy();
|
||||
$users = M::spy();
|
||||
$identities = M::spy();
|
||||
$userDetails = new SocialNormUser([]);
|
||||
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
|
||||
|
||||
$identities->shouldReceive('userExists')->andReturn(false);
|
||||
$users->shouldReceive('create')->andReturn($user);
|
||||
|
||||
$authenticator = new Authenticator($auth, $users, $identities);
|
||||
$authenticator->login('provider', $userDetails);
|
||||
|
||||
$users->shouldHaveReceived('create');
|
||||
$users->shouldHaveReceived('store')->with($user);
|
||||
$identities->shouldHaveReceived('store');
|
||||
$auth->shouldHaveReceived('login')->with($user);
|
||||
}
|
||||
|
||||
public function test_login_uses_existing_user_if_matching_user_exists()
|
||||
{
|
||||
$providerAlias = 'provider';
|
||||
|
||||
$userDetails = new SocialNormUser([]);
|
||||
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
|
||||
|
||||
$auth = M::spy();
|
||||
|
||||
$users = M::spy([
|
||||
'findByIdentity' => $user
|
||||
]);
|
||||
|
||||
$identities = M::spy([
|
||||
'userExists' => true,
|
||||
'getByProvider' => new OAuthIdentity,
|
||||
]);
|
||||
|
||||
$authenticator = new Authenticator($auth, $users, $identities);
|
||||
$authenticator->login('provider', $userDetails);
|
||||
|
||||
$users->shouldNotHaveReceived('create');
|
||||
$users->shouldHaveReceived('store')->with($user);
|
||||
$identities->shouldHaveReceived('store');
|
||||
$auth->shouldHaveReceived('login')->with($user);
|
||||
}
|
||||
|
||||
public function test_if_a_user_is_returned_from_the_callback_that_user_is_used()
|
||||
{
|
||||
$providerAlias = 'provider';
|
||||
|
||||
$userDetails = new SocialNormUser([]);
|
||||
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
|
||||
$otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
|
||||
|
||||
$auth = M::spy();
|
||||
|
||||
$users = M::spy([
|
||||
'findByIdentity' => $user
|
||||
]);
|
||||
|
||||
$identities = M::spy([
|
||||
'userExists' => true,
|
||||
'getByProvider' => new OAuthIdentity,
|
||||
]);
|
||||
|
||||
$authenticator = new Authenticator($auth, $users, $identities);
|
||||
$authenticator->login('provider', $userDetails, function () use ($otherUser) {
|
||||
return $otherUser;
|
||||
});
|
||||
|
||||
$users->shouldNotHaveReceived('create');
|
||||
$users->shouldHaveReceived('store')->with($otherUser);
|
||||
$identities->shouldHaveReceived('store');
|
||||
$auth->shouldHaveReceived('login')->with($otherUser);
|
||||
}
|
||||
|
||||
public function test_if_nothing_is_returned_from_the_callback_the_found_or_created_user_is_used()
|
||||
{
|
||||
$providerAlias = 'provider';
|
||||
|
||||
$userDetails = new SocialNormUser([]);
|
||||
$foundUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
|
||||
$otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
|
||||
|
||||
$auth = M::spy();
|
||||
|
||||
$users = M::spy([
|
||||
'findByIdentity' => $foundUser
|
||||
]);
|
||||
|
||||
$identities = M::spy([
|
||||
'userExists' => true,
|
||||
'getByProvider' => new OAuthIdentity,
|
||||
]);
|
||||
|
||||
$authenticator = new Authenticator($auth, $users, $identities);
|
||||
$authenticator->login('provider', $userDetails, function () {
|
||||
return;
|
||||
});
|
||||
|
||||
$users->shouldNotHaveReceived('create');
|
||||
$users->shouldHaveReceived('store')->with($foundUser);
|
||||
$identities->shouldHaveReceived('store');
|
||||
$auth->shouldHaveReceived('login')->with($foundUser);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as DB;
|
||||
|
||||
abstract class FunctionalTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->configureDatabase();
|
||||
$this->migrateIdentitiesTable();
|
||||
}
|
||||
|
||||
protected function configureDatabase()
|
||||
{
|
||||
$db = new DB;
|
||||
$db->addConnection(array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => ':memory:',
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'prefix' => '',
|
||||
));
|
||||
$db->bootEloquent();
|
||||
$db->setAsGlobal();
|
||||
}
|
||||
|
||||
public function migrateIdentitiesTable()
|
||||
{
|
||||
DB::schema()->create('oauth_identities', function($table) {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->string('provider_user_id');
|
||||
$table->string('provider');
|
||||
$table->string('access_token');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
use AdamWathan\EloquentOAuth\OAuthIdentity;
|
||||
use AdamWathan\EloquentOAuth\EloquentIdentityStore;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
use Mockery as M;
|
||||
use SocialNorm\User as UserDetails;
|
||||
|
||||
class IdentityStoreTest extends FunctionalTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
Eloquent::unguard();
|
||||
}
|
||||
|
||||
public function test_get_by_provider()
|
||||
{
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 1,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'foobar',
|
||||
'access_token' => 'abc123',
|
||||
));
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 2,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'bazfoo',
|
||||
'access_token' => 'def456',
|
||||
));
|
||||
$details = new UserDetails(array(
|
||||
'access_token' => 'new-token',
|
||||
'id' => 'bazfoo',
|
||||
'nickname' => 'john.doe',
|
||||
'full_name' => 'John Doe',
|
||||
'email' => 'john.doe@example.com',
|
||||
'avatar' => 'http://example.com/photos/john_doe.jpg',
|
||||
));
|
||||
$identities = new EloquentIdentityStore;
|
||||
$identity = $identities->getByProvider('facebook', $details);
|
||||
$this->assertEquals(2, $identity->user_id);
|
||||
$this->assertEquals('facebook', $identity->provider);
|
||||
$this->assertEquals('bazfoo', $identity->provider_user_id);
|
||||
$this->assertEquals('def456', $identity->access_token);
|
||||
}
|
||||
|
||||
public function test_get_by_provider_when_no_match()
|
||||
{
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 1,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'foobar',
|
||||
'access_token' => 'abc123',
|
||||
));
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 2,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'bazfoo',
|
||||
'access_token' => 'def456',
|
||||
));
|
||||
$details = new UserDetails(array(
|
||||
'access_token' => 'new-token',
|
||||
'id' => 'missing-id',
|
||||
'nickname' => 'john.doe',
|
||||
'full_name' => 'John Doe',
|
||||
'email' => 'john.doe@example.com',
|
||||
'avatar' => 'http://example.com/photos/john_doe.jpg',
|
||||
));
|
||||
$identities = new EloquentIdentityStore;
|
||||
$identity = $identities->getByProvider('facebook', $details);
|
||||
$this->assertNull($identity);
|
||||
}
|
||||
|
||||
public function test_flush()
|
||||
{
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 1,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'foobar',
|
||||
'access_token' => 'abc123',
|
||||
));
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 2,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'bazfoo',
|
||||
'access_token' => 'def456',
|
||||
));
|
||||
|
||||
$this->assertEquals(1, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count());
|
||||
|
||||
$identities = new EloquentIdentityStore;
|
||||
$user = M::mock();
|
||||
$user->shouldReceive('getKey')->andReturn(2);
|
||||
$identities->flush($user, 'facebook');
|
||||
|
||||
$this->assertEquals(0, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count());
|
||||
}
|
||||
|
||||
public function test_store()
|
||||
{
|
||||
$identity = new OAuthIdentity(array(
|
||||
'user_id' => 1,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'foobar',
|
||||
'access_token' => 'abc123',
|
||||
));
|
||||
|
||||
$this->assertEquals(0, OAuthIdentity::count());
|
||||
|
||||
$identities = new EloquentIdentityStore;
|
||||
$identities->store($identity);
|
||||
|
||||
$this->assertEquals(1, OAuthIdentity::count());
|
||||
}
|
||||
|
||||
public function test_user_exists_returns_true_when_user_exists()
|
||||
{
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 2,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'bazfoo',
|
||||
'access_token' => 'def456',
|
||||
));
|
||||
$details = new UserDetails(array(
|
||||
'access_token' => 'new-token',
|
||||
'id' => 'bazfoo',
|
||||
'nickname' => 'john.doe',
|
||||
'full_name' => 'John Doe',
|
||||
'email' => 'john.doe@example.com',
|
||||
'avatar' => 'http://example.com/photos/john_doe.jpg',
|
||||
));
|
||||
$identities = new EloquentIdentityStore;
|
||||
$this->assertTrue($identities->userExists('facebook', $details));
|
||||
}
|
||||
|
||||
public function test_user_exists_returns_false_when_user_doesnt_exist()
|
||||
{
|
||||
OAuthIdentity::create(array(
|
||||
'user_id' => 2,
|
||||
'provider' => 'facebook',
|
||||
'provider_user_id' => 'foobar',
|
||||
'access_token' => 'def456',
|
||||
));
|
||||
$details = new UserDetails(array(
|
||||
'access_token' => 'new-token',
|
||||
'id' => 'bazfoo',
|
||||
'nickname' => 'john.doe',
|
||||
'full_name' => 'John Doe',
|
||||
'email' => 'john.doe@example.com',
|
||||
'avatar' => 'http://example.com/photos/john_doe.jpg',
|
||||
));
|
||||
$identities = new EloquentIdentityStore;
|
||||
$this->assertFalse($identities->userExists('facebook', $details));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use Mockery as M;
|
||||
use AdamWathan\EloquentOAuth\OAuthManager;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Illuminate\Routing\UrlGenerator;
|
||||
use Illuminate\Routing\RouteCollection;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OAuthManagerTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
M::close();
|
||||
}
|
||||
|
||||
public function test_it_returns_a_redirect_to_the_authorize_url()
|
||||
{
|
||||
$redirector = $this->buildRedirector();
|
||||
$authenticator = M::mock('AdamWathan\EloquentOAuth\Authenticator');
|
||||
$socialnorm = M::mock('SocialNorm\SocialNorm');
|
||||
$socialnorm->shouldReceive('authorize')->with('example')->andReturn('http://example.com/authorize');
|
||||
|
||||
$oauth = new OAuthManager($redirector, $authenticator, $socialnorm);
|
||||
$response = $oauth->authorize('example');
|
||||
$this->assertEquals('http://example.com/authorize', $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function test_it_logs_the_user_in()
|
||||
{
|
||||
$providerAlias = 'twitbook';
|
||||
$socialnormUser = new SocialNorm\User([]);
|
||||
$callback = function () {};
|
||||
|
||||
$redirector = $this->buildRedirector();
|
||||
|
||||
$authenticator = M::spy('AdamWathan\EloquentOAuth\Authenticator');
|
||||
|
||||
$socialnorm = M::mock('SocialNorm\SocialNorm');
|
||||
$socialnorm->shouldReceive('getUser')
|
||||
->with($providerAlias)
|
||||
->andReturn($socialnormUser);
|
||||
|
||||
$oauth = new OAuthManager($redirector, $authenticator, $socialnorm);
|
||||
$oauth->login($providerAlias, $callback);
|
||||
|
||||
$authenticator->shouldHaveReceived('login')->with($providerAlias, $socialnormUser, $callback);
|
||||
}
|
||||
|
||||
private function buildRedirector()
|
||||
{
|
||||
return new Redirector(new UrlGenerator(new RouteCollection, new Request));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user