X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/be4f3d62cd37c7b83eb86bbf5fffa00d20acf2ec..refs/pull/3593/head:/tests/Auth/SocialAuthTest.php diff --git a/tests/Auth/SocialAuthTest.php b/tests/Auth/SocialAuthTest.php index 5bfe0c222..67da771a5 100644 --- a/tests/Auth/SocialAuthTest.php +++ b/tests/Auth/SocialAuthTest.php @@ -1,20 +1,28 @@ -make(); + $user = User::factory()->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 = $this->mock(Factory::class); + $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('/')); @@ -37,13 +45,12 @@ class SocialAuthTest extends TestCase config([ 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', - 'APP_URL' => 'http://localhost' + '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 = $this->mock(Factory::class); + $mockSocialDriver = Mockery::mock(Provider::class); + $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class); $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123'); @@ -54,8 +61,8 @@ class SocialAuthTest extends TestCase // Test login routes $resp = $this->get('/login'); - $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]'); - $resp = $this->followingRedirects()->get("/login/service/google"); + $this->withHtml($resp)->assertElementExists('a#social-login-google[href$="/login/service/google"]'); + $resp = $this->followingRedirects()->get('/login/service/google'); $resp->assertSee('login-form'); // Test social callback @@ -64,33 +71,57 @@ class SocialAuthTest extends TestCase $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google'])); $resp = $this->get('/login'); - $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]'); - $resp = $this->followingRedirects()->get("/login/service/github"); + $this->withHtml($resp)->assertElementExists('a#social-login-github[href$="/login/service/github"]'); + $resp = $this->followingRedirects()->get('/login/service/github'); $resp->assertSee('login-form'); - // Test social callback with matching social account - \DB::table('social_accounts')->insert([ - 'user_id' => $this->getAdmin()->id, - 'driver' => 'github', - 'driver_id' => 'logintest123' + DB::table('social_accounts')->insert([ + 'user_id' => $this->getAdmin()->id, + 'driver' => 'github', + 'driver_id' => 'logintest123', ]); $resp = $this->followingRedirects()->get('/login/service/github/callback'); - $resp->assertDontSee("login-form"); + $resp->assertDontSee('login-form'); + $this->assertActivityExists(ActivityType::AUTH_LOGIN, null, 'github; (' . $this->getAdmin()->id . ') ' . $this->getAdmin()->name); + } + + public function test_social_account_detach() + { + $editor = $this->getEditor(); + config([ + 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', + 'APP_URL' => 'http://localhost', + ]); + + $socialAccount = SocialAccount::query()->forceCreate([ + 'user_id' => $editor->id, + 'driver' => 'github', + 'driver_id' => 'logintest123', + ]); + + $resp = $this->actingAs($editor)->get($editor->getEditUrl()); + $this->withHtml($resp)->assertElementContains('form[action$="/login/service/github/detach"]', 'Disconnect Account'); + + $resp = $this->post('/login/service/github/detach'); + $resp->assertRedirect($editor->getEditUrl()); + $resp = $this->followRedirects($resp); + $resp->assertSee('Github account was successfully disconnected from your profile.'); + + $this->assertDatabaseMissing('social_accounts', ['id' => $socialAccount->id]); } public function test_social_autoregister() { config([ 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc', - 'APP_URL' => 'http://localhost' + 'APP_URL' => 'http://localhost', ]); - $user = factory(\BookStack\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 = User::factory()->make(); + $mockSocialite = $this->mock(Factory::class); + $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); @@ -122,14 +153,13 @@ class SocialAuthTest extends TestCase { config([ 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc', - '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\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 = User::factory()->make(); + $mockSocialite = $this->mock(Factory::class); + $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); @@ -148,4 +178,38 @@ class SocialAuthTest extends TestCase $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]); } + public function test_google_select_account_option_changes_redirect_url() + { + config()->set('services.google.select_account', 'true'); + + $resp = $this->get('/login/service/google'); + $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location')); + } + + public function test_social_registration_with_no_name_uses_email_as_name() + { + $user = User::factory()->make(['email' => 'nonameuser@example.com']); + + $this->setSettings(['registration-enabled' => 'true']); + config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']); + + $mockSocialite = $this->mock(Factory::class); + $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]); + } }