jsonApi.php 9.6 KB

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