Skip to content

Commit

Permalink
[5.x] fix: duplicate route names (#313)
Browse files Browse the repository at this point in the history
* fix: duplicate route names

* fix: inertia routes

* test route caching

* lint

* fix: tests

* Fix caching
  • Loading branch information
joelbutcher authored Nov 17, 2023
1 parent e0d3fb4 commit 819a30b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 30 deletions.
3 changes: 1 addition & 2 deletions routes/socialstream-inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

Route::group(['middleware' => config('socialstream.middleware', ['web'])], function () {
Route::get('/oauth/{provider}', [OAuthController::class, 'redirect'])->name('oauth.redirect');
Route::get('/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');
Route::post('/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');
Route::match(['get', 'post'], '/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');

Route::delete('/user/connected-account/{id}', [ConnectedAccountController::class, 'destroy'])
->middleware(['auth'])
Expand Down
3 changes: 1 addition & 2 deletions routes/socialstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@

Route::group(['middleware' => config('socialstream.middleware', ['web'])], function () {
Route::get('/oauth/{provider}', [OAuthController::class, 'redirect'])->name('oauth.redirect');
Route::get('/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');
Route::post('/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');
Route::match(['get', 'post'], '/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');
});
4 changes: 2 additions & 2 deletions src/Actions/AuthenticateOAuthCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ class_exists(FortifyFeatures::class) &&
protected function alreadyAuthenticated(Authenticatable $user, ?ConnectedAccount $account, string $provider, ProviderUser $providerAccount): RedirectResponse
{
// Get the route
$route = match(true) {
$route = match (true) {
Route::has('filament.admin.home') => route('filament.admin.home'),
Route::has('filament.home') => route('filament.home'),
$this->hasComposerPackage('laravel/breeze') => match(true) {
$this->hasComposerPackage('laravel/breeze') => match (true) {
Route::has('profile.show') => route('profile.show'),
Route::has('profile.edit') => route('profile.edit'),
Route::has('profile') => route('profile'),
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectedAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
class ConnectedAccount extends Model
{
use HasFactory;
use HasTimestamps;
use HasOAuth2Tokens;
use HasTimestamps;

/**
* The attributes that are mass assignable.
Expand Down
2 changes: 1 addition & 1 deletion src/Policies/ConnectedAccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JoelButcher\Socialstream\Policies;

use JoelButcher\Socialstream\ConnectedAccount;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use JoelButcher\Socialstream\ConnectedAccount;

class ConnectedAccountPolicy
{
Expand Down
3 changes: 1 addition & 2 deletions src/SocialstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected function bootLaravelJetstream(): void
}

Socialstream::setUserPasswordsUsing(SetUserPassword::class);
Socialstream::createUsersFromProviderUsing(match(Jetstream::hasTeamFeatures()) {
Socialstream::createUsersFromProviderUsing(match (Jetstream::hasTeamFeatures()) {
true => CreateUserWithTeamsFromProvider::class,
false => CreateUserFromProvider::class,
});
Expand All @@ -216,7 +216,6 @@ protected function bootLaravelJetstream(): void
});
}


if (! $this->app->runningInConsole()) {
return;
}
Expand Down
80 changes: 80 additions & 0 deletions tests/Feature/RouteCachingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace JoelButcher\Socialstream\Tests\Feature;

use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\GithubProvider;
use Laravel\Socialite\Two\User as SocialiteUser;
use Mockery;

use function Pest\Laravel\get;
use function Pest\Laravel\post;

uses(RefreshDatabase::class);

it('caches routes and redirects to provider', function () {
$this->defineCacheRoutes(file_get_contents(
__DIR__.'/../../workbench/routes/web.php'
));

get('/oauth/github')
->assertRedirect();
});

it('caches routes and authenticates via GET', function () {
$this->defineCacheRoutes(file_get_contents(
__DIR__.'/../../workbench/routes/web.php'
));

$user = (new SocialiteUser())
->map([
'id' => fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
'avatar' => null,
'avatar_original' => null,
])
->setToken('user-token')
->setRefreshToken('refresh-token')
->setExpiresIn(3600);

$provider = Mockery::mock(GithubProvider::class);
$provider->shouldReceive('user')->once()->andReturn($user);

Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider);

session()->put('socialstream.previous_url', route('register'));

get('oauth/github/callback')->assertRedirect(RouteServiceProvider::HOME);
});

it('caches routes and authenticates via POST', function () {
$this->defineCacheRoutes(file_get_contents(
__DIR__.'/../../workbench/routes/web.php'
));

$user = (new SocialiteUser())
->map([
'id' => fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
'avatar' => null,
'avatar_original' => null,
])
->setToken('user-token')
->setRefreshToken('refresh-token')
->setExpiresIn(3600);

$provider = Mockery::mock(GithubProvider::class);
$provider->shouldReceive('user')->once()->andReturn($user);

Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider);

session()->put('socialstream.previous_url', route('register'));

post('oauth/github/callback')->assertRedirect(RouteServiceProvider::HOME);
});
16 changes: 8 additions & 8 deletions tests/Feature/SocialstreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Route;
Expand All @@ -18,9 +17,10 @@
use Laravel\Socialite\Two\User as SocialiteUser;
use Mockery;
use Symfony\Component\HttpFoundation\RedirectResponse;

use function Pest\Laravel\get;

uses(WithFaker::class, RefreshDatabase::class);
uses(RefreshDatabase::class);

it('redirects users', function (): void {
$response = get('http://localhost/oauth/github');
Expand Down Expand Up @@ -74,7 +74,7 @@ public function generate(string $provider): RedirectResponse
test('users can register', function (): void {
$user = (new SocialiteUser())
->map([
'id' => $githubId = $this->faker->numerify('########'),
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
Expand Down Expand Up @@ -114,7 +114,7 @@ public function generate(string $provider): RedirectResponse

$user->connectedAccounts()->create([
'provider' => 'github',
'provider_id' => $githubId = $this->faker->numerify('########'),
'provider_id' => $githubId = fake()->numerify('########'),
'email' => '[email protected]',
'token' => Str::random(64),
]);
Expand Down Expand Up @@ -162,7 +162,7 @@ public function generate(string $provider): RedirectResponse

$user = (new SocialiteUser())
->map([
'id' => $githubId = $this->faker->numerify('########'),
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
Expand Down Expand Up @@ -199,7 +199,7 @@ public function generate(string $provider): RedirectResponse

$user = (new SocialiteUser())
->map([
'id' => $githubId = $this->faker->numerify('########'),
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
Expand Down Expand Up @@ -242,7 +242,7 @@ public function generate(string $provider): RedirectResponse

$user = (new SocialiteUser())
->map([
'id' => $githubId = $this->faker->numerify('########'),
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
Expand Down Expand Up @@ -278,7 +278,7 @@ public function generate(string $provider): RedirectResponse

$user = (new SocialiteUser())
->map([
'id' => $githubId = $this->faker->numerify('########'),
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'avatar' => null,
Expand Down
20 changes: 8 additions & 12 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@

namespace JoelButcher\Socialstream\Tests;

use Illuminate\Support\Facades\Config;
use JoelButcher\Socialstream\SocialstreamServiceProvider;
use Laravel\Socialite\SocialiteServiceProvider;
use Mockery;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();

Config::set('services.github', [
'client_id' => 'github-client-id',
'client_secret' => 'github-client-secret',
'redirect' => 'https://example.test/oauth/github/callback',
]);
}

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

Mockery::close();
}

Expand All @@ -43,6 +33,12 @@ protected function getEnvironmentSetUp($app): void
'database' => ':memory:',
'prefix' => '',
]);

$app['config']->set('services.github', [
'client_id' => 'github-client-id',
'client_secret' => 'github-client-secret',
'redirect' => 'https://example.test/oauth/github/callback',
]);
}

protected function migrate(): void
Expand Down
17 changes: 17 additions & 0 deletions workbench/routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Support\Facades\Route;
use JoelButcher\Socialstream\Http\Controllers\OAuthController;

Route::group(['middleware' => config('socialstream.middleware', ['web'])], function () {
Route::get('/oauth/{provider}', [OAuthController::class, 'redirect'])->name('oauth.redirect');
Route::match(['get', 'post'], '/oauth/{provider}/callback', [OAuthController::class, 'callback'])->name('oauth.callback');
});

Route::post('/login', function () {
return 'login';
})->name('login');

Route::post('/register', function () {
return 'register';
})->name('register');

0 comments on commit 819a30b

Please sign in to comment.