2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Tests;
|
|
|
|
|
2017-02-03 21:49:49 +00:00
|
|
|
use BrowserKitTest;
|
2016-05-19 15:01:28 +01:00
|
|
|
use Lcobucci\JWT\Builder;
|
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
2017-02-03 21:49:49 +00:00
|
|
|
class MicropubTest extends BrowserKitTest
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
protected $appurl;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->appurl = config('app.url');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubRequestWithoutToken()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post');
|
|
|
|
$this->assertResponseStatus(400);
|
2016-07-04 13:53:33 +01:00
|
|
|
$this->seeJson(['error_description' => 'No token provided with request']);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubRequestWithoutValidToken()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer abc123']);
|
|
|
|
$this->assertResponseStatus(400);
|
2016-07-04 13:53:33 +01:00
|
|
|
$this->seeJson(['error_description' => 'The provided token did not pass validation']);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubRequestWithValidToken()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
2016-07-04 13:53:33 +01:00
|
|
|
$this->seeJson(['response' => 'token']);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubRequestForSyndication()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', ['q' => 'syndicate-to'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
2016-06-29 23:53:48 +01:00
|
|
|
$this->seeJson(['uid' => 'https://twitter.com/jonnybarnes']);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubRequestForNearbyPlacesThatExist()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', ['q' => 'geo:53.5,-2.38'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
|
|
|
$this->see('the-bridgewater-pub');
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:28:31 +01:00
|
|
|
public function testMicropubRequestForNearbyPlacesThatExistWithUncertaintyParameter()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', ['q' => 'geo:53.5,-2.38;u=35'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
|
|
|
$this->see('the-bridgewater-pub');
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
public function testMicropubRequestForNearbyPlacesThatDoNotExist()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', ['q' => 'geo:1.23,4.56'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
|
|
|
$this->see('[]');
|
|
|
|
}
|
|
|
|
|
2016-07-04 15:35:17 +01:00
|
|
|
public function testMicropubRequestForConfig()
|
|
|
|
{
|
|
|
|
$this->call('GET', $this->appurl . '/api/post', ['q' => 'config'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
|
|
|
$this->seeJson(['uid' => 'https://twitter.com/jonnybarnes']);
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
public function testMicropubRequestCreateNewNote()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$note = $faker->text;
|
|
|
|
$this->call(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'h' => 'entry',
|
|
|
|
'content' => $note
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
);
|
|
|
|
$this->seeInDatabase('notes', ['note' => $note]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubRequestCreateNewPlace()
|
|
|
|
{
|
|
|
|
$this->call(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'h' => 'card',
|
|
|
|
'name' => 'The Barton Arms',
|
|
|
|
'geo' => 'geo:53.4974,-2.3768'
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
);
|
|
|
|
$this->seeInDatabase('places', ['slug' => 'the-barton-arms']);
|
|
|
|
}
|
|
|
|
|
2016-07-11 17:32:58 +01:00
|
|
|
public function testMicropubJSONRequestCreateNewNote()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$note = $faker->text;
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-entry'],
|
|
|
|
'properties' => [
|
|
|
|
'content' => [$note],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'created'
|
2016-09-23 16:55:25 +01:00
|
|
|
])->assertResponseStatus(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubJSONRequestCreateNewNoteWithoutToken()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$note = $faker->text;
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-entry'],
|
|
|
|
'properties' => [
|
|
|
|
'content' => [$note],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'error',
|
|
|
|
'error' => 'no_token'
|
|
|
|
])->assertResponseStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubJSONRequestCreateNewNoteWithInvalidToken()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$note = $faker->text;
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-entry'],
|
|
|
|
'properties' => [
|
|
|
|
'content' => [$note],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getInvalidToken()]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'error',
|
|
|
|
'error' => 'invalid_token'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubJSONRequestCreateNewPlace()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-card'],
|
|
|
|
'properties' => [
|
|
|
|
'name' => $faker->name,
|
|
|
|
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude
|
|
|
|
],
|
|
|
|
],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'created'
|
|
|
|
])->assertResponseStatus(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubJSONRequestCreateNewPlaceWithoutToken()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-entry'],
|
|
|
|
'properties' => [
|
|
|
|
'name' => $faker->name,
|
|
|
|
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude
|
|
|
|
],
|
|
|
|
]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'error',
|
|
|
|
'error' => 'no_token'
|
|
|
|
])->assertResponseStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMicropubJSONRequestCreateNewPlaceWithInvalidToken()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-entry'],
|
|
|
|
'properties' => [
|
|
|
|
'name' => $faker->name,
|
|
|
|
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude
|
|
|
|
],
|
|
|
|
],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getInvalidToken()]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'error',
|
|
|
|
'error' => 'invalid_token'
|
2016-07-11 17:32:58 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-10-01 14:26:07 +01:00
|
|
|
public function testMicropubJSONRequestCreateNewPlaceWithUncertaintyParam()
|
|
|
|
{
|
|
|
|
$faker = \Faker\Factory::create();
|
|
|
|
$this->json(
|
|
|
|
'POST',
|
|
|
|
$this->appurl . '/api/post',
|
|
|
|
[
|
|
|
|
'type' => ['h-card'],
|
|
|
|
'properties' => [
|
|
|
|
'name' => $faker->name,
|
|
|
|
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude . ';u=35'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
)->seeJson([
|
|
|
|
'response' => 'created'
|
|
|
|
])->assertResponseStatus(201);
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
private function getToken()
|
|
|
|
{
|
|
|
|
$signer = new Sha256();
|
|
|
|
$token = (new Builder())
|
|
|
|
->set('client_id', 'https://quill.p3k.io')
|
2016-06-23 14:27:00 +01:00
|
|
|
->set('me', 'https://jonnybarnes.localhost')
|
2016-05-19 15:01:28 +01:00
|
|
|
->set('scope', 'post')
|
|
|
|
->set('issued_at', time())
|
|
|
|
->sign($signer, env('APP_KEY'))
|
|
|
|
->getToken();
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
2016-09-23 16:55:25 +01:00
|
|
|
|
|
|
|
private function getInvalidToken()
|
|
|
|
{
|
|
|
|
$signer = new Sha256();
|
|
|
|
$token = (new Builder())
|
|
|
|
->set('client_id', 'https://quill.p3k.io')
|
|
|
|
->set('me', 'https://jonnybarnes.localhost')
|
|
|
|
->set('scope', 'view')
|
|
|
|
->set('issued_at', time())
|
|
|
|
->sign($signer, env('APP_KEY'))
|
|
|
|
->getToken();
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|