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.

171 lines
5.7 KiB

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