]> BookStack Code Mirror - bookstack/blob - tests/Auth/SocialAuthTest.php
Typo fix
[bookstack] / tests / Auth / SocialAuthTest.php
1 <?php namespace Tests;
2
3 class SocialAuthTest extends TestCase
4 {
5
6     public function test_social_registration()
7     {
8         // http://docs.mockery.io/en/latest/reference/startup_methods.html
9         $user = factory(\BookStack\Auth\User::class)->make();
10
11         $this->setSettings(['registration-enabled' => 'true']);
12         config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
13
14         $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
15         $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
16         $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
17         $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
18
19         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
20         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
21         $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
22
23         $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
24         $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
25         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
26         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
27
28         $this->get('/register/service/google');
29         $this->get('/login/service/google/callback');
30         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
31         $user = $user->whereEmail($user->email)->first();
32         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
33     }
34
35     public function test_social_login()
36     {
37         config([
38             'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
39             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
40             'APP_URL' => 'http://localhost'
41         ]);
42
43         $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
44         $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
45         $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
46         $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
47
48         $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
49
50         $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
51         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
52         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
53         $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
54
55         // Test login routes
56         $resp = $this->get('/login');
57         $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
58         $resp = $this->followingRedirects()->get("/login/service/google");
59         $resp->assertSee('login-form');
60
61         // Test social callback
62         $resp = $this->followingRedirects()->get('/login/service/google/callback');
63         $resp->assertSee('login-form');
64         $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
65
66         $resp = $this->get('/login');
67         $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
68         $resp = $this->followingRedirects()->get("/login/service/github");
69         $resp->assertSee('login-form');
70
71
72         // Test social callback with matching social account
73         \DB::table('social_accounts')->insert([
74             'user_id' => $this->getAdmin()->id,
75             'driver' => 'github',
76             'driver_id' => 'logintest123'
77         ]);
78         $resp = $this->followingRedirects()->get('/login/service/github/callback');
79         $resp->assertDontSee("login-form");
80     }
81
82     public function test_social_autoregister()
83     {
84         config([
85             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
86             'APP_URL' => 'http://localhost'
87         ]);
88
89         $user = factory(\BookStack\Auth\User::class)->make();
90         $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
91         $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
92         $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
93         $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
94
95         $mockSocialUser->shouldReceive('getId')->times(4)->andReturn(1);
96         $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
97         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
98         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
99
100         $mockSocialDriver->shouldReceive('user')->times(2)->andReturn($mockSocialUser);
101         $mockSocialite->shouldReceive('driver')->times(4)->with('google')->andReturn($mockSocialDriver);
102         $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
103
104         $googleAccountNotUsedMessage = trans('errors.social_account_not_used', ['socialAccount' => 'Google']);
105
106         $this->get('/login/service/google');
107         $resp = $this->followingRedirects()->get('/login/service/google/callback');
108         $resp->assertSee($googleAccountNotUsedMessage);
109
110         config(['services.google.auto_register' => true]);
111
112         $this->get('/login/service/google');
113         $resp = $this->followingRedirects()->get('/login/service/google/callback');
114         $resp->assertDontSee($googleAccountNotUsedMessage);
115
116         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
117         $user = $user->whereEmail($user->email)->first();
118         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
119     }
120
121     public function test_social_auto_email_confirm()
122     {
123         config([
124             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
125             'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
126         ]);
127
128         $user = factory(\BookStack\Auth\User::class)->make();
129         $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
130         $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
131         $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
132         $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
133
134         $mockSocialUser->shouldReceive('getId')->times(3)->andReturn(1);
135         $mockSocialUser->shouldReceive('getEmail')->times(2)->andReturn($user->email);
136         $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
137         $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
138
139         $mockSocialDriver->shouldReceive('user')->times(1)->andReturn($mockSocialUser);
140         $mockSocialite->shouldReceive('driver')->times(2)->with('google')->andReturn($mockSocialDriver);
141         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
142
143         $this->get('/login/service/google');
144         $this->get('/login/service/google/callback');
145
146         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
147         $user = $user->whereEmail($user->email)->first();
148         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
149     }
150
151     public function test_google_select_account_option_changes_redirect_url()
152     {
153         config()->set('services.google.select_account', 'true');
154
155         $resp = $this->get('/login/service/google');
156         $this->assertContains('prompt=select_account', $resp->headers->get('Location'));
157     }
158
159 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.