Skip to content

Commit

Permalink
add socialite test
Browse files Browse the repository at this point in the history
  • Loading branch information
bert-w committed Feb 23, 2024
1 parent 57d2173 commit aa3e6a6
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 15 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ build
composer.lock
coverage
docs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"autoload": {
"psr-4": {
"DutchCodingCompany\\FilamentSocialite\\": "src",
"DutchCodingCompany\\FilamentSocialite\\Tests\\": "tests",
"DutchCodingCompany\\FilamentSocialite\\Database\\Factories\\": "database/factories"
}
},
Expand Down
1 change: 0 additions & 1 deletion database/migrations/create_socialite_users_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use DutchCodingCompany\FilamentSocialite\Facades\FilamentSocialite;

return new class extends Migration {
public function up()
Expand Down
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
12 changes: 0 additions & 12 deletions tests/BootTest.php

This file was deleted.

33 changes: 33 additions & 0 deletions tests/Fixtures/TestSocialiteUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DutchCodingCompany\FilamentSocialite\Tests\Fixtures;

use Laravel\Socialite\Contracts\User;

class TestSocialiteUser implements User
{
public function getId()
{
return 'test-socialite-user-id';
}

public function getNickname()
{
return 'test-socialite-user-nickname';
}

public function getName()
{
return 'test-socialite-user-name';
}

public function getEmail()
{
return '[email protected]';
}

public function getAvatar()
{
return null;
}
}
43 changes: 43 additions & 0 deletions tests/Fixtures/TestUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace DutchCodingCompany\FilamentSocialite\Tests\Fixtures;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class TestUser extends Model implements Authenticatable
{
protected $table = 'users';

protected $guarded = [];

public function getAuthIdentifierName()
{
//
}

public function getAuthIdentifier()
{
//
}

public function getAuthPassword()
{
//
}

public function getRememberToken()
{
//
}

public function setRememberToken($value)
{
//
}

public function getRememberTokenName()
{
//
}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/create_socialite_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
public function up()
{
Schema::create('socialite_users', function (Blueprint $table) {
$table->id();

$table->foreignId('user_id');
$table->string('provider');
$table->string('provider_id');

$table->timestamps();

$table->unique([
'provider',
'provider_id',
]);
});
}

public function down()
{
Schema::dropIfExists('socialite_users');
}
};
58 changes: 58 additions & 0 deletions tests/SocialiteLoginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace DutchCodingCompany\FilamentSocialite\Tests;

use DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestSocialiteUser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Laravel\Socialite\Contracts\Provider;
use Laravel\Socialite\Facades\Socialite;
use Mockery;

class SocialiteLoginTest extends TestCase
{
use RefreshDatabase;

public function testLogin(): void
{
$response = $this
->getJson("/$this->panelName/oauth/github")
->assertStatus(302);

$state = session()->get('state');

$location = $response->headers->get('location');

parse_str($location, $urlQuery);

// Test if the correct state is sent to the endpoint in the "Location" header.
$this->assertEquals($state, $urlQuery['state']);

// Assert decrypting of the state gives the correct panel name.
$this->assertEquals($this->panelName, Crypt::decrypt($state));

Socialite::shouldReceive('driver')
->with('github')
->andReturn(
Mockery::mock(Provider::class)
->shouldReceive('user')
->andReturn(new TestSocialiteUser())
->getMock()
);

// Fake oauth response.
$response = $this
->getJson("/oauth/callback/github?state=$state")
->assertStatus(302);

$this->assertDatabaseHas('socialite_users', [
'provider' => 'github',
'provider_id' => 'test-socialite-user-id',
]);

$this->assertDatabaseHas('users', [
'name' => 'test-socialite-user-name',
'email' => '[email protected]',
]);
}
}
82 changes: 81 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,112 @@

namespace DutchCodingCompany\FilamentSocialite\Tests;

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\FilamentSocialiteServiceProvider;
use DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestUser;
use Filament\Facades\Filament;
use Filament\FilamentServiceProvider;
use Filament\Pages\Dashboard;
use Filament\Panel;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Encryption\Encrypter;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Schema;
use Laravel\Socialite\SocialiteServiceProvider;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
use LazilyRefreshDatabase;
use LazilyRefreshDatabase;

protected string $panelName = 'testpanel';

protected function setUp(): void
{
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'DutchCodingCompany\\FilamentSocialite\\Database\\Factories\\'.class_basename($modelName).'Factory'
fn (string $modelName
) => 'DutchCodingCompany\\FilamentSocialite\\Database\\Factories\\'.class_basename($modelName).'Factory'
);

$this->app->make(Kernel::class)->pushMiddleware(StartSession::class);
}

protected function getPackageProviders($app)
{
$this->registerTestPanel();

return [
FilamentServiceProvider::class,
FilamentSocialiteServiceProvider::class,
SocialiteServiceProvider::class,
LivewireServiceProvider::class,
];
}

protected function registerTestPanel(): void
{
Filament::registerPanel(
fn (): Panel => Panel::make()
->default()
->id($this->panelName)
->path($this->panelName)
->login()
->pages([
Dashboard::class,
])
->plugins([
FilamentSocialitePlugin::make()
->setProviders([
'github' => [
'label' => 'GitHub',
'icon' => 'fab-github',
'color' => 'danger',
'outlined' => false,
],
'gitlab' => [
'label' => 'GitLab',
'icon' => 'fab-gitlab',
],
])
->setRegistrationEnabled(true)
->setUserModelClass(TestUser::class)
]),
);
}

public function getEnvironmentSetUp($app)
{
config()->set('mysql.driver', ':memory:');

config()->set('app.key', 'base64:'.base64_encode(
Encrypter::generateKey('AES-256-CBC')
));

config()->set('services.github', [
'client_id' => 'abcdmockedabcd',
'client_secret' => 'defgmockeddefg',
'redirect' => 'http://localhost/oauth/callback/github',
]);

config()->set('database.default', 'testing');
}

protected function defineDatabaseMigrations()
{
$this->loadLaravelMigrations();
$this->loadMigrationsFrom(__DIR__.'/Fixtures');

$this->artisan('migrate', ['--database' => 'testing'])->run();

Schema::table('users', static function (Blueprint $table): void {
$table->string('password')->nullable()->change();
});
}
}

0 comments on commit aa3e6a6

Please sign in to comment.