JsonApiTest.php 10 KB

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