You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.3 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class TokenTest extends PHPUnit_Framework_TestCase {
  5. private $http;
  6. public function setUp() {
  7. $this->client = new Token();
  8. $this->client->http = new p3k\HTTPTest(dirname(__FILE__).'/data/');
  9. }
  10. private function token($params) {
  11. $request = new Request($params);
  12. $response = new Response();
  13. return $this->client->token($request, $response);
  14. }
  15. public function testMissingURL() {
  16. $response = $this->token([]);
  17. $body = $response->getContent();
  18. $this->assertEquals(400, $response->getStatusCode());
  19. $data = json_decode($body);
  20. $this->assertObjectHasAttribute('error', $data);
  21. $this->assertEquals('invalid_request', $data->error);
  22. }
  23. public function testInvalidURL() {
  24. $url = 'ftp://example.com/foo';
  25. $response = $this->token(['source' => $url, 'code' => '1234']);
  26. $body = $response->getContent();
  27. $this->assertEquals(400, $response->getStatusCode());
  28. $data = json_decode($body);
  29. $this->assertObjectHasAttribute('error', $data);
  30. $this->assertEquals('invalid_url', $data->error);
  31. }
  32. public function testMissingCode() {
  33. $response = $this->token(['source' => 'http://example.com/']);
  34. $body = $response->getContent();
  35. $this->assertEquals(400, $response->getStatusCode());
  36. $data = json_decode($body);
  37. $this->assertObjectHasAttribute('error', $data);
  38. $this->assertEquals('invalid_request', $data->error);
  39. }
  40. public function testNoLinkHeaders() {
  41. $url = 'http://private.example.com/no-link-headers';
  42. $response = $this->token(['source' => $url, 'code' => '1234']);
  43. $body = $response->getContent();
  44. $this->assertEquals(200, $response->getStatusCode());
  45. $data = json_decode($body);
  46. $this->assertObjectHasAttribute('error', $data);
  47. $this->assertEquals('no_token_endpoint', $data->error);
  48. }
  49. public function testNoTokenEndpointOneLinkHeader() {
  50. $url = 'http://private.example.com/no-token-endpoint-one-link-header';
  51. $response = $this->token(['source' => $url, 'code' => '1234']);
  52. $body = $response->getContent();
  53. $this->assertEquals(200, $response->getStatusCode());
  54. $data = json_decode($body);
  55. $this->assertObjectHasAttribute('error', $data);
  56. $this->assertEquals('no_token_endpoint', $data->error);
  57. }
  58. public function testNoTokenEndpointTwoLinkHeaders() {
  59. $url = 'http://private.example.com/no-token-endpoint-two-link-headers';
  60. $response = $this->token(['source' => $url, 'code' => '1234']);
  61. $body = $response->getContent();
  62. $this->assertEquals(200, $response->getStatusCode());
  63. $data = json_decode($body);
  64. $this->assertObjectHasAttribute('error', $data);
  65. $this->assertEquals('no_token_endpoint', $data->error);
  66. }
  67. public function testTokenEndpointInOAuth2Rel() {
  68. $url = 'http://private.example.com/oauth2-token-endpoint';
  69. $response = $this->token(['source' => $url, 'code' => '1234']);
  70. $body = $response->getContent();
  71. $this->assertEquals(200, $response->getStatusCode());
  72. $data = json_decode($body);
  73. $this->assertObjectNotHasAttribute('error', $data);
  74. $this->assertEquals('1234', $data->access_token);
  75. }
  76. public function testTokenEndpointInIndieAuthRel() {
  77. $url = 'http://private.example.com/token-endpoint';
  78. $response = $this->token(['source' => $url, 'code' => '1234']);
  79. $body = $response->getContent();
  80. $this->assertEquals(200, $response->getStatusCode());
  81. $data = json_decode($body);
  82. $this->assertObjectNotHasAttribute('error', $data);
  83. $this->assertEquals('1234', $data->access_token);
  84. }
  85. public function testTokenEndpointWithMultipleRelLinks() {
  86. $url = 'http://private.example.com/multiple-rels';
  87. $response = $this->token(['source' => $url, 'code' => '1234']);
  88. $body = $response->getContent();
  89. $this->assertEquals(200, $response->getStatusCode());
  90. $data = json_decode($body);
  91. $this->assertObjectNotHasAttribute('error', $data);
  92. $this->assertEquals('1234', $data->access_token);
  93. }
  94. public function testBadTokenEndpointResponse() {
  95. $url = 'http://private.example.com/token-endpoint-bad-response';
  96. $response = $this->token(['source' => $url, 'code' => '1234']);
  97. $body = $response->getContent();
  98. $this->assertEquals(200, $response->getStatusCode());
  99. $data = json_decode($body);
  100. $this->assertObjectHasAttribute('error', $data);
  101. $this->assertEquals('this-string-passed-through-from-token-endpoint', $data->error);
  102. }
  103. public function testTokenEndpointTimeout() {
  104. $url = 'http://private.example.com/token-endpoint-timeout';
  105. $response = $this->token(['source' => $url, 'code' => '1234']);
  106. $body = $response->getContent();
  107. $this->assertEquals(200, $response->getStatusCode());
  108. $data = json_decode($body);
  109. $this->assertObjectHasAttribute('error', $data);
  110. $this->assertEquals('timeout', $data->error);
  111. }
  112. public function testTokenEndpointReturnsNotJSON() {
  113. $url = 'http://private.example.com/token-endpoint-notjson';
  114. $response = $this->token(['source' => $url, 'code' => '1234']);
  115. $body = $response->getContent();
  116. $this->assertEquals(200, $response->getStatusCode());
  117. $this->assertEquals('text/plain', $response->headers->get('content-type'));
  118. $this->assertEquals('Invalid request', $body);
  119. }
  120. }