Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp testing login

I want to test login function if it works propperly and only lets valid and active users in.

My user fixture contains:

array(
        'password' => '*emptyPasswordHash*', // empty password
        'username' => 'Lorem',
        'balance' => 0,
        'currency' => 'USD',
        'id' => 1,
        'user_group_id' => 3, //Customer
        'active' => 1,
        'hash' => 'LoremHash'
    ),

My test function looks like this:

function testLogin() {
            //test valid login
            $result = $this->testAction('/users/login', array(
                'data' => array(
                    'User' => array(
                        'username' => 'Lorem',
                        'pass' => '',
                        'remember' => true
                )),
                'method' => 'post',
                'return' => 'view'
            ));

            debug($result);

}

The login form has 3 inputs: username, password and remember

I have set $this->Auth->autoRedirect = false; in the UsersController::beforeFilter and I am doing some cookie setting stuff

when I debug($this->data); in UsersController::login() it shows the exact same data when testing and when logging normaly. But while testing the login fails and I get $this->Auth->loginError message instead of a login.

How do I test login action propperly?

like image 581
Elwhis Avatar asked Mar 29 '11 02:03

Elwhis


People also ask

How do I debug a single unit test in Visual Studio?

To start debugging: In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Because test methods can run in any order, set breakpoints in all the test methods that you want to debug. In Test Explorer, select the test method(s) and then choose Debug on the right-click menu.


1 Answers

if you use cakes Auth component and dont hack it up you dont need to...

https://github.com/cakephp/cakephp/blob/master/cake/tests/cases/libs/controller/components/auth.test.php#L545

and if you really want to, look at how the pro's do it :)

like image 61
dogmatic69 Avatar answered Sep 30 '22 01:09

dogmatic69