Skip to content
Snippets Groups Projects
Commit 71380d40 authored by Maxime FRIESS's avatar Maxime FRIESS :blue_heart:
Browse files

Merge branch 'feature/tests' into 'develop'

Added unit tests

See merge request !3
parents 42f289d8 77aacc26
Branches
Tags
1 merge request!3Added unit tests
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=false
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=sqlite
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
JWT_SECRET=1nTDX71DXyIr6YM9soRu8eba3Jaa7mlZTXHUYlwg5ZqInBZF8kVKglpr5wynAMTU
JWT_ALGO=HS256
......@@ -39,6 +39,7 @@ class Kernel extends HttpKernel
],
'api' => [
\App\Http\Middleware\ForceJsonResponse::class,
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
......
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class ForceJsonResponse
{
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
<?php
namespace Tests\Feature;
// use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_the_application_returns_a_successful_response()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class LoginTest extends TestCase
{
use RefreshDatabase;
public function test_empty() {
$user = User::create([
'login' => 'test',
'email' => 'test@localhost',
'password' => Hash::make('test'),
]);
$response = $this->postJson("/api/auth/login", []);
$response->assertStatus(422);
}
public function test_wrong_password() {
$user = User::create([
'login' => 'test',
'email' => 'test@localhost',
'password' => Hash::make('test'),
]);
$response = $this->postJson("/api/auth/login", [
"login" => "test",
"password" => "123"
]);
$response->assertStatus(401);
}
public function test_good() {
$user = User::create([
'login' => 'test',
'email' => 'test@localhost',
'password' => Hash::make('test'),
]);
$response = $this->postJson("/api/auth/login", [
"login" => "test",
"password" => "test",
]);
$response->assertStatus(200);
$response->assertJsonStructure([
"token",
"user" => [
"id",
"login",
"email",
"created_at",
"updated_at"
]
]);
}
}
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class LogoutTest extends TestCase
{
use RefreshDatabase;
public function test_no_auth()
{
$response = $this->post('/api/auth/logout');
$response->assertStatus(401);
}
public function test_wrong_token()
{
$response = $this->withHeaders([
"Authorization" => "Bearer randombearer"
])->post('/api/auth/logout');
$response->assertStatus(401);
}
public function test_good()
{
$user = User::create([
'login' => 'test',
'email' => 'test@localhost',
'password' => Hash::make('test'),
]);
$token = Auth::login($user);
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->post('/api/auth/logout');
$response->assertStatus(204);
}
}
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class RefreshTest extends TestCase
{
use RefreshDatabase;
public function test_no_auth()
{
$response = $this->post('/api/auth/refresh');
$response->assertStatus(401);
}
public function test_wrong_token()
{
$response = $this->withHeaders([
"Authorization" => "Bearer randombearer"
])->post('/api/auth/refresh');
$response->assertStatus(401);
}
public function test_good()
{
$user = User::create([
'login' => 'test',
'email' => 'test@localhost',
'password' => Hash::make('test'),
]);
$token = Auth::login($user);
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->post('/api/auth/refresh');
$response->assertStatus(200);
$response->assertJsonStructure([
"token",
"user" => [
"id",
"login",
"email",
"created_at",
"updated_at"
]
]);
}
}
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class RegisterTest extends TestCase
{
use RefreshDatabase;
public function test_empty()
{
$response = $this->postJson('/api/auth/register', []);
$response->assertStatus(422);
$this->assertDatabaseCount('users', 0);
}
public function test_password_dont_match()
{
$response = $this->postJson('/api/auth/register', [
"login" => "test",
"email" => "test@localhost",
"password" => "testtest",
"password_confirmation" => "nothtesame",
]);
$response->assertStatus(422);
$this->assertDatabaseCount('users', 0);
}
public function test_invalid_email()
{
$response = $this->postJson('/api/auth/register', [
"login" => "test",
"email" => "somerandomshit",
"password" => "testtest",
"password_confirmation" => "testtest",
]);
$response->assertStatus(422);
$this->assertDatabaseCount('users', 0);
}
public function test_good()
{
$response = $this->postJson('/api/auth/register', [
"login" => "test",
"email" => "test@localhost",
"password" => "testtest",
"password_confirmation" => "testtest",
]);
$response->assertStatus(200);
$response->assertJsonStructure([
"token",
"user" => [
"id",
"login",
"email",
"created_at",
"updated_at"
]
]);
$this->assertDatabaseHas('users', [
'login' => 'test'
]);
}
}
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_that_true_is_true()
{
$this->assertTrue(true);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment