Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing filters in laravel 4

I'm new to laravel. I am trying to test that authentication works for my website and I want to test the authentication process in a test case. I create a in-memory sqlite database, I create a new User and use ->save() method of eloquent to store it in the database. I have setup an authentication filter which checks for the username in the database and depending on that it either allows the user to login or returns "invalid credentials"

// my UserTest.php file :

class UserTest extends TestCase {

public function testUsernameIsNotRequired()
{
    // Create a new User
    $user = new User;
    $user->username = "[email protected]";
    $user->password = "123456";
    //$user->password_confirmation = "password";

    // User should save
    $this->assertTrue($user->save());

    // Save the errors
    $password = $user->getAuthPassword();

    // There should be 0 error
    $this->assertEquals("123456",$password);
    $this->seed();

    $this->be($user);

    $this->assertTrue(Redirect::route('authFilter'));
}
}

just to let you know that the in-memory db is lost once the test is complete as all the connections to it are lost so I want to check that the user that I saved to my db is properly inserted and second I want to check if I can login to my website using the information of this new user.

// my filters.php file :

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});


Route::filter('auth.basic', function()
{
    return Auth::basic('username');
});

Route::filter('guest', function()
{
    if (Auth::check()) return Redirect::to('/');
});

Route::filter('csrf', function()
{
    if (Session::token() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

I was trying to attach a filter to a route so that I can redirect to the route during my test and which calls the auth.basic filter so I can test my filter, I know Im doing a lot of things wrong so please feel free to correct any mistakes that you come accross

my routes.php file :>

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('/authtest', array('as'=>'/authtest','before' => 'auth.basic', function()
{
     return View::make('hello');
}));

Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
    Route::resource('url', 'UrlController');
});

Route::get('authFilter', array('as'=>'authFilter','before' => 'auth.basic', function()
{
    return Auth::basic('username');
}));

I also have a uri controller which has all the pages for my website

this is what I followed to create my uri controller for the moment

I need a test case that creates a user stores it into the in-memory database and then authenticates using that users information. If any one knows laravel testing for filters please let me know I looked up the documentation for testing filters but I guess it is not well documented.

thank you

like image 624
tanzeelrana Avatar asked Jul 03 '13 21:07

tanzeelrana


People also ask

What is filters in Laravel?

Advanced Laravel Model Filters is a package that gives you fine-grained control and reusable classes you can use to filter Eloquent models. The readme has an example where perhaps you'd like to filter by the name column: 1$filters = EloquentFilters::make([ 2 new NameFilter($request->name) 3]);


2 Answers

Filters are disabled in tests on Laravel 4.

You can use Route::enableFilters() in your test to force the filters on.

You can read up on the various discussions about why/why not to test filters;

  • https://github.com/laravel/framework/issues/344
  • https://github.com/laravel/framework/issues/766
  • http://forums.laravel.io/viewtopic.php?id=7404
  • https://github.com/laravel/framework/issues/682
like image 172
Laurence Avatar answered Nov 15 '22 23:11

Laurence


You can keep most of this in the unit test.

public function testMyFilter() {
 // Set up route with filter.                                                
 Route::get('testFilter', ['before' => 'myfilter',  function()   
 {                                                                  
         return 'Hello World';                                      
 }]);
 // Enable filters.
 Route::enableFilters(); 

 // Be a user.
 $this->be($this->user);

 // Make a request that will use the filter.                                   
 $response = $this->client->request('GET', 'testFilter'); 

 // Check the response.
 $this->assertResponseOk(); 
}
like image 45
Jeremy French Avatar answered Nov 15 '22 21:11

Jeremy French