]> BookStack Code Mirror - bookstack/blobdiff - tests/Auth/SocialAuthTest.php
#47 - Fixes the issues with the test case.
[bookstack] / tests / Auth / SocialAuthTest.php
index d5a7e692115c568121e688d548cbe5dc75e8d21f..e3494d073e84ff59d52201f6c4ebe2e6242fd783 100644 (file)
@@ -1,6 +1,6 @@
-<?php
+<?php namespace Tests;
 
-class SocialAuthTest extends TestCase
+class SocialAuthTest extends BrowserKitTest
 {
 
     public function test_social_registration()
@@ -11,10 +11,10 @@ class SocialAuthTest extends TestCase
         $this->setSettings(['registration-enabled' => 'true']);
         config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
 
-        $mockSocialite = Mockery::mock('Laravel\Socialite\Contracts\Factory');
+        $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
         $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
-        $mockSocialDriver = Mockery::mock('Laravel\Socialite\Contracts\Provider');
-        $mockSocialUser = Mockery::mock('\Laravel\Socialite\Contracts\User');
+        $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
+        $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
 
         $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
         $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
@@ -32,4 +32,46 @@ class SocialAuthTest extends TestCase
         $this->seeInDatabase('social_accounts', ['user_id' => $user->id]);
     }
 
+    public function test_social_login()
+    {
+        config([
+            'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
+            'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
+            'APP_URL' => 'http://localhost'
+        ]);
+
+        $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory');
+        $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
+        $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider');
+        $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User');
+
+        $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
+
+        $mockSocialDriver->shouldReceive('user')->twice()->andReturn($mockSocialUser);
+        $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
+        $mockSocialite->shouldReceive('driver')->twice()->with('github')->andReturn($mockSocialDriver);
+        $mockSocialDriver->shouldReceive('redirect')->twice()->andReturn(redirect('/'));
+
+        // Test login routes
+        $this->visit('/login')->seeElement('#social-login-google')
+            ->click('#social-login-google')
+            ->seePageIs('/login');
+
+        // Test social callback
+        $this->visit('/login/service/google/callback')->seePageIs('/login')
+            ->see(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
+
+        $this->visit('/login')->seeElement('#social-login-github')
+        ->click('#social-login-github')
+        ->seePageIs('/login');
+
+        // Test social callback with matching social account
+        \DB::table('social_accounts')->insert([
+            'user_id' => $this->getAdmin()->id,
+            'driver' => 'github',
+            'driver_id' => 'logintest123'
+        ]);
+        $this->visit('/login/service/github/callback')->seePageIs('/');
+    }
+
 }