jsonApi.php 9.5 KB

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