Get Laravel Dusk installed using PhantomJS so it can work with Travis CI easily

This commit is contained in:
Jonny Barnes 2017-02-18 12:27:21 +00:00
parent db44423c2e
commit 94969e7f97
25 changed files with 314 additions and 1123 deletions

View file

@ -1,78 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ArticlesTest extends BrowserKitTest
{
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'));
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* @return void
*/
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Jonny Barnes');
});
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Tests\Browser\Pages;
use Laravel\Dusk\Browser;
class HomePage extends Page
{
/**
* Get the URL for the page.
*
* @return string
*/
public function url()
{
return '/';
}
/**
* Assert that the browser is on the page.
*
* @return void
*/
public function assert(Browser $browser)
{
//
}
/**
* Get the element shortcuts for the page.
*
* @return array
*/
public function elements()
{
return [
'@element' => '#selector',
];
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Tests\Browser\Pages;
use Laravel\Dusk\Page as BasePage;
abstract class Page extends BasePage
{
/**
* Get the global element shortcuts for the site.
*
* @return array
*/
public static function siteElements()
{
return [
'@element' => '#selector',
];
}
}

2
tests/Browser/screenshots/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*
!.gitignore

View file

@ -1,52 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ContactsTest extends BrowserKitTest
{
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">');
}
}

View file

@ -1,17 +1,11 @@
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
abstract class BrowserKitTest extends BaseTestCase
trait CreatesApplication
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
@ -20,9 +14,7 @@ abstract class BrowserKitTest extends BaseTestCase
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}

35
tests/DuskTestCase.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace Tests;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
//static::startChromeDriver();
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::phantomjs()
);
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

View file

@ -1,79 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class IndieAuthTest extends BrowserKitTest
{
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 `start` method redirects to the client on error.
*
* @return void
*/
public function testIndieAuthControllerBeginAuthRedirectsToClientOnFail()
{
$response = $this->call('GET', $this->appurl . '/indieauth/start', ['me' => 'http://example.org']);
$this->assertSame($this->appurl . '/micropub/create', $response->headers->get('Location'));
}
/**
* Now we test the `start` method as a whole.
*
* @return void
*/
public function testIndieAuthControllerBeginAuthRedirectsToEndpoint()
{
$response = $this->call('GET', $this->appurl . '/indieauth/start', ['me' => $this->appurl]);
$this->assertSame(
'https://indieauth.com/auth?me=',
substr($response->headers->get('Location'), 0, 30)
);
$response = null;
}
}

View file

@ -1,69 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
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 BrowserKitTest
{
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 . '/micropub/create')
->see('IndieAuth');
}
public function testClientPageRecentAuth()
{
$this->visit($this->appurl . '/micropub/create')
->see($this->appurl);
}
public function testClientCreatesNewNoteWithTag()
{
//in this test, the syndication targets are blank
$faker = \Faker\Factory::create();
$note = 'Fake note from #PHPUnit: ' . $faker->text;
$this->visit($this->appurl . '/micropub/create')
->type($note, 'content')
->press('Submit');
$this->seeInDatabase('notes', ['note' => $note]);
$this->visit($this->appurl . '/notes/tagged/PHPUnit')
->see('PHPUnit');
//my client has made a request to my endpoint, which then adds
//to the db, so database transaction dont work
//so lets manually delete the new entry
//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 didnt get deleted
$this->fail('Didnt delete the note from the index');
}
}
$newNote = \App\Note::where('note', $note)->first();
$newNote->forceDelete();
}
}

View file

@ -1,271 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
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 BrowserKitTest
{
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'
]);
}
public function testMicropubJSONRequestCreateNewPlaceWithUncertaintyParam()
{
$faker = \Faker\Factory::create();
$this->json(
'POST',
$this->appurl . '/api/post',
[
'type' => ['h-card'],
'properties' => [
'name' => $faker->name,
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude . ';u=35'
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
)->seeJson([
'response' => 'created'
])->assertResponseStatus(201);
}
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;
}
}

View file

@ -1,33 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotesAdminTest extends BrowserKitTest
{
use DatabaseTransactions;
protected $appurl;
protected $notesAdminController;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
$this->notesAdminController = new \App\Http\Controllers\NotesAdminController();
}
public function testCreatedNoteDispatchesSendWebmentionsJob()
{
$this->expectsJobs(\App\Jobs\SendWebMentions::class);
$this->withSession(['loggedin' => true])
->visit($this->appurl . '/admin/note/new')
->type('Mentioning', 'content')
->press('Submit');
}
}

View file

@ -1,182 +0,0 @@
<?php
namespace App\Tests;
use Cache;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotesTest extends BrowserKitTest
{
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" 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" 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 a correct profile link is formed from a generic URL.
*
* @return void
*/
public function testCreatePhotoLinkWithNonCachedImage()
{
$homepage = 'https://example.org/profile.png';
$expected = 'https://example.org/profile.png';
$this->assertEquals($expected, $this->notesController->createPhotoLink($homepage));
}
/**
* Test a correct profile link is formed from a generic URL.
*
* @return void
*/
public function testCreatePhotoLinkWithCachedImage()
{
$homepage = 'https://aaronparecki.com/profile.png';
$expected = '/assets/profile-images/aaronparecki.com/image';
$this->assertEquals($expected, $this->notesController->createPhotoLink($homepage));
}
/**
* Test a correct profile link is formed from a twitter URL.
*
* @return void
*/
public function testCreatePhotoLinkWithTwimgProfileImageURL()
{
$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 testCreatePhotoLinkWithCachedTwitterURL()
{
$twitterURL = 'https://twitter.com/example';
$expected = 'https://pbs.twimg.com/static_profile_link.jpg';
Cache::put($twitterURL, $expected, 1);
$this->assertEquals($expected, $this->notesController->createPhotoLink($twitterURL));
}
}

View file

@ -1,52 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlacesTest extends BrowserKitTest
{
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);
}
}

View file

@ -1,25 +1,10 @@
<?php
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
/**
* 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;
}
use CreatesApplication;
}

View file

@ -1,43 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TokenServiceTest extends BrowserKitTest
{
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);
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}

View file

@ -1,92 +0,0 @@
<?php
namespace App\Tests;
use BrowserKitTest;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class WebMentionsTest extends BrowserKitTest
{
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');
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.4 KiB