6

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?

Elwhis
  • 1,241
  • 2
  • 23
  • 45

2 Answers2

1

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

dogmatic69
  • 7,574
  • 4
  • 31
  • 49
  • https://github.com/cakephp/cakephp/blob/26d526f6240239094aa714b86093cd36fbb9a1f3/cake/tests/cases/libs/controller/components/auth.test.php#L561 – dogmatic69 Mar 05 '13 at 14:09
0

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.

JanWillem
  • 352
  • 1
  • 2
  • 10