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