Refactore Auth feature tests

This commit is contained in:
Bubka 2020-03-04 17:19:13 +01:00
parent 9f3a770f21
commit 25ee8f58fe
4 changed files with 240 additions and 161 deletions

View File

@ -0,0 +1,90 @@
<?php
namespace Tests\Feature\Auth;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class ForgotPasswordTest extends TestCase
{
/** @var \App\User */
protected $user;
/**
* Testing submitting the email password request without
* email address.
*/
public function testSubmitEmailPasswordRequestWithoutEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => ''
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with an invalid
* email address.
*/
public function testSubmitEmailPasswordRequestWithInvalidEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => 'nametest.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with an unknown
* email address.
*/
public function testSubmitEmailPasswordRequestWithUnknownEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => 'name@test.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with a valid email address.
*/
public function testSubmitEmailPasswordRequest()
{
Notification::fake();
$this->user = factory(User::class)->create([
'name' => 'user',
'email' => 'user@example.org',
'password' => bcrypt('password'),
'email_verified_at' => now(),
'remember_token' => \Illuminate\Support\Str::random(10),
]);
//$this->expectsNotification($this->user, ResetPassword::class);
$response = $this->json('POST', '/api/password/email', [
'email' => $this->user->email
]);
$response->assertStatus(200);
$token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
$this->assertNotNull($token);
// Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
// return Hash::check($notification->token, $token->token) === true;
// });
}
}

View File

@ -0,0 +1,146 @@
<?php
namespace Tests\Feature\Auth;
use App\User;
use Tests\TestCase;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\RequestGuard;
class LoginTest extends TestCase
{
/** @var \App\User */
protected $user;
/**
* @test
*/
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create();
}
/**
* test User login via API
*
* @test
*/
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'password'
]);
$response->assertStatus(200)
->assertJsonStructure([
'message' => ['token']
]);
}
/**
* test User login with missing values via API
*
* @test
*/
public function testUserLoginWithMissingValues()
{
$response = $this->json('POST', '/api/login', [
'email' => '',
'password' => ''
]);
$response->assertStatus(422);
}
/**
* test User login with invalid credentials via API
*
* @test
*/
public function testUserLoginWithInvalidCredential()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response->assertStatus(401)
->assertJson([
'message' => 'unauthorised'
]);
}
/**
* test User login with invalid credentials via API
*
* @test
*/
public function testTooManyAttempsWithInvalidCredential()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response->assertStatus(429);
}
/**
* test User logout via API
*
* @test
*/
public function testUserLogout()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'password'
]);
$headers = ['Authorization' => "Bearer " . $response->original['message']['token']];
$response = $this->json('POST', '/api/logout', [], $headers)
->assertStatus(200)
->assertJson([
'message' => 'signed out',
]);
}
}

View File

@ -1,14 +1,11 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Auth;
use App\User;
use Tests\TestCase;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class UserTest extends TestCase
class RegisterTest extends TestCase
{
/** @var \App\User */
protected $user;
@ -100,85 +97,6 @@ public function testUserCreationWithInvalidData()
}
/**
* test User login via API
*
* @test
*/
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'password'
]);
$response->assertStatus(200)
->assertJsonStructure([
'message' => ['token']
]);
}
/**
* test User login with missing values via API
*
* @test
*/
public function testUserLoginWithMissingValues()
{
$response = $this->json('POST', '/api/login', [
'email' => '',
'password' => ''
]);
$response->assertStatus(422);
}
/**
* test User login with invalid credentials via API
*
* @test
*/
public function testUserLoginWithInvalidCredential()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response->assertStatus(401)
->assertJson([
'message' => 'unauthorised'
]);
}
/**
* test User logout via API
*
* @test
*/
public function testUserLogout()
{
$credentials = [
'email' => $this->user->email,
'password' => 'password'
];
Auth::attempt($credentials);
$token = Auth::user()->createToken('testToken')->accessToken;
$headers = ['Authorization' => "Bearer $token"];
$response = $this->json('POST', '/api/logout', [], $headers)
->assertStatus(200)
->assertJson([
'message' => 'signed out',
]);
}
/**
* test User creation via API
*

View File

@ -1,93 +1,18 @@
<?php
namespace Tests\Feature;
namespace Tests\Feature\Auth;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class PasswordResetTest extends TestCase
class ResetPasswordTest extends TestCase
{
/** @var \App\User */
protected $user;
/**
* Testing submitting the email password request without
* email address.
*/
public function testSubmitEmailPasswordRequestWithoutEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => ''
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with an invalid
* email address.
*/
public function testSubmitEmailPasswordRequestWithInvalidEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => 'nametest.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with an unknown
* email address.
*/
public function testSubmitEmailPasswordRequestWithUnknownEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => 'name@test.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with a valid email address.
*/
public function testSubmitEmailPasswordRequest()
{
Notification::fake();
$this->user = factory(User::class)->create([
'name' => 'user',
'email' => 'user@example.org',
'password' => bcrypt('password'),
'email_verified_at' => now(),
'remember_token' => \Illuminate\Support\Str::random(10),
]);
//$this->expectsNotification($this->user, ResetPassword::class);
$response = $this->json('POST', '/api/password/email', [
'email' => $this->user->email
]);
$response->assertStatus(200);
$token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
$this->assertNotNull($token);
// Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
// return Hash::check($notification->token, $token->token) === true;
// });
}
/**
* Testing submitting the reset password without