Work on webmwtion code refactoring

This commit is contained in:
Jonny Barnes 2016-08-03 16:08:30 +01:00
parent 84c7969a4e
commit a9f089098c
8 changed files with 395 additions and 248 deletions

View file

@ -2,6 +2,7 @@
namespace App\Tests;
use Cache;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
@ -131,15 +132,15 @@ class NotesTest extends TestCase
}
/**
* Test the bridgy url shim method.
* Test a correct profile link is formed from a generic URL.
*
* @return void
*/
public function testBridgy()
public function testCreatePhotoLinkWithNonCachedImage()
{
$url = 'https://brid-gy.appspot.com/comment/twitter/jonnybarnes/497778866816299008/497781260937203712';
$expected = 'https://twitter.com/_/status/497781260937203712';
$this->assertEquals($expected, $this->notesController->bridgyReply($url));
$homepage = 'https://example.org/profile.png';
$expected = 'https://example.org/profile.png';
$this->assertEquals($expected, $this->notesController->createPhotoLink($homepage));
}
/**
@ -147,10 +148,10 @@ class NotesTest extends TestCase
*
* @return void
*/
public function testCreatePhotoLinkWithGenericURL()
public function testCreatePhotoLinkWithCachedImage()
{
$homepage = 'https://example.org';
$expected = '/assets/profile-images/example.org/image';
$homepage = 'https://aaronparecki.com/profile.png';
$expected = '/assets/profile-images/aaronparecki.com/image';
$this->assertEquals($expected, $this->notesController->createPhotoLink($homepage));
}
@ -159,7 +160,7 @@ class NotesTest extends TestCase
*
* @return void
*/
public function testCreatePhotoLinkWithTwitterProfileImageURL()
public function testCreatePhotoLinkWithTwimgProfileImageURL()
{
$twitterProfileImage = 'http://pbs.twimg.com/1234';
$expected = 'https://pbs.twimg.com/1234';
@ -171,9 +172,11 @@ class NotesTest extends TestCase
*
* @return void
*/
public function testCreatePhotoLinkWithTwitterURL()
public function testCreatePhotoLinkWithCachedTwitterURL()
{
$twitterURL = 'https://twitter.com/example';
$this->assertNull($this->notesController->createPhotoLink($twitterURL));
$expected = 'https://pbs.twimg.com/static_profile_link.jpg';
Cache::put($twitterURL, $expected, 1);
$this->assertEquals($expected, $this->notesController->createPhotoLink($twitterURL));
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ProcessWebMentionTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
}
/**
* A basic test.
*
* @return void
*/
public function testExample()
{
}
}

92
tests/WebMentionsTest.php Normal file
View file

@ -0,0 +1,92 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class WebMentionsTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
}
/**
* Test webmentions without source and target are rejected.
*
* @return void
*/
public function testWebmentionsWithoutSourceAndTargetAreRejected()
{
$this->call('POST', $this->appurl . '/webmention', ['source' => 'https://example.org/post/123']);
$this->assertResponseStatus(400)
->see('You need both the target and source parameters');
}
/**
* Test invalid target gets a 400 response.
*
* @return void
*/
public function testInvalidTargetReturns400Response()
{
$this->call('POST', $this->appurl . '/webmention', [
'source' => 'https://example.org/post/123',
'target' => $this->appurl . '/invalid/target'
]);
$this->assertResponseStatus(400)
->see('Invalid request');
}
/**
* Test blog target gets a 501 response.
*
* @return void
*/
public function testBlogpostTargetReturns501Response()
{
$this->call('POST', $this->appurl . '/webmention', [
'source' => 'https://example.org/post/123',
'target' => $this->appurl . '/blog/target'
]);
$this->assertResponseStatus(501)
->see('I dont accept webmentions for blog posts yet.');
}
/**
* Test that a non-existant note gives a 400 response.
*
* @return void
*/
public function testNonexistantNoteReturns400Response()
{
$this->call('POST', $this->appurl . '/webmention', [
'source' => 'https://example.org/post/123',
'target' => $this->appurl . '/notes/ZZZZZ'
]);
$this->assertResponseStatus(400)
->see('This note doesnt exist.');
}
/**
* Test a legit webmention triggers the ProcessWebMention job.
*
* @return void
*/
public function testLegitimateWebmnetionTriggersProcessWebMentionJob()
{
$this->expectsJobs(\App\Jobs\ProcessWebMention::class);
$this->call('POST', $this->appurl . '/webmention', [
'source' => 'https://example.org/post/123',
'target' => $this->appurl . '/notes/B'
]);
$this->assertResponseStatus(202)
->see('Webmention received, it will be processed shortly');
}
}