]> BookStack Code Mirror - bookstack/blob - tests/Auth/SocialAuthTest.php
Add support Windows Authentication via SAML
[bookstack] / tests / Auth / SocialAuthTest.php
1 <?php namespace Tests;
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
9 class SocialAuthTest extends TestCase
10 {
11
12     public function test_social_registration()
13     {
14         $user = factory(User::class)->make();
15
16         $this->setSettings(['registration-enabled' => 'true']);
17         config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
18
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);
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 = 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);
52
53         $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
54
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('/'));
59
60         // Test login routes
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');
65
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']));
70
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');
75
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_autoregister()
88     {
89         config([
90             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
91             'APP_URL' => 'http://localhost'
92         ]);
93
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);
99
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');
104
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('/'));
108
109         $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
110
111         $this->get('/login/service/google');
112         $resp = $this->followingRedirects()->get('/login/service/google/callback');
113         $resp->assertSee($googleAccountNotUsedMessage);
114
115         config(['services.google.auto_register' => true]);
116
117         $this->get('/login/service/google');
118         $resp = $this->followingRedirects()->get('/login/service/google/callback');
119         $resp->assertDontSee($googleAccountNotUsedMessage);
120
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]);
124     }
125
126     public function test_social_auto_email_confirm()
127     {
128         config([
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
131         ]);
132
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);
138
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');
143
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('/'));
147
148         $this->get('/login/service/google');
149         $this->get('/login/service/google/callback');
150
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]);
154     }
155
156     public function test_google_select_account_option_changes_redirect_url()
157     {
158         config()->set('services.google.select_account', 'true');
159
160         $resp = $this->get('/login/service/google');
161         $this->assertStringContainsString('prompt=select_account', $resp->headers->get('Location'));
162     }
163
164     public function test_social_registration_with_no_name_uses_email_as_name()
165     {
166         $user = factory(User::class)->make(['email' => '[email protected]']);
167
168         $this->setSettings(['registration-enabled' => 'true']);
169         config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
170
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);
175
176         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
177         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
178         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
179
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');
184
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]);
190     }
191
192 }