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