]> BookStack Code Mirror - bookstack/blob - tests/Auth/SocialAuthTest.php
5739f9a7d4cf3989683b48d4ad499773be15bec8
[bookstack] / tests / Auth / SocialAuthTest.php
1 <?php
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\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->visit('/register/service/google');
29         $this->visit('/login/service/google/callback');
30         $this->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
31         $user = $user->whereEmail($user->email)->first();
32         $this->seeInDatabase('social_accounts', ['user_id' => $user->id]);
33     }
34
35     public function test_social_login()
36     {
37         $user = factory(\BookStack\User::class)->make();
38
39         config([
40             'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
41             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
42             'APP_URL' => 'http://localhost'
43         ]);
44
45         $mockSocialite = Mockery::mock('Laravel\Socialite\Contracts\Factory');
46         $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
47         $mockSocialDriver = Mockery::mock('Laravel\Socialite\Contracts\Provider');
48         $mockSocialUser = Mockery::mock('\Laravel\Socialite\Contracts\User');
49
50         $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
51
52         $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
53         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
54         $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
55         $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
56
57         // Test login routes
58         $this->visit('/login')->seeElement('#social-login-google')
59             ->click('#social-login-google')
60             ->seePageIs('/login');
61
62         // Test social callback
63         $this->visit('/login/service/google/callback')->seePageIs('/login')
64             ->see(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
65
66         $this->visit('/login')->seeElement('#social-login-github')
67         ->click('#social-login-github')
68         ->seePageIs('/login');
69
70         // Test social callback with matching social account
71         DB::table('social_accounts')->insert([
72             'user_id' => $this->getAdmin()->id,
73             'driver' => 'github',
74             'driver_id' => 'logintest123'
75         ]);
76         $this->visit('/login/service/github/callback')->seePageIs('/');
77     }
78
79 }