jonnybarnes.uk/tests/MicropubTest.php
Jonny Barnes 2c6b9df97a Squashed commit of the following:
commit e7f1c4c84579b959fe2997ff4d2315ee53acfe9a
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu Sep 29 11:22:44 2016 +0100

    Add latest change, fix spelling errors in rest of changelog

commit 637b5107b557f1c2a56327a40b3d7b4b7297fa29
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 21:13:06 2016 +0100

    Add a test for uncertainty parameter

commit 5e7504b323debf9c91e0cca428b4dca7dda0789d
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 21:11:12 2016 +0100

    Format the ternaty operator correctly

commit 19c978a5ac59cd7dfdceee9a8f1aaa6539d8ac66
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 20:46:19 2016 +0100

    Fix typo

commit d4e5b5fc384d0ccd715ea28a51821f958f6c2caa
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 20:41:23 2016 +0100

    Add paraphanalia

commit a96f104894de6c06dc5e41044482de2355cb4965
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 20:35:17 2016 +0100

    Add the geolocation uncertainty to nearby places request

commit 0f6d5649e2f3316040d7453f5fcc96ac1f0ac783
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 20:34:38 2016 +0100

    Use a query parameter for geolocation uncertainty

commit a9147a074315fabb0d76e5991b34a70b2143761c
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 19:58:08 2016 +0100

    Make use of uncertainty parameter when finding nearby places

commit bad384b5d8be3ea8905d8f6ca3c20f448869853d
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Sep 28 19:28:42 2016 +0100

    Support sending uncertainty value to micropub endpoint
2016-09-29 11:28:31 +01:00

252 lines
7.8 KiB
PHP

<?php
namespace App\Tests;
use TestCase;
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;
class MicropubTest extends TestCase
{
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);
$this->seeJson(['error_description' => 'No token provided with request']);
}
public function testMicropubRequestWithoutValidToken()
{
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer abc123']);
$this->assertResponseStatus(400);
$this->seeJson(['error_description' => 'The provided token did not pass validation']);
}
public function testMicropubRequestWithValidToken()
{
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->seeJson(['response' => 'token']);
}
public function testMicropubRequestForSyndication()
{
$this->call('GET', $this->appurl . '/api/post', ['q' => 'syndicate-to'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->seeJson(['uid' => 'https://twitter.com/jonnybarnes']);
}
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');
}
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');
}
public function testMicropubRequestForNearbyPlacesThatDoNotExist()
{
$this->call('GET', $this->appurl . '/api/post', ['q' => 'geo:1.23,4.56'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->see('[]');
}
public function testMicropubRequestForConfig()
{
$this->call('GET', $this->appurl . '/api/post', ['q' => 'config'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->seeJson(['uid' => 'https://twitter.com/jonnybarnes']);
}
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']);
}
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'
])->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'
]);
}
private function getToken()
{
$signer = new Sha256();
$token = (new Builder())
->set('client_id', 'https://quill.p3k.io')
->set('me', 'https://jonnybarnes.localhost')
->set('scope', 'post')
->set('issued_at', time())
->sign($signer, env('APP_KEY'))
->getToken();
return $token;
}
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;
}
}