jsonApi.php 9.7 KB

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