Dan Brown

Added tests to cover social login actions

Closes #244
...@@ -98,7 +98,6 @@ class SocialAuthService ...@@ -98,7 +98,6 @@ class SocialAuthService
98 98
99 // Get any attached social accounts or users 99 // Get any attached social accounts or users
100 $socialAccount = $this->socialAccount->where('driver_id', '=', $socialId)->first(); 100 $socialAccount = $this->socialAccount->where('driver_id', '=', $socialId)->first();
101 - $user = $this->userRepo->getByEmail($socialUser->getEmail());
102 $isLoggedIn = auth()->check(); 101 $isLoggedIn = auth()->check();
103 $currentUser = user(); 102 $currentUser = user();
104 103
......
...@@ -34,10 +34,10 @@ ...@@ -34,10 +34,10 @@
34 <hr class="margin-top"> 34 <hr class="margin-top">
35 <h3 class="text-muted">{{ trans('auth.social_login') }}</h3> 35 <h3 class="text-muted">{{ trans('auth.social_login') }}</h3>
36 @if(isset($socialDrivers['google'])) 36 @if(isset($socialDrivers['google']))
37 - <a href="{{ baseUrl("/login/service/google") }}" style="color: #DC4E41;"><i class="zmdi zmdi-google-plus-box zmdi-hc-4x"></i></a> 37 + <a id="social-login-google" href="{{ baseUrl("/login/service/google") }}" style="color: #DC4E41;"><i class="zmdi zmdi-google-plus-box zmdi-hc-4x"></i></a>
38 @endif 38 @endif
39 @if(isset($socialDrivers['github'])) 39 @if(isset($socialDrivers['github']))
40 - <a href="{{ baseUrl("/login/service/github") }}" style="color:#444;"><i class="zmdi zmdi-github zmdi-hc-4x"></i></a> 40 + <a id="social-login-github" href="{{ baseUrl("/login/service/github") }}" style="color:#444;"><i class="zmdi zmdi-github zmdi-hc-4x"></i></a>
41 @endif 41 @endif
42 @endif 42 @endif
43 </div> 43 </div>
......
...@@ -32,4 +32,48 @@ class SocialAuthTest extends TestCase ...@@ -32,4 +32,48 @@ class SocialAuthTest extends TestCase
32 $this->seeInDatabase('social_accounts', ['user_id' => $user->id]); 32 $this->seeInDatabase('social_accounts', ['user_id' => $user->id]);
33 } 33 }
34 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 +
35 } 79 }
......