5 use BookStack\Access\SocialAccount;
6 use BookStack\Activity\ActivityType;
7 use BookStack\Users\Models\User;
8 use Illuminate\Support\Facades\DB;
9 use Laravel\Socialite\Contracts\Factory;
10 use Laravel\Socialite\Contracts\Provider;
14 class SocialAuthTest extends TestCase
16 public function test_social_registration()
18 $user = User::factory()->make();
20 $this->setSettings(['registration-enabled' => 'true']);
21 config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc']);
23 $mockSocialite = $this->mock(Factory::class);
24 $mockSocialDriver = Mockery::mock(Provider::class);
25 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
27 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
28 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
29 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
31 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
32 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
33 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
34 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
36 $this->get('/register/service/google');
37 $this->get('/login/service/google/callback');
38 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
39 $user = $user->whereEmail($user->email)->first();
40 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
43 public function test_social_login()
46 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
47 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
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 $this->withHtml($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 $this->withHtml($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->users->admin()->id,
81 'driver_id' => 'logintest123',
83 $resp = $this->followingRedirects()->get('/login/service/github/callback');
84 $resp->assertDontSee('login-form');
85 $this->assertActivityExists(ActivityType::AUTH_LOGIN, null, 'github; (' . $this->users->admin()->id . ') ' . $this->users->admin()->name);
88 public function test_social_account_attach()
91 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
93 $editor = $this->users->editor();
95 $mockSocialite = $this->mock(Factory::class);
96 $mockSocialDriver = Mockery::mock(Provider::class);
97 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
99 $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
100 $mockSocialUser->shouldReceive('getAvatar')->andReturn(null);
102 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
103 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/login/service/google/callback'));
104 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
107 $resp = $this->actingAs($editor)->followingRedirects()->get('/login/service/google');
108 $resp->assertSee('Access & Security');
110 // Test social callback with matching social account
111 $this->assertDatabaseHas('social_accounts', [
112 'user_id' => $editor->id,
113 'driver' => 'google',
114 'driver_id' => 'logintest123',
118 public function test_social_account_detach()
120 $editor = $this->users->editor();
122 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
125 $socialAccount = SocialAccount::query()->forceCreate([
126 'user_id' => $editor->id,
127 'driver' => 'github',
128 'driver_id' => 'logintest123',
131 $resp = $this->actingAs($editor)->get('/my-account/auth');
132 $this->withHtml($resp)->assertElementContains('form[action$="/login/service/github/detach"]', 'Disconnect Account');
134 $resp = $this->post('/login/service/github/detach');
135 $resp->assertRedirect('/my-account/auth#social-accounts');
136 $resp = $this->followRedirects($resp);
137 $resp->assertSee('Github account was successfully disconnected from your profile.');
139 $this->assertDatabaseMissing('social_accounts', ['id' => $socialAccount->id]);
142 public function test_social_autoregister()
145 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
148 $user = User::factory()->make();
149 $mockSocialite = $this->mock(Factory::class);
150 $mockSocialDriver = Mockery::mock(Provider::class);
151 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
153 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
154 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
155 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
156 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
158 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
159 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
160 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
162 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
164 $this->get('/login/service/google');
165 $resp = $this->followingRedirects()->get('/login/service/google/callback');
166 $resp->assertSee($googleAccountNotUsedMessage);
168 config(['services.google.auto_register' => true]);
170 $this->get('/login/service/google');
171 $resp = $this->followingRedirects()->get('/login/service/google/callback');
172 $resp->assertDontSee($googleAccountNotUsedMessage);
174 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
175 $user = $user->whereEmail($user->email)->first();
176 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
179 public function test_social_auto_email_confirm()
182 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
183 'services.google.auto_register' => true, 'services.google.auto_confirm' => true,
186 $user = User::factory()->make();
187 $mockSocialite = $this->mock(Factory::class);
188 $mockSocialDriver = Mockery::mock(Provider::class);
189 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
191 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
192 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
193 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
194 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
196 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
197 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
198 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
200 $this->get('/login/service/google');
201 $this->get('/login/service/google/callback');
203 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
204 $user = $user->whereEmail($user->email)->first();
205 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
208 public function test_google_select_account_option_changes_redirect_url()
210 config()->set('services.google.select_account', 'true');
212 $resp = $this->get('/login/service/google');
213 $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
216 public function test_social_registration_with_no_name_uses_email_as_name()
218 $user = User::factory()->make(['email' => 'nonameuser@example.com']);
220 $this->setSettings(['registration-enabled' => 'true']);
221 config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc']);
223 $mockSocialite = $this->mock(Factory::class);
224 $mockSocialDriver = Mockery::mock(Provider::class);
225 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
227 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
228 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
229 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
231 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
232 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
233 $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
234 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
236 $this->get('/register/service/github');
237 $this->get('/login/service/github/callback');
238 $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
239 $user = $user->whereEmail($user->email)->first();
240 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);