JsonApiTest.php 11 KB

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