]> BookStack Code Mirror - bookstack/blob - tests/Auth/SocialAuthTest.php
Added login/register theme events
[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 = $this->mock(Factory::class);
21         $mockSocialDriver = Mockery::mock(Provider::class);
22         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
23
24         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
25         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
26         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
27
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');
32
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]);
38     }
39
40     public function test_social_login()
41     {
42         config([
43             'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
44             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
45             'APP_URL' => 'http://localhost'
46         ]);
47
48         $mockSocialite = $this->mock(Factory::class);
49         $mockSocialDriver = Mockery::mock(Provider::class);
50         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
51
52         $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
53
54         $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
55         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
56         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
57         $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
58
59         // Test login routes
60         $resp = $this->get('/login');
61         $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
62         $resp = $this->followingRedirects()->get("/login/service/google");
63         $resp->assertSee('login-form');
64
65         // Test social callback
66         $resp = $this->followingRedirects()->get('/login/service/google/callback');
67         $resp->assertSee('login-form');
68         $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
69
70         $resp = $this->get('/login');
71         $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
72         $resp = $this->followingRedirects()->get("/login/service/github");
73         $resp->assertSee('login-form');
74
75
76         // Test social callback with matching social account
77         DB::table('social_accounts')->insert([
78             'user_id' => $this->getAdmin()->id,
79             'driver' => 'github',
80             'driver_id' => 'logintest123'
81         ]);
82         $resp = $this->followingRedirects()->get('/login/service/github/callback');
83         $resp->assertDontSee("login-form");
84     }
85
86     public function test_social_autoregister()
87     {
88         config([
89             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
90             'APP_URL' => 'http://localhost'
91         ]);
92
93         $user = factory(User::class)->make();
94         $mockSocialite = $this->mock(Factory::class);
95         $mockSocialDriver = Mockery::mock(Provider::class);
96         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
97
98         $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
99         $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
100         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
101         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
102
103         $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
104         $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
105         $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
106
107         $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
108
109         $this->get('/login/service/google');
110         $resp = $this->followingRedirects()->get('/login/service/google/callback');
111         $resp->assertSee($googleAccountNotUsedMessage);
112
113         config(['services.google.auto_register' => true]);
114
115         $this->get('/login/service/google');
116         $resp = $this->followingRedirects()->get('/login/service/google/callback');
117         $resp->assertDontSee($googleAccountNotUsedMessage);
118
119         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
120         $user = $user->whereEmail($user->email)->first();
121         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
122     }
123
124     public function test_social_auto_email_confirm()
125     {
126         config([
127             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
128             'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
129         ]);
130
131         $user = factory(User::class)->make();
132         $mockSocialite = $this->mock(Factory::class);
133         $mockSocialDriver = Mockery::mock(Provider::class);
134         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
135
136         $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
137         $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
138         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
139         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
140
141         $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
142         $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
143         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
144
145         $this->get('/login/service/google');
146         $this->get('/login/service/google/callback');
147
148         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
149         $user = $user->whereEmail($user->email)->first();
150         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
151     }
152
153     public function test_google_select_account_option_changes_redirect_url()
154     {
155         config()->set('services.google.select_account', 'true');
156
157         $resp = $this->get('/login/service/google');
158         $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
159     }
160
161     public function test_social_registration_with_no_name_uses_email_as_name()
162     {
163         $user = factory(User::class)->make(['email' => '[email protected]']);
164
165         $this->setSettings(['registration-enabled' => 'true']);
166         config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
167
168         $mockSocialite = $this->mock(Factory::class);
169         $mockSocialDriver = Mockery::mock(Provider::class);
170         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
171
172         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
173         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
174         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
175
176         $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
177         $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
178         $mockSocialUser->shouldReceive('getName')->once()->andReturn('');
179         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
180
181         $this->get('/register/service/github');
182         $this->get('/login/service/github/callback');
183         $this->assertDatabaseHas('users', ['name' => 'nonameuser', 'email' => $user->email]);
184         $user = $user->whereEmail($user->email)->first();
185         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
186     }
187
188 }