|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class OAuth1ProviderTestAbstract |
| 4 | + * |
| 5 | + * @filesource OAuth1ProviderTestAbstract.php |
| 6 | + * @created 09.09.2018 |
| 7 | + * @package chillerlan\OAuthTest\Providers |
| 8 | + * @author smiley <smiley@chillerlan.net> |
| 9 | + * @copyright 2018 smiley |
| 10 | + * @license MIT |
| 11 | + */ |
| 12 | + |
| 13 | +namespace chillerlan\OAuthTest\Providers; |
| 14 | + |
| 15 | +use chillerlan\HTTP\Psr7\{Request, Response}; |
| 16 | +use chillerlan\HTTP\Psr17; |
| 17 | +use chillerlan\OAuth\Core\{AccessToken, OAuth1Interface}; |
| 18 | + |
| 19 | +/** |
| 20 | + * @property \chillerlan\OAuth\Core\OAuth1Interface $provider |
| 21 | + */ |
| 22 | +abstract class OAuth1ProviderTestAbstract extends ProviderTestAbstract{ |
| 23 | + |
| 24 | + protected $responses = [ |
| 25 | + '/oauth1/request_token' => 'oauth_token=test_request_token&oauth_token_secret=test_request_token_secret&oauth_callback_confirmed=true', |
| 26 | + '/oauth1/access_token' => 'oauth_token=test_access_token&oauth_token_secret=test_access_token_secret&oauth_callback_confirmed=true', |
| 27 | + '/oauth1/api/request' => '{"data":"such data! much wow!"}', |
| 28 | + ]; |
| 29 | + |
| 30 | + protected function setUp(){ |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->setProperty($this->provider, 'requestTokenURL', 'https://localhost/oauth1/request_token'); |
| 34 | + $this->setProperty($this->provider, 'accessTokenURL', 'https://localhost/oauth1/access_token'); |
| 35 | + $this->setProperty($this->provider, 'apiURL', 'https://localhost/oauth1/api/request'); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public function testOAuth1Instance(){ |
| 40 | + $this->assertInstanceOf(OAuth1Interface::class, $this->provider); |
| 41 | + } |
| 42 | + |
| 43 | + public function testGetAuthURL(){ |
| 44 | + parse_str(parse_url($this->provider->getAuthURL(), PHP_URL_QUERY), $query); |
| 45 | + |
| 46 | + $this->assertSame('test_request_token', $query['oauth_token']); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetSignature(){ |
| 50 | + $signature = $this |
| 51 | + ->getMethod('getSignature') |
| 52 | + ->invokeArgs($this->provider, ['http://localhost/api/whatever', ['foo' => 'bar', 'oauth_signature' => 'should not see me!'], 'GET']); |
| 53 | + |
| 54 | + $this->assertSame('ygg22quLhpyegiyr7yl4hLAP9S8=', $signature); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @expectedException \chillerlan\OAuth\Core\ProviderException |
| 59 | + * @expectedExceptionMessage getSignature: invalid url |
| 60 | + */ |
| 61 | + public function testGetSignatureInvalidURLException(){ |
| 62 | + $this |
| 63 | + ->getMethod('getSignature') |
| 64 | + ->invokeArgs($this->provider, ['whatever', [], 'GET']); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGetAccessToken(){ |
| 68 | + $token = new AccessToken(['accessTokenSecret' => 'test_request_token_secret']); |
| 69 | + $this->storage->storeAccessToken($this->provider->serviceName, $token); |
| 70 | + |
| 71 | + $token = $this->provider->getAccessToken('test_request_token', 'verifier'); |
| 72 | + |
| 73 | + $this->assertSame('test_access_token', $token->accessToken); |
| 74 | + $this->assertSame('test_access_token_secret', $token->accessTokenSecret); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @expectedException \chillerlan\OAuth\Core\ProviderException |
| 79 | + * @expectedExceptionMessage unable to parse token response |
| 80 | + */ |
| 81 | + public function testParseTokenResponseNoData(){ |
| 82 | + $this->getMethod('parseTokenResponse')->invokeArgs($this->provider, [new Response]); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @expectedException \chillerlan\OAuth\Core\ProviderException |
| 87 | + * @expectedExceptionMessage error retrieving access token |
| 88 | + */ |
| 89 | + public function testParseTokenResponseError(){ |
| 90 | + $this |
| 91 | + ->getMethod('parseTokenResponse') |
| 92 | + ->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('error=whatever'))]) |
| 93 | + ; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @expectedException \chillerlan\OAuth\Core\ProviderException |
| 98 | + * @expectedExceptionMessage invalid token |
| 99 | + */ |
| 100 | + public function testParseTokenResponseNoToken(){ |
| 101 | + $this |
| 102 | + ->getMethod('parseTokenResponse') |
| 103 | + ->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('oauth_token=whatever'))]) |
| 104 | + ; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @expectedException \chillerlan\OAuth\Core\ProviderException |
| 109 | + * @expectedExceptionMessage oauth callback unconfirmed |
| 110 | + */ |
| 111 | + public function testParseTokenResponseCallbackUnconfirmed(){ |
| 112 | + $this |
| 113 | + ->getMethod('parseTokenResponse') |
| 114 | + ->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('oauth_token=whatever&oauth_token_secret=whatever_secret')), true]) |
| 115 | + ; |
| 116 | + } |
| 117 | + |
| 118 | + public function testGetRequestAuthorization(){ |
| 119 | + |
| 120 | + $authHeader = $this->provider |
| 121 | + ->getRequestAuthorization( |
| 122 | + new Request('GET', 'https://foo.bar'), |
| 123 | + new AccessToken(['accessTokenSecret' => 'test_token_secret', 'accessToken' => 'test_token']) |
| 124 | + ) |
| 125 | + ->getHeaderLine('Authorization'); |
| 126 | + |
| 127 | + $this->assertContains('OAuth oauth_consumer_key="'.$this->options->key.'"', $authHeader); |
| 128 | + $this->assertContains('oauth_token="test_token"', $authHeader); |
| 129 | + } |
| 130 | + |
| 131 | + public function testRequest(){ |
| 132 | + $token = new AccessToken(['accessTokenSecret' => 'test_request_token_secret']); |
| 133 | + $this->storage->storeAccessToken($this->provider->serviceName, $token); |
| 134 | + |
| 135 | + $this->assertSame('such data! much wow!', json_decode($this->provider->request('')->getBody()->getContents())->data); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments