mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-23 13:31:27 +02:00
Refactore Auth feature tests
This commit is contained in:
parent
9f3a770f21
commit
25ee8f58fe
90
tests/Feature/Auth/ForgotPasswordTest.php
Normal file
90
tests/Feature/Auth/ForgotPasswordTest.php
Normal 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;
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
146
tests/Feature/Auth/LoginTest.php
Normal file
146
tests/Feature/Auth/LoginTest.php
Normal 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',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,14 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests\Unit;
|
namespace Tests\Unit\Auth;
|
||||||
|
|
||||||
use App\User;
|
use App\User;
|
||||||
use Tests\TestCase;
|
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 */
|
/** @var \App\User */
|
||||||
protected $user;
|
protected $user;
|
||||||
@ -100,85 +97,6 @@ class UserTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* test User creation via API
|
||||||
*
|
*
|
@ -1,93 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature\Auth;
|
||||||
|
|
||||||
use App\User;
|
use App\User;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Illuminate\Support\Facades\Password;
|
use Illuminate\Support\Facades\Password;
|
||||||
use Illuminate\Auth\Notifications\ResetPassword;
|
|
||||||
use Illuminate\Support\Facades\Notification;
|
use Illuminate\Support\Facades\Notification;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class PasswordResetTest extends TestCase
|
class ResetPasswordTest extends TestCase
|
||||||
{
|
{
|
||||||
/** @var \App\User */
|
/** @var \App\User */
|
||||||
protected $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
|
* Testing submitting the reset password without
|
Loading…
x
Reference in New Issue
Block a user