We need to manually check the indieauth endpoint ourselves now
This commit is contained in:
parent
b93a8587a3
commit
e456f688a3
3 changed files with 135 additions and 60 deletions
|
@ -4,32 +4,45 @@ declare(strict_types=1);
|
|||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use IndieAuth\Client;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use IndieAuth\Client as IndieAuthClient;
|
||||
use JsonException;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TokenEndpointTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
/**
|
||||
* @test
|
||||
* @throws JsonException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function tokenEndpointIssuesToken(): void
|
||||
{
|
||||
$mockClient = Mockery::mock(Client::class);
|
||||
$mockClient->shouldReceive('discoverAuthorizationEndpoint')
|
||||
$mockIndieAuthClient = Mockery::mock(IndieAuthClient::class);
|
||||
$mockIndieAuthClient->shouldReceive('discoverAuthorizationEndpoint')
|
||||
->with(normalize_url(config('app.url')))
|
||||
->once()
|
||||
->andReturn('https://indieauth.com/auth');
|
||||
$mockClient->shouldReceive('verifyIndieAuthCode')
|
||||
->andReturn([
|
||||
$mockHandler = new MockHandler([
|
||||
new \GuzzleHttp\Psr7\Response(200, [], json_encode([
|
||||
'me' => config('app.url'),
|
||||
'scope' => 'create update',
|
||||
]);
|
||||
$this->app->instance(Client::class, $mockClient);
|
||||
], JSON_THROW_ON_ERROR)),
|
||||
]);
|
||||
$handlerStack = HandlerStack::create($mockHandler);
|
||||
$mockGuzzleClient = new GuzzleClient(['handler' => $handlerStack]);
|
||||
$this->app->instance(IndieAuthClient::class, $mockIndieAuthClient);
|
||||
$this->app->instance(GuzzleClient::class, $mockGuzzleClient);
|
||||
$response = $this->post('/api/token', [
|
||||
'me' => config('app.url'),
|
||||
'code' => 'abc123',
|
||||
'redirect_uri' => config('app.url') . '/indieauth-callback',
|
||||
'client_id' => config('app.url') . '/micropub-client',
|
||||
'state' => mt_rand(1000, 10000),
|
||||
'state' => random_int(1000, 10000),
|
||||
]);
|
||||
$response->assertJson([
|
||||
'me' => config('app.url'),
|
||||
|
@ -37,51 +50,62 @@ class TokenEndpointTest extends TestCase
|
|||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
/**
|
||||
* @test
|
||||
* @throws JsonException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function tokenEndpointReturnsErrorWhenAuthEndpointLacksMeData(): void
|
||||
{
|
||||
$mockClient = Mockery::mock(Client::class);
|
||||
$mockClient->shouldReceive('discoverAuthorizationEndpoint')
|
||||
$mockIndieAuthClient = Mockery::mock(IndieAuthClient::class);
|
||||
$mockIndieAuthClient->shouldReceive('discoverAuthorizationEndpoint')
|
||||
->with(normalize_url(config('app.url')))
|
||||
->once()
|
||||
->andReturn('https://indieauth.com/auth');
|
||||
$mockClient->shouldReceive('verifyIndieAuthCode')
|
||||
->andReturn([
|
||||
$mockHandler = new MockHandler([
|
||||
new \GuzzleHttp\Psr7\Response(400, [], json_encode([
|
||||
'error' => 'error_message',
|
||||
]);
|
||||
$this->app->instance(Client::class, $mockClient);
|
||||
], JSON_THROW_ON_ERROR)),
|
||||
]);
|
||||
$handlerStack = HandlerStack::create($mockHandler);
|
||||
$mockGuzzleClient = new GuzzleClient(['handler' => $handlerStack]);
|
||||
$this->app->instance(IndieAuthClient::class, $mockIndieAuthClient);
|
||||
$this->app->instance(GuzzleClient::class, $mockGuzzleClient);
|
||||
$response = $this->post('/api/token', [
|
||||
'me' => config('app.url'),
|
||||
'code' => 'abc123',
|
||||
'redirect_uri' => config('app.url') . '/indieauth-callback',
|
||||
'client_id' => config('app.url') . '/micropub-client',
|
||||
'state' => mt_rand(1000, 10000),
|
||||
'state' => random_int(1000, 10000),
|
||||
]);
|
||||
$response->assertStatus(401);
|
||||
$response->assertJson([
|
||||
'error' => 'There was an error verifying the authorisation code.',
|
||||
'error' => 'There was an error verifying the IndieAuth code',
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
/**
|
||||
* @test
|
||||
* @throws Exception
|
||||
*/
|
||||
public function tokenEndpointReturnsErrorWhenNoAuthEndpointFound(): void
|
||||
{
|
||||
$mockClient = Mockery::mock(Client::class);
|
||||
$mockClient->shouldReceive('discoverAuthorizationEndpoint')
|
||||
$mockIndieAuthClient = Mockery::mock(IndieAuthClient::class);
|
||||
$mockIndieAuthClient->shouldReceive('discoverAuthorizationEndpoint')
|
||||
->with(normalize_url(config('app.url')))
|
||||
->once()
|
||||
->andReturn(null);
|
||||
$this->app->instance(Client::class, $mockClient);
|
||||
$this->app->instance(IndieAuthClient::class, $mockIndieAuthClient);
|
||||
$response = $this->post('/api/token', [
|
||||
'me' => config('app.url'),
|
||||
'code' => 'abc123',
|
||||
'redirect_uri' => config('app.url') . '/indieauth-callback',
|
||||
'client_id' => config('app.url') . '/micropub-client',
|
||||
'state' => mt_rand(1000, 10000),
|
||||
'state' => random_int(1000, 10000),
|
||||
]);
|
||||
$response->assertStatus(400);
|
||||
$response->assertJson([
|
||||
'error' => 'Can’t determine the authorisation endpoint.', ]
|
||||
);
|
||||
'error' => 'Could not discover the authorization endpoint for ' . config('app.url'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue