diff --git a/tests/AuthTest.php b/tests/AuthTest.php index 99f132d3b..1c5628d9c 100644 --- a/tests/AuthTest.php +++ b/tests/AuthTest.php @@ -112,18 +112,6 @@ class AuthTest extends TestCase ->seePageIs('/login'); } - /** - * Quickly sets an array of settings. - * @param $settingsArray - */ - private function setSettings($settingsArray) - { - $settings = app('BookStack\Services\SettingService'); - foreach ($settingsArray as $key => $value) { - $settings->put($key, $value); - } - } - /** * Perform a login * @param string $email diff --git a/tests/SocialAuthTest.php b/tests/SocialAuthTest.php new file mode 100644 index 000000000..820ecd4dd --- /dev/null +++ b/tests/SocialAuthTest.php @@ -0,0 +1,42 @@ +make(); + + $this->setSettings(['registration-enabled' => 'true']); + $this->setEnvironment(['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->shouldReceive('driver')->twice()->with('google')->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($user->name); + $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder'); + + $this->visit('/register/service/google'); + $this->visit('/login/service/google/callback'); + $this->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]); + $user = $user->whereEmail($user->email)->first(); + $this->seeInDatabase('social_accounts', ['user_id' => $user->id]); + } + + protected function setEnvironment($array) + { + foreach ($array as $key => $value) { + putenv("$key=$value"); + } + } + +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 67a18777a..3f7d846f7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -36,4 +36,16 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase } return $this->actingAs($this->admin); } + + /** + * Quickly sets an array of settings. + * @param $settingsArray + */ + protected function setSettings($settingsArray) + { + $settings = app('BookStack\Services\SettingService'); + foreach ($settingsArray as $key => $value) { + $settings->put($key, $value); + } + } }