]> BookStack Code Mirror - bookstack/blob - tests/Auth/SocialAuthTest.php
Code cleanup, bug squashing
[bookstack] / tests / Auth / SocialAuthTest.php
1 <?php namespace Tests\Auth;
2
3 use BookStack\Auth\User;
4 use DB;
5 use Laravel\Socialite\Contracts\Factory;
6 use Laravel\Socialite\Contracts\Provider;
7 use Mockery;
8 use Tests\TestCase;
9
10 class SocialAuthTest extends TestCase
11 {
12
13     public function test_social_registration()
14     {
15         $user = factory(User::class)->make();
16
17         $this->setSettings(['registration-enabled' => 'true']);
18         config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
19
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);
24
25         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
26         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
27         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
28
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');
33
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]);
39     }
40
41     public function test_social_login()
42     {
43         config([
44             'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
45             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
46             'APP_URL' => 'http://localhost'
47         ]);
48
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);
53
54         $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
55
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('/'));
60
61         // Test login routes
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');
66
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']));
71
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');
76
77
78         // Test social callback with matching social account
79         DB::table('social_accounts')->insert([
80             'user_id' => $this->getAdmin()->id,
81             'driver' => 'github',
82             'driver_id' => 'logintest123'
83         ]);
84         $resp = $this->followingRedirects()->get('/login/service/github/callback');
85         $resp->assertDontSee("login-form");
86     }
87
88     public function test_social_autoregister()
89     {
90         config([
91             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
92             'APP_URL' => 'http://localhost'
93         ]);
94
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);
100
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');
105
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('/'));
109
110         $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
111
112         $this->get('/login/service/google');
113         $resp = $this->followingRedirects()->get('/login/service/google/callback');
114         $resp->assertSee($googleAccountNotUsedMessage);
115
116         config(['services.google.auto_register' => true]);
117
118         $this->get('/login/service/google');
119         $resp = $this->followingRedirects()->get('/login/service/google/callback');
120         $resp->assertDontSee($googleAccountNotUsedMessage);
121
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]);
125     }
126
127     public function test_social_auto_email_confirm()
128     {
129         config([
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
132         ]);
133
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);
139
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');
144
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('/'));
148
149         $this->get('/login/service/google');
150         $this->get('/login/service/google/callback');
151
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]);
155     }
156
157     public function test_google_select_account_option_changes_redirect_url()
158     {
159         config()->set('services.google.select_account', 'true');
160
161         $resp = $this->get('/login/service/google');
162         $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
163     }
164
165     public function test_social_registration_with_no_name_uses_email_as_name()
166     {
167         $user = factory(User::class)->make(['email' => '[email protected]']);
168
169         $this->setSettings(['registration-enabled' => 'true']);
170         config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
171
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);
176
177         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
178         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
179         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
180
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');
185
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]);
191     }
192
193 }