Assert status in Laravel Dusk - laravel

I have a test which verify status. It should be 200
When I run this
public function testBasicTest()
{
$response = $this->json('GET', '/');
$response->assertStatus(200);
}
It pass but when I add url: /clients
always status 404
public function testBasicTest()
{
$response = $this->json('GET', '/clients');
$response->assertStatus(200);
}
Error:
Expected status code 200 but received 404.
Failed asserting that false is true.

change your route.php like below. I added `/clients' route in ti.
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/clients', function () {
return view('welcome');
});

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});

Related

How to access data of rate limiters defined in RouteServiceProvider from inside any controller?

In my app/Providers/RouteServiceProvider.php I defined a rate limiter:
protected function configureRateLimiting()
{
RateLimiter::for('auth', function (Request $request) {
return Limit::perMinute(10)->by($request->ip());
});
}
I added this as middleware to my route. Now, in my custom controller I would like to read the remaining attempts. How do I get this info? I tried using RateLimiter::limiter but that returns the callback defined in the for method.
web.php routes:
Route::group(['as' => 'auth.',], function () {
Route::get('login', [AuthController::class, 'index'])->name('login');
Route::post('authenticate', [AuthController::class, 'authenticate'])->name('authenticate')->middleware(['throttle:auth']);
});
Function in my AuthController:
public function authenticate(Request $request)
{
dd( RateLimiter::limiter('auth')->remaining() ); // callback error
dd( RateLimiter::remaining('auth') ); // error because I'd have to hardcode the max attempts here
}

laravel showing 1 after i login

When I log in Laravel, first thing that shows is 1 then it redirects me to my intended page. I have checked everywhere, there is no middleware which makes 1 to show up. I don't get what is the problem.
This is my login code:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
class LoginController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated()
{
return redirect('admin/dashboard');
}
public function showLoginForm()
{
return view('admin.login');
}
protected function validateLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required'
]);
}
public function logout()
{
session::flush();
auth::logout();
return back();
}
}
These are my routes
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
Auth::routes();
// Route::get('/home', 'HomeController#index')->name('home');
Route::post('/login', 'Auth\LoginController#attemptLogin')->name('login');
Route::middleware('auth')->prefix('/admin')->name('admin.')->group(function() {
Route::get('/dashboard', 'DashboardController#index')->name('dashboard');
Route::post('/logout', 'Auth\LoginController#logout')->name('logout');
});
There is not much in my routes as I just started to build my site and I am facing this error.

Non-static method Illuminate\Routing\Route::middleware() should not be called statically

I have encountered this error in this file : route/api.php
This error refers to line 16
<?php
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
16- Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request::user();
});
Route::post('login', 'Api\AuthController#login');
Problem solved by changing this line
from
use Illuminate\Routing\Route;
to
use Illuminate\Support\Facades\Route;
Since You've not defined in Your question what is error message I can only predict that You're using middleware incorrectly.
Middleware should be assigned to group.
Route::middleware('auth:api')->group(function() {
Router::get('/user', function (Request $request) {
return $request::user();
});
});
or You've to use middleware after defining the route handler:
Route::get('/user', function(Request $request) {
return $request::user();
})->middleware('auth:api');
Because the method middleware is not static method, according to the assigning-middleware-to-routes try it like this:
Route::get('/user', function (Request $request) {
return $request::user();
})->middleware('auth:api');

Redirect issue in middleware for admin login in Laravel

When login to admin,then page is redirect But
'The page isn't redirecting properly' message show. Please help me
Middleware - IsAdmin.php
public function handle($request, Closure $next)
{
if (Auth::user()->role_id == 1)
{
return redirect ('/administrator');
}
return $next($request);
}
web.php
Route::group(['prefix'=>'administrator','as' => 'admin.','middleware' => ['auth'],'middleware' => 'isAdmin'], function(){
Route::get('/','AdminController#index');
Route::resource('user','AdminController');
Route::resource('card','AdminCardController');
});
Thanx in advanced..
Make sure you register your middlware in the karnel and try the following code:
Route::prefix('admin')->group(function () {
Route::middleware(['auth', 'IsAdmin'])->group(function () {
// Your routes
});
});

Laravel route group middleware issue

I keep some laravel routes in the middleware auth group as:
Route::group(['middleware'=>'auth'],function(){
Route::controller('Activities', 'ActivitiesController');
Route::get('foo','FooController#getFoo');
.....
});
When I try to login to access these page, I am unable to login and url redirect to login page again and again. But If I use constructor as:
public function __construct()
{
$this->middleware('auth');
}
In those controllers It works perfectly. What is route group problem?
Route has a ::middleware class that you can use:
Routes > web.php
Route::middleware(['auth'])->group(function(){
Route::get('/activities', 'ActivitiesController#index');
});
You can also use Route::resource(); which I prefer. If you don't know what it does, here are the docs: https://laravel.com/docs/5.8/controllers#resource-controllers
This works for me , in route
Route::group(['middleware'=>'auth'],function(){
Route::controller('activities', 'ActivitiesController');
});
then controller
<?php namespace App\Http\Controllers;
class ActivitiesController extends Controller {
public function getIndex() {
return 'you are in;
}
}
on attempt to visit /activities I was redirected to login page , and on success back to \activities with 'you are in'.
In web.php:
$roleGeneral = role1.'~'.role2.'~'.role3.'~'.role4;
Route::group(['middleware' => ['permission.role:'.$roleGeneral]], function() {})
In Kernel.php:
protected $routeMiddleware = [...,
'permission.role' => \App\Http\Middleware\CheckPermission::class,
];
In CheckPermission.php:
public function handle($request, Closure $next, $role)
{
$roleArr = explode('~', $role);
$token = JWTAuth::getToken();
$user = JWTAuth::toUser($token);
$roleLogin = SysRoleModel::where('id', $user->role_id)->first();
if (in_array($roleLogin['name'], $roleArr)){
return $next($request);
}else{
return \Redirect::back()->withMessage('You are not authorized to access!');
}
}

Resources