JsonApiTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. use PrivateBin\Controller;
  3. use PrivateBin\Data\Filesystem;
  4. use PrivateBin\Persistence\ServerSalt;
  5. use PrivateBin\Request;
  6. class JsonApiTest extends PHPUnit_Framework_TestCase
  7. {
  8. protected $_model;
  9. protected $_path;
  10. public function setUp()
  11. {
  12. /* Setup Routine */
  13. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  14. $this->_model = Filesystem::getInstance(array('dir' => $this->_path));
  15. ServerSalt::setPath($this->_path);
  16. $_POST = array();
  17. $_GET = array();
  18. $_SERVER = array();
  19. if ($this->_model->exists(Helper::getPasteId())) {
  20. $this->_model->delete(Helper::getPasteId());
  21. }
  22. $options = parse_ini_file(CONF_SAMPLE, true);
  23. $options['purge']['dir'] = $this->_path;
  24. $options['traffic']['dir'] = $this->_path;
  25. $options['model_options']['dir'] = $this->_path;
  26. Helper::confBackup();
  27. Helper::createIniFile(CONF, $options);
  28. }
  29. public function tearDown()
  30. {
  31. /* Tear Down Routine */
  32. unlink(CONF);
  33. Helper::confRestore();
  34. Helper::rmDir($this->_path);
  35. }
  36. /**
  37. * @runInSeparateProcess
  38. */
  39. public function testCreate()
  40. {
  41. $options = parse_ini_file(CONF, true);
  42. $options['traffic']['limit'] = 0;
  43. Helper::createIniFile(CONF, $options);
  44. $_POST = Helper::getPastePost();
  45. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  46. $_SERVER['REQUEST_METHOD'] = 'POST';
  47. $_SERVER['REMOTE_ADDR'] = '::1';
  48. ob_start();
  49. new Controller;
  50. $content = ob_get_contents();
  51. ob_end_clean();
  52. $response = json_decode($content, true);
  53. $this->assertEquals(0, $response['status'], 'outputs status');
  54. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  55. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  56. $paste = $this->_model->read($response['id']);
  57. $this->assertEquals(
  58. hash_hmac('sha256', $response['id'], $paste['meta']['salt']),
  59. $response['deletetoken'],
  60. 'outputs valid delete token'
  61. );
  62. }
  63. /**
  64. * @runInSeparateProcess
  65. */
  66. public function testPut()
  67. {
  68. $options = parse_ini_file(CONF, true);
  69. $options['traffic']['limit'] = 0;
  70. Helper::createIniFile(CONF, $options);
  71. $paste = Helper::getPastePost();
  72. $file = tempnam(sys_get_temp_dir(), 'FOO');
  73. file_put_contents($file, http_build_query($paste));
  74. Request::setInputStream($file);
  75. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  76. $_GET[Helper::getPasteId()] = '';
  77. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  78. $_SERVER['REQUEST_METHOD'] = 'PUT';
  79. $_SERVER['REMOTE_ADDR'] = '::1';
  80. ob_start();
  81. new Controller;
  82. $content = ob_get_contents();
  83. ob_end_clean();
  84. unlink($file);
  85. $response = json_decode($content, true);
  86. $this->assertEquals(0, $response['status'], 'outputs status');
  87. $this->assertEquals(Helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
  88. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  89. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  90. $paste = $this->_model->read($response['id']);
  91. $this->assertEquals(
  92. hash_hmac('sha256', $response['id'], $paste['meta']['salt']),
  93. $response['deletetoken'],
  94. 'outputs valid delete token'
  95. );
  96. }
  97. /**
  98. * @runInSeparateProcess
  99. */
  100. public function testDelete()
  101. {
  102. $this->_model->create(Helper::getPasteId(), Helper::getPaste());
  103. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
  104. $paste = $this->_model->read(Helper::getPasteId());
  105. $file = tempnam(sys_get_temp_dir(), 'FOO');
  106. file_put_contents($file, http_build_query(array(
  107. 'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste['meta']['salt']),
  108. )));
  109. Request::setInputStream($file);
  110. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  111. $_GET[Helper::getPasteId()] = '';
  112. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  113. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  114. ob_start();
  115. new Controller;
  116. $content = ob_get_contents();
  117. ob_end_clean();
  118. unlink($file);
  119. $response = json_decode($content, true);
  120. $this->assertEquals(0, $response['status'], 'outputs status');
  121. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  122. }
  123. /**
  124. * @runInSeparateProcess
  125. */
  126. public function testDeleteWithPost()
  127. {
  128. $this->_model->create(Helper::getPasteId(), Helper::getPaste());
  129. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
  130. $paste = $this->_model->read(Helper::getPasteId());
  131. $_POST = array(
  132. 'pasteid' => Helper::getPasteId(),
  133. 'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste['meta']['salt']),
  134. );
  135. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  136. $_SERVER['REQUEST_METHOD'] = 'POST';
  137. ob_start();
  138. new Controller;
  139. $content = ob_get_contents();
  140. ob_end_clean();
  141. $response = json_decode($content, true);
  142. $this->assertEquals(0, $response['status'], 'outputs status');
  143. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  144. }
  145. /**
  146. * @runInSeparateProcess
  147. */
  148. public function testRead()
  149. {
  150. $paste = Helper::getPaste();
  151. $this->_model->create(Helper::getPasteId(), $paste);
  152. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  153. $_GET[Helper::getPasteId()] = '';
  154. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  155. ob_start();
  156. new Controller;
  157. $content = ob_get_contents();
  158. ob_end_clean();
  159. $response = json_decode($content, true);
  160. $this->assertEquals(0, $response['status'], 'outputs success status');
  161. $this->assertEquals(Helper::getPasteId(), $response['id'], 'outputs data correctly');
  162. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  163. $this->assertEquals($paste['ct'], $response['ct'], 'outputs data correctly');
  164. $this->assertEquals($paste['meta']['created'], $response['meta']['created'], 'outputs postdate correctly');
  165. $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
  166. $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
  167. }
  168. /**
  169. * @runInSeparateProcess
  170. */
  171. public function testJsonLdPaste()
  172. {
  173. $paste = Helper::getPaste();
  174. $this->_model->create(Helper::getPasteId(), $paste);
  175. $_GET['jsonld'] = 'paste';
  176. ob_start();
  177. new Controller;
  178. $content = ob_get_contents();
  179. ob_end_clean();
  180. $this->assertEquals(str_replace(
  181. '?jsonld=',
  182. '/?jsonld=',
  183. file_get_contents(PUBLIC_PATH . '/js/paste.jsonld')
  184. ), $content, 'outputs data correctly');
  185. }
  186. /**
  187. * @runInSeparateProcess
  188. */
  189. public function testJsonLdComment()
  190. {
  191. $paste = Helper::getPaste();
  192. $this->_model->create(Helper::getPasteId(), $paste);
  193. $_GET['jsonld'] = 'comment';
  194. ob_start();
  195. new Controller;
  196. $content = ob_get_contents();
  197. ob_end_clean();
  198. $this->assertEquals(str_replace(
  199. '?jsonld=',
  200. '/?jsonld=',
  201. file_get_contents(PUBLIC_PATH . '/js/comment.jsonld')
  202. ), $content, 'outputs data correctly');
  203. }
  204. /**
  205. * @runInSeparateProcess
  206. */
  207. public function testJsonLdPasteMeta()
  208. {
  209. $paste = Helper::getPaste();
  210. $this->_model->create(Helper::getPasteId(), $paste);
  211. $_GET['jsonld'] = 'pastemeta';
  212. ob_start();
  213. new Controller;
  214. $content = ob_get_contents();
  215. ob_end_clean();
  216. $this->assertEquals(str_replace(
  217. '?jsonld=',
  218. '/?jsonld=',
  219. file_get_contents(PUBLIC_PATH . '/js/pastemeta.jsonld')
  220. ), $content, 'outputs data correctly');
  221. }
  222. /**
  223. * @runInSeparateProcess
  224. */
  225. public function testJsonLdCommentMeta()
  226. {
  227. $paste = Helper::getPaste();
  228. $this->_model->create(Helper::getPasteId(), $paste);
  229. $_GET['jsonld'] = 'commentmeta';
  230. ob_start();
  231. new Controller;
  232. $content = ob_get_contents();
  233. ob_end_clean();
  234. $this->assertEquals(str_replace(
  235. '?jsonld=',
  236. '/?jsonld=',
  237. file_get_contents(PUBLIC_PATH . '/js/commentmeta.jsonld')
  238. ), $content, 'outputs data correctly');
  239. }
  240. /**
  241. * @runInSeparateProcess
  242. */
  243. public function testJsonLdInvalid()
  244. {
  245. $paste = Helper::getPaste();
  246. $this->_model->create(Helper::getPasteId(), $paste);
  247. $_GET['jsonld'] = CONF;
  248. ob_start();
  249. new Controller;
  250. $content = ob_get_contents();
  251. ob_end_clean();
  252. $this->assertEquals('{}', $content, 'does not output nasty data');
  253. }
  254. }