jonnybarnes.uk/tests/Feature/MicropubControllerTest.php
Jonny Barnes 05d42467cf Micropub Update and “Swarm” Support
Much better micropub client/endpoint and the ability
to support ownyourswarm.

Squashed commit of the following:

commit b5be117b852b7a598da72325a4eaf831526c700a
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 18 15:02:00 2017 +0100

    Add a token endpoint test

commit 71b7ff68d088180770ca8c2fb43e33bf4b385a96
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 18 13:49:00 2017 +0100

    Fix phpcs issue

commit 54d96d32f280127061254e7bc6dfe196e2e35a39
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 18 13:47:08 2017 +0100

    Move token code into its own controller

commit 3ed2b4d36d57a9b3bf2a532eb262ec71fc9aa046
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 18 13:01:09 2017 +0100

    Improve/fix code for issuing a token

commit 8c411a1df1d59f12dd1c1a4ac884f53dbd1b9351
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 18 12:59:33 2017 +0100

    Remove sprurious comment

commit 1b8a3b6502a2b982f737fb4b58602995451e38b9
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Mon May 15 11:57:03 2017 +0100

    Wrap helper functions in check for existence

commit 75c706b5a6c1fca7bf45038409689416a3b5ba4d
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Mon May 15 11:40:17 2017 +0100

    Fix a test error, array_key_exists must be run on an array

commit 685e96501b8dc906c6945fca39721fb79fa34a8b
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Mon May 15 11:11:51 2017 +0100

    Remove errant dd()

commit 02536ebaa6daec2a78bc79c44392ac5a82c3200e
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun May 14 21:37:17 2017 +0100

    Test a typical ownyourswarm request

commit 31d959e35ccec9dac5986f93732d928c08e246c8
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun May 14 21:36:37 2017 +0100

    Better tagging of notes

commit e3d4ff8b050ba41febed4d3ab0d30898f0056b33
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun May 14 21:35:35 2017 +0100

    When a note has a “checkin” try and create the place if it doesn’t already exist

commit 4fc0dd0121ca09915a13f3c21c97611db1d744a6
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri May 12 12:26:25 2017 +0100

    better handling of exceptions, `return` the exctacted response method

commit 2e3ca3297d2f494eb88af732519bd7ea8bcc3611
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri May 12 11:22:52 2017 +0100

    compser update and minor style tweak

commit b883d03cc349798230986a5cb50e23e370ce5a09
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 11 19:03:52 2017 +0100

    Improve artisan tinker; see https://blog.tighten.co/supercharge-your-laravel-tinker-workflow

commit 8de63172fc7d367870624ff25d1ada92af2d61a7
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 11 17:28:16 2017 +0100

    Tweak code, make it slightly more readable by catching custom exception, and moving a repeated response into its own function

commit 8ff0a1196d254d8788477d26b548f2ecff0a7986
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 11 17:21:39 2017 +0100

    Add a custom exception to catch

commit 3a568da65ef22b1b676ea8378cd51ce88750b6af
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Thu May 11 17:21:11 2017 +0100

    composer update && yarn upgrade

commit b70242e616827eab6a2132f3e691ec91be689fb5
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed May 10 20:00:03 2017 +0100

    modify some returns for style purposes

commit b65170ba1515cbb07beb66fcb3358d69d86cf3a2
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed May 10 19:39:46 2017 +0100

    composer update

commit cb6198db03a8e8c5c365e88d565401dd6420be13
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed May 10 17:07:33 2017 +0100

    composer update

commit 91cdd9e17ba192b833da76e0243829cd037170f3
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Apr 28 21:41:12 2017 +0100

    verify input array contains necessary data

commit 6b230278bfcf2707b7ea1af8e15acd0d7cd27623
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Apr 28 21:40:30 2017 +0100

    pass the right input content to the data array

commit 96f30d25810751328f75964e81899496add7292e
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Apr 28 21:19:28 2017 +0100

    Add support for the published property

commit 8168a14969711ff5f84d29eca73036980f9b5a6b
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Apr 28 18:48:23 2017 +0100

    Fix some bugs in the `normalize_url()` function and add some tests for the helpers functions

commit 34adcebefa7cafec8d26d438b0046120751780be
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Apr 28 15:12:08 2017 +0100

    Fix PSR-2 issues, exluding group use declarations

commit 4b6651c318d534db1fcb83e1b66562c6dd560165
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Apr 28 12:36:54 2017 +0100

    composer update

commit a0788ffb6bd4d24245986bf83fe349b8e39786cd
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Apr 26 12:44:06 2017 +0100

    Make the retreived a Note a model instance instead of collection, update the content of JSON resposes for updating a note

commit f444cfd570a8316a8bb961b901ca2beb3ba74cd2
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Apr 26 12:43:23 2017 +0100

    Add tests for updating a post

commit ada7e513263b1b0519600538a8a2cb757c74d520
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Apr 26 12:42:50 2017 +0100

    Add swarm_url column

commit f4e1dba1b315b3d923049f9f5c7a47a730267cb7
Merge: 7208ec5 400857a
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:33:28 2017 +0100

    Merge pull request #50 from jonnybarnes/analysis-XNDLxp

    Apply fixes from StyleCI

commit 400857af57f873bf63b452fdf65ed2632eef9311
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 14:33:06 2017 +0000

    Apply fixes from StyleCI

commit 7208ec53ff51f0c5d002c222c465767c46c275b2
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:30:02 2017 +0100

    Correct some tests, for example the error returned for the specific mistake in `getInvalidToken()` has changed

commit 60550667156d7306cf750768b89fa329742c3927
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:29:06 2017 +0100

    Fix some typos

commit 0324cb010e77606e0f99e8bb68376b79995abffc
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:20:27 2017 +0100

    Allow notes to be updated by a micropub client endpoint

commit b3b3170b359548d21ddae9a9572c182d2a7d51be
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:18:48 2017 +0100

    When a note with a photo is being created, adjust for if URL is on my own endpoint, or somehwere else

commit a2437879b000728a2e7d2b91fa642f7cdfd1e698
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:17:25 2017 +0100

    Modify method that returns URL to check if path is already a fully qualified URL

commit 64eb53e0f87cb5ee55013de5ed8e2487eee36f0c
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Apr 25 15:16:12 2017 +0100

    Add a method to find notes based on NewBase60 id
2017-05-18 15:15:53 +01:00

357 lines
11 KiB
PHP

<?php
namespace Tests\Feature;
use Tests\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 MicropubControllerTest extends TestCase
{
use DatabaseTransactions;
/**
* Test a GET request for the micropub endpoint without a token gives a
* 400 response. Also check the error message.
*
* @return void
*/
public function test_micropub_request_without_token_returns_401_response()
{
$response = $this->get('/api/post');
$response->assertStatus(401);
$response->assertJsonFragment(['error_description' => 'No access token was provided in the request']);
}
/**
* Test a GET request for the micropub endpoint without a valid token gives
* a 400 response. Also check the error message.
*
* @return void
*/
public function test_micropub_request_without_valid_token_returns_400_response()
{
$response = $this->call('GET', '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer abc123']);
$response->assertStatus(400);
$response->assertJsonFragment(['error_description' => 'The provided token did not pass validation']);
}
/**
* Test a GET request for the micropub endpoint with a valid token gives a
* 200 response. Check token information is returned in the response.
*
* @return void
*/
public function test_micropub_request_with_valid_token_returns_200_response()
{
$response = $this->call('GET', '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertStatus(200);
$response->assertJsonFragment(['response' => 'token']);
}
/**
* Test a GET request for syndication targets.
*
* @return void
*/
public function test_micropub_request_for_syndication_targets()
{
$response = $this->call('GET', '/api/post', ['q' => 'syndicate-to'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']);
}
/**
* Test a request for places.
*
* @return void
*/
public function test_micropub_request_for_nearby_places()
{
$response = $this->call('GET', '/api/post', ['q' => 'geo:53.5,-2.38'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => [['slug' =>'the-bridgewater-pub']]]);
}
/**
* Test a request for places, this time with an uncertainty parameter.
*
* @return void
*/
public function test_micropub_request_for_nearby_places_with_uncertainty_parameter()
{
$response = $this->call('GET', '/api/post', ['q' => 'geo:53.5,-2.38;u=35'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => [['slug' => 'the-bridgewater-pub']]]);
}
/**
* Test a request for places, where there will be an “empty” response.
*
* @return void
*/
public function test_micropub_request_for_nearby_places_where_non_exist()
{
$response = $this->call('GET', '/api/post', ['q' => 'geo:1.23,4.56'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJson(['places' => []]);
}
/**
* Test a request for the micropub config.
*
* @return void
*/
public function test_micropub_request_for_config()
{
$response = $this->call('GET', '/api/post', ['q' => 'config'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']);
}
/**
* Test a valid micropub requests creates a new note.
*
* @return void
*/
public function test_micropub_request_creates_new_note()
{
$faker = \Faker\Factory::create();
$note = $faker->text;
$response = $this->call(
'POST',
'/api/post',
[
'h' => 'entry',
'content' => $note
],
[],
[],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertJson(['response' => 'created']);
$this->assertDatabaseHas('notes', ['note' => $note]);
}
/**
* Test a valid micropub requests creates a new place.
*
* @return void
*/
public function test_micropub_request_creates_new_place()
{
$response = $this->call(
'POST',
'/api/post',
[
'h' => 'card',
'name' => 'The Barton Arms',
'geo' => 'geo:53.4974,-2.3768'
],
[],
[],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertJson(['response' => 'created']);
$this->assertDatabaseHas('places', ['slug' => 'the-barton-arms']);
}
/**
* Test a valid micropub requests using JSON syntax creates a new note.
*
* @return void
*/
public function test_micropub_request_with_json_syntax_creates_new_note()
{
$faker = \Faker\Factory::create();
$note = $faker->text;
$response = $this->json(
'POST',
'/api/post',
[
'type' => ['h-entry'],
'properties' => [
'content' => [$note],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response
->assertStatus(201)
->assertJson(['response' => 'created']);
}
/**
* Test a micropub requests using JSON syntax without a token returns an
* error. Also check the message.
*
* @return void
*/
public function test_micropub_request_with_json_syntax_without_token_returns_error()
{
$faker = \Faker\Factory::create();
$note = $faker->text;
$response = $this->json(
'POST',
'/api/post',
[
'type' => ['h-entry'],
'properties' => [
'content' => [$note],
],
]
);
$response
->assertJson([
'response' => 'error',
'error' => 'unauthorized'
])
->assertStatus(401);
}
/**
* Test a micropub requests using JSON syntax without a valis token returns
* an error. Also check the message.
*
* @return void
*/
public function test_micropub_request_with_json_syntax_with_invalid_token_returns_error()
{
$faker = \Faker\Factory::create();
$note = $faker->text;
$response = $this->json(
'POST',
'/api/post',
[
'type' => ['h-entry'],
'properties' => [
'content' => [$note],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getInvalidToken()]
);
$response
->assertJson([
'response' => 'error',
'error' => 'insufficient_scope'
])
->assertStatus(401);
}
public function test_micropub_request_with_json_syntax_creates_new_place()
{
$faker = \Faker\Factory::create();
$response = $this->json(
'POST',
'/api/post',
[
'type' => ['h-card'],
'properties' => [
'name' => $faker->name,
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response
->assertJson(['response' => 'created'])
->assertStatus(201);
}
public function test_micropub_request_with_json_syntax_and_uncertainty_parameter_creates_new_place()
{
$faker = \Faker\Factory::create();
$response = $this->json(
'POST',
'/api/post',
[
'type' => ['h-card'],
'properties' => [
'name' => $faker->name,
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude . ';u=35'
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response
->assertJson(['response' => 'created'])
->assertStatus(201);
}
public function test_micropub_request_with_json_syntax_update_replace_post()
{
$response = $this->json(
'POST',
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/A',
'replace' => [
'content' => ['replaced content'],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response
->assertJson(['response' => 'updated'])
->assertStatus(200);
}
public function test_micropub_request_with_json_syntax_update_add_post()
{
$response = $this->json(
'POST',
'/api/post',
[
'action' => 'update',
'url' => config('app.url') . '/notes/A',
'add' => [
'syndication' => ['https://www.swarmapp.com/checkin/123'],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response
->assertJson(['response' => 'updated'])
->assertStatus(200);
$this->assertDatabaseHas('notes', [
'swarm_url' => 'https://www.swarmapp.com/checkin/123'
]);
}
/**
* Generate a valid token to be used in the tests.
*
* @return Lcobucci\JWT\Token\Plain $token
*/
private function getToken()
{
$signer = new Sha256();
$token = (new Builder())
->set('client_id', 'https://quill.p3k.io')
->set('me', 'https://jonnybarnes.localhost')
->set('scope', 'create update')
->set('issued_at', time())
->sign($signer, env('APP_KEY'))
->getToken();
return $token;
}
/**
* Generate an invalid token to be used in the tests.
*
* @return Lcobucci\JWT\Token\Plain $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') //error here
->set('issued_at', time())
->sign($signer, env('APP_KEY'))
->getToken();
return $token;
}
}