Socialite callback test

This commit is contained in:
Čarodej
2021-12-31 12:23:44 +01:00
parent 22ae148a22
commit 22733619d1
8 changed files with 1355 additions and 961 deletions

View File

@@ -1,6 +1,13 @@
<?php
namespace Tests\Feature\Accounts;
use DB;
use Domain\Settings\Models\Setting;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Http;
use Laravel\Socialite\Two\FacebookProvider;
use Mockery;
use Storage;
use Notification;
use Tests\TestCase;
@@ -9,6 +16,7 @@ use Domain\Folders\Models\Folder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\URL;
use Illuminate\Auth\Notifications\VerifyEmail;
use Laravel\Socialite\Contracts\Factory as Socialite;
class UserAccountTest extends TestCase
{
@@ -32,21 +40,86 @@ class UserAccountTest extends TestCase
Storage::disk('local')
->assertExists('files/' . User::first()->id);
}
/**
* @test
*/
public function it_socialite_redirect()
{
$response = $this->call('GET', 'api/socialite/google/redirect');
$response = $this->get('api/socialite/google/redirect');
$this->assertStringContainsString('accounts.google.com/o/oauth2/auth', $response['url']);
}
/**
* @test
*/
/**
* @test
*/
public function it_socialite_callback()
{
{
// Set default settings
DB::table('settings')->insert([
[
'name' => 'registration',
'value' => 1,
], [
'name' => 'storage_default',
'value' => 5,
]
]);
// Create fake image
$fakeImage = UploadedFile::fake()
->image('fake-avatar.jpg');
Http::fake([
'https://vuefilemanager.com/avatar.jpg' => Http::response($fakeImage->getContent()),
]);
// Create fake user
$socialiteUser = $this->createMock(\Laravel\Socialite\Two\User::class);
$socialiteUser->token = 'fake_token';
$socialiteUser->id = 'fake_id';
$socialiteUser->name = 'Jane Doe';
$socialiteUser->email = 'howdy@hi5ve.digital';
$socialiteUser->avatar = 'https://vuefilemanager.com/avatar.jpg';
// Mock user with FB provider
$provider = $this->createMock(FacebookProvider::class);
$provider->expects($this->any())
->method('user')
->willReturn($socialiteUser);
// Mock socialite
$stub = $this->createMock(Socialite::class);
$stub->expects($this->any())
->method('driver')
->willReturn($provider);
// Replace Socialite Instance with mock
$this->app->instance(Socialite::class, $stub);
$this->getJson('/api/socialite/facebook/callback');
$this
->assertDatabaseHas('users', [
'email' => 'howdy@hi5ve.digital',
'oauth_provider' => 'facebook',
'password' => null,
])
->assertDatabaseHas('user_settings', [
'name' => 'Jane Doe',
]);
$user = User::first();
collect(config('vuefilemanager.avatar_sizes'))
->each(
fn($size) => Storage::disk('local')
->assertExists("avatars/{$size['name']}-{$user->settings->getRawOriginal('avatar')}")
);
}
/**
* todo: finish test
*/
@@ -131,8 +204,7 @@ class UserAccountTest extends TestCase
collect(config('vuefilemanager.avatar_sizes'))
->each(
fn ($size) =>
Storage::disk('local')
fn($size) => Storage::disk('local')
->assertExists("avatars/{$size['name']}-{$user->settings->getRawOriginal('avatar')}")
);
}
@@ -151,7 +223,7 @@ class UserAccountTest extends TestCase
->assertStatus(200)
->assertExactJson([
'data' => [
'id' => (string) $user->id,
'id' => (string)$user->id,
'type' => 'user',
'attributes' => [
'storage_capacity' => '5',
@@ -169,14 +241,14 @@ class UserAccountTest extends TestCase
'capacity' => '5',
'capacity_formatted' => '5GB',
],
'created_at_formatted' => format_date($user->created_at, '%d. %B. %Y'),
'created_at' => $user->created_at->toJson(),
'updated_at' => $user->updated_at->toJson(),
'created_at_formatted' => format_date($user->created_at, '%d. %B. %Y'),
'created_at' => $user->created_at->toJson(),
'updated_at' => $user->updated_at->toJson(),
],
'relationships' => [
'settings' => [
'data' => [
'id' => (string) $user->id,
'id' => (string)$user->id,
'type' => 'settings',
'attributes' => [
'avatar' => $user->settings->avatar,