3 class SocialAuthTest extends TestCase
6 public function test_social_registration()
8 // http://docs.mockery.io/en/latest/reference/startup_methods.html
9 $user = factory(\BookStack\Auth\User::class)->make();
11 $this->setSettings(['registration-enabled' => 'true']);
12 config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
14 $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
15 $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
16 $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
17 $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
19 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
20 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
21 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
23 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
24 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
25 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
26 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
28 $this->get('/register/service/google');
29 $this->get('/login/service/google/callback');
30 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
31 $user = $user->whereEmail($user->email)->first();
32 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
35 public function test_social_login()
38 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
39 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
40 'APP_URL' => 'http://localhost'
43 $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
44 $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
45 $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
46 $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
48 $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
50 $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
51 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
52 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
53 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
56 $resp = $this->get('/login');
57 $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
58 $resp = $this->followingRedirects()->get("/login/service/google");
59 $resp->assertSee('login-form');
61 // Test social callback
62 $resp = $this->followingRedirects()->get('/login/service/google/callback');
63 $resp->assertSee('login-form');
64 $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
66 $resp = $this->get('/login');
67 $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
68 $resp = $this->followingRedirects()->get("/login/service/github");
69 $resp->assertSee('login-form');
72 // Test social callback with matching social account
73 \DB::table('social_accounts')->insert([
74 'user_id' => $this->getAdmin()->id,
76 'driver_id' => 'logintest123'
78 $resp = $this->followingRedirects()->get('/login/service/github/callback');
79 $resp->assertDontSee("login-form");
82 public function test_social_autoregister()
85 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
86 'APP_URL' => 'http://localhost'
89 $user = factory(\BookStack\Auth\User::class)->make();
90 $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
91 $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
92 $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
93 $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
95 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
96 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
97 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
98 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
100 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
101 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
102 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
104 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
106 $this->get('/login/service/google');
107 $resp = $this->followingRedirects()->get('/login/service/google/callback');
108 $resp->assertSee($googleAccountNotUsedMessage);
110 config(['services.google.auto_register' => true]);
112 $this->get('/login/service/google');
113 $resp = $this->followingRedirects()->get('/login/service/google/callback');
114 $resp->assertDontSee($googleAccountNotUsedMessage);
116 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
117 $user = $user->whereEmail($user->email)->first();
118 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
121 public function test_social_auto_email_confirm()
124 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
125 'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
128 $user = factory(\BookStack\Auth\User::class)->make();
129 $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
130 $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
131 $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
132 $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
134 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
135 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
136 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
137 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
139 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
140 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
141 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
143 $this->get('/login/service/google');
144 $this->get('/login/service/google/callback');
146 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
147 $user = $user->whereEmail($user->email)->first();
148 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
151 public function test_google_select_account_option_changes_redirect_url()
153 config()->set('services.google.select_account', 'true');
155 $resp = $this->get('/login/service/google');
156 $this->assertContains('prompt=select_account', $resp->headers->get('Location'));