This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
AuthControllerWithZendAuthenticationServiceTest.php
118 lines (96 loc) · 3.69 KB
/
AuthControllerWithZendAuthenticationServiceTest.php
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
<?php
/**
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace ZFTest\OAuth2\Controller;
use Mockery as M;
use Mockery\Loader;
use PDO;
use ReflectionProperty;
use Zend\Stdlib\Parameters;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class AuthControllerWithZendAuthenticationServiceTest extends AbstractHttpControllerTestCase
{
protected $loader;
protected $db;
public function setUp()
{
$this->setApplicationConfig(
include __DIR__ . '/../TestAsset/zend.authenticationservice.application.config.php'
);
$this->loader = new Loader;
$this->loader->register();
parent::setUp();
$this->setupDb();
}
public function setupDb()
{
$pdo = $this->getApplication()->getServiceManager()->get('ZF\OAuth2\Adapter\PdoAdapter');
$r = new ReflectionProperty($pdo, 'db');
$r->setAccessible(true);
$db = $r->getValue($pdo);
$sql = file_get_contents(__DIR__ . '/../TestAsset/database/db_oauth2.sql');
$db->exec($sql);
$this->db = $db;
}
public function getDb()
{
return $this->db;
}
public function getAuthenticationService()
{
$storage = M::mock('Zend\Authentication\Storage\StorageInterface');
$storage->shouldReceive('isEmpty')->once()->andReturn(false);
$storage->shouldReceive('read')->once()->andReturn(123);
$authentication = $this->getApplication()->
getServiceManager()->get('Zend\Authentication\AuthenticationService');
$authentication->setStorage($storage);
return $authentication;
}
public function testAuthorizeCode()
{
$request = $this->getRequest();
$request->setQuery(new Parameters(array(
'response_type' => 'code',
'client_id' => 'testclient',
'state' => 'xyz',
'redirect_uri' => '/oauth/receivecode',
)));
$request->setPost(new Parameters(array(
'authorized' => 'yes',
)));
$request->setMethod('POST');
$this->getAuthenticationService();
$this->dispatch('/oauth/authorize');
$this->assertTrue($this->getResponse()->isRedirect(), var_export($this->getResponse(), 1));
$this->assertControllerName('ZF\OAuth2\Controller\Auth');
$this->assertActionName('authorize');
$location = $this->getResponse()->getHeaders()->get('Location')->getUri();
if (preg_match('#code=([0-9a-f]+)#', $location, $matches)) {
$code = $matches[1];
}
// test data in database is correct
$query = sprintf(
'SELECT * FROM oauth_authorization_codes WHERE authorization_code = \'%s\'',
$code
);
$row = $this->getDb()
->query($query)
->fetch();
$this->assertEquals(null, $row['user_id']);
// test get token from authorized code
$request = $this->getRequest();
$request->getPost()->set('grant_type', 'authorization_code');
$request->getPost()->set('code', $code);
$request->getPost()->set('redirect_uri', '/oauth/receivecode');
$request->getServer()->set('PHP_AUTH_USER', 'testclient');
$request->getServer()->set('PHP_AUTH_PW', 'testpass');
$this->dispatch('/oauth');
$this->assertControllerName('ZF\OAuth2\Controller\Auth');
$this->assertActionName('token');
$this->assertResponseStatusCode(200);
$response = json_decode($this->getResponse()->getContent(), true);
$this->assertNotEmpty($response['access_token']);
}
}