From 66a4d382604dd5cc4e3c70f31d5b5ef1a25f256e Mon Sep 17 00:00:00 2001 From: Will Browning Date: Thu, 27 Jun 2019 09:25:01 +0100 Subject: [PATCH] Added login and registration tests --- tests/Feature/LoginTest.php | 39 ++++++++ tests/Feature/RegistrationTest.php | 139 +++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 tests/Feature/LoginTest.php create mode 100644 tests/Feature/RegistrationTest.php diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php new file mode 100644 index 0000000..8427935 --- /dev/null +++ b/tests/Feature/LoginTest.php @@ -0,0 +1,39 @@ +user = factory(User::class)->create([ + 'username' => 'johndoe', + 'password' => Hash::make('mypassword') + ]); + $this->user->recipients()->save($this->user->defaultRecipient); + } + + /** @test */ + public function user_can_login_successfully() + { + $response = $this->post('/login', [ + 'username' => 'johndoe', + 'password' => 'mypassword' + ]); + + $response + ->assertRedirect('/') + ->assertSessionHasNoErrors(); + } +} diff --git a/tests/Feature/RegistrationTest.php b/tests/Feature/RegistrationTest.php new file mode 100644 index 0000000..a3b0443 --- /dev/null +++ b/tests/Feature/RegistrationTest.php @@ -0,0 +1,139 @@ +post('/register', [ + 'username' => 'johndoe', + 'email' => 'johndoe@example.com', + 'email_confirmation' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => true, + ]); + + $response + ->assertRedirect('/') + ->assertSessionHasNoErrors(); + + $this->assertDatabaseHas('users', [ + 'username' => 'johndoe' + ]); + } + + /** @test */ + public function user_must_use_valid_username() + { + $response = $this->post('/register', [ + 'username' => 'john_doe', + 'email' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => true, + ]); + + $response->assertSessionHasErrors(['username']); + + $this->assertDatabaseMissing('users', [ + 'username' => 'john_doe' + ]); + } + + /** @test */ + public function user_must_confirm_email() + { + $response = $this->post('/register', [ + 'username' => 'johndoe', + 'email' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => true, + ]); + + $response->assertSessionHasErrors(['email']); + + $this->assertDatabaseMissing('users', [ + 'username' => 'johndoe' + ]); + } + + /** @test */ + public function user_must_accept_terms() + { + $response = $this->post('/register', [ + 'username' => 'johndoe', + 'email' => 'johndoe@example.com', + 'email_confirmation' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => false, + ]); + + $response->assertSessionHasErrors(['terms']); + + $this->assertDatabaseMissing('users', [ + 'username' => 'johndoe' + ]); + } + + /** @test */ + public function user_cannot_register_with_existing_username() + { + factory(User::class)->create(['username' => 'johndoe']); + + $response = $this->post('/register', [ + 'username' => 'johndoe', + 'email' => 'johndoe@example.com', + 'email_confirmation' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => true, + ]); + + $response->assertSessionHasErrors(['username']); + } + + /** @test */ + public function user_cannot_register_with_blacklisted_username() + { + $response = $this->post('/register', [ + 'username' => 'www', + 'email' => 'johndoe@example.com', + 'email_confirmation' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => true, + ]); + + $response->assertSessionHasErrors(['username']); + + $this->assertDatabaseMissing('users', [ + 'username' => 'www' + ]); + } + + /** @test */ + public function user_cannot_register_with_deleted_username() + { + DeletedUsername::create(['username' => 'johndoe']); + + $response = $this->post('/register', [ + 'username' => 'johndoe', + 'email' => 'johndoe@example.com', + 'email_confirmation' => 'johndoe@example.com', + 'password' => 'mypassword', + 'terms' => true, + ]); + + $response->assertSessionHasErrors(['username']); + + $this->assertDatabaseMissing('users', [ + 'username' => 'johndoe' + ]); + } +}