forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenAuthenticateTest.php
More file actions
120 lines (108 loc) · 3.39 KB
/
Copy pathTokenAuthenticateTest.php
File metadata and controls
120 lines (108 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* MultiColumnAuthenticateTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('AuthComponent', 'Controller/Component');
App::uses('TokenAuthenticate', 'Authenticate.Controller/Component/Auth');
App::uses('AppModel', 'Model');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('Controller', 'Controller');
/**
* Test case for FormAuthentication
*
* @package Cake.Test.Case.Controller.Component.Auth
*/
class TokenAuthenticateTest extends CakeTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.authenticate.multi_user'
);
/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Collection = $this->getMock('ComponentCollection');
$this->auth = new TokenAuthenticate($this->Collection, array(
'fields' => array(
'username' => 'user',
'password' => 'password',
'token' => 'token'
),
'userModel' => 'MultiUser',
));
$password = Security::hash('password', null, true);
$User = ClassRegistry::init('MultiUser');
$User->updateAll(array('password' => $User->getDataSource()->value($password)));
$this->response = $this->getMock('CakeResponse');
}
/**
* test authenticate token as query parameter
*
* @return void
*/
public function testAuthenticateTokenParameter() {
$this->auth->settings['_parameter'] = 'token';
$request = new CakeRequest('posts/index?_token=54321');
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);
$expected = array(
'id' => '1',
'user' => 'mariano',
'email' => 'mariano@example.com',
'token' => '12345',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
);
$request = new CakeRequest('posts/index?_token=12345');
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);
$this->auth->settings['parameter'] = 'tokenname';
$request = new CakeRequest('posts/index?tokenname=12345');
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);
}
/**
* test authenticate token as request header
*
* @return void
*/
public function testAuthenticateTokenHeader() {
$_SERVER['HTTP_X_APITOKEN'] = '54321';
$request = new CakeRequest('posts/index', false);
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);
$expected = array(
'id' => '1',
'user' => 'mariano',
'email' => 'mariano@example.com',
'token' => '12345',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
);
$_SERVER['HTTP_X_APITOKEN'] = '12345';
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);
}
}