5 use BookStack\Auth\SocialAccount;
6 use BookStack\Auth\User;
8 use Laravel\Socialite\Contracts\Factory;
9 use Laravel\Socialite\Contracts\Provider;
13 class SocialAuthTest extends TestCase
15 public function test_social_registration()
17 $user = factory(User::class)->make();
19 $this->setSettings(['registration-enabled' => 'true']);
20 config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
22 $mockSocialite = $this->mock(Factory::class);
23 $mockSocialDriver = Mockery::mock(Provider::class);
24 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
26 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
27 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
28 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
30 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
31 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
32 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
33 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
35 $this->get('/register/service/google');
36 $this->get('/login/service/google/callback');
37 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
38 $user = $user->whereEmail($user->email)->first();
39 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
42 public function test_social_login()
45 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
46 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
47 'APP_URL' => 'http://localhost',
50 $mockSocialite = $this->mock(Factory::class);
51 $mockSocialDriver = Mockery::mock(Provider::class);
52 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
54 $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
56 $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
57 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
58 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
59 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
62 $resp = $this->get('/login');
63 $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
64 $resp = $this->followingRedirects()->get('/login/service/google');
65 $resp->assertSee('login-form');
67 // Test social callback
68 $resp = $this->followingRedirects()->get('/login/service/google/callback');
69 $resp->assertSee('login-form');
70 $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
72 $resp = $this->get('/login');
73 $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
74 $resp = $this->followingRedirects()->get('/login/service/github');
75 $resp->assertSee('login-form');
77 // Test social callback with matching social account
78 DB::table('social_accounts')->insert([
79 'user_id' => $this->getAdmin()->id,
81 'driver_id' => 'logintest123',
83 $resp = $this->followingRedirects()->get('/login/service/github/callback');
84 $resp->assertDontSee('login-form');
87 public function test_social_account_detach()
89 $editor = $this->getEditor();
91 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
92 'APP_URL' => 'http://localhost',
95 $socialAccount = SocialAccount::query()->forceCreate([
96 'user_id' => $editor->id,
98 'driver_id' => 'logintest123',
101 $resp = $this->actingAs($editor)->get($editor->getEditUrl());
102 $resp->assertElementContains('form[action$="/login/service/github/detach"]', 'Disconnect Account');
104 $resp = $this->post('/login/service/github/detach');
105 $resp->assertRedirect($editor->getEditUrl());
106 $resp = $this->followRedirects($resp);
107 $resp->assertSee('Github account was successfully disconnected from your profile.');
109 $this->assertDatabaseMissing('social_accounts', ['id' => $socialAccount->id]);
112 public function test_social_autoregister()
115 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
116 'APP_URL' => 'http://localhost',
119 $user = factory(User::class)->make();
120 $mockSocialite = $this->mock(Factory::class);
121 $mockSocialDriver = Mockery::mock(Provider::class);
122 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
124 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
125 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
126 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
127 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
129 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
130 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
131 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
133 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
135 $this->get('/login/service/google');
136 $resp = $this->followingRedirects()->get('/login/service/google/callback');
137 $resp->assertSee($googleAccountNotUsedMessage);
139 config(['services.google.auto_register' => true]);
141 $this->get('/login/service/google');
142 $resp = $this->followingRedirects()->get('/login/service/google/callback');
143 $resp->assertDontSee($googleAccountNotUsedMessage);
145 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
146 $user = $user->whereEmail($user->email)->first();
147 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
150 public function test_social_auto_email_confirm()
153 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
154 'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true,
157 $user = factory(User::class)->make();
158 $mockSocialite = $this->mock(Factory::class);
159 $mockSocialDriver = Mockery::mock(Provider::class);
160 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
162 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
163 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
164 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
165 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
167 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
168 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
169 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
171 $this->get('/login/service/google');
172 $this->get('/login/service/google/callback');
174 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
175 $user = $user->whereEmail($user->email)->first();
176 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
179 public function test_google_select_account_option_changes_redirect_url()
181 config()->set('services.google.select_account', 'true');
183 $resp = $this->get('/login/service/google');
184 $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
187 public function test_social_registration_with_no_name_uses_email_as_name()
191 $this->setSettings(['registration-enabled' => 'true']);
192 config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
194 $mockSocialite = $this->mock(Factory::class);
195 $mockSocialDriver = Mockery::mock(Provider::class);
196 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
198 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
199 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
200 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
202 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
203 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
204 $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
205 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
207 $this->get('/register/service/github');
208 $this->get('/login/service/github/callback');
209 $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
210 $user = $user->whereEmail($user->email)->first();
211 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);