Saturday, August 20, 2011

Testing Authentication/Authorization in 2.0

Hi, 

I have a UsersController, and I've configured AuthComponent to use 'Controller' authorization mechanism. I also setup everything to allow just the index action and deny everything else. It works just fine when I go through the browser: ie, I can see the list of users and I get redirected to the login page when I try to add a new one. 

What I want to do is write a test case that verifies that the add action requires a user to be logged in (the current behavior). I have the following partial testing code: 

public testAuthentication {        
    $data = array ( 'User' => array ( 'email' => 'test@email.com', 'password' => 'testPwd'));
    $this->Users = $this->generate('Users', array('components' => array('Session', 'Auth')));
    $result      = $this->testAction(
            '/users/add',
            array(
             'method' => 'post',
             'return' => 'result',
             'data'   => $data,
            )
        );

        $result      = $this->Users->User->read();
}

I added logs in the isAuthorized() method of the UserController, and it's not called by my test case (so the test case is able to add a new user). It's only called when I run this through the browser. 

Is what I'm trying to test supported? Am I missing something? 

Thanks,
-Javier

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: