2016-05-19 15:01:28 +01:00
|
|
|
|
<?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 MicropubClientTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
protected $appurl;
|
|
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$this->appurl = config('app.url');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the client gets shown for an unauthorised request.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testClientPageUnauthorised()
|
|
|
|
|
{
|
|
|
|
|
$this->visit($this->appurl . '/notes/new')
|
|
|
|
|
->see('IndieAuth');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testClientPageRecentAuth()
|
|
|
|
|
{
|
2016-07-13 20:10:15 +01:00
|
|
|
|
$syndication = [
|
|
|
|
|
[
|
|
|
|
|
'target' => 'https://twitter.com/jonnybarnes',
|
|
|
|
|
'name' => 'jonnybarnes on Twitter',
|
|
|
|
|
]
|
|
|
|
|
];
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$this->withSession([
|
|
|
|
|
'me' => $this->appurl,
|
2016-07-04 13:53:33 +01:00
|
|
|
|
'syndication' => $syndication,
|
2016-05-19 15:01:28 +01:00
|
|
|
|
])->visit($this->appurl . '/notes/new')
|
|
|
|
|
->see($this->appurl)
|
2016-07-04 13:53:33 +01:00
|
|
|
|
->see('https://twitter.com/jonnybarnes');
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-25 22:43:28 +01:00
|
|
|
|
public function testClientCreatesNewNoteWithTag()
|
2016-05-19 15:01:28 +01:00
|
|
|
|
{
|
2016-11-03 19:45:56 +00:00
|
|
|
|
//in this test, the syndication targets are blank
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$faker = \Faker\Factory::create();
|
2016-05-25 22:43:28 +01:00
|
|
|
|
$note = 'Fake note from #PHPUnit: ' . $faker->text;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$this->withSession([
|
|
|
|
|
'me' => $this->appurl,
|
|
|
|
|
'token' => $this->getToken()
|
|
|
|
|
])->visit($this->appurl . '/notes/new')
|
|
|
|
|
->type($note, 'content')
|
|
|
|
|
->press('Submit');
|
|
|
|
|
$this->seeInDatabase('notes', ['note' => $note]);
|
2016-05-25 22:43:28 +01:00
|
|
|
|
$this->visit($this->appurl . '/notes/tagged/PHPUnit')
|
|
|
|
|
->see('PHPUnit');
|
2016-10-01 14:26:07 +01:00
|
|
|
|
//my client has made a request to my endpoint, which then adds
|
|
|
|
|
//to the db, so database transaction don’t work
|
|
|
|
|
//so lets manually delete the new entry
|
2016-11-29 17:30:11 +00:00
|
|
|
|
//first, if we are using algolia, we need to delete it
|
|
|
|
|
if (env('SCOUT_DRIVER') == 'algolia') {
|
|
|
|
|
//we need to allow the index to update in order to query it
|
|
|
|
|
sleep(2);
|
|
|
|
|
$client = new \AlgoliaSearch\Client(env('ALGOLIA_APP_ID'), env('ALGOLIA_SECRET'));
|
|
|
|
|
$index = $client->initIndex('notes');
|
|
|
|
|
//here we query for the new note and tell algolia too delete it
|
|
|
|
|
$res = $index->deleteByQuery('Fake note from');
|
|
|
|
|
if ($res == 0) {
|
|
|
|
|
//somehow the new not didn’t get deleted
|
|
|
|
|
$this->fail('Didn’t delete the note from the index');
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-25 23:00:57 +00:00
|
|
|
|
$newNote = \App\Note::where('note', $note)->first();
|
2016-11-29 17:30:11 +00:00
|
|
|
|
$newNote->forceDelete();
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getToken()
|
|
|
|
|
{
|
|
|
|
|
$signer = new Sha256();
|
|
|
|
|
$token = (new Builder())
|
|
|
|
|
->set('client_id', 'https://quill.p3k.io')
|
|
|
|
|
->set('me', $this->appurl)
|
|
|
|
|
->set('scope', 'post')
|
|
|
|
|
->set('issued_at', time())
|
|
|
|
|
->sign($signer, env('APP_KEY'))
|
|
|
|
|
->getToken();
|
|
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
}
|