Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ActionsAuthorizeTest file
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Controller.Component.Auth
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ActionsAuthorize', 'Controller/Component/Auth');
20
App::uses('ComponentCollection', 'Controller');
21
App::uses('AclComponent', 'Controller/Component');
22
App::uses('CakeRequest', 'Network');
23
App::uses('CakeResponse', 'Network');
24
 
25
/**
26
 * Class ActionsAuthorizeTest
27
 *
28
 * @package       Cake.Test.Case.Controller.Component.Auth
29
 */
30
class ActionsAuthorizeTest extends CakeTestCase {
31
 
32
/**
33
 * setUp
34
 *
35
 * @return void
36
 */
37
	public function setUp() {
38
		parent::setUp();
39
		$this->controller = $this->getMock('Controller', array(), array(), '', false);
40
		$this->Acl = $this->getMock('AclComponent', array(), array(), '', false);
41
		$this->Collection = $this->getMock('ComponentCollection');
42
 
43
		$this->auth = new ActionsAuthorize($this->Collection);
44
		$this->auth->settings['actionPath'] = '/controllers';
45
	}
46
 
47
/**
48
 * setup the mock acl.
49
 *
50
 * @return void
51
 */
52
	protected function _mockAcl() {
53
		$this->Collection->expects($this->any())
54
			->method('load')
55
			->with('Acl')
56
			->will($this->returnValue($this->Acl));
57
	}
58
 
59
/**
60
 * test failure
61
 *
62
 * @return void
63
 */
64
	public function testAuthorizeFailure() {
65
		$user = array(
66
			'User' => array(
67
				'id' => 1,
68
				'user' => 'mariano'
69
			)
70
		);
71
		$request = new CakeRequest('/posts/index', false);
72
		$request->addParams(array(
73
			'plugin' => null,
74
			'controller' => 'posts',
75
			'action' => 'index'
76
		));
77
 
78
		$this->_mockAcl();
79
 
80
		$this->Acl->expects($this->once())
81
			->method('check')
82
			->with($user, 'controllers/Posts/index')
83
			->will($this->returnValue(false));
84
 
85
		$this->assertFalse($this->auth->authorize($user['User'], $request));
86
	}
87
 
88
/**
89
 * test isAuthorized working.
90
 *
91
 * @return void
92
 */
93
	public function testAuthorizeSuccess() {
94
		$user = array(
95
			'User' => array(
96
				'id' => 1,
97
				'user' => 'mariano'
98
			)
99
		);
100
		$request = new CakeRequest('/posts/index', false);
101
		$request->addParams(array(
102
			'plugin' => null,
103
			'controller' => 'posts',
104
			'action' => 'index'
105
		));
106
 
107
		$this->_mockAcl();
108
 
109
		$this->Acl->expects($this->once())
110
			->method('check')
111
			->with($user, 'controllers/Posts/index')
112
			->will($this->returnValue(true));
113
 
114
		$this->assertTrue($this->auth->authorize($user['User'], $request));
115
	}
116
 
117
/**
118
 * testAuthorizeSettings
119
 *
120
 * @return void
121
 */
122
	public function testAuthorizeSettings() {
123
		$request = new CakeRequest('/posts/index', false);
124
		$request->addParams(array(
125
			'plugin' => null,
126
			'controller' => 'posts',
127
			'action' => 'index'
128
		));
129
 
130
		$this->_mockAcl();
131
 
132
		$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
133
		$user = array(
134
			'id' => 1,
135
			'user' => 'mariano'
136
		);
137
 
138
		$expected = array('TestPlugin.TestPluginAuthUser' => array('id' => 1, 'user' => 'mariano'));
139
		$this->Acl->expects($this->once())
140
			->method('check')
141
			->with($expected, 'controllers/Posts/index')
142
			->will($this->returnValue(true));
143
 
144
		$this->assertTrue($this->auth->authorize($user, $request));
145
	}
146
 
147
/**
148
 * test action()
149
 *
150
 * @return void
151
 */
152
	public function testActionMethod() {
153
		$request = new CakeRequest('/posts/index', false);
154
		$request->addParams(array(
155
			'plugin' => null,
156
			'controller' => 'posts',
157
			'action' => 'index'
158
		));
159
 
160
		$result = $this->auth->action($request);
161
		$this->assertEquals('controllers/Posts/index', $result);
162
	}
163
 
164
/**
165
 * Make sure that action() doesn't create double slashes anywhere.
166
 *
167
 * @return void
168
 */
169
	public function testActionNoDoubleSlash() {
170
		$this->auth->settings['actionPath'] = '/controllers/';
171
		$request = new CakeRequest('/posts/index', false);
172
		$request->addParams(array(
173
			'plugin' => null,
174
			'controller' => 'posts',
175
			'action' => 'index'
176
		));
177
		$result = $this->auth->action($request);
178
		$this->assertEquals('controllers/Posts/index', $result);
179
	}
180
 
181
/**
182
 * test action() and plugins
183
 *
184
 * @return void
185
 */
186
	public function testActionWithPlugin() {
187
		$request = new CakeRequest('/debug_kit/posts/index', false);
188
		$request->addParams(array(
189
			'plugin' => 'debug_kit',
190
			'controller' => 'posts',
191
			'action' => 'index'
192
		));
193
 
194
		$result = $this->auth->action($request);
195
		$this->assertEquals('controllers/DebugKit/Posts/index', $result);
196
	}
197
}