]> BookStack Code Mirror - bookstack/blob - tests/Auth/SocialAuthTest.php
Adding Lithuanian language
[bookstack] / tests / Auth / SocialAuthTest.php
1 <?php
2
3 namespace Tests\Auth;
4
5 use BookStack\Auth\SocialAccount;
6 use BookStack\Auth\User;
7 use DB;
8 use Laravel\Socialite\Contracts\Factory;
9 use Laravel\Socialite\Contracts\Provider;
10 use Mockery;
11 use Tests\TestCase;
12
13 class SocialAuthTest extends TestCase
14 {
15     public function test_social_registration()
16     {
17         $user = factory(User::class)->make();
18
19         $this->setSettings(['registration-enabled' => 'true']);
20         config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
21
22         $mockSocialite = $this->mock(Factory::class);
23         $mockSocialDriver = Mockery::mock(Provider::class);
24         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
25
26         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
27         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
28         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
29
30         $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
31         $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
32         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
33         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
34
35         $this->get('/register/service/google');
36         $this->get('/login/service/google/callback');
37         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
38         $user = $user->whereEmail($user->email)->first();
39         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
40     }
41
42     public function test_social_login()
43     {
44         config([
45             'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
46             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
47             'APP_URL'       => 'http://localhost',
48         ]);
49
50         $mockSocialite = $this->mock(Factory::class);
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         // Test social callback with matching social account
78         DB::table('social_accounts')->insert([
79             'user_id'   => $this->getAdmin()->id,
80             'driver'    => 'github',
81             'driver_id' => 'logintest123',
82         ]);
83         $resp = $this->followingRedirects()->get('/login/service/github/callback');
84         $resp->assertDontSee('login-form');
85     }
86
87     public function test_social_account_detach()
88     {
89         $editor = $this->getEditor();
90         config([
91             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
92             'APP_URL'       => 'http://localhost',
93         ]);
94
95         $socialAccount = SocialAccount::query()->forceCreate([
96             'user_id'   => $editor->id,
97             'driver'    => 'github',
98             'driver_id' => 'logintest123',
99         ]);
100
101         $resp = $this->actingAs($editor)->get($editor->getEditUrl());
102         $resp->assertElementContains('form[action$="/login/service/github/detach"]', 'Disconnect Account');
103
104         $resp = $this->post('/login/service/github/detach');
105         $resp->assertRedirect($editor->getEditUrl());
106         $resp = $this->followRedirects($resp);
107         $resp->assertSee('Github account was successfully disconnected from your profile.');
108
109         $this->assertDatabaseMissing('social_accounts', ['id' => $socialAccount->id]);
110     }
111
112     public function test_social_autoregister()
113     {
114         config([
115             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
116             'APP_URL'                   => 'http://localhost',
117         ]);
118
119         $user = factory(User::class)->make();
120         $mockSocialite = $this->mock(Factory::class);
121         $mockSocialDriver = Mockery::mock(Provider::class);
122         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
123
124         $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
125         $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
126         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
127         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
128
129         $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
130         $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
131         $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
132
133         $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
134
135         $this->get('/login/service/google');
136         $resp = $this->followingRedirects()->get('/login/service/google/callback');
137         $resp->assertSee($googleAccountNotUsedMessage);
138
139         config(['services.google.auto_register' => true]);
140
141         $this->get('/login/service/google');
142         $resp = $this->followingRedirects()->get('/login/service/google/callback');
143         $resp->assertDontSee($googleAccountNotUsedMessage);
144
145         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
146         $user = $user->whereEmail($user->email)->first();
147         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
148     }
149
150     public function test_social_auto_email_confirm()
151     {
152         config([
153             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
154             'APP_URL'                   => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true,
155         ]);
156
157         $user = factory(User::class)->make();
158         $mockSocialite = $this->mock(Factory::class);
159         $mockSocialDriver = Mockery::mock(Provider::class);
160         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
161
162         $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
163         $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
164         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
165         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
166
167         $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
168         $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
169         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
170
171         $this->get('/login/service/google');
172         $this->get('/login/service/google/callback');
173
174         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
175         $user = $user->whereEmail($user->email)->first();
176         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
177     }
178
179     public function test_google_select_account_option_changes_redirect_url()
180     {
181         config()->set('services.google.select_account', 'true');
182
183         $resp = $this->get('/login/service/google');
184         $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
185     }
186
187     public function test_social_registration_with_no_name_uses_email_as_name()
188     {
189         $user = factory(User::class)->make(['email' => '[email protected]']);
190
191         $this->setSettings(['registration-enabled' => 'true']);
192         config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
193
194         $mockSocialite = $this->mock(Factory::class);
195         $mockSocialDriver = Mockery::mock(Provider::class);
196         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
197
198         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
199         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
200         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
201
202         $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
203         $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
204         $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
205         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
206
207         $this->get('/register/service/github');
208         $this->get('/login/service/github/callback');
209         $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
210         $user = $user->whereEmail($user->email)->first();
211         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
212     }
213 }