Initial commit to new repo

This commit is contained in:
Jonny Barnes 2016-05-19 15:01:28 +01:00
parent a267f9bfcc
commit a5173c981b
292 changed files with 17472 additions and 0 deletions

78
tests/ArticlesTest.php Normal file
View file

@ -0,0 +1,78 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ArticlesTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
}
/**
* Test the `/blog` page returns the article, this
* means the database is being hit.
*
* @return void
*/
public function testArticlesPage()
{
$this->visit($this->appurl . '/blog')
->see('My New Blog');
}
/**
* Test the `/blog/{year}` page returns the article, this
* means the database is being hit.
*
* @return void
*/
public function testArticlesYearPage()
{
$this->visit($this->appurl . '/blog/2016')
->see('My New Blog');
}
/**
* Test the `/blog/{year}/{month}` page returns the article,
* this means the database is being hit.
*
* @return void
*/
public function testArticlesMonthPage()
{
$this->visit($this->appurl . '/blog/2016/01')
->see('My New Blog');
}
/**
* Test a single article page.
*
* @return void
*/
public function testSingleArticlePage()
{
$this->visit($this->appurl . '/blog/2016/01/my-new-blog')
->see('My New Blog');
}
/**
* Test the RSS feed.
*
* @return void
*/
public function testRSSFeed()
{
$response = $this->call('GET', $this->appurl . '/feed');
$this->assertEquals('application/rss+xml', $response->headers->get('Content-Type'));
}
}

52
tests/ContactsTest.php Normal file
View file

@ -0,0 +1,52 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ContactsTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
}
/**
* Test the `/contacts` page and see if response is OK.
*
* @return void
*/
public function testContactsPage()
{
$this->visit($this->appurl . '/contacts')
->assertResponseOK();
}
/**
* Test an individual contact page with default profile image.
*
* @return void
*/
public function testContactPageWithDefaultPic()
{
$this->visit($this->appurl . '/contacts/tantek')
->see('<img src="/assets/profile-images/default-image" alt="" class="u-photo">');
}
/**
* Test an individual contact page with a specific profile image.
*
* @return void
*/
public function testContactPageWithSpecificPic()
{
$this->visit($this->appurl . '/contacts/aaron')
->see('<img src="/assets/profile-images/aaronparecki.com/image" alt="" class="u-photo">');
}
}

19
tests/ExampleTest.php Normal file
View file

@ -0,0 +1,19 @@
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit(config('app.url') . '/')
->see('Jonny Barnes');
}
}

79
tests/IndieAuthTest.php Normal file
View file

@ -0,0 +1,79 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class IndieAuthTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
}
/**
* Test the getAuthorizationEndpoint calls the correct service methods,
* though these methods are actually mocked.
*
* @return void
*/
public function testIndieAuthServiceDiscoversEndpoint()
{
$service = new \App\Services\IndieAuthService();
$client = new \IndieAuth\Client();
$result = $service->getAuthorizationEndpoint($this->appurl, $client);
$this->assertSame('https://indieauth.com/auth', $result);
}
/**
* Test that the Service build the correct redirect URL.
*
* @return void
*/
public function testIndieAuthServiceBuildRedirectURL()
{
$client = new \IndieAuth\Client();
$service = new \App\Services\IndieAuthService();
$result = $service->buildAuthorizationURL(
'https://indieauth.com/auth',
$this->appurl,
$client
);
$this->assertSame(
'https://indieauth.com/auth?me=',
substr($result, 0, 30)
);
}
/**
* Test the `beginauth` method redirects to the client on error.
*
* @return void
*/
public function testIndieAuthControllerBeginAuthRedirectsToClientOnFail()
{
$response = $this->call('GET', $this->appurl . '/beginauth', ['me' => 'http://example.org']);
$this->assertSame($this->appurl . '/notes/new', $response->headers->get('Location'));
}
/**
* Now we test the `beginauth` method as a whole.
*
* @return void
*/
public function testIndieAuthControllerBeginAuthRedirectsToEndpoint()
{
$response = $this->call('GET', $this->appurl . '/beginauth', ['me' => $this->appurl]);
$this->assertSame(
'https://indieauth.com/auth?me=',
substr($response->headers->get('Location'), 0, 30)
);
$response = null;
}
}

View file

@ -0,0 +1,73 @@
<?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()
{
$this->withSession([
'me' => $this->appurl,
'syndication' => 'mp-syndicate-to=twitter.com%2Fjbl5',
])->visit($this->appurl . '/notes/new')
->see($this->appurl)
->see('twitter.com/jbl5');
}
/**
* This currently creates a new note that stays in the database.
*/
public function testClientCreatesNewNote()
{
$faker = \Faker\Factory::create();
$note = 'Fake note from PHPUnit: ' . $faker->text;
$this->withSession([
'me' => $this->appurl,
'token' => $this->getToken()
])->visit($this->appurl . '/notes/new')
->type($note, 'content')
->press('Submit');
$this->seeInDatabase('notes', ['note' => $note]);
}
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;
}
}

112
tests/MicropubTest.php Normal file
View file

@ -0,0 +1,112 @@
<?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->see('No OAuth token sent with request.');
}
public function testMicropubRequestWithoutValidToken()
{
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer abc123']);
$this->assertResponseStatus(400);
$this->see('Invalid token');
}
public function testMicropubRequestWithValidToken()
{
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->see('me=https%3A%2F%2Fjbl5.dev');
}
public function testMicropubRequestForSyndication()
{
$this->call('GET', $this->appurl . '/api/post', ['q' => 'syndicate-to'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->see('twitter.com%2Fjonnybarnes');
}
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 testMicropubRequestForNearbyPlacesThatDoNotExist()
{
$this->call('GET', $this->appurl . '/api/post', ['q' => 'geo:1.23,4.56'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->see('[]');
}
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()
{
$faker = \Faker\Factory::create();
$note = $faker->text;
$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']);
}
private function getToken()
{
$signer = new Sha256();
$token = (new Builder())
->set('client_id', 'https://quill.p3k.io')
->set('me', 'https://jbl5.dev')
->set('scope', 'post')
->set('issued_at', time())
->sign($signer, env('APP_KEY'))
->getToken();
return $token;
}
}

179
tests/NotesTest.php Normal file
View file

@ -0,0 +1,179 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotesTest extends TestCase
{
protected $appurl;
protected $notesController;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
$this->notesController = new \App\Http\Controllers\NotesController();
}
/**
* Test the `/notes` page returns 200, this should
* mean the database is being hit.
*
* @return void
*/
public function testNotesPage()
{
$this->visit($this->appurl . '/notes')
->assertResponseOk();
}
/**
* Test a specific note so that `singleNote()` get called.
*
* @return void
*/
public function testSpecificNote()
{
$this->visit($this->appurl . '/notes/B')
->see('#beer');
}
/**
* Test that `/note/{decID}` redirects to `/notes/{nb60id}`.
*
* @return void
*/
public function testDecIDRedirect()
{
$this->get($this->appurl . '/note/11')
->assertRedirectedTo(config('app.url') . '/notes/B');
}
/**
* Visit the tagged page and see text from the note.
*
* @return void
*/
public function testTaggedNotesPage()
{
$this->visit($this->appurl . '/notes/tagged/beer')
->see('at the local.');
}
/**
* Look for a default image in the contacts h-card.
*
* @return void
*/
public function testDefaultImageUsed()
{
$this->visit($this->appurl . '/notes/C')
->see('<img class="u-photo photo logo" alt="" src="/assets/profile-images/default-image">');
}
/**
* Look for a specific profile image in the contacts h-card.
*
* @return void
*/
public function testProfileImageUsed()
{
$this->visit($this->appurl . '/notes/D')
->see('<img class="u-photo photo logo" alt="" src="/assets/profile-images/aaronparecki.com/image">');
}
/**
* Look for twitter URL when theres no associated contact.
*
* @return void
*/
public function testTwitterLinkCreatedWhenNoContactFound()
{
$this->visit($this->appurl . '/notes/E')
->see('<a href="https://twitter.com/bob">@bob</a>');
}
/**
* Test hashtag linking.
*
* @return void
*/
public function testHashtags()
{
$this->visit($this->appurl . '/notes/B')
->see('<a rel="tag" class="p-category" href="/notes/tagged/beer">#beer</a>');
}
/**
* Look for the client name after the note.
*
* @return void
*/
public function testClientNameDisplayed()
{
$this->visit($this->appurl . '/notes/D')
->see('JBL5');
}
/**
* Look for the client URL after the note.
*
* @return void
*/
public function testClientURLDisplayed()
{
$this->visit($this->appurl . '/notes/E')
->see('quill.p3k.io');
}
/**
* Test the bridgy url shim method.
*
* @return void
*/
public function testBridgy()
{
$url = 'https://brid-gy.appspot.com/comment/twitter/jonnybarnes/497778866816299008/497781260937203712';
$expected = 'https://twitter.com/_/status/497781260937203712';
$this->assertEquals($expected, $this->notesController->bridgyReply($url));
}
/**
* Test a correct profile link is formed from a generic URL.
*
* @return void
*/
public function testCreatePhotoLinkWithGenericURL()
{
$homepage = 'https://example.org';
$expected = '/assets/profile-images/example.org/image';
$this->assertEquals($expected, $this->notesController->createPhotoLink($homepage));
}
/**
* Test a correct profile link is formed from a twitter URL.
*
* @return void
*/
public function testCreatePhotoLinkWithTwitterProfileImageURL()
{
$twitterProfileImage = 'http://pbs.twimg.com/1234';
$expected = 'https://pbs.twimg.com/1234';
$this->assertEquals($expected, $this->notesController->createPhotoLink($twitterProfileImage));
}
/**
* Test `null` is returned for a twitter profile.
*
* @return void
*/
public function testCreatePhotoLinkWithTwitterURL()
{
$twitterURL = 'https://twitter.com/example';
$this->assertNull($this->notesController->createPhotoLink($twitterURL));
}
}

52
tests/PlacesTest.php Normal file
View file

@ -0,0 +1,52 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlacesTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
}
/**
* Test the `/places` page for OK response.
*
* @return void
*/
public function testPlacesPage()
{
$this->visit($this->appurl . '/places')
->assertResponseOK();
}
/**
* Test a specific place.
*
* @return void
*/
public function testSinglePlace()
{
$this->visit($this->appurl . '/places/the-bridgewater-pub')
->see('The Bridgewater Pub');
}
/**
* Test the nearby method returns a collection.
*
* @return void
*/
public function testNearbyMethod()
{
$nearby = \App\Place::near(53.5, -2.38, 1000);
$this->assertEquals('the-bridgewater-pub', $nearby[0]->slug);
}
}

25
tests/TestCase.php Normal file
View file

@ -0,0 +1,25 @@
<?php
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TokenServiceTest extends TestCase
{
protected $appurl;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
$this->tokenService = new \App\Services\TokenService();
}
/**
* Given the token is dependent on a random nonce, the time of creation and
* the APP_KEY, to test, we shall create a token, and then verify it.
*
* @return void
*/
public function testTokenCreationAndValidation()
{
$data = [
'me' => 'https://example.org',
'client_id' => 'https://quill.p3k.io',
'scope' => 'post'
];
$token = $this->tokenService->getNewToken($data);
$valid = $this->tokenService->validateToken($token);
$validData = [
'me' => $valid->getClaim('me'),
'client_id' => $valid->getClaim('client_id'),
'scope' => $valid->getClaim('scope')
];
$this->assertSame($data, $validData);
}
}

BIN
tests/aaron.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB