Question

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?

Was it helpful?

Solution

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 :)

OTHER TIPS

Did you also set your custom function, like described in The CakePHP manual:

Normally, the AuthComponent will attempt to verify that the login credentials you've entered are accurate by comparing them to what's been stored in your user model. However, there are times where you might want to do some additional work in determining proper credentials. By setting this variable to one of several different values, you can do different things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top