JsonApiTest.php 9.8 KB

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