YourlsProxyTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. use PrivateBin\Configuration;
  3. use PrivateBin\YourlsProxy;
  4. class YourlsProxyTest extends PHPUnit_Framework_TestCase
  5. {
  6. private $_conf;
  7. private $_path;
  8. private $_mock_yourls_service;
  9. public function setUp()
  10. {
  11. /* Setup Routine */
  12. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  13. if (!is_dir($this->_path)) {
  14. mkdir($this->_path);
  15. }
  16. $this->_mock_yourls_service = $this->_path . DIRECTORY_SEPARATOR . 'yourls.json';
  17. $options = parse_ini_file(CONF_SAMPLE, true);
  18. $options['main']['basepath'] = 'https://example.com';
  19. $options['main']['urlshortener'] = 'https://example.com/shortenviayourls?link=';
  20. $options['yourls']['apiurl'] = $this->_mock_yourls_service;
  21. Helper::confBackup();
  22. Helper::createIniFile(CONF, $options);
  23. $this->_conf = new Configuration;
  24. }
  25. public function tearDown()
  26. {
  27. /* Tear Down Routine */
  28. unlink(CONF);
  29. Helper::confRestore();
  30. Helper::rmDir($this->_path);
  31. }
  32. public function testYourlsProxy()
  33. {
  34. // the real service answer is more complex, but we only look for the shorturl & statusCode
  35. file_put_contents($this->_mock_yourls_service, '{"shorturl":"https:\/\/example.com\/1","statusCode":200}');
  36. $yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
  37. $this->assertFalse($yourls->isError());
  38. $this->assertEquals($yourls->getUrl(), 'https://example.com/1');
  39. }
  40. public function testForeignUrl()
  41. {
  42. $yourls = new YourlsProxy($this->_conf, 'https://other.example.com/?foo#bar');
  43. $this->assertTrue($yourls->isError());
  44. $this->assertEquals($yourls->getError(), 'Trying to shorten a URL that isn\'t pointing at our instance.');
  45. }
  46. public function testYourlsError()
  47. {
  48. // when statusCode is not 200, shorturl may not have been set
  49. file_put_contents($this->_mock_yourls_service, '{"statusCode":403}');
  50. $yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
  51. $this->assertTrue($yourls->isError());
  52. $this->assertEquals($yourls->getError(), 'Error parsing YOURLS response.');
  53. }
  54. public function testServerError()
  55. {
  56. // simulate some other server error that results in a non-JSON reply
  57. file_put_contents($this->_mock_yourls_service, '');
  58. $yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
  59. $this->assertTrue($yourls->isError());
  60. $this->assertEquals($yourls->getError(), 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".');
  61. }
  62. }