jsonApi.php 9.9 KB

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