1 | <?php |
---|
2 | define('CONFIG_REFRESH_LIFETIME', '30'); |
---|
3 | define('ROOTPATH', __DIR__ . '/../../../'); |
---|
4 | |
---|
5 | #require_once(__DIR__ . '/../../../library/oauth2/lib/OAuth2.php'); |
---|
6 | #require_once(__DIR__ . '/../../../library/oauth2/lib/IOAuth2Storage.php'); |
---|
7 | #require_once(__DIR__ . '/../../../library/oauth2/lib/IOAuth2RefreshTokens.php'); |
---|
8 | require_once(__DIR__ . '/../../../api/controller.php'); |
---|
9 | |
---|
10 | require_once(__DIR__ . '/../../../rest/oauth/OAuth2StorageUserCredential.php'); |
---|
11 | |
---|
12 | /*Classe de teste para autenticação do tipo password(Resource Owner User Credentials)*/ |
---|
13 | class OAuth2StorageUserCredentialTest extends PHPUnit_Framework_TestCase { |
---|
14 | |
---|
15 | private $class = null; |
---|
16 | private $client_id = 666; |
---|
17 | private $user_id = 666; |
---|
18 | private $client_secret = 'secret123'; |
---|
19 | private $username = 'user2'; |
---|
20 | private $password = 'prognus'; |
---|
21 | private $accessToken = 'aaaaaa259f553ac148f01b6bbcbb101'; |
---|
22 | private $refreshToken = 'rrrrrr03fff3b8cdc51206244529abd'; |
---|
23 | private $grant_type = 'password'; |
---|
24 | |
---|
25 | private $test_token = 'ac148a8526008422f01ac148a8526008422f01'; |
---|
26 | |
---|
27 | //configura classe para testes |
---|
28 | public function setUp() { |
---|
29 | $this->class = new OAuth2StorageUserCredential(); |
---|
30 | |
---|
31 | //insere access token |
---|
32 | $this->class->setAccessToken($this->accessToken, $this->client_id, $this->user_id, time(), 'all', $this->refreshToken); |
---|
33 | |
---|
34 | //insere refresh token |
---|
35 | $this->class->setRefreshToken($this->refreshToken, $this->client_id, $this->user_id, time(), 'all'); |
---|
36 | |
---|
37 | } |
---|
38 | //finaliza classe |
---|
39 | public function tearDown() { |
---|
40 | $this->class->unsetRefreshToken($this->refreshToken); |
---|
41 | $this->class->unsetAccessToken($this->accessToken); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /* |
---|
46 | * verifica credenciais do usuario |
---|
47 | * credenciais devem estar no LDAP |
---|
48 | */ |
---|
49 | //(login correto) |
---|
50 | public function testCheckUserCredentials() { |
---|
51 | $res = $this->class->checkUserCredentials(1, $this->username, $this->password); |
---|
52 | $check = (!empty($res))? true : false; |
---|
53 | $this->assertTrue($check); |
---|
54 | } |
---|
55 | //login errado |
---|
56 | public function testCheckUserCredentialsFalse() { |
---|
57 | $this->assertFalse($this->class->checkUserCredentials(1, 'blablabla', 'nonono')); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | /* |
---|
62 | * Verifica se as credencias do client são válidas |
---|
63 | * as credencias devem estar no banco de dados para o funcionamento do test |
---|
64 | */ |
---|
65 | //login correto |
---|
66 | public function testCheckClientCredentials() { |
---|
67 | $this->assertTrue($this->class->checkClientCredentials($this->client_id, $this->client_secret)); |
---|
68 | } |
---|
69 | //login errado |
---|
70 | public function testCheckClientCredentialsFalse() { |
---|
71 | $this->assertFalse($this->class->checkClientCredentials(1, '1')); |
---|
72 | } |
---|
73 | //caso não passe o client_secret |
---|
74 | public function testCheckClientCredentialsWithoutClientSecret() { |
---|
75 | $this->assertFalse($this->class->checkClientCredentials(1, null)); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | /*busca detalhes do cliente |
---|
80 | * de acordo com a rfc apenas retona a uri de retorno cadastrada (registed redirect uri) |
---|
81 | */ |
---|
82 | //sucesso (o retorno sera uma string) |
---|
83 | public function testGetClientDetails() { |
---|
84 | $res = $this->class->getClientDetails($this->client_id); |
---|
85 | $this->assertTrue(is_string($res)); |
---|
86 | } |
---|
87 | //falha (o retorno eh falso) |
---|
88 | public function testGetClientDetailsFalse() { |
---|
89 | $res = $this->class->getClientDetails(-1); |
---|
90 | $this->assertFalse($res); |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * Busca as informações de um token de acesso (AccessToken) |
---|
95 | * |
---|
96 | */ |
---|
97 | //sucesso retorna um array com as chaves 'token', 'client', 'user', 'expires', 'scope' |
---|
98 | public function testGetAccessToken() { |
---|
99 | $res = $this->class->getAccessToken($this->accessToken); |
---|
100 | |
---|
101 | if ( isset($res['refresh_token']) && $res['client_id'] && $res['user_id'] && $res['expires'] && $res['scope']) { |
---|
102 | $res = true; |
---|
103 | } |
---|
104 | else { |
---|
105 | $res = false; |
---|
106 | } |
---|
107 | $this->assertTrue($res); |
---|
108 | } |
---|
109 | //falha token não existe |
---|
110 | public function testGetAccessTokenFalse() { |
---|
111 | $this->assertFalse($this->class->getAccessToken(-1)); |
---|
112 | } |
---|
113 | |
---|
114 | /* |
---|
115 | * Busca as informações de um token de refresh (RefreshToken) |
---|
116 | * |
---|
117 | */ |
---|
118 | //sucesso retorna um array com as chaves 'token', 'client', 'user', 'expires', 'scope' |
---|
119 | public function testGetRefreshToken() { |
---|
120 | $res = $this->class->getRefreshToken($this->refreshToken); |
---|
121 | |
---|
122 | if ( isset($res['refresh_token']) && $res['client_id'] && $res['user_id'] && $res['expires'] && $res['scope']) { |
---|
123 | $res = true; |
---|
124 | } |
---|
125 | else { |
---|
126 | $res = false; |
---|
127 | } |
---|
128 | $this->assertTrue($res); |
---|
129 | } |
---|
130 | //falha token não existe |
---|
131 | public function testGetRefreshTokenFalse() { |
---|
132 | $this->assertFalse($this->class->getRefreshToken(-1)); |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | * Verifica se client pode autenticar via password |
---|
137 | */ |
---|
138 | //deve retornar true |
---|
139 | public function testcheckRestrictedGrantType() { |
---|
140 | $this->assertTrue($this->class->checkRestrictedGrantType($this->client_id, $this->grant_type)); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | ?> |
---|