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 = Mockery::mock(Factory::class);
21 $this->app[Factory::class] = $mockSocialite;
22 $mockSocialDriver = Mockery::mock(Provider::class);
23 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
25 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
26 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
27 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
29 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
30 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
31 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
32 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
34 $this->get('/register/service/google');
35 $this->get('/login/service/google/callback');
36 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
37 $user = $user->whereEmail($user->email)->first();
38 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
41 public function test_social_login()
44 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
45 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
46 'APP_URL' => 'http://localhost'
49 $mockSocialite = Mockery::mock(Factory::class);
50 $this->app[Factory::class] = $mockSocialite;
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');
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");
88 public function test_social_autoregister()
91 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
92 'APP_URL' => 'http://localhost'
95 $user = factory(User::class)->make();
96 $mockSocialite = Mockery::mock(Factory::class);
97 $this->app[Factory::class] = $mockSocialite;
98 $mockSocialDriver = Mockery::mock(Provider::class);
99 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
101 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
102 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
103 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
104 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
106 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
107 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
108 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
110 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
112 $this->get('/login/service/google');
113 $resp = $this->followingRedirects()->get('/login/service/google/callback');
114 $resp->assertSee($googleAccountNotUsedMessage);
116 config(['services.google.auto_register' => true]);
118 $this->get('/login/service/google');
119 $resp = $this->followingRedirects()->get('/login/service/google/callback');
120 $resp->assertDontSee($googleAccountNotUsedMessage);
122 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
123 $user = $user->whereEmail($user->email)->first();
124 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
127 public function test_social_auto_email_confirm()
130 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
131 'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
134 $user = factory(User::class)->make();
135 $mockSocialite = Mockery::mock(Factory::class);
136 $this->app[Factory::class] = $mockSocialite;
137 $mockSocialDriver = Mockery::mock(Provider::class);
138 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
140 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
141 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
142 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
143 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
145 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
146 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
147 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
149 $this->get('/login/service/google');
150 $this->get('/login/service/google/callback');
152 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
153 $user = $user->whereEmail($user->email)->first();
154 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
157 public function test_google_select_account_option_changes_redirect_url()
159 config()->set('services.google.select_account', 'true');
161 $resp = $this->get('/login/service/google');
162 $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
165 public function test_social_registration_with_no_name_uses_email_as_name()
169 $this->setSettings(['registration-enabled' => 'true']);
170 config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
172 $mockSocialite = Mockery::mock(Factory::class);
173 $this->app[Factory::class] = $mockSocialite;
174 $mockSocialDriver = Mockery::mock(Provider::class);
175 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
177 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
178 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
179 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
181 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
182 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
183 $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
184 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
186 $this->get('/register/service/github');
187 $this->get('/login/service/github/callback');
188 $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
189 $user = $user->whereEmail($user->email)->first();
190 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);