]> BookStack Code Mirror - bookstack/blobdiff - tests/Auth/SocialAuthTest.php
respective book and chapter structure added.
[bookstack] / tests / Auth / SocialAuthTest.php
index 1a7a3fccce4d3ea6a0ef69340c228ede6979752e..89b8fd167dcdedf4d7dfea043e43b58a2245f241 100644 (file)
@@ -1,23 +1,26 @@
-<?php namespace Tests;
+<?php
 
-use BookStack\Auth\User;
-use DB;
+namespace Tests\Auth;
+
+use BookStack\Access\SocialAccount;
+use BookStack\Activity\ActivityType;
+use BookStack\Users\Models\User;
+use Illuminate\Support\Facades\DB;
 use Laravel\Socialite\Contracts\Factory;
 use Laravel\Socialite\Contracts\Provider;
 use Mockery;
+use Tests\TestCase;
 
 class SocialAuthTest extends TestCase
 {
-
     public function test_social_registration()
     {
-        $user = factory(User::class)->make();
+        $user = User::factory()->make();
 
         $this->setSettings(['registration-enabled' => 'true']);
-        config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
+        config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc']);
 
-        $mockSocialite = Mockery::mock(Factory::class);
-        $this->app[Factory::class] = $mockSocialite;
+        $mockSocialite = $this->mock(Factory::class);
         $mockSocialDriver = Mockery::mock(Provider::class);
         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
 
@@ -42,11 +45,9 @@ class SocialAuthTest extends TestCase
         config([
             'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
             'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
-            'APP_URL' => 'http://localhost'
         ]);
 
-        $mockSocialite = Mockery::mock(Factory::class);
-        $this->app[Factory::class] = $mockSocialite;
+        $mockSocialite = $this->mock(Factory::class);
         $mockSocialDriver = Mockery::mock(Provider::class);
         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
 
@@ -59,8 +60,8 @@ class SocialAuthTest extends TestCase
 
         // Test login routes
         $resp = $this->get('/login');
-        $resp->assertElementExists('a#social-login-google[href$="/login/service/google"]');
-        $resp = $this->followingRedirects()->get("/login/service/google");
+        $this->withHtml($resp)->assertElementExists('a#social-login-google[href$="/login/service/google"]');
+        $resp = $this->followingRedirects()->get('/login/service/google');
         $resp->assertSee('login-form');
 
         // Test social callback
@@ -69,31 +70,83 @@ class SocialAuthTest extends TestCase
         $resp->assertSee(trans('errors.social_account_not_used', ['socialAccount' => 'Google']));
 
         $resp = $this->get('/login');
-        $resp->assertElementExists('a#social-login-github[href$="/login/service/github"]');
-        $resp = $this->followingRedirects()->get("/login/service/github");
+        $this->withHtml($resp)->assertElementExists('a#social-login-github[href$="/login/service/github"]');
+        $resp = $this->followingRedirects()->get('/login/service/github');
         $resp->assertSee('login-form');
 
-
         // Test social callback with matching social account
         DB::table('social_accounts')->insert([
-            'user_id' => $this->getAdmin()->id,
-            'driver' => 'github',
-            'driver_id' => 'logintest123'
+            'user_id'   => $this->users->admin()->id,
+            'driver'    => 'github',
+            'driver_id' => 'logintest123',
         ]);
         $resp = $this->followingRedirects()->get('/login/service/github/callback');
-        $resp->assertDontSee("login-form");
+        $resp->assertDontSee('login-form');
+        $this->assertActivityExists(ActivityType::AUTH_LOGIN, null, 'github; (' . $this->users->admin()->id . ') ' . $this->users->admin()->name);
+    }
+
+    public function test_social_account_attach()
+    {
+        config([
+            'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc',
+        ]);
+        $editor = $this->users->editor();
+
+        $mockSocialite = $this->mock(Factory::class);
+        $mockSocialDriver = Mockery::mock(Provider::class);
+        $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
+
+        $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123');
+        $mockSocialUser->shouldReceive('getAvatar')->andReturn(null);
+
+        $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
+        $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/login/service/google/callback'));
+        $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
+
+        // Test login routes
+        $resp = $this->actingAs($editor)->followingRedirects()->get('/login/service/google');
+        $resp->assertSee('Access & Security');
+
+        // Test social callback with matching social account
+        $this->assertDatabaseHas('social_accounts', [
+            'user_id'   => $editor->id,
+            'driver'    => 'google',
+            'driver_id' => 'logintest123',
+        ]);
+    }
+
+    public function test_social_account_detach()
+    {
+        $editor = $this->users->editor();
+        config([
+            'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc',
+        ]);
+
+        $socialAccount = SocialAccount::query()->forceCreate([
+            'user_id'   => $editor->id,
+            'driver'    => 'github',
+            'driver_id' => 'logintest123',
+        ]);
+
+        $resp = $this->actingAs($editor)->get('/my-account/auth');
+        $this->withHtml($resp)->assertElementContains('form[action$="/login/service/github/detach"]', 'Disconnect Account');
+
+        $resp = $this->post('/login/service/github/detach');
+        $resp->assertRedirect('/my-account/auth#social-accounts');
+        $resp = $this->followRedirects($resp);
+        $resp->assertSee('Github account was successfully disconnected from your profile.');
+
+        $this->assertDatabaseMissing('social_accounts', ['id' => $socialAccount->id]);
     }
 
     public function test_social_autoregister()
     {
         config([
             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
-            'APP_URL' => 'http://localhost'
         ]);
 
-        $user = factory(User::class)->make();
-        $mockSocialite = Mockery::mock(Factory::class);
-        $this->app[Factory::class] = $mockSocialite;
+        $user = User::factory()->make();
+        $mockSocialite = $this->mock(Factory::class);
         $mockSocialDriver = Mockery::mock(Provider::class);
         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
 
@@ -127,12 +180,11 @@ class SocialAuthTest extends TestCase
     {
         config([
             'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
-            'APP_URL' => 'http://localhost', 'services.google.auto_register' => true, 'services.google.auto_confirm' => true
+            'services.google.auto_register' => true, 'services.google.auto_confirm' => true,
         ]);
 
-        $user = factory(User::class)->make();
-        $mockSocialite = Mockery::mock(Factory::class);
-        $this->app[Factory::class] = $mockSocialite;
+        $user = User::factory()->make();
+        $mockSocialite = $this->mock(Factory::class);
         $mockSocialDriver = Mockery::mock(Provider::class);
         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
 
@@ -163,13 +215,12 @@ class SocialAuthTest extends TestCase
 
     public function test_social_registration_with_no_name_uses_email_as_name()
     {
-        $user = factory(User::class)->make(['email' => '[email protected]']);
+        $user = User::factory()->make(['email' => '[email protected]']);
 
         $this->setSettings(['registration-enabled' => 'true']);
-        config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
+        config(['GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc']);
 
-        $mockSocialite = Mockery::mock(Factory::class);
-        $this->app[Factory::class] = $mockSocialite;
+        $mockSocialite = $this->mock(Factory::class);
         $mockSocialDriver = Mockery::mock(Provider::class);
         $mockSocialUser = Mockery::mock(\Laravel\Socialite\Contracts\User::class);
 
@@ -188,5 +239,4 @@ class SocialAuthTest extends TestCase
         $user = $user->whereEmail($user->email)->first();
         $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id]);
     }
-
 }