Skip to content

Commit

Permalink
Merge pull request #345 from DnD-Montreal/240-Implement-performance-t…
Browse files Browse the repository at this point in the history
…ests

Implement performance tests
  • Loading branch information
m-triassi authored Jan 30, 2022
2 parents 68dfd5a + bbb5c53 commit 9d00f14
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 14 deletions.
41 changes: 41 additions & 0 deletions app/Http/Controllers/LocustAuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Character;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\App;


use Laravel\Sanctum\HasApiTokens;

class LocustAuthController extends Controller
{
use HasApiTokens;

public function getToken(Request $request)
{
if ($request->bearerToken() == config('app.key') && App::environment('load')) {
$user = User::where('password', 'DOESNTMATTER')->get();
$token = $user->first()->createToken('bearer')->plainTextToken;

return [
'token' => $token
];
}
}

public function deleteCharacters()
{
if (App::environment('load')) {
$user = User::where('password', 'DOESNTMATTER')->get();

if ($user->first()->email == '[email protected]') {
$characters = Character::where('user_id', $user->first()->id);
$characters->delete();
}
}
}
}
3 changes: 2 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
//'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'throttle' => \App\Http\Middleware\Throttle::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}
27 changes: 27 additions & 0 deletions app/Http/Middleware/Throttle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\App;

class Throttle extends ThrottleRequests
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
{
if (App::environment('load')) {
return $next($request);
} else {
return parent::handle($request, $next, $maxAttempts, $decayMinutes, $prefix);
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"guzzlehttp/guzzle": "^7.0.1",
"inertiajs/inertia-laravel": "^0.4.0",
"laravel/framework": "^8.54",
"laravel/sanctum": "^2.6",
"laravel/sanctum": "^2.14",
"laravel/tinker": "^2.5",
"lorisleiva/laravel-actions": "^2.1",
"tightenco/ziggy": "^1.0"
Expand Down
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LocustAuthController;

/*
|--------------------------------------------------------------------------
Expand All @@ -13,7 +14,11 @@
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('/locust', [App\Http\Controllers\LocustAuthController::class, 'getToken']);

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
Route::group(['middleware' => ['auth:sanctum']], function () {
Route::delete('/locust', [App\Http\Controllers\LocustAuthController::class, 'deleteCharacters']);

Route::post('/character', [App\Http\Controllers\CharacterController::class, 'store'])
->name("character.store");
});
42 changes: 42 additions & 0 deletions tests/Feature/Http/Controllers/LocustAuthControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Http\Controllers;

use Tests\TestCase;

class LocustAuthControllerTest extends TestCase
{
/**
* @test
*/
public function test_get_token()
{
$token = config('app.key');

app()->detectEnvironment(function () {
return 'load';
});

$this->withHeaders(['Authorization' => 'Bearer '.$token])->get(route('character.index'));
$response = $this->withHeaders(['Authorization' => 'Bearer '.$token])->get('api/locust');

$response->assertStatus(200);
}

/**
* @test
*/
public function test_delete_characters_with_locust_api_key()
{
$token = config('app.key');

app()->detectEnvironment(function () {
return 'load';
});

$this->withHeaders(['Authorization' => 'Bearer '.$token])->get(route('character.index'));
$response = $this->withHeaders(['Authorization' => 'Bearer '.$token])->delete('api/locust');

$response->assertStatus(200);
}
}

0 comments on commit 9d00f14

Please sign in to comment.