Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari committed Oct 7, 2024
1 parent 0a2a47f commit 5b2f16e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/Feature/Console/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function test_it_can_purge_tokens()
'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_device_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
], array_column($query, 'query'));
}

Expand All @@ -38,6 +39,7 @@ public function test_it_can_purge_revoked_tokens()
'delete from "oauth_access_tokens" where ("revoked" = 1)',
'delete from "oauth_auth_codes" where ("revoked" = 1)',
'delete from "oauth_refresh_tokens" where ("revoked" = 1)',
'delete from "oauth_device_codes" where ("revoked" = 1)',
], array_column($query, 'query'));
}

Expand All @@ -54,6 +56,7 @@ public function test_it_can_purge_expired_tokens()
'delete from "oauth_access_tokens" where ("expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_auth_codes" where ("expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_refresh_tokens" where ("expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_device_codes" where ("expires_at" < \'2000-01-01 00:00:00\')',
], array_column($query, 'query'));
}

Expand All @@ -70,6 +73,7 @@ public function test_it_can_purge_revoked_and_expired_tokens()
'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_device_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
], array_column($query, 'query'));
}

Expand All @@ -86,6 +90,7 @@ public function test_it_can_purge_tokens_by_hours()
'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
'delete from "oauth_device_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')',
], array_column($query, 'query'));
}
}
46 changes: 46 additions & 0 deletions tests/Feature/RevokedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Laravel\Passport\Bridge\AccessTokenRepository as BridgeAccessTokenRepository;
use Laravel\Passport\Bridge\AuthCode;
use Laravel\Passport\Bridge\AuthCodeRepository as BridgeAuthCodeRepository;
use Laravel\Passport\Bridge\DeviceCode;
use Laravel\Passport\Bridge\DeviceCodeRepository as BridgeDeviceCodeRepository;
use Laravel\Passport\Bridge\RefreshToken;
use Laravel\Passport\Bridge\RefreshTokenRepository as BridgeRefreshTokenRepository;
use Laravel\Passport\Tests\Feature\PassportTestCase;
Expand Down Expand Up @@ -90,6 +92,31 @@ public function test_it_can_determine_if_a_refresh_token_is_not_revoked()
$this->assertFalse($repository->isRefreshTokenRevoked('tokenId'));
}

public function test_it_can_determine_if_a_device_code_is_revoked()
{
$repository = $this->deviceCodeRepository();
$this->persistNewDeviceCode($repository, 'deviceCodeId');

$repository->revokeDeviceCode('deviceCodeId');

$this->assertTrue($repository->isDeviceCodeRevoked('deviceCodeId'));
}

public function test_a_device_code_is_also_revoked_if_it_cannot_be_found()
{
$repository = $this->deviceCodeRepository();

$this->assertTrue($repository->isDeviceCodeRevoked('notExistingDeviceCodeId'));
}

public function test_it_can_determine_if_a_device_code_is_not_revoked()
{
$repository = $this->deviceCodeRepository();
$this->persistNewDeviceCode($repository, 'deviceCodeId');

$this->assertFalse($repository->isDeviceCodeRevoked('deviceCodeId'));
}

private function accessTokenRepository(): BridgeAccessTokenRepository
{
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
Expand Down Expand Up @@ -144,4 +171,23 @@ private function persistNewRefreshToken(BridgeRefreshTokenRepository $repository

$repository->persistNewRefreshToken($refreshToken);
}

private function deviceCodeRepository(): BridgeDeviceCodeRepository
{
return new BridgeDeviceCodeRepository;
}

private function persistNewDeviceCode(BridgeDeviceCodeRepository $repository, string $id): void
{
$deviceCode = m::mock(DeviceCode::class);
$deviceCode->shouldReceive('getIdentifier')->andReturn($id);
$deviceCode->shouldReceive('getClient->getIdentifier')->andReturn('clientId');
$deviceCode->shouldReceive('getExpiryDateTime')->andReturn(CarbonImmutable::now());
$deviceCode->shouldReceive('getScopes')->andReturn([]);
$deviceCode->shouldReceive('getUserCode')->andReturn('userCode');
$deviceCode->shouldReceive('isLastPolledAtDirty')->andReturn(false);
$deviceCode->shouldReceive('isUserDirty')->andReturn(false);

$repository->persistDeviceCode($deviceCode);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/PassportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laravel\Passport\AuthCode;
use Laravel\Passport\Client;
use Laravel\Passport\DeviceCode;
use Laravel\Passport\Passport;
use Laravel\Passport\RefreshToken;
use Laravel\Passport\Token;
Expand Down Expand Up @@ -65,6 +66,13 @@ public function test_refresh_token_model_can_be_changed()

Passport::useRefreshTokenModel(RefreshToken::class);
}
public function test_device_code_instance_can_be_created()
{
$deviceCode = Passport::deviceCode();

$this->assertInstanceOf(DeviceCode::class, $deviceCode);
$this->assertInstanceOf(Passport::deviceCodeModel(), $deviceCode);
}
}

class RefreshTokenStub extends RefreshToken
Expand Down

0 comments on commit 5b2f16e

Please sign in to comment.