From d342a09e6546a90027470ea0e78f2330d4e6c56d Mon Sep 17 00:00:00 2001 From: Cretu Eusebiu Date: Tue, 18 Jul 2017 10:04:48 +0300 Subject: [PATCH] Add some tests --- tests/Feature/ExampleTest.php | 23 -------------- tests/Feature/LoginTest.php | 55 ++++++++++++++++++++++++++++++++++ tests/Feature/RegisterTest.php | 21 +++++++++++++ tests/TestCase.php | 14 +++++++++ 4 files changed, 90 insertions(+), 23 deletions(-) delete mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Feature/LoginTest.php create mode 100644 tests/Feature/RegisterTest.php diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 486dc271a..000000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,23 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php new file mode 100644 index 000000000..f5e4c644d --- /dev/null +++ b/tests/Feature/LoginTest.php @@ -0,0 +1,55 @@ +user = factory(User::class)->create(); + } + + /** @test */ + public function can_authenticate() + { + $this->postJson('/api/login', [ + 'email' => $this->user->email, + 'password' => 'secret', + ]) + ->assertSuccessful() + ->assertJsonStructure(['token', 'expires_in']) + ->assertJson(['token_type' => 'bearer']); + } + + /** @test */ + public function can_fetch_the_current_user() + { + $this->actingAs($this->user) + ->getJson('/api/user') + ->assertSuccessful() + ->assertJsonStructure(['id', 'name', 'email']); + } + + /** @test */ + public function can_log_out() + { + $token = $this->postJson('/api/login', [ + 'email' => $this->user->email, + 'password' => 'secret', + ])->json()['token']; + + $this->json('POST', "/api/logout?token=$token") + ->assertSuccessful(); + + $this->getJson("/api/user?token=$token") + ->assertStatus(401); + } +} diff --git a/tests/Feature/RegisterTest.php b/tests/Feature/RegisterTest.php new file mode 100644 index 000000000..9861c9e0b --- /dev/null +++ b/tests/Feature/RegisterTest.php @@ -0,0 +1,21 @@ +postJson('/api/register', [ + 'name' => 'Test User', + 'email' => 'test@test.app', + 'password' => 'secret', + 'password_confirmation' => 'secret', + ]) + ->assertSuccessful() + ->assertJsonStructure(['id', 'name', 'email']); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a69..b77fbc473 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,23 @@ namespace Tests; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + use DatabaseTransactions; + + /** + * Setup the test environment. + * + * @return void + */ + protected function setUp() + { + parent::setUp(); + + $this->artisan('migrate'); + } }