Added login and registration tests
This commit is contained in:
parent
223df50a84
commit
66a4d38260
2 changed files with 178 additions and 0 deletions
39
tests/Feature/LoginTest.php
Normal file
39
tests/Feature/LoginTest.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class LoginTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
protected $user;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
139
tests/Feature/RegistrationTest.php
Normal file
139
tests/Feature/RegistrationTest.php
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\DeletedUsername;
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class RegistrationTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function user_can_register_successfully()
|
||||||
|
{
|
||||||
|
$response = $this->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'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue