JsonApiTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $response = json_decode($content, true);
  94. $this->assertEquals(0, $response['status'], 'outputs status');
  95. $this->assertEquals(Helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
  96. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  97. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  98. $paste = $this->_model->read($response['id']);
  99. $this->assertEquals(
  100. hash_hmac('sha256', $response['id'], $paste->meta->salt),
  101. $response['deletetoken'],
  102. 'outputs valid delete token'
  103. );
  104. }
  105. /**
  106. * @runInSeparateProcess
  107. */
  108. public function testDelete()
  109. {
  110. $this->reset();
  111. $this->_model->create(Helper::getPasteId(), Helper::getPaste());
  112. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
  113. $paste = $this->_model->read(Helper::getPasteId());
  114. $file = tempnam(sys_get_temp_dir(), 'FOO');
  115. file_put_contents($file, http_build_query(array(
  116. 'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt),
  117. )));
  118. Request::setInputStream($file);
  119. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  120. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  121. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  122. ob_start();
  123. new PrivateBin;
  124. $content = ob_get_contents();
  125. ob_end_clean();
  126. $response = json_decode($content, true);
  127. $this->assertEquals(0, $response['status'], 'outputs status');
  128. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  129. }
  130. /**
  131. * @runInSeparateProcess
  132. */
  133. public function testDeleteWithPost()
  134. {
  135. $this->reset();
  136. $this->_model->create(Helper::getPasteId(), Helper::getPaste());
  137. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
  138. $paste = $this->_model->read(Helper::getPasteId());
  139. $_POST = array(
  140. 'action' => 'delete',
  141. 'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt),
  142. );
  143. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  144. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  145. $_SERVER['REQUEST_METHOD'] = 'POST';
  146. ob_start();
  147. new PrivateBin;
  148. $content = ob_get_contents();
  149. ob_end_clean();
  150. $response = json_decode($content, true);
  151. $this->assertEquals(0, $response['status'], 'outputs status');
  152. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  153. }
  154. /**
  155. * @runInSeparateProcess
  156. */
  157. public function testRead()
  158. {
  159. $this->reset();
  160. $paste = Helper::getPasteWithAttachment();
  161. $paste['meta']['attachment'] = $paste['attachment'];
  162. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  163. unset($paste['attachment']);
  164. unset($paste['attachmentname']);
  165. $this->_model->create(Helper::getPasteId(), $paste);
  166. $_SERVER['QUERY_STRING'] = Helper::getPasteId();
  167. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  168. ob_start();
  169. new PrivateBin;
  170. $content = ob_get_contents();
  171. ob_end_clean();
  172. $response = json_decode($content, true);
  173. $this->assertEquals(0, $response['status'], 'outputs success status');
  174. $this->assertEquals(Helper::getPasteId(), $response['id'], 'outputs data correctly');
  175. $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
  176. $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
  177. $this->assertEquals($paste['meta']['attachment'], $response['attachment'], 'outputs attachment correctly');
  178. $this->assertEquals($paste['meta']['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
  179. $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
  180. $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
  181. $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
  182. $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
  183. $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
  184. }
  185. /**
  186. * @runInSeparateProcess
  187. */
  188. public function testJsonLdPaste()
  189. {
  190. $this->reset();
  191. $paste = Helper::getPasteWithAttachment();
  192. $this->_model->create(Helper::getPasteId(), $paste);
  193. $_GET['jsonld'] = 'paste';
  194. ob_start();
  195. new PrivateBin;
  196. $content = ob_get_contents();
  197. ob_end_clean();
  198. $this->assertEquals(str_replace(
  199. '?jsonld=',
  200. '/?jsonld=',
  201. file_get_contents(PUBLIC_PATH . '/js/paste.jsonld')
  202. ), $content, 'outputs data correctly');
  203. }
  204. /**
  205. * @runInSeparateProcess
  206. */
  207. public function testJsonLdComment()
  208. {
  209. $this->reset();
  210. $paste = Helper::getPasteWithAttachment();
  211. $this->_model->create(Helper::getPasteId(), $paste);
  212. $_GET['jsonld'] = 'comment';
  213. ob_start();
  214. new PrivateBin;
  215. $content = ob_get_contents();
  216. ob_end_clean();
  217. $this->assertEquals(str_replace(
  218. '?jsonld=',
  219. '/?jsonld=',
  220. file_get_contents(PUBLIC_PATH . '/js/comment.jsonld')
  221. ), $content, 'outputs data correctly');
  222. }
  223. /**
  224. * @runInSeparateProcess
  225. */
  226. public function testJsonLdPasteMeta()
  227. {
  228. $this->reset();
  229. $paste = Helper::getPasteWithAttachment();
  230. $this->_model->create(Helper::getPasteId(), $paste);
  231. $_GET['jsonld'] = 'pastemeta';
  232. ob_start();
  233. new PrivateBin;
  234. $content = ob_get_contents();
  235. ob_end_clean();
  236. $this->assertEquals(str_replace(
  237. '?jsonld=',
  238. '/?jsonld=',
  239. file_get_contents(PUBLIC_PATH . '/js/pastemeta.jsonld')
  240. ), $content, 'outputs data correctly');
  241. }
  242. /**
  243. * @runInSeparateProcess
  244. */
  245. public function testJsonLdCommentMeta()
  246. {
  247. $this->reset();
  248. $paste = Helper::getPasteWithAttachment();
  249. $this->_model->create(Helper::getPasteId(), $paste);
  250. $_GET['jsonld'] = 'commentmeta';
  251. ob_start();
  252. new PrivateBin;
  253. $content = ob_get_contents();
  254. ob_end_clean();
  255. $this->assertEquals(str_replace(
  256. '?jsonld=',
  257. '/?jsonld=',
  258. file_get_contents(PUBLIC_PATH . '/js/commentmeta.jsonld')
  259. ), $content, 'outputs data correctly');
  260. }
  261. /**
  262. * @runInSeparateProcess
  263. */
  264. public function testJsonLdInvalid()
  265. {
  266. $this->reset();
  267. $paste = Helper::getPasteWithAttachment();
  268. $this->_model->create(Helper::getPasteId(), $paste);
  269. $_GET['jsonld'] = '../cfg/conf.ini';
  270. ob_start();
  271. new PrivateBin;
  272. $content = ob_get_contents();
  273. ob_end_clean();
  274. $this->assertEquals('{}', $content, 'does not output nasty data');
  275. }
  276. }