1 <?php namespace Tests\Auth;
3 use BookStack\Auth\User;
5 use Laravel\Socialite\Contracts\Factory;
6 use Laravel\Socialite\Contracts\Provider;
10 class SocialAuthTest extends TestCase
13 public function test_social_registration()
15 $user = factory(User::class)->make();
17 $this->setSettings(['registration-enabled' => 'true']);
18 config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
20 $mockSocialite = $this->mock(Factory::class);
21 $mockSocialDriver = Mockery::mock(Provider::class);
22 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
24 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
25 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
26 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
28 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
29 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
30 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
31 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
33 $this->get('/register/service/google');
34 $this->get('/login/service/google/callback');
35 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
36 $user = $user->whereEmail($user->email)->first();
37 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
40 public function test_social_login()
43 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
44 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
45 'APP_URL' => 'http://localhost'
48 $mockSocialite = $this->mock(Factory::class);
49 $mockSocialDriver = Mockery::mock(Provider::class);
50 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
52 $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
54 $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
55 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
56 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
57 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
60 $resp = $this->get('/login');
61 $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
62 $resp = $this->followingRedirects()->get("/login/service/google");
63 $resp->assertSee('login-form');
65 // Test social callback
66 $resp = $this->followingRedirects()->get('/login/service/google/callback');
67 $resp->assertSee('login-form');
68 $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
70 $resp = $this->get('/login');
71 $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
72 $resp = $this->followingRedirects()->get("/login/service/github");
73 $resp->assertSee('login-form');
76 // Test social callback with matching social account
77 DB::table('social_accounts')->insert([
78 'user_id' => $this->getAdmin()->id,
80 'driver_id' => 'logintest123'
82 $resp = $this->followingRedirects()->get('/login/service/github/callback');
83 $resp->assertDontSee("login-form");
86 public function test_social_autoregister()
89 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
90 'APP_URL' => 'http://localhost'
93 $user = factory(User::class)->make();
94 $mockSocialite = $this->mock(Factory::class);
95 $mockSocialDriver = Mockery::mock(Provider::class);
96 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
98 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
99 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
100 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
101 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
103 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
104 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
105 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
107 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
109 $this->get('/login/service/google');
110 $resp = $this->followingRedirects()->get('/login/service/google/callback');
111 $resp->assertSee($googleAccountNotUsedMessage);
113 config(['services.google.auto_register' => true]);
115 $this->get('/login/service/google');
116 $resp = $this->followingRedirects()->get('/login/service/google/callback');
117 $resp->assertDontSee($googleAccountNotUsedMessage);
119 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
120 $user = $user->whereEmail($user->email)->first();
121 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
124 public function test_social_auto_email_confirm()
127 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
128 'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
131 $user = factory(User::class)->make();
132 $mockSocialite = $this->mock(Factory::class);
133 $mockSocialDriver = Mockery::mock(Provider::class);
134 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
136 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
137 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
138 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
139 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
141 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
142 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
143 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
145 $this->get('/login/service/google');
146 $this->get('/login/service/google/callback');
148 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
149 $user = $user->whereEmail($user->email)->first();
150 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
153 public function test_google_select_account_option_changes_redirect_url()
155 config()->set('services.google.select_account', 'true');
157 $resp = $this->get('/login/service/google');
158 $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
161 public function test_social_registration_with_no_name_uses_email_as_name()
165 $this->setSettings(['registration-enabled' => 'true']);
166 config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
168 $mockSocialite = $this->mock(Factory::class);
169 $mockSocialDriver = Mockery::mock(Provider::class);
170 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
172 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
173 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
174 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
176 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
177 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
178 $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
179 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
181 $this->get('/register/service/github');
182 $this->get('/login/service/github/callback');
183 $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
184 $user = $user->whereEmail($user->email)->first();
185 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);