mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-27 20:51:57 +00:00
Updated social auth to take name from email if empty
- Added tests to cover. Fixes #1853
This commit is contained in:
parent
ccf92331c2
commit
59aefe5371
@ -123,6 +123,11 @@ class SocialController extends Controller
|
|||||||
'password' => Str::random(32)
|
'password' => Str::random(32)
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Take name from email address if empty
|
||||||
|
if (!$userData['name']) {
|
||||||
|
$userData['name'] = explode('@', $userData['email'])[0];
|
||||||
|
}
|
||||||
|
|
||||||
$user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
|
$user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
|
||||||
auth()->login($user);
|
auth()->login($user);
|
||||||
|
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
<?php namespace Tests;
|
<?php namespace Tests;
|
||||||
|
|
||||||
|
use BookStack\Auth\User;
|
||||||
|
use DB;
|
||||||
|
use Laravel\Socialite\Contracts\Factory;
|
||||||
|
use Laravel\Socialite\Contracts\Provider;
|
||||||
|
use Mockery;
|
||||||
|
|
||||||
class SocialAuthTest extends TestCase
|
class SocialAuthTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function test_social_registration()
|
public function test_social_registration()
|
||||||
{
|
{
|
||||||
// http://docs.mockery.io/en/latest/reference/startup_methods.html
|
$user = factory(User::class)->make();
|
||||||
$user = factory(\BookStack\Auth\User::class)->make();
|
|
||||||
|
|
||||||
$this->setSettings(['registration-enabled' => 'true']);
|
$this->setSettings(['registration-enabled' => 'true']);
|
||||||
config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
|
config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
|
||||||
|
|
||||||
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
|
$mockSocialite = Mockery::mock(Factory::class);
|
||||||
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
|
$this->app[Factory::class] = $mockSocialite;
|
||||||
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
|
$mockSocialDriver = Mockery::mock(Provider::class);
|
||||||
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
|
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
|
||||||
|
|
||||||
$mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
|
$mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
|
||||||
$mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
|
$mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
|
||||||
@ -40,10 +45,10 @@ class SocialAuthTest extends TestCase
|
|||||||
'APP_URL' => 'http://localhost'
|
'APP_URL' => 'http://localhost'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
|
$mockSocialite = Mockery::mock(Factory::class);
|
||||||
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
|
$this->app[Factory::class] = $mockSocialite;
|
||||||
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
|
$mockSocialDriver = Mockery::mock(Provider::class);
|
||||||
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
|
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
|
||||||
|
|
||||||
$mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
|
$mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
|
||||||
|
|
||||||
@ -70,7 +75,7 @@ class SocialAuthTest extends TestCase
|
|||||||
|
|
||||||
|
|
||||||
// Test social callback with matching social account
|
// Test social callback with matching social account
|
||||||
\DB::table('social_accounts')->insert([
|
DB::table('social_accounts')->insert([
|
||||||
'user_id' => $this->getAdmin()->id,
|
'user_id' => $this->getAdmin()->id,
|
||||||
'driver' => 'github',
|
'driver' => 'github',
|
||||||
'driver_id' => 'logintest123'
|
'driver_id' => 'logintest123'
|
||||||
@ -86,11 +91,11 @@ class SocialAuthTest extends TestCase
|
|||||||
'APP_URL' => 'http://localhost'
|
'APP_URL' => 'http://localhost'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = factory(\BookStack\Auth\User::class)->make();
|
$user = factory(User::class)->make();
|
||||||
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
|
$mockSocialite = Mockery::mock(Factory::class);
|
||||||
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
|
$this->app[Factory::class] = $mockSocialite;
|
||||||
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
|
$mockSocialDriver = Mockery::mock(Provider::class);
|
||||||
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
|
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
|
||||||
|
|
||||||
$mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
|
$mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
|
||||||
$mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
|
$mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
|
||||||
@ -125,11 +130,11 @@ class SocialAuthTest extends TestCase
|
|||||||
'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
|
'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = factory(\BookStack\Auth\User::class)->make();
|
$user = factory(User::class)->make();
|
||||||
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
|
$mockSocialite = Mockery::mock(Factory::class);
|
||||||
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
|
$this->app[Factory::class] = $mockSocialite;
|
||||||
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
|
$mockSocialDriver = Mockery::mock(Provider::class);
|
||||||
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
|
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
|
||||||
|
|
||||||
$mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
|
$mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
|
||||||
$mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
|
$mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
|
||||||
@ -156,4 +161,32 @@ class SocialAuthTest extends TestCase
|
|||||||
$this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
|
$this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_social_registration_with_no_name_uses_email_as_name()
|
||||||
|
{
|
||||||
|
$user = factory(User::class)->make(['email' => 'nonameuser@example.com']);
|
||||||
|
|
||||||
|
$this->setSettings(['registration-enabled' => 'true']);
|
||||||
|
config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
|
||||||
|
|
||||||
|
$mockSocialite = Mockery::mock(Factory::class);
|
||||||
|
$this->app[Factory::class] = $mockSocialite;
|
||||||
|
$mockSocialDriver = Mockery::mock(Provider::class);
|
||||||
|
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
|
||||||
|
|
||||||
|
$mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
|
||||||
|
$mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
|
||||||
|
$mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
|
||||||
|
|
||||||
|
$mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
|
||||||
|
$mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
|
||||||
|
$mockSocialUser->shouldReceive('getName')->once()->andReturn('');
|
||||||
|
$mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
|
||||||
|
|
||||||
|
$this->get('/register/service/github');
|
||||||
|
$this->get('/login/service/github/callback');
|
||||||
|
$this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
|
||||||
|
$user = $user->whereEmail($user->email)->first();
|
||||||
|
$this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user