Improve tests

This commit is contained in:
Jonny Barnes 2022-05-14 17:44:14 +01:00
parent 427debaba4
commit 48d1c9a00b
18 changed files with 267 additions and 14 deletions

View file

@ -25,6 +25,19 @@ class ActivityStreamTest extends TestCase
]);
}
/** @test */
public function notesPageContainsAuthorActivityStreamData(): void
{
$response = $this->get('/notes', ['Accept' => 'application/activity+json']);
$response->assertHeader('Content-Type', 'application/activity+json');
$response->assertJson([
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => config('app.url'),
'type' => 'Person',
'name' => config('user.displayname'),
]);
}
/** @test */
public function requestForNoteIncludesActivityStreamData(): void
{

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Tests\Feature\Admin;
use App\Models\User;
use Tests\TestCase;
class AdminTest extends TestCase
@ -31,4 +32,51 @@ class AdminTest extends TestCase
]);
$response->assertRedirect('/login');
}
/** @test */
public function loginSucceeds(): void
{
User::factory([
'name' => 'admin',
'password' => bcrypt('password'),
])->create();
$response = $this->post('/login', [
'name' => 'admin',
'password' => 'password',
]);
$response->assertRedirect('/');
}
/** @test */
public function whenLoggedInRedirectsToAdminPage(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/login');
$response->assertRedirect('/');
}
/** @test */
public function loggedOutUsersSimplyRedirected(): void
{
$response = $this->get('/logout');
$response->assertRedirect('/');
}
/** @test */
public function loggedInUsersShownLogoutForm(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/logout');
$response->assertViewIs('logout');
}
/** @test */
public function loggedInUsersCanLogout(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/logout');
$response->assertRedirect('/');
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace Tests\Feature;
use App\Models\Article;
use App\Models\Bookmark;
use App\Models\Like;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class FrontPageTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function frontPageLoadsAllContent(): void
{
Note::factory()->create(['note' => 'Note 1']);
Article::factory()->create(['title' => 'Article 1']);
Bookmark::factory()->create(['url' => 'https://example.com']);
Like::factory()->create([
'url' => 'https://example.org/1',
'content' => 'Like 1',
]);
$this->get('/')
->assertSee('Note 1')
->assertSee('Article 1')
->assertSee('https://example.com')
->assertSee('Like 1');
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class HorizonTest extends TestCase
{
/**
* Horizon has its own test suite, here we just test it has been installed successfully.
*
* @test
* @return void
*/
public function horizonIsInstalled(): void
{
$user = User::factory()->create([
'name' => 'jonny',
]);
$response = $this->actingAs($user)->get('/horizon');
$response->assertStatus(200);
}
}

View file

@ -233,6 +233,44 @@ class LikesTest extends TestCase
$this->assertEquals('Jonny Barnes', Like::find($id)->author_name);
}
/** @test */
public function noErrorForFailureToPosseWithBridgy(): void
{
$like = new Like();
$like->url = 'https://twitter.com/jonnybarnes/status/1050823255123251200';
$like->save();
$id = $like->id;
$job = new ProcessLike($like);
$mock = new MockHandler([
new Response(404, [], 'Not found'),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->bind(Client::class, function () use ($client) {
return $client;
});
$info = (object) [
'author_name' => 'Jonny Barnes',
'author_url' => 'https://twitter.com/jonnybarnes',
'html' => '<div>HTML of the tweet embed</div>',
];
$codebirdMock = $this->getMockBuilder(Codebird::class)
->addMethods(['statuses_oembed'])
->getMock();
$codebirdMock->method('statuses_oembed')
->willReturn($info);
$this->app->instance(Codebird::class, $codebirdMock);
$authorship = new Authorship();
$job->handle($client, $authorship);
$this->assertEquals('Jonny Barnes', Like::find($id)->author_name);
}
/** @test */
public function unknownLikeGivesNotFoundResponse(): void
{

View file

@ -34,6 +34,50 @@ class MicropubMediaTest extends TestCase
$response->assertJson(['url' => null]);
}
/** @test */
public function getRequestWithInvalidTokenReturnsErrorResponse(): void
{
$response = $this->get(
'/api/media?q=last',
['HTTP_Authorization' => 'Bearer abc123']
);
$response->assertStatus(400);
$response->assertJsonFragment(['error_description' => 'The provided token did not pass validation']);
}
/** @test */
public function getRequestWithTokenWithoutScopeReturnsErrorResponse(): void
{
$response = $this->get(
'/api/media?q=last',
['HTTP_Authorization' => 'Bearer ' . $this->getTokenWithNoScope()]
);
$response->assertStatus(400);
$response->assertJsonFragment(['error_description' => 'The provided token has no scopes']);
}
/** @test */
public function getRequestWithTokenWithInsufficientScopeReturnsErrorResponse(): void
{
$response = $this->get(
'/api/media?q=last',
['HTTP_Authorization' => 'Bearer ' . $this->getTokenWithIncorrectScope()]
);
$response->assertStatus(401);
$response->assertJsonFragment(['error_description' => 'The tokens scope does not have the necessary requirements.']);
}
/** @test */
public function emptyGetRequestWithTokenReceivesOkResponse(): void
{
$response = $this->get(
'/api/media',
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertStatus(200);
$response->assertJson(['status' => 'OK']);
}
/** @test */
public function clientCanListLastUpload(): void
{
@ -124,6 +168,22 @@ class MicropubMediaTest extends TestCase
unlink(storage_path('app/') . $filename);
}
/** @test */
public function mediaEndpointUploadRequiresFile(): void
{
$response = $this->post(
'/api/media',
[],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertStatus(400);
$response->assertJson([
'response' => 'error',
'error' => 'invalid_request',
'error_description' => 'No file was sent with the request',
]);
}
/** @test */
public function errorResponseForUnknownQValue(): void
{