5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\SocialAccount;
7 use BookStack\Auth\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', 'APP_URL' => 'http://localhost']);
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',
48 'APP_URL' => 'http://localhost',
51 $mockSocialite = $this->mock(Factory::class);
52 $mockSocialDriver = Mockery::mock(Provider::class);
53 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
55 $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
57 $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
58 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
59 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
60 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
63 $resp = $this->get('/login');
64 $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
65 $resp = $this->followingRedirects()->get('/login/service/google');
66 $resp->assertSee('login-form');
68 // Test social callback
69 $resp = $this->followingRedirects()->get('/login/service/google/callback');
70 $resp->assertSee('login-form');
71 $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
73 $resp = $this->get('/login');
74 $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
75 $resp = $this->followingRedirects()->get('/login/service/github');
76 $resp->assertSee('login-form');
78 // Test social callback with matching social account
79 DB::table('social_accounts')->insert([
80 'user_id' => $this->getAdmin()->id,
82 'driver_id' => 'logintest123',
84 $resp = $this->followingRedirects()->get('/login/service/github/callback');
85 $resp->assertDontSee('login-form');
86 $this->assertActivityExists(ActivityType::AUTH_LOGIN, null, 'github; (' . $this->getAdmin()->id . ') ' . $this->getAdmin()->name);
89 public function test_social_account_detach()
91 $editor = $this->getEditor();
93 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
94 'APP_URL' => 'http://localhost',
97 $socialAccount = SocialAccount::query()->forceCreate([
98 'user_id' => $editor->id,
100 'driver_id' => 'logintest123',
103 $resp = $this->actingAs($editor)->get($editor->getEditUrl());
104 $resp->assertElementContains('form[action$="/login/service/github/detach"]', 'Disconnect Account');
106 $resp = $this->post('/login/service/github/detach');
107 $resp->assertRedirect($editor->getEditUrl());
108 $resp = $this->followRedirects($resp);
109 $resp->assertSee('Github account was successfully disconnected from your profile.');
111 $this->assertDatabaseMissing('social_accounts', ['id' => $socialAccount->id]);
114 public function test_social_autoregister()
117 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
118 'APP_URL' => 'http://localhost',
121 $user = User::factory()->make();
122 $mockSocialite = $this->mock(Factory::class);
123 $mockSocialDriver = Mockery::mock(Provider::class);
124 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
126 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
127 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
128 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
129 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
131 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
132 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
133 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
135 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
137 $this->get('/login/service/google');
138 $resp = $this->followingRedirects()->get('/login/service/google/callback');
139 $resp->assertSee($googleAccountNotUsedMessage);
141 config(['services.google.auto_register' => true]);
143 $this->get('/login/service/google');
144 $resp = $this->followingRedirects()->get('/login/service/google/callback');
145 $resp->assertDontSee($googleAccountNotUsedMessage);
147 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
148 $user = $user->whereEmail($user->email)->first();
149 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
152 public function test_social_auto_email_confirm()
155 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
156 'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true,
159 $user = User::factory()->make();
160 $mockSocialite = $this->mock(Factory::class);
161 $mockSocialDriver = Mockery::mock(Provider::class);
162 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
164 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
165 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
166 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
167 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
169 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
170 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
171 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
173 $this->get('/login/service/google');
174 $this->get('/login/service/google/callback');
176 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
177 $user = $user->whereEmail($user->email)->first();
178 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
181 public function test_google_select_account_option_changes_redirect_url()
183 config()->set('services.google.select_account', 'true');
185 $resp = $this->get('/login/service/google');
186 $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
189 public function test_social_registration_with_no_name_uses_email_as_name()
191 $user = User::factory()->make(['email' => 'nonameuser@example.com']);
193 $this->setSettings(['registration-enabled' => 'true']);
194 config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
196 $mockSocialite = $this->mock(Factory::class);
197 $mockSocialDriver = Mockery::mock(Provider::class);
198 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
200 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
201 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
202 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
204 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
205 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
206 $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
207 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
209 $this->get('/register/service/github');
210 $this->get('/login/service/github/callback');
211 $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
212 $user = $user->whereEmail($user->email)->first();
213 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);