Updated social auth to take name from email if empty

- Added tests to cover.

Fixes #1853
This commit is contained in:
Dan Brown 2020-03-10 19:09:22 +00:00
parent ccf92331c2
commit 59aefe5371
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 59 additions and 21 deletions

View File

@ -123,6 +123,11 @@ class SocialController extends Controller
'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);
auth()->login($user);

View File

@ -1,20 +1,25 @@
<?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
{
public function test_social_registration()
{
// http://docs.mockery.io/en/latest/reference/startup_methods.html
$user = factory(\BookStack\Auth\User::class)->make();
$user = factory(User::class)->make();
$this->setSettings(['registration-enabled' => 'true']);
config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
$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('google')->andReturn($mockSocialDriver);
$mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
@ -40,10 +45,10 @@ class SocialAuthTest extends TestCase
'APP_URL' => 'http://localhost'
]);
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
$mockSocialite = Mockery::mock(Factory::class);
$this->app[Factory::class] = $mockSocialite;
$mockSocialDriver = Mockery::mock(Provider::class);
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
$mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
@ -70,7 +75,7 @@ class SocialAuthTest extends TestCase
// Test social callback with matching social account
\DB::table('social_accounts')->insert([
DB::table('social_accounts')->insert([
'user_id' => $this->getAdmin()->id,
'driver' => 'github',
'driver_id' => 'logintest123'
@ -86,11 +91,11 @@ class SocialAuthTest extends TestCase
'APP_URL' => 'http://localhost'
]);
$user = factory(\BookStack\Auth\User::class)->make();
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
$user = factory(User::class)->make();
$mockSocialite = Mockery::mock(Factory::class);
$this->app[Factory::class] = $mockSocialite;
$mockSocialDriver = Mockery::mock(Provider::class);
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
$mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
$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
]);
$user = factory(\BookStack\Auth\User::class)->make();
$mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
$this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
$mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
$mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
$user = factory(User::class)->make();
$mockSocialite = Mockery::mock(Factory::class);
$this->app[Factory::class] = $mockSocialite;
$mockSocialDriver = Mockery::mock(Provider::class);
$mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
$mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
$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'));
}
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]);
}
}