3 use BookStack\Auth\User;
5 use Laravel\Socialite\Contracts\Factory;
6 use Laravel\Socialite\Contracts\Provider;
9 class SocialAuthTest extends TestCase
12 public function test_social_registration()
14 $user = factory(User::class)->make();
16 $this->setSettings(['registration-enabled' => 'true']);
17 config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
19 $mockSocialite = Mockery::mock(Factory::class);
20 $this->app[Factory::class] = $mockSocialite;
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 = Mockery::mock(Factory::class);
49 $this->app[Factory::class] = $mockSocialite;
50 $mockSocialDriver = Mockery::mock(Provider::class);
51 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
53 $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
55 $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
56 $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
57 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
58 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
61 $resp = $this->get('/login');
62 $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
63 $resp = $this->followingRedirects()->get("/login/service/google");
64 $resp->assertSee('login-form');
66 // Test social callback
67 $resp = $this->followingRedirects()->get('/login/service/google/callback');
68 $resp->assertSee('login-form');
69 $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
71 $resp = $this->get('/login');
72 $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
73 $resp = $this->followingRedirects()->get("/login/service/github");
74 $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_autoregister()
90 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
91 'APP_URL' => 'http://localhost'
94 $user = factory(User::class)->make();
95 $mockSocialite = Mockery::mock(Factory::class);
96 $this->app[Factory::class] = $mockSocialite;
97 $mockSocialDriver = Mockery::mock(Provider::class);
98 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
100 $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
101 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
102 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
103 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
105 $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
106 $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
107 $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
109 $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
111 $this->get('/login/service/google');
112 $resp = $this->followingRedirects()->get('/login/service/google/callback');
113 $resp->assertSee($googleAccountNotUsedMessage);
115 config(['services.google.auto_register' => true]);
117 $this->get('/login/service/google');
118 $resp = $this->followingRedirects()->get('/login/service/google/callback');
119 $resp->assertDontSee($googleAccountNotUsedMessage);
121 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
122 $user = $user->whereEmail($user->email)->first();
123 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
126 public function test_social_auto_email_confirm()
129 'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
130 'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
133 $user = factory(User::class)->make();
134 $mockSocialite = Mockery::mock(Factory::class);
135 $this->app[Factory::class] = $mockSocialite;
136 $mockSocialDriver = Mockery::mock(Provider::class);
137 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
139 $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
140 $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
141 $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
142 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
144 $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
145 $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
146 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
148 $this->get('/login/service/google');
149 $this->get('/login/service/google/callback');
151 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
152 $user = $user->whereEmail($user->email)->first();
153 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
156 public function test_google_select_account_option_changes_redirect_url()
158 config()->set('services.google.select_account', 'true');
160 $resp = $this->get('/login/service/google');
161 $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
164 public function test_social_registration_with_no_name_uses_email_as_name()
168 $this->setSettings(['registration-enabled' => 'true']);
169 config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
171 $mockSocialite = Mockery::mock(Factory::class);
172 $this->app[Factory::class] = $mockSocialite;
173 $mockSocialDriver = Mockery::mock(Provider::class);
174 $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
176 $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
177 $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
178 $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
180 $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
181 $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
182 $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
183 $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
185 $this->get('/register/service/github');
186 $this->get('/login/service/github/callback');
187 $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
188 $user = $user->whereEmail($user->email)->first();
189 $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);