Update Admin tests for new auth implementation

This commit is contained in:
Jonny Barnes 2019-03-23 15:41:01 +00:00
parent 2e79492b01
commit dad45c4ab1
8 changed files with 136 additions and 91 deletions

View file

@ -1,5 +1,6 @@
<?php <?php
use Illuminate\Support\Str;
use Faker\Generator as Faker; use Faker\Generator as Faker;
/* /*
@ -12,12 +13,12 @@ use Faker\Generator as Faker;
| model instances for testing / seeding your application's database. | model instances for testing / seeding your application's database.
| |
*/ */
$factory->define(App\User::class, function (Faker $faker) { $factory->define(App\Models\User::class, function (Faker $faker) {
static $password; static $password;
return [ return [
'name' => $faker->name, 'name' => mb_strtolower($faker->firstName),
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'), 'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10), 'remember_token' => Str::random(10),
]; ];
}); });

View file

@ -3,13 +3,20 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use Tests\TestCase; use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AdminHomeControllerTest extends TestCase class AdminHomeControllerTest extends TestCase
{ {
use DatabaseTransactions;
public function test_admin_homepage() public function test_admin_homepage()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin'); ->get('/admin');
$response->assertViewIs('admin.welcome'); $response->assertViewIs('admin.welcome');
} }
} }

View file

@ -3,6 +3,7 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use Tests\TestCase; use Tests\TestCase;
use App\Models\User;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -12,21 +13,27 @@ class ArticlesTest extends TestCase
public function test_index_page() public function test_index_page()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/blog'); ->get('/admin/blog');
$response->assertSeeText('Select article to edit:'); $response->assertSeeText('Select article to edit:');
} }
public function test_create_page() public function test_create_page()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/blog/create'); ->get('/admin/blog/create');
$response->assertSeeText('Title (URL)'); $response->assertSeeText('Title (URL)');
} }
public function test_create_new_article() public function test_create_new_article()
{ {
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/blog', [ ->post('/admin/blog', [
'title' => 'Test Title', 'title' => 'Test Title',
'main' => 'Article content' 'main' => 'Article content'
@ -36,6 +43,7 @@ class ArticlesTest extends TestCase
public function test_create_new_article_with_upload() public function test_create_new_article_with_upload()
{ {
$user = factory(User::class)->create();
$faker = \Faker\Factory::create(); $faker = \Faker\Factory::create();
$text = $faker->text; $text = $faker->text;
if ($fh = fopen(sys_get_temp_dir() . '/article.md', 'w')) { if ($fh = fopen(sys_get_temp_dir() . '/article.md', 'w')) {
@ -45,7 +53,7 @@ class ArticlesTest extends TestCase
$path = sys_get_temp_dir() . '/article.md'; $path = sys_get_temp_dir() . '/article.md';
$file = new UploadedFile($path, 'article.md', 'text/plain', filesize($path), null, true); $file = new UploadedFile($path, 'article.md', 'text/plain', filesize($path), null, true);
$this->withSession(['loggedin' => true]) $this->actingAs($user)
->post('/admin/blog', [ ->post('/admin/blog', [
'title' => 'Uploaded Article', 'title' => 'Uploaded Article',
'article' => $file, 'article' => $file,
@ -59,14 +67,18 @@ class ArticlesTest extends TestCase
public function test_see_edit_form() public function test_see_edit_form()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/blog/1/edit'); ->get('/admin/blog/1/edit');
$response->assertSeeText('This is *my* new blog. It uses `Markdown`.'); $response->assertSeeText('This is *my* new blog. It uses `Markdown`.');
} }
public function test_edit_article() public function test_edit_article()
{ {
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/blog/1', [ ->post('/admin/blog/1', [
'_method' => 'PUT', '_method' => 'PUT',
'title' => 'My New Blog', 'title' => 'My New Blog',
@ -80,7 +92,9 @@ class ArticlesTest extends TestCase
public function test_delete_article() public function test_delete_article()
{ {
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/blog/1', [ ->post('/admin/blog/1', [
'_method' => 'DELETE', '_method' => 'DELETE',
]); ]);

View file

@ -3,6 +3,7 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use Tests\TestCase; use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
class ClientsTest extends TestCase class ClientsTest extends TestCase
@ -11,21 +12,27 @@ class ClientsTest extends TestCase
public function test_index_page() public function test_index_page()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/clients'); ->get('/admin/clients');
$response->assertSeeText('Clients'); $response->assertSeeText('Clients');
} }
public function test_create_page() public function test_create_page()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/clients/create'); ->get('/admin/clients/create');
$response->assertSeeText('New Client'); $response->assertSeeText('New Client');
} }
public function test_create_new_client() public function test_create_new_client()
{ {
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/clients', [ ->post('/admin/clients', [
'client_name' => 'Micropublish', 'client_name' => 'Micropublish',
'client_url' => 'https://micropublish.net' 'client_url' => 'https://micropublish.net'
@ -38,14 +45,18 @@ class ClientsTest extends TestCase
public function test_see_edit_form() public function test_see_edit_form()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/clients/1/edit'); ->get('/admin/clients/1/edit');
$response->assertSee('https://jbl5.dev/notes/new'); $response->assertSee('https://jbl5.dev/notes/new');
} }
public function test_edit_client() public function test_edit_client()
{ {
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/clients/1', [ ->post('/admin/clients/1', [
'_method' => 'PUT', '_method' => 'PUT',
'client_url' => 'https://jbl5.dev/notes/new', 'client_url' => 'https://jbl5.dev/notes/new',
@ -59,7 +70,9 @@ class ClientsTest extends TestCase
public function test_delete_client() public function test_delete_client()
{ {
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/clients/1', [ ->post('/admin/clients/1', [
'_method' => 'DELETE', '_method' => 'DELETE',
]); ]);

View file

@ -3,6 +3,7 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use Tests\TestCase; use Tests\TestCase;
use App\Models\User;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use App\Models\Contact; use App\Models\Contact;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
@ -26,25 +27,25 @@ class ContactsTest extends TestCase
public function test_index_page() public function test_index_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->get('/admin/contacts'); $response = $this->actingAs($user)->get('/admin/contacts');
$response->assertViewIs('admin.contacts.index'); $response->assertViewIs('admin.contacts.index');
} }
public function test_create_page() public function test_create_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->get('/admin/contacts/create'); $response = $this->actingAs($user)->get('/admin/contacts/create');
$response->assertViewIs('admin.contacts.create'); $response->assertViewIs('admin.contacts.create');
} }
public function test_create_new_contact() public function test_create_new_contact()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->post('/admin/contacts', [ $this->actingAs($user)->post('/admin/contacts', [
'name' => 'Fred Bloggs', 'name' => 'Fred Bloggs',
'nick' => 'fred', 'nick' => 'fred',
'homepage' => 'https://fred.blog/gs', 'homepage' => 'https://fred.blog/gs',
@ -58,17 +59,17 @@ class ContactsTest extends TestCase
public function test_see_edit_form() public function test_see_edit_form()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->get('/admin/contacts/1/edit'); $response = $this->actingAs($user)->get('/admin/contacts/1/edit');
$response->assertViewIs('admin.contacts.edit'); $response->assertViewIs('admin.contacts.edit');
} }
public function test_update_contact_no_uploaded_avatar() public function test_update_contact_no_uploaded_avatar()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->post('/admin/contacts/1', [ $this->actingAs($user)->post('/admin/contacts/1', [
'_method' => 'PUT', '_method' => 'PUT',
'name' => 'Tantek Celik', 'name' => 'Tantek Celik',
'nick' => 'tantek', 'nick' => 'tantek',
@ -86,9 +87,9 @@ class ContactsTest extends TestCase
copy(__DIR__ . '/../../aaron.png', sys_get_temp_dir() . '/tantek.png'); copy(__DIR__ . '/../../aaron.png', sys_get_temp_dir() . '/tantek.png');
$path = sys_get_temp_dir() . '/tantek.png'; $path = sys_get_temp_dir() . '/tantek.png';
$file = new UploadedFile($path, 'tantek.png', 'image/png', filesize($path), null, true); $file = new UploadedFile($path, 'tantek.png', 'image/png', filesize($path), null, true);
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->post('/admin/contacts/1', [ $this->actingAs($user)->post('/admin/contacts/1', [
'_method' => 'PUT', '_method' => 'PUT',
'name' => 'Tantek Celik', 'name' => 'Tantek Celik',
'nick' => 'tantek', 'nick' => 'tantek',
@ -104,9 +105,9 @@ class ContactsTest extends TestCase
public function test_delete_contact() public function test_delete_contact()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true
])->post('/admin/contacts/1', [ $this->actingAs($user)->post('/admin/contacts/1', [
'_method' => 'DELETE', '_method' => 'DELETE',
]); ]);
$this->assertDatabaseMissing('contacts', [ $this->assertDatabaseMissing('contacts', [
@ -129,10 +130,9 @@ HTML;
$handler = HandlerStack::create($mock); $handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]); $client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client); $this->app->instance(Client::class, $client);
$user = factory(User::class)->create();
$response = $this->withSession([ $this->actingAs($user)->get('/admin/contacts/1/getavatar');
'loggedin' => true,
])->get('/admin/contacts/1/getavatar');
$this->assertFileEquals( $this->assertFileEquals(
__DIR__ . '/../../aaron.png', __DIR__ . '/../../aaron.png',
@ -148,10 +148,9 @@ HTML;
$handler = HandlerStack::create($mock); $handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]); $client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client); $this->app->instance(Client::class, $client);
$user = factory(User::class)->create();
$response = $this->withSession([ $response = $this->actingAs($user)->get('/admin/contacts/1/getavatar');
'loggedin' => true,
])->get('/admin/contacts/1/getavatar');
$response->assertRedirect('/admin/contacts/1/edit'); $response->assertRedirect('/admin/contacts/1/edit');
} }
@ -170,10 +169,9 @@ HTML;
$handler = HandlerStack::create($mock); $handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]); $client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client); $this->app->instance(Client::class, $client);
$user = factory(User::class)->create();
$response = $this->withSession([ $response = $this->actingAs($user)->get('/admin/contacts/1/getavatar');
'loggedin' => true,
])->get('/admin/contacts/1/getavatar');
$response->assertRedirect('/admin/contacts/1/edit'); $response->assertRedirect('/admin/contacts/1/edit');
} }
@ -184,10 +182,9 @@ HTML;
'nick' => 'fred', 'nick' => 'fred',
'name' => 'Fred Bloggs', 'name' => 'Fred Bloggs',
]); ]);
$user = factory(User::class)->create();
$response = $this->withSession([ $response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar');
'loggedin' => true,
])->get('/admin/contacts/' . $contact->id . '/getavatar');
$response->assertRedirect('/admin/contacts/' . $contact->id . '/edit'); $response->assertRedirect('/admin/contacts/' . $contact->id . '/edit');
} }

View file

@ -2,11 +2,11 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use App\Models\User;
use Tests\TestCase; use Tests\TestCase;
use App\Models\Like; use App\Models\Like;
use App\Jobs\ProcessLike; use App\Jobs\ProcessLike;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
class LikesTest extends TestCase class LikesTest extends TestCase
@ -15,14 +15,18 @@ class LikesTest extends TestCase
public function test_index_page() public function test_index_page()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/likes'); ->get('/admin/likes');
$response->assertSeeText('Likes'); $response->assertSeeText('Likes');
} }
public function test_create_page() public function test_create_page()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/likes/create'); ->get('/admin/likes/create');
$response->assertSeeText('New Like'); $response->assertSeeText('New Like');
} }
@ -30,7 +34,9 @@ class LikesTest extends TestCase
public function test_create_new_like() public function test_create_new_like()
{ {
Queue::fake(); Queue::fake();
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/likes', [ ->post('/admin/likes', [
'like_url' => 'https://example.com' 'like_url' => 'https://example.com'
]); ]);
@ -42,7 +48,9 @@ class LikesTest extends TestCase
public function test_see_edit_form() public function test_see_edit_form()
{ {
$response = $this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/admin/likes/1/edit'); ->get('/admin/likes/1/edit');
$response->assertSee('Edit Like'); $response->assertSee('Edit Like');
} }
@ -50,7 +58,9 @@ class LikesTest extends TestCase
public function test_edit_like() public function test_edit_like()
{ {
Queue::fake(); Queue::fake();
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/likes/1', [ ->post('/admin/likes/1', [
'_method' => 'PUT', '_method' => 'PUT',
'like_url' => 'https://example.com', 'like_url' => 'https://example.com',
@ -65,7 +75,9 @@ class LikesTest extends TestCase
{ {
$like = Like::find(1); $like = Like::find(1);
$url = $like->url; $url = $like->url;
$this->withSession(['loggedin' => true]) $user = factory(User::class)->create();
$this->actingAs($user)
->post('/admin/likes/1', [ ->post('/admin/likes/1', [
'_method' => 'DELETE', '_method' => 'DELETE',
]); ]);

View file

@ -2,6 +2,7 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use App\Models\User;
use Tests\TestCase; use Tests\TestCase;
use App\Jobs\SendWebMentions; use App\Jobs\SendWebMentions;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
@ -13,25 +14,25 @@ class NotesTest extends TestCase
public function test_index_page() public function test_index_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->get('/admin/notes'); $response = $this->actingAs($user)->get('/admin/notes');
$response->assertViewIs('admin.notes.index'); $response->assertViewIs('admin.notes.index');
} }
public function test_create_page() public function test_create_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->get('/admin/notes/create'); $response = $this->actingAs($user)->get('/admin/notes/create');
$response->assertViewIs('admin.notes.create'); $response->assertViewIs('admin.notes.create');
} }
public function test_create_a_new_note() public function test_create_a_new_note()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->post('/admin/notes', [ $this->actingAs($user)->post('/admin/notes', [
'content' => 'A new test note', 'content' => 'A new test note',
]); ]);
$this->assertDatabaseHas('notes', [ $this->assertDatabaseHas('notes', [
@ -41,19 +42,18 @@ class NotesTest extends TestCase
public function test_edit_page() public function test_edit_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->get('/admin/notes/1/edit'); $response = $this->actingAs($user)->get('/admin/notes/1/edit');
$response->assertViewIs('admin.notes.edit'); $response->assertViewIs('admin.notes.edit');
} }
public function test_edit_a_note() public function test_edit_a_note()
{ {
Queue::fake(); Queue::fake();
$user = factory(User::class)->create();
$this->withSession([ $this->actingAs($user)->post('/admin/notes/1', [
'loggedin' => true,
])->post('/admin/notes/1', [
'_method' => 'PUT', '_method' => 'PUT',
'content' => 'An edited note', 'content' => 'An edited note',
'webmentions' => true, 'webmentions' => true,
@ -67,9 +67,9 @@ class NotesTest extends TestCase
public function test_delete_note() public function test_delete_note()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->post('/admin/notes/1', [ $this->actingAs($user)->post('/admin/notes/1', [
'_method' => 'DELETE', '_method' => 'DELETE',
]); ]);
$this->assertSoftDeleted('notes', [ $this->assertSoftDeleted('notes', [

View file

@ -2,6 +2,7 @@
namespace Tests\Feature\Admin; namespace Tests\Feature\Admin;
use App\Models\User;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -11,25 +12,25 @@ class PlacesTest extends TestCase
public function test_index_page() public function test_index_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->get('/admin/places'); $response = $this->actingAs($user)->get('/admin/places');
$response->assertViewIs('admin.places.index'); $response->assertViewIs('admin.places.index');
} }
public function test_create_page() public function test_create_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->get('/admin/places/create'); $response = $this->actingAs($user)->get('/admin/places/create');
$response->assertViewIs('admin.places.create'); $response->assertViewIs('admin.places.create');
} }
public function test_create_new_place() public function test_create_new_place()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->post('/admin/places', [ $this->actingAs($user)->post('/admin/places', [
'name' => 'Test Place', 'name' => 'Test Place',
'description' => 'A dummy place for feature tests', 'description' => 'A dummy place for feature tests',
'latitude' => '1.23', 'latitude' => '1.23',
@ -43,17 +44,17 @@ class PlacesTest extends TestCase
public function test_edit_page() public function test_edit_page()
{ {
$response = $this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->get('/admin/places/1/edit'); $response = $this->actingAs($user)->get('/admin/places/1/edit');
$response->assertViewIs('admin.places.edit'); $response->assertViewIs('admin.places.edit');
} }
public function test_updating_a_place() public function test_updating_a_place()
{ {
$this->withSession([ $user = factory(User::class)->create();
'loggedin' => true,
])->post('/admin/places/1', [ $this->actingAs($user)->post('/admin/places/1', [
'_method' => 'PUT', '_method' => 'PUT',
'name' => 'The Bridgewater', 'name' => 'The Bridgewater',
'description' => 'Who uses “Pub” anyway', 'description' => 'Who uses “Pub” anyway',