1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

My Account: Updated and started adding to tests

- Updated existing tests now affected by my-account changes.
- Updated some existing tests to more accuractly check the scenario.
- Updated some code styling in SocialController.
- Fixed redirects for social account flows to fit my-account.
- Added test for social account attaching.
- Added test for api token redirect handling.
This commit is contained in:
Dan Brown
2023-10-19 14:18:42 +01:00
parent 12946414b0
commit fabc854390
14 changed files with 302 additions and 227 deletions

View File

@@ -18,7 +18,7 @@ class SocialAuthTest extends TestCase
$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 = $this->mock(Factory::class);
$mockSocialDriver = Mockery::mock(Provider::class);
@@ -45,7 +45,6 @@ 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 = $this->mock(Factory::class);
@@ -86,12 +85,41 @@ class SocialAuthTest extends TestCase
$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',
'APP_URL' => 'http://localhost',
]);
$socialAccount = SocialAccount::query()->forceCreate([
@@ -100,11 +128,11 @@ class SocialAuthTest extends TestCase
'driver_id' => 'logintest123',
]);
$resp = $this->actingAs($editor)->get($editor->getEditUrl());
$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($editor->getEditUrl());
$resp->assertRedirect('/my-account/auth#social-accounts');
$resp = $this->followRedirects($resp);
$resp->assertSee('Github account was successfully disconnected from your profile.');
@@ -115,7 +143,6 @@ class SocialAuthTest extends TestCase
{
config([
'services.google.client_id' => 'abc123', 'services.google.client_secret' => '123abc',
'APP_URL' => 'http://localhost',
]);
$user = User::factory()->make();
@@ -153,7 +180,7 @@ 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 = User::factory()->make();
@@ -191,7 +218,7 @@ class SocialAuthTest extends TestCase
$user = User::factory()->make(['email' => 'nonameuser@example.com']);
$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 = $this->mock(Factory::class);
$mockSocialDriver = Mockery::mock(Provider::class);