coreSpec.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('Core base tests', function() {
  22. afterEach(function() {
  23. // many tests call window.initCore so need to unregister global events
  24. // ideally in the future we'll need a window.unloadCore() function
  25. $(document).off('ajaxError.main');
  26. $(document).off('unload.main');
  27. $(document).off('beforeunload.main');
  28. OC._userIsNavigatingAway = false;
  29. OC._reloadCalled = false;
  30. });
  31. describe('Base values', function() {
  32. it('Sets webroots', function() {
  33. expect(OC.getRootPath()).toBeDefined();
  34. expect(OC.appswebroots).toBeDefined();
  35. });
  36. });
  37. describe('basename', function() {
  38. it('Returns the nothing if no file name given', function() {
  39. expect(OC.basename('')).toEqual('');
  40. });
  41. it('Returns the nothing if dir is root', function() {
  42. expect(OC.basename('/')).toEqual('');
  43. });
  44. it('Returns the same name if no path given', function() {
  45. expect(OC.basename('some name.txt')).toEqual('some name.txt');
  46. });
  47. it('Returns the base name if root path given', function() {
  48. expect(OC.basename('/some name.txt')).toEqual('some name.txt');
  49. });
  50. it('Returns the base name if double root path given', function() {
  51. expect(OC.basename('//some name.txt')).toEqual('some name.txt');
  52. });
  53. it('Returns the base name if subdir given without root', function() {
  54. expect(OC.basename('subdir/some name.txt')).toEqual('some name.txt');
  55. });
  56. it('Returns the base name if subdir given with root', function() {
  57. expect(OC.basename('/subdir/some name.txt')).toEqual('some name.txt');
  58. });
  59. it('Returns the base name if subdir given with double root', function() {
  60. expect(OC.basename('//subdir/some name.txt')).toEqual('some name.txt');
  61. });
  62. it('Returns the base name if subdir has dot', function() {
  63. expect(OC.basename('/subdir.dat/some name.txt')).toEqual('some name.txt');
  64. });
  65. it('Returns dot if file name is dot', function() {
  66. expect(OC.basename('/subdir/.')).toEqual('.');
  67. });
  68. // TODO: fix the source to make it work like PHP's basename
  69. it('Returns the dir itself if no file name given', function() {
  70. // TODO: fix the source to make it work like PHP's dirname
  71. // expect(OC.basename('subdir/')).toEqual('subdir');
  72. expect(OC.basename('subdir/')).toEqual('');
  73. });
  74. it('Returns the dir itself if no file name given with root', function() {
  75. // TODO: fix the source to make it work like PHP's dirname
  76. // expect(OC.basename('/subdir/')).toEqual('subdir');
  77. expect(OC.basename('/subdir/')).toEqual('');
  78. });
  79. });
  80. describe('dirname', function() {
  81. it('Returns the nothing if no file name given', function() {
  82. expect(OC.dirname('')).toEqual('');
  83. });
  84. it('Returns the root if dir is root', function() {
  85. // TODO: fix the source to make it work like PHP's dirname
  86. // expect(OC.dirname('/')).toEqual('/');
  87. expect(OC.dirname('/')).toEqual('');
  88. });
  89. it('Returns the root if dir is double root', function() {
  90. // TODO: fix the source to make it work like PHP's dirname
  91. // expect(OC.dirname('//')).toEqual('/');
  92. expect(OC.dirname('//')).toEqual('/'); // oh no...
  93. });
  94. it('Returns dot if dir is dot', function() {
  95. expect(OC.dirname('.')).toEqual('.');
  96. });
  97. it('Returns dot if no root given', function() {
  98. // TODO: fix the source to make it work like PHP's dirname
  99. // expect(OC.dirname('some dir')).toEqual('.');
  100. expect(OC.dirname('some dir')).toEqual('some dir'); // oh no...
  101. });
  102. it('Returns the dir name if file name and root path given', function() {
  103. // TODO: fix the source to make it work like PHP's dirname
  104. // expect(OC.dirname('/some name.txt')).toEqual('/');
  105. expect(OC.dirname('/some name.txt')).toEqual('');
  106. });
  107. it('Returns the dir name if double root path given', function() {
  108. expect(OC.dirname('//some name.txt')).toEqual('/'); // how lucky...
  109. });
  110. it('Returns the dir name if subdir given without root', function() {
  111. expect(OC.dirname('subdir/some name.txt')).toEqual('subdir');
  112. });
  113. it('Returns the dir name if subdir given with root', function() {
  114. expect(OC.dirname('/subdir/some name.txt')).toEqual('/subdir');
  115. });
  116. it('Returns the dir name if subdir given with double root', function() {
  117. // TODO: fix the source to make it work like PHP's dirname
  118. // expect(OC.dirname('//subdir/some name.txt')).toEqual('/subdir');
  119. expect(OC.dirname('//subdir/some name.txt')).toEqual('//subdir'); // oh...
  120. });
  121. it('Returns the dir name if subdir has dot', function() {
  122. expect(OC.dirname('/subdir.dat/some name.txt')).toEqual('/subdir.dat');
  123. });
  124. it('Returns the dir name if file name is dot', function() {
  125. expect(OC.dirname('/subdir/.')).toEqual('/subdir');
  126. });
  127. it('Returns the dir name if no file name given', function() {
  128. expect(OC.dirname('subdir/')).toEqual('subdir');
  129. });
  130. it('Returns the dir name if no file name given with root', function() {
  131. expect(OC.dirname('/subdir/')).toEqual('/subdir');
  132. });
  133. });
  134. describe('escapeHTML', function() {
  135. it('Returns nothing if no string was given', function() {
  136. expect(escapeHTML('')).toEqual('');
  137. });
  138. it('Returns a sanitized string if a string containing HTML is given', function() {
  139. expect(escapeHTML('There needs to be a <script>alert(\"Unit\" + \'test\')</script> for it!')).toEqual('There needs to be a &lt;script&gt;alert(&quot;Unit&quot; + &#039;test&#039;)&lt;/script&gt; for it!');
  140. });
  141. it('Returns the string without modification if no potentially dangerous character is passed.', function() {
  142. expect(escapeHTML('This is a good string without HTML.')).toEqual('This is a good string without HTML.');
  143. });
  144. });
  145. describe('joinPaths', function() {
  146. it('returns empty string with no or empty arguments', function() {
  147. expect(OC.joinPaths()).toEqual('');
  148. expect(OC.joinPaths('')).toEqual('');
  149. expect(OC.joinPaths('', '')).toEqual('');
  150. });
  151. it('returns joined path sections', function() {
  152. expect(OC.joinPaths('abc')).toEqual('abc');
  153. expect(OC.joinPaths('abc', 'def')).toEqual('abc/def');
  154. expect(OC.joinPaths('abc', 'def', 'ghi')).toEqual('abc/def/ghi');
  155. });
  156. it('keeps leading slashes', function() {
  157. expect(OC.joinPaths('/abc')).toEqual('/abc');
  158. expect(OC.joinPaths('/abc', '')).toEqual('/abc');
  159. expect(OC.joinPaths('', '/abc')).toEqual('/abc');
  160. expect(OC.joinPaths('/abc', 'def')).toEqual('/abc/def');
  161. expect(OC.joinPaths('/abc', 'def', 'ghi')).toEqual('/abc/def/ghi');
  162. });
  163. it('keeps trailing slashes', function() {
  164. expect(OC.joinPaths('', 'abc/')).toEqual('abc/');
  165. expect(OC.joinPaths('abc/')).toEqual('abc/');
  166. expect(OC.joinPaths('abc/', '')).toEqual('abc/');
  167. expect(OC.joinPaths('abc', 'def/')).toEqual('abc/def/');
  168. expect(OC.joinPaths('abc', 'def', 'ghi/')).toEqual('abc/def/ghi/');
  169. });
  170. it('splits paths in specified strings and discards extra slashes', function() {
  171. expect(OC.joinPaths('//abc//')).toEqual('/abc/');
  172. expect(OC.joinPaths('//abc//def//')).toEqual('/abc/def/');
  173. expect(OC.joinPaths('//abc//', '//def//')).toEqual('/abc/def/');
  174. expect(OC.joinPaths('//abc//', '//def//', '//ghi//')).toEqual('/abc/def/ghi/');
  175. expect(OC.joinPaths('//abc//def//', '//ghi//jkl/mno/', '//pqr//'))
  176. .toEqual('/abc/def/ghi/jkl/mno/pqr/');
  177. expect(OC.joinPaths('/abc', '/def')).toEqual('/abc/def');
  178. expect(OC.joinPaths('/abc/', '/def')).toEqual('/abc/def');
  179. expect(OC.joinPaths('/abc/', 'def')).toEqual('/abc/def');
  180. });
  181. it('discards empty sections', function() {
  182. expect(OC.joinPaths('abc', '', 'def')).toEqual('abc/def');
  183. });
  184. it('returns root if only slashes', function() {
  185. expect(OC.joinPaths('//')).toEqual('/');
  186. expect(OC.joinPaths('/', '/')).toEqual('/');
  187. expect(OC.joinPaths('/', '//', '/')).toEqual('/');
  188. });
  189. });
  190. describe('isSamePath', function() {
  191. it('recognizes empty paths are equal', function() {
  192. expect(OC.isSamePath('', '')).toEqual(true);
  193. expect(OC.isSamePath('/', '')).toEqual(true);
  194. expect(OC.isSamePath('//', '')).toEqual(true);
  195. expect(OC.isSamePath('/', '/')).toEqual(true);
  196. expect(OC.isSamePath('/', '//')).toEqual(true);
  197. });
  198. it('recognizes path with single sections as equal regardless of extra slashes', function() {
  199. expect(OC.isSamePath('abc', 'abc')).toEqual(true);
  200. expect(OC.isSamePath('/abc', 'abc')).toEqual(true);
  201. expect(OC.isSamePath('//abc', 'abc')).toEqual(true);
  202. expect(OC.isSamePath('abc', '/abc')).toEqual(true);
  203. expect(OC.isSamePath('abc/', 'abc')).toEqual(true);
  204. expect(OC.isSamePath('abc/', 'abc/')).toEqual(true);
  205. expect(OC.isSamePath('/abc/', 'abc/')).toEqual(true);
  206. expect(OC.isSamePath('/abc/', '/abc/')).toEqual(true);
  207. expect(OC.isSamePath('//abc/', '/abc/')).toEqual(true);
  208. expect(OC.isSamePath('//abc//', '/abc/')).toEqual(true);
  209. expect(OC.isSamePath('abc', 'def')).toEqual(false);
  210. expect(OC.isSamePath('/abc', 'def')).toEqual(false);
  211. expect(OC.isSamePath('//abc', 'def')).toEqual(false);
  212. expect(OC.isSamePath('abc', '/def')).toEqual(false);
  213. expect(OC.isSamePath('abc/', 'def')).toEqual(false);
  214. expect(OC.isSamePath('abc/', 'def/')).toEqual(false);
  215. expect(OC.isSamePath('/abc/', 'def/')).toEqual(false);
  216. expect(OC.isSamePath('/abc/', '/def/')).toEqual(false);
  217. expect(OC.isSamePath('//abc/', '/def/')).toEqual(false);
  218. expect(OC.isSamePath('//abc//', '/def/')).toEqual(false);
  219. });
  220. it('recognizes path with multiple sections as equal regardless of extra slashes', function() {
  221. expect(OC.isSamePath('abc/def', 'abc/def')).toEqual(true);
  222. expect(OC.isSamePath('/abc/def', 'abc/def')).toEqual(true);
  223. expect(OC.isSamePath('abc/def', '/abc/def')).toEqual(true);
  224. expect(OC.isSamePath('abc/def/', '/abc/def/')).toEqual(true);
  225. expect(OC.isSamePath('/abc/def/', '/abc/def/')).toEqual(true);
  226. expect(OC.isSamePath('/abc/def/', 'abc/def/')).toEqual(true);
  227. expect(OC.isSamePath('//abc/def/', 'abc/def/')).toEqual(true);
  228. expect(OC.isSamePath('//abc/def//', 'abc/def/')).toEqual(true);
  229. expect(OC.isSamePath('abc/def', 'abc/ghi')).toEqual(false);
  230. expect(OC.isSamePath('/abc/def', 'abc/ghi')).toEqual(false);
  231. expect(OC.isSamePath('abc/def', '/abc/ghi')).toEqual(false);
  232. expect(OC.isSamePath('abc/def/', '/abc/ghi/')).toEqual(false);
  233. expect(OC.isSamePath('/abc/def/', '/abc/ghi/')).toEqual(false);
  234. expect(OC.isSamePath('/abc/def/', 'abc/ghi/')).toEqual(false);
  235. expect(OC.isSamePath('//abc/def/', 'abc/ghi/')).toEqual(false);
  236. expect(OC.isSamePath('//abc/def//', 'abc/ghi/')).toEqual(false);
  237. });
  238. it('recognizes path entries with dot', function() {
  239. expect(OC.isSamePath('.', '')).toEqual(true);
  240. expect(OC.isSamePath('.', '.')).toEqual(true);
  241. expect(OC.isSamePath('.', '/')).toEqual(true);
  242. expect(OC.isSamePath('/.', '/')).toEqual(true);
  243. expect(OC.isSamePath('/./', '/')).toEqual(true);
  244. expect(OC.isSamePath('/./', '/.')).toEqual(true);
  245. expect(OC.isSamePath('/./', '/./')).toEqual(true);
  246. expect(OC.isSamePath('/./', '/./')).toEqual(true);
  247. expect(OC.isSamePath('a/./b', 'a/b')).toEqual(true);
  248. expect(OC.isSamePath('a/b/.', 'a/b')).toEqual(true);
  249. expect(OC.isSamePath('./a/b', 'a/b')).toEqual(true);
  250. });
  251. });
  252. describe('filePath', function() {
  253. beforeEach(function() {
  254. OC.webroot = 'http://localhost';
  255. OC.appswebroots.files = OC.getRootPath() + '/apps3/files';
  256. });
  257. afterEach(function() {
  258. delete OC.appswebroots.files;
  259. });
  260. it('Uses a direct link for css and images,' , function() {
  261. expect(OC.filePath('core', 'css', 'style.css')).toEqual('http://localhost/core/css/style.css');
  262. expect(OC.filePath('files', 'css', 'style.css')).toEqual('http://localhost/apps3/files/css/style.css');
  263. expect(OC.filePath('core', 'img', 'image.png')).toEqual('http://localhost/core/img/image.png');
  264. expect(OC.filePath('files', 'img', 'image.png')).toEqual('http://localhost/apps3/files/img/image.png');
  265. });
  266. it('Routes PHP files via index.php,' , function() {
  267. expect(OC.filePath('core', 'ajax', 'test.php')).toEqual('http://localhost/index.php/core/ajax/test.php');
  268. expect(OC.filePath('files', 'ajax', 'test.php')).toEqual('http://localhost/index.php/apps/files/ajax/test.php');
  269. });
  270. });
  271. describe('getCanonicalLocale', function() {
  272. var localeStub;
  273. beforeEach(function() {
  274. localeStub = sinon.stub(OC, 'getLocale');
  275. });
  276. afterEach(function() {
  277. localeStub.restore();
  278. });
  279. it("Returns primary locales as is", function() {
  280. localeStub.returns('de');
  281. expect(OC.getCanonicalLocale()).toEqual('de');
  282. localeStub.returns('zu');
  283. expect(OC.getCanonicalLocale()).toEqual('zu');
  284. });
  285. it("Returns extended locales with hyphens", function() {
  286. localeStub.returns('az_Cyrl_AZ');
  287. expect(OC.getCanonicalLocale()).toEqual('az-Cyrl-AZ');
  288. localeStub.returns('de_DE');
  289. expect(OC.getCanonicalLocale()).toEqual('de-DE');
  290. });
  291. });
  292. describe('Link functions', function() {
  293. var TESTAPP = 'testapp';
  294. var TESTAPP_ROOT = OC.getRootPath() + '/appsx/testapp';
  295. beforeEach(function() {
  296. OC.appswebroots[TESTAPP] = TESTAPP_ROOT;
  297. });
  298. afterEach(function() {
  299. // restore original array
  300. delete OC.appswebroots[TESTAPP];
  301. });
  302. it('Generates correct links for core apps', function() {
  303. expect(OC.linkTo('core', 'somefile.php')).toEqual(OC.getRootPath() + '/core/somefile.php');
  304. expect(OC.linkTo('admin', 'somefile.php')).toEqual(OC.getRootPath() + '/admin/somefile.php');
  305. });
  306. it('Generates correct links for regular apps', function() {
  307. expect(OC.linkTo(TESTAPP, 'somefile.php')).toEqual(OC.getRootPath() + '/index.php/apps/' + TESTAPP + '/somefile.php');
  308. });
  309. it('Generates correct remote links', function() {
  310. expect(OC.linkToRemote('webdav')).toEqual(window.location.protocol + '//' + window.location.host + OC.getRootPath() + '/remote.php/webdav');
  311. });
  312. describe('Images', function() {
  313. it('Generates image path with given extension', function() {
  314. expect(OC.imagePath('core', 'somefile.jpg')).toEqual(OC.getRootPath() + '/core/img/somefile.jpg');
  315. expect(OC.imagePath(TESTAPP, 'somefile.jpg')).toEqual(TESTAPP_ROOT + '/img/somefile.jpg');
  316. });
  317. it('Generates image path with svg extension', function() {
  318. expect(OC.imagePath('core', 'somefile')).toEqual(OC.getRootPath() + '/core/img/somefile.svg');
  319. expect(OC.imagePath(TESTAPP, 'somefile')).toEqual(TESTAPP_ROOT + '/img/somefile.svg');
  320. });
  321. });
  322. });
  323. describe('Query string building', function() {
  324. it('Returns empty string when empty params', function() {
  325. expect(OC.buildQueryString()).toEqual('');
  326. expect(OC.buildQueryString({})).toEqual('');
  327. });
  328. it('Encodes regular query strings', function() {
  329. expect(OC.buildQueryString({
  330. a: 'abc',
  331. b: 'def'
  332. })).toEqual('a=abc&b=def');
  333. });
  334. it('Encodes special characters', function() {
  335. expect(OC.buildQueryString({
  336. unicode: '汉字'
  337. })).toEqual('unicode=%E6%B1%89%E5%AD%97');
  338. expect(OC.buildQueryString({
  339. b: 'spaace value',
  340. 'space key': 'normalvalue',
  341. 'slash/this': 'amp&ersand'
  342. })).toEqual('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  343. });
  344. it('Encodes data types and empty values', function() {
  345. expect(OC.buildQueryString({
  346. 'keywithemptystring': '',
  347. 'keywithnull': null,
  348. 'keywithundefined': null,
  349. something: 'else'
  350. })).toEqual('keywithemptystring=&keywithnull&keywithundefined&something=else');
  351. expect(OC.buildQueryString({
  352. 'booleanfalse': false,
  353. 'booleantrue': true
  354. })).toEqual('booleanfalse=false&booleantrue=true');
  355. expect(OC.buildQueryString({
  356. 'number': 123
  357. })).toEqual('number=123');
  358. });
  359. });
  360. describe('Session heartbeat', function() {
  361. var clock,
  362. oldConfig,
  363. routeStub,
  364. counter;
  365. beforeEach(function() {
  366. clock = sinon.useFakeTimers();
  367. oldConfig = window.oc_config;
  368. routeStub = sinon.stub(OC, 'generateUrl').returns('/csrftoken');
  369. counter = 0;
  370. fakeServer.autoRespond = true;
  371. fakeServer.autoRespondAfter = 0;
  372. fakeServer.respondWith(/\/csrftoken/, function(xhr) {
  373. counter++;
  374. xhr.respond(200, {'Content-Type': 'application/json'}, '{"token": "pgBEsb3MzTb1ZPd2mfDZbQ6/0j3OrXHMEZrghHcOkg8=:3khw5PSa+wKQVo4f26exFD3nplud9ECjJ8/Y5zk5/k4="}');
  375. });
  376. $(document).off('ajaxComplete'); // ignore previously registered heartbeats
  377. });
  378. afterEach(function() {
  379. clock.restore();
  380. /* jshint camelcase: false */
  381. window.oc_config = oldConfig;
  382. routeStub.restore();
  383. $(document).off('ajaxError');
  384. $(document).off('ajaxComplete');
  385. });
  386. it('sends heartbeat half the session lifetime when heartbeat enabled', function() {
  387. /* jshint camelcase: false */
  388. window.oc_config = {
  389. session_keepalive: true,
  390. session_lifetime: 300
  391. };
  392. window.initCore();
  393. expect(routeStub.calledWith('/csrftoken')).toEqual(true);
  394. expect(counter).toEqual(0);
  395. // less than half, still nothing
  396. clock.tick(100 * 1000);
  397. expect(counter).toEqual(0);
  398. // reach past half (160), one call
  399. clock.tick(55 * 1000);
  400. expect(counter).toEqual(1);
  401. // almost there to the next, still one
  402. clock.tick(140 * 1000);
  403. expect(counter).toEqual(1);
  404. // past it, second call
  405. clock.tick(20 * 1000);
  406. expect(counter).toEqual(2);
  407. });
  408. it('does not send heartbeat when heartbeat disabled', function() {
  409. /* jshint camelcase: false */
  410. window.oc_config = {
  411. session_keepalive: false,
  412. session_lifetime: 300
  413. };
  414. window.initCore();
  415. expect(routeStub.notCalled).toEqual(true);
  416. expect(counter).toEqual(0);
  417. clock.tick(1000000);
  418. // still nothing
  419. expect(counter).toEqual(0);
  420. });
  421. it('limits the heartbeat between one minute and one day', function() {
  422. /* jshint camelcase: false */
  423. var setIntervalStub = sinon.stub(window, 'setInterval');
  424. window.oc_config = {
  425. session_keepalive: true,
  426. session_lifetime: 5
  427. };
  428. window.initCore();
  429. expect(setIntervalStub.getCall(0).args[1]).toEqual(60 * 1000);
  430. setIntervalStub.reset();
  431. window.oc_config = {
  432. session_keepalive: true,
  433. session_lifetime: 48 * 3600
  434. };
  435. window.initCore();
  436. expect(setIntervalStub.getCall(0).args[1]).toEqual(24 * 3600 * 1000);
  437. setIntervalStub.restore();
  438. });
  439. });
  440. describe('Parse query string', function() {
  441. it('Parses query string from full URL', function() {
  442. var query = OC.parseQueryString('http://localhost/stuff.php?q=a&b=x');
  443. expect(query).toEqual({q: 'a', b: 'x'});
  444. });
  445. it('Parses query string from query part alone', function() {
  446. var query = OC.parseQueryString('q=a&b=x');
  447. expect(query).toEqual({q: 'a', b: 'x'});
  448. });
  449. it('Returns null hash when empty query', function() {
  450. var query = OC.parseQueryString('');
  451. expect(query).toEqual(null);
  452. });
  453. it('Returns empty hash when empty query with question mark', function() {
  454. var query = OC.parseQueryString('?');
  455. expect(query).toEqual({});
  456. });
  457. it('Decodes regular query strings', function() {
  458. var query = OC.parseQueryString('a=abc&b=def');
  459. expect(query).toEqual({
  460. a: 'abc',
  461. b: 'def'
  462. });
  463. });
  464. it('Ignores empty parts', function() {
  465. var query = OC.parseQueryString('&q=a&&b=x&');
  466. expect(query).toEqual({q: 'a', b: 'x'});
  467. });
  468. it('Ignores lone equal signs', function() {
  469. var query = OC.parseQueryString('&q=a&=&b=x&');
  470. expect(query).toEqual({q: 'a', b: 'x'});
  471. });
  472. it('Includes extra equal signs in value', function() {
  473. var query = OC.parseQueryString('u=a=x&q=a=b');
  474. expect(query).toEqual({u: 'a=x', q: 'a=b'});
  475. });
  476. it('Decodes plus as space', function() {
  477. var query = OC.parseQueryString('space+key=space+value');
  478. expect(query).toEqual({'space key': 'space value'});
  479. });
  480. it('Decodes special characters', function() {
  481. var query = OC.parseQueryString('unicode=%E6%B1%89%E5%AD%97');
  482. expect(query).toEqual({unicode: '汉字'});
  483. query = OC.parseQueryString('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  484. expect(query).toEqual({
  485. b: 'spaace value',
  486. 'space key': 'normalvalue',
  487. 'slash/this': 'amp&ersand'
  488. });
  489. });
  490. it('Decodes empty values', function() {
  491. var query = OC.parseQueryString('keywithemptystring=&keywithnostring');
  492. expect(query).toEqual({
  493. 'keywithemptystring': '',
  494. 'keywithnostring': null
  495. });
  496. });
  497. it('Does not interpret data types', function() {
  498. var query = OC.parseQueryString('booleanfalse=false&booleantrue=true&number=123');
  499. expect(query).toEqual({
  500. 'booleanfalse': 'false',
  501. 'booleantrue': 'true',
  502. 'number': '123'
  503. });
  504. });
  505. });
  506. describe('Generate Url', function() {
  507. it('returns absolute urls', function() {
  508. expect(OC.generateUrl('csrftoken')).toEqual(OC.getRootPath() + '/index.php/csrftoken');
  509. expect(OC.generateUrl('/csrftoken')).toEqual(OC.getRootPath() + '/index.php/csrftoken');
  510. });
  511. it('substitutes parameters which are escaped by default', function() {
  512. expect(OC.generateUrl('apps/files/download/{file}', {file: '<">ImAnUnescapedString/!'})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!');
  513. });
  514. it('substitutes parameters which can also be unescaped via option flag', function() {
  515. expect(OC.generateUrl('apps/files/download/{file}', {file: 'subfolder/Welcome.txt'}, {escape: false})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/subfolder/Welcome.txt');
  516. });
  517. it('substitutes multiple parameters which are escaped by default', function() {
  518. expect(OC.generateUrl('apps/files/download/{file}/{id}', {file: '<">ImAnUnescapedString/!', id: 5})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!/5');
  519. });
  520. it('substitutes multiple parameters which can also be unescaped via option flag', function() {
  521. expect(OC.generateUrl('apps/files/download/{file}/{id}', {file: 'subfolder/Welcome.txt', id: 5}, {escape: false})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/subfolder/Welcome.txt/5');
  522. });
  523. it('doesnt error out with no params provided', function () {
  524. expect(OC.generateUrl('apps/files/download{file}')).toEqual(OC.getRootPath() + '/index.php/apps/files/download%7Bfile%7D');
  525. });
  526. });
  527. describe('Main menu mobile toggle', function() {
  528. var clock;
  529. var $toggle;
  530. var $navigation;
  531. beforeEach(function() {
  532. jQuery.fx.off = true;
  533. clock = sinon.useFakeTimers();
  534. $('#testArea').append('<div id="header">' +
  535. '<a class="menutoggle header-appname-container" href="#">' +
  536. '<h1 class="header-appname"></h1>' +
  537. '<div class="icon-caret"></div>' +
  538. '</a>' +
  539. '</div>' +
  540. '<div id="navigation"></div>');
  541. $toggle = $('#header').find('.menutoggle');
  542. $navigation = $('#navigation');
  543. });
  544. afterEach(function() {
  545. jQuery.fx.off = false;
  546. clock.restore();
  547. $(document).off('ajaxError');
  548. });
  549. it('Sets up menu toggle', function() {
  550. window.initCore();
  551. expect($navigation.hasClass('menu')).toEqual(true);
  552. });
  553. it('Clicking menu toggle toggles navigation in', function() {
  554. window.initCore();
  555. // fore show more apps icon since otherwise it would be hidden since no icons are available
  556. clock.tick(1 * 1000);
  557. $('#more-apps').show();
  558. expect($navigation.is(':visible')).toEqual(false);
  559. $toggle.click();
  560. clock.tick(1 * 1000);
  561. expect($navigation.is(':visible')).toEqual(true);
  562. $toggle.click();
  563. clock.tick(1 * 1000);
  564. expect($navigation.is(':visible')).toEqual(false);
  565. });
  566. });
  567. describe('Util', function() {
  568. var locale;
  569. var localeStub;
  570. beforeEach(function() {
  571. locale = OC.getCanonicalLocale();
  572. localeStub = sinon.stub(OC, 'getCanonicalLocale');
  573. localeStub.returns(locale);
  574. });
  575. afterEach(function() {
  576. localeStub.restore();
  577. });
  578. describe('humanFileSize', function() {
  579. // cit() will skip tests if toLocaleString() is not supported.
  580. // See https://github.com/ariya/phantomjs/issues/12581
  581. //
  582. // Please run these tests in Chrome/Firefox manually.
  583. var cit = 4.2.toLocaleString("de") !== "4,2" ? xit : it;
  584. it('renders file sizes with the correct unit', function() {
  585. var data = [
  586. [0, '0 B'],
  587. ["0", '0 B'],
  588. ["A", 'NaN B'],
  589. [125, '125 B'],
  590. [128000, '125 KB'],
  591. [128000000, '122.1 MB'],
  592. [128000000000, '119.2 GB'],
  593. [128000000000000, '116.4 TB']
  594. ];
  595. for (var i = 0; i < data.length; i++) {
  596. expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
  597. }
  598. });
  599. it('renders file sizes with the correct unit for small sizes', function() {
  600. var data = [
  601. [0, '0 KB'],
  602. [125, '< 1 KB'],
  603. [128000, '125 KB'],
  604. [128000000, '122.1 MB'],
  605. [128000000000, '119.2 GB'],
  606. [128000000000000, '116.4 TB']
  607. ];
  608. for (var i = 0; i < data.length; i++) {
  609. expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]);
  610. }
  611. });
  612. cit('renders file sizes with the correct locale', function() {
  613. localeStub.returns("de");
  614. var data = [
  615. [0, '0 B'],
  616. ["0", '0 B'],
  617. ["A", 'NaN B'],
  618. [125, '125 B'],
  619. [128000, '125 KB'],
  620. [128000000, '122,1 MB'],
  621. [128000000000, '119,2 GB'],
  622. [128000000000000, '116,4 TB']
  623. ];
  624. for (var i = 0; i < data.length; i++) {
  625. expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
  626. }
  627. });
  628. });
  629. describe('computerFileSize', function() {
  630. it('correctly parses file sizes from a human readable formated string', function() {
  631. var data = [
  632. ['125', 125],
  633. ['125.25', 125],
  634. ['125.25B', 125],
  635. ['125.25 B', 125],
  636. ['0 B', 0],
  637. ['99999999999999999999999999999999999999999999 B', 99999999999999999999999999999999999999999999],
  638. ['0 MB', 0],
  639. ['0 kB', 0],
  640. ['0kB', 0],
  641. ['125 B', 125],
  642. ['125b', 125],
  643. ['125 KB', 128000],
  644. ['125kb', 128000],
  645. ['122.1 MB', 128031130],
  646. ['122.1mb', 128031130],
  647. ['119.2 GB', 127990025421],
  648. ['119.2gb', 127990025421],
  649. ['116.4 TB', 127983153473126],
  650. ['116.4tb', 127983153473126],
  651. ['8776656778888777655.4tb', 9.650036181387265e+30],
  652. [1234, null],
  653. [-1234, null],
  654. ['-1234 B', null],
  655. ['B', null],
  656. ['40/0', null],
  657. ['40,30 kb', null],
  658. [' 122.1 MB ', 128031130],
  659. ['122.1 MB ', 128031130],
  660. [' 122.1 MB ', 128031130],
  661. [' 122.1 MB ', 128031130],
  662. ['122.1 MB ', 128031130],
  663. [' 125', 125],
  664. [' 125 ', 125],
  665. ];
  666. for (var i = 0; i < data.length; i++) {
  667. expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);
  668. }
  669. });
  670. it('returns null if the parameter is not a string', function() {
  671. expect(OC.Util.computerFileSize(NaN)).toEqual(null);
  672. expect(OC.Util.computerFileSize(125)).toEqual(null);
  673. });
  674. it('returns null if the string is unparsable', function() {
  675. expect(OC.Util.computerFileSize('')).toEqual(null);
  676. expect(OC.Util.computerFileSize('foobar')).toEqual(null);
  677. });
  678. });
  679. describe('stripTime', function() {
  680. it('strips time from dates', function() {
  681. expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24)))
  682. .toEqual(new Date(2014, 2, 24, 0, 0, 0, 0));
  683. });
  684. });
  685. });
  686. describe('naturalSortCompare', function() {
  687. // cit() will skip tests if running on PhantomJS because it has issues with
  688. // localeCompare(). See https://github.com/ariya/phantomjs/issues/11063
  689. //
  690. // Please make sure to run these tests in Chrome/Firefox manually
  691. // to make sure they run.
  692. var cit = window.isPhantom?xit:it;
  693. // must provide the same results as \OC_Util::naturalSortCompare
  694. it('sorts alphabetically', function() {
  695. var a = [
  696. 'def',
  697. 'xyz',
  698. 'abc'
  699. ];
  700. a.sort(OC.Util.naturalSortCompare);
  701. expect(a).toEqual([
  702. 'abc',
  703. 'def',
  704. 'xyz'
  705. ]);
  706. });
  707. cit('sorts with different casing', function() {
  708. var a = [
  709. 'aaa',
  710. 'bbb',
  711. 'BBB',
  712. 'AAA'
  713. ];
  714. a.sort(OC.Util.naturalSortCompare);
  715. expect(a).toEqual([
  716. 'aaa',
  717. 'AAA',
  718. 'bbb',
  719. 'BBB'
  720. ]);
  721. });
  722. it('sorts with numbers', function() {
  723. var a = [
  724. '124.txt',
  725. 'abc1',
  726. '123.txt',
  727. 'abc',
  728. 'abc2',
  729. 'def (2).txt',
  730. 'ghi 10.txt',
  731. 'abc12',
  732. 'def.txt',
  733. 'def (1).txt',
  734. 'ghi 2.txt',
  735. 'def (10).txt',
  736. 'abc10',
  737. 'def (12).txt',
  738. 'z',
  739. 'ghi.txt',
  740. 'za',
  741. 'ghi 1.txt',
  742. 'ghi 12.txt',
  743. 'zz',
  744. '15.txt',
  745. '15b.txt'
  746. ];
  747. a.sort(OC.Util.naturalSortCompare);
  748. expect(a).toEqual([
  749. '15.txt',
  750. '15b.txt',
  751. '123.txt',
  752. '124.txt',
  753. 'abc',
  754. 'abc1',
  755. 'abc2',
  756. 'abc10',
  757. 'abc12',
  758. 'def.txt',
  759. 'def (1).txt',
  760. 'def (2).txt',
  761. 'def (10).txt',
  762. 'def (12).txt',
  763. 'ghi.txt',
  764. 'ghi 1.txt',
  765. 'ghi 2.txt',
  766. 'ghi 10.txt',
  767. 'ghi 12.txt',
  768. 'z',
  769. 'za',
  770. 'zz'
  771. ]);
  772. });
  773. it('sorts with chinese characters', function() {
  774. var a = [
  775. '十.txt',
  776. '一.txt',
  777. '二.txt',
  778. '十 2.txt',
  779. '三.txt',
  780. '四.txt',
  781. 'abc.txt',
  782. '五.txt',
  783. '七.txt',
  784. '八.txt',
  785. '九.txt',
  786. '六.txt',
  787. '十一.txt',
  788. '波.txt',
  789. '破.txt',
  790. '莫.txt',
  791. '啊.txt',
  792. '123.txt'
  793. ];
  794. a.sort(OC.Util.naturalSortCompare);
  795. expect(a).toEqual([
  796. '123.txt',
  797. 'abc.txt',
  798. '一.txt',
  799. '七.txt',
  800. '三.txt',
  801. '九.txt',
  802. '二.txt',
  803. '五.txt',
  804. '八.txt',
  805. '六.txt',
  806. '十.txt',
  807. '十 2.txt',
  808. '十一.txt',
  809. '啊.txt',
  810. '四.txt',
  811. '波.txt',
  812. '破.txt',
  813. '莫.txt'
  814. ]);
  815. });
  816. cit('sorts with umlauts', function() {
  817. var a = [
  818. 'öh.txt',
  819. 'Äh.txt',
  820. 'oh.txt',
  821. 'Üh 2.txt',
  822. 'Üh.txt',
  823. 'ah.txt',
  824. 'Öh.txt',
  825. 'uh.txt',
  826. 'üh.txt',
  827. 'äh.txt'
  828. ];
  829. a.sort(OC.Util.naturalSortCompare);
  830. expect(a).toEqual([
  831. 'ah.txt',
  832. 'äh.txt',
  833. 'Äh.txt',
  834. 'oh.txt',
  835. 'öh.txt',
  836. 'Öh.txt',
  837. 'uh.txt',
  838. 'üh.txt',
  839. 'Üh.txt',
  840. 'Üh 2.txt'
  841. ]);
  842. });
  843. });
  844. describe('Plugins', function() {
  845. var plugin;
  846. beforeEach(function() {
  847. plugin = {
  848. name: 'Some name',
  849. attach: function(obj) {
  850. obj.attached = true;
  851. },
  852. detach: function(obj) {
  853. obj.attached = false;
  854. }
  855. };
  856. OC.Plugins.register('OC.Test.SomeName', plugin);
  857. });
  858. it('attach plugin to object', function() {
  859. var obj = {something: true};
  860. OC.Plugins.attach('OC.Test.SomeName', obj);
  861. expect(obj.attached).toEqual(true);
  862. OC.Plugins.detach('OC.Test.SomeName', obj);
  863. expect(obj.attached).toEqual(false);
  864. });
  865. it('only call handler for target name', function() {
  866. var obj = {something: true};
  867. OC.Plugins.attach('OC.Test.SomeOtherName', obj);
  868. expect(obj.attached).not.toBeDefined();
  869. OC.Plugins.detach('OC.Test.SomeOtherName', obj);
  870. expect(obj.attached).not.toBeDefined();
  871. });
  872. });
  873. describe('Notifications', function() {
  874. var showSpy;
  875. var showHtmlSpy;
  876. var hideSpy;
  877. var clock;
  878. beforeEach(function() {
  879. clock = sinon.useFakeTimers();
  880. showSpy = sinon.spy(OC.Notification, 'show');
  881. showHtmlSpy = sinon.spy(OC.Notification, 'showHtml');
  882. hideSpy = sinon.spy(OC.Notification, 'hide');
  883. $('#testArea').append('<div id="notification"></div>');
  884. });
  885. afterEach(function() {
  886. showSpy.restore();
  887. showHtmlSpy.restore();
  888. hideSpy.restore();
  889. // jump past animations
  890. clock.tick(10000);
  891. clock.restore();
  892. });
  893. describe('showTemporary', function() {
  894. it('shows a plain text notification with default timeout', function() {
  895. var $row = OC.Notification.showTemporary('My notification test');
  896. expect(showSpy.calledOnce).toEqual(true);
  897. expect(showSpy.firstCall.args[0]).toEqual('My notification test');
  898. expect(showSpy.firstCall.args[1]).toEqual({isHTML: false, timeout: 7});
  899. expect($row).toBeDefined();
  900. expect($row.text()).toEqual('My notification test');
  901. });
  902. it('shows a HTML notification with default timeout', function() {
  903. var $row = OC.Notification.showTemporary('<a>My notification test</a>', { isHTML: true });
  904. expect(showSpy.notCalled).toEqual(true);
  905. expect(showHtmlSpy.calledOnce).toEqual(true);
  906. expect(showHtmlSpy.firstCall.args[0]).toEqual('<a>My notification test</a>');
  907. expect(showHtmlSpy.firstCall.args[1]).toEqual({isHTML: true, timeout: 7});
  908. expect($row).toBeDefined();
  909. expect($row.text()).toEqual('My notification test');
  910. });
  911. it('hides itself after 7 seconds', function() {
  912. var $row = OC.Notification.showTemporary('');
  913. // travel in time +7000 milliseconds
  914. clock.tick(7000);
  915. expect(hideSpy.calledOnce).toEqual(true);
  916. expect(hideSpy.firstCall.args[0]).toEqual($row);
  917. });
  918. });
  919. describe('show', function() {
  920. it('hides itself after a given time', function() {
  921. OC.Notification.show('', { timeout: 10 });
  922. // travel in time +9 seconds
  923. clock.tick(9000);
  924. expect(hideSpy.notCalled).toEqual(true);
  925. // travel in time +1 seconds
  926. clock.tick(1000);
  927. expect(hideSpy.calledOnce).toEqual(true);
  928. });
  929. it('does not hide itself after a given time if a timeout of 0 is defined', function() {
  930. OC.Notification.show('', { timeout: 0 });
  931. // travel in time +1000 seconds
  932. clock.tick(1000000);
  933. expect(hideSpy.notCalled).toEqual(true);
  934. });
  935. it('does not hide itself if no timeout given to show', function() {
  936. OC.Notification.show('');
  937. // travel in time +1000 seconds
  938. clock.tick(1000000);
  939. expect(hideSpy.notCalled).toEqual(true);
  940. });
  941. });
  942. it('cumulates several notifications', function() {
  943. var $row1 = OC.Notification.showTemporary('One');
  944. var $row2 = OC.Notification.showTemporary('Two', {timeout: 2});
  945. var $row3 = OC.Notification.showTemporary('Three');
  946. var $el = $('#notification');
  947. var $rows = $el.find('.row');
  948. expect($rows.length).toEqual(3);
  949. expect($rows.eq(0).is($row1)).toEqual(true);
  950. expect($rows.eq(1).is($row2)).toEqual(true);
  951. expect($rows.eq(2).is($row3)).toEqual(true);
  952. clock.tick(3000);
  953. $rows = $el.find('.row');
  954. expect($rows.length).toEqual(2);
  955. expect($rows.eq(0).is($row1)).toEqual(true);
  956. expect($rows.eq(1).is($row3)).toEqual(true);
  957. });
  958. it('shows close button for error types', function() {
  959. var $row = OC.Notification.showTemporary('One');
  960. var $rowError = OC.Notification.showTemporary('Two', {type: 'error'});
  961. expect($row.find('.close').length).toEqual(0);
  962. expect($rowError.find('.close').length).toEqual(1);
  963. // after clicking, row is gone
  964. $rowError.find('.close').click();
  965. var $rows = $('#notification').find('.row');
  966. expect($rows.length).toEqual(1);
  967. expect($rows.eq(0).is($row)).toEqual(true);
  968. });
  969. it('fades out the last notification but not the other ones', function() {
  970. var fadeOutStub = sinon.stub($.fn, 'fadeOut');
  971. var $row1 = OC.Notification.show('One', {type: 'error'});
  972. var $row2 = OC.Notification.show('Two', {type: 'error'});
  973. OC.Notification.showTemporary('Three', {timeout: 2});
  974. var $el = $('#notification');
  975. var $rows = $el.find('.row');
  976. expect($rows.length).toEqual(3);
  977. clock.tick(3000);
  978. $rows = $el.find('.row');
  979. expect($rows.length).toEqual(2);
  980. $row1.find('.close').click();
  981. clock.tick(1000);
  982. expect(fadeOutStub.notCalled).toEqual(true);
  983. $row2.find('.close').click();
  984. clock.tick(1000);
  985. expect(fadeOutStub.calledOnce).toEqual(true);
  986. expect($el.is(':empty')).toEqual(false);
  987. fadeOutStub.yield();
  988. expect($el.is(':empty')).toEqual(true);
  989. fadeOutStub.restore();
  990. });
  991. it('hides the first notification when calling hide without arguments', function() {
  992. OC.Notification.show('One');
  993. var $row2 = OC.Notification.show('Two');
  994. spyOn(console, 'warn');
  995. var $el = $('#notification');
  996. var $rows = $el.find('.row');
  997. expect($rows.length).toEqual(2);
  998. OC.Notification.hide();
  999. expect(console.warn).toHaveBeenCalled();
  1000. $rows = $el.find('.row');
  1001. expect($rows.length).toEqual(1);
  1002. expect($rows.eq(0).is($row2)).toEqual(true);
  1003. });
  1004. it('hides the given notification when calling hide with argument', function() {
  1005. var $row1 = OC.Notification.show('One');
  1006. var $row2 = OC.Notification.show('Two');
  1007. var $el = $('#notification');
  1008. var $rows = $el.find('.row');
  1009. expect($rows.length).toEqual(2);
  1010. OC.Notification.hide($row2);
  1011. $rows = $el.find('.row');
  1012. expect($rows.length).toEqual(1);
  1013. expect($rows.eq(0).is($row1)).toEqual(true);
  1014. });
  1015. });
  1016. describe('global ajax errors', function() {
  1017. var reloadStub, ajaxErrorStub, clock;
  1018. var notificationStub;
  1019. var waitTimeMs = 6500;
  1020. var oldCurrentUser;
  1021. beforeEach(function() {
  1022. oldCurrentUser = OC.currentUser;
  1023. OC.currentUser = 'dummy';
  1024. clock = sinon.useFakeTimers();
  1025. reloadStub = sinon.stub(OC, 'reload');
  1026. notificationStub = sinon.stub(OC.Notification, 'show');
  1027. // unstub the error processing method
  1028. ajaxErrorStub = OC._processAjaxError;
  1029. ajaxErrorStub.restore();
  1030. window.initCore();
  1031. });
  1032. afterEach(function() {
  1033. OC.currentUser = oldCurrentUser;
  1034. reloadStub.restore();
  1035. notificationStub.restore();
  1036. clock.restore();
  1037. });
  1038. it('reloads current page in case of auth error', function() {
  1039. var dataProvider = [
  1040. [200, false],
  1041. [400, false],
  1042. [0, false],
  1043. [401, true],
  1044. [302, true],
  1045. [303, true],
  1046. [307, true]
  1047. ];
  1048. for (var i = 0; i < dataProvider.length; i++) {
  1049. var xhr = { status: dataProvider[i][0] };
  1050. var expectedCall = dataProvider[i][1];
  1051. reloadStub.reset();
  1052. OC._reloadCalled = false;
  1053. $(document).trigger(new $.Event('ajaxError'), xhr);
  1054. // trigger timers
  1055. clock.tick(waitTimeMs);
  1056. if (expectedCall) {
  1057. expect(reloadStub.calledOnce).toEqual(true);
  1058. } else {
  1059. expect(reloadStub.notCalled).toEqual(true);
  1060. }
  1061. }
  1062. });
  1063. it('reload only called once in case of auth error', function() {
  1064. var xhr = { status: 401 };
  1065. $(document).trigger(new $.Event('ajaxError'), xhr);
  1066. $(document).trigger(new $.Event('ajaxError'), xhr);
  1067. // trigger timers
  1068. clock.tick(waitTimeMs);
  1069. expect(reloadStub.calledOnce).toEqual(true);
  1070. });
  1071. it('does not reload the page if the user was navigating away', function() {
  1072. var xhr = { status: 0 };
  1073. OC._userIsNavigatingAway = true;
  1074. clock.tick(100);
  1075. $(document).trigger(new $.Event('ajaxError'), xhr);
  1076. clock.tick(waitTimeMs);
  1077. expect(reloadStub.notCalled).toEqual(true);
  1078. });
  1079. it('displays notification', function() {
  1080. var xhr = { status: 401 };
  1081. notificationUpdateStub = sinon.stub(OC.Notification, 'showUpdate');
  1082. $(document).trigger(new $.Event('ajaxError'), xhr);
  1083. clock.tick(waitTimeMs);
  1084. expect(notificationUpdateStub.notCalled).toEqual(false);
  1085. });
  1086. it('shows a temporary notification if the connection is lost', function() {
  1087. var xhr = { status: 0 };
  1088. spyOn(OC, '_ajaxConnectionLostHandler');
  1089. $(document).trigger(new $.Event('ajaxError'), xhr);
  1090. clock.tick(101);
  1091. expect(OC._ajaxConnectionLostHandler.calls.count()).toBe(1);
  1092. });
  1093. });
  1094. describe('Snapper', function() {
  1095. var snapConstructorStub;
  1096. var snapperStub;
  1097. var clock;
  1098. beforeEach(function() {
  1099. snapConstructorStub = sinon.stub(window, 'Snap');
  1100. snapperStub = {};
  1101. snapperStub.enable = sinon.stub();
  1102. snapperStub.disable = sinon.stub();
  1103. snapperStub.close = sinon.stub();
  1104. snapConstructorStub.returns(snapperStub);
  1105. clock = sinon.useFakeTimers();
  1106. // _.now could have been set to Date.now before Sinon replaced it
  1107. // with a fake version, so _.now must be stubbed to ensure that the
  1108. // fake Date.now will be called instead of the original one.
  1109. _.now = sinon.stub(_, 'now').callsFake(function() {
  1110. return new Date().getTime();
  1111. });
  1112. $('#testArea').append('<div id="app-navigation">The navigation bar</div><div id="app-content">Content</div>');
  1113. });
  1114. afterEach(function() {
  1115. snapConstructorStub.restore();
  1116. clock.restore();
  1117. _.now.restore();
  1118. // Remove the event handler for the resize event added to the window
  1119. // due to calling window.initCore() when there is an #app-navigation
  1120. // element.
  1121. $(window).off('resize');
  1122. viewport.reset();
  1123. });
  1124. it('is enabled on a narrow screen', function() {
  1125. viewport.set(480);
  1126. window.initCore();
  1127. expect(snapConstructorStub.calledOnce).toBe(true);
  1128. expect(snapperStub.enable.calledOnce).toBe(true);
  1129. expect(snapperStub.disable.called).toBe(false);
  1130. });
  1131. it('is disabled when disallowing the gesture on a narrow screen', function() {
  1132. viewport.set(480);
  1133. window.initCore();
  1134. expect(snapperStub.enable.calledOnce).toBe(true);
  1135. expect(snapperStub.disable.called).toBe(false);
  1136. expect(snapperStub.close.called).toBe(false);
  1137. OC.disallowNavigationBarSlideGesture();
  1138. expect(snapperStub.enable.calledOnce).toBe(true);
  1139. expect(snapperStub.disable.calledOnce).toBe(true);
  1140. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1141. expect(snapperStub.close.called).toBe(false);
  1142. });
  1143. it('is not disabled again when disallowing the gesture twice on a narrow screen', function() {
  1144. viewport.set(480);
  1145. window.initCore();
  1146. expect(snapperStub.enable.calledOnce).toBe(true);
  1147. expect(snapperStub.disable.called).toBe(false);
  1148. expect(snapperStub.close.called).toBe(false);
  1149. OC.disallowNavigationBarSlideGesture();
  1150. expect(snapperStub.enable.calledOnce).toBe(true);
  1151. expect(snapperStub.disable.calledOnce).toBe(true);
  1152. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1153. expect(snapperStub.close.called).toBe(false);
  1154. OC.disallowNavigationBarSlideGesture();
  1155. expect(snapperStub.enable.calledOnce).toBe(true);
  1156. expect(snapperStub.disable.calledOnce).toBe(true);
  1157. expect(snapperStub.close.called).toBe(false);
  1158. });
  1159. it('is enabled when allowing the gesture after disallowing it on a narrow screen', function() {
  1160. viewport.set(480);
  1161. window.initCore();
  1162. expect(snapperStub.enable.calledOnce).toBe(true);
  1163. expect(snapperStub.disable.called).toBe(false);
  1164. expect(snapperStub.close.called).toBe(false);
  1165. OC.disallowNavigationBarSlideGesture();
  1166. expect(snapperStub.enable.calledOnce).toBe(true);
  1167. expect(snapperStub.disable.calledOnce).toBe(true);
  1168. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1169. expect(snapperStub.close.called).toBe(false);
  1170. OC.allowNavigationBarSlideGesture();
  1171. expect(snapperStub.enable.calledTwice).toBe(true);
  1172. expect(snapperStub.disable.calledOnce).toBe(true);
  1173. expect(snapperStub.close.called).toBe(false);
  1174. });
  1175. it('is not enabled again when allowing the gesture twice after disallowing it on a narrow screen', function() {
  1176. viewport.set(480);
  1177. window.initCore();
  1178. expect(snapperStub.enable.calledOnce).toBe(true);
  1179. expect(snapperStub.disable.called).toBe(false);
  1180. expect(snapperStub.close.called).toBe(false);
  1181. OC.disallowNavigationBarSlideGesture();
  1182. expect(snapperStub.enable.calledOnce).toBe(true);
  1183. expect(snapperStub.disable.calledOnce).toBe(true);
  1184. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1185. expect(snapperStub.close.called).toBe(false);
  1186. OC.allowNavigationBarSlideGesture();
  1187. expect(snapperStub.enable.calledTwice).toBe(true);
  1188. expect(snapperStub.disable.calledOnce).toBe(true);
  1189. expect(snapperStub.close.called).toBe(false);
  1190. OC.allowNavigationBarSlideGesture();
  1191. expect(snapperStub.enable.calledTwice).toBe(true);
  1192. expect(snapperStub.disable.calledOnce).toBe(true);
  1193. expect(snapperStub.close.called).toBe(false);
  1194. });
  1195. it('is disabled on a wide screen', function() {
  1196. viewport.set(1280);
  1197. window.initCore();
  1198. expect(snapConstructorStub.calledOnce).toBe(true);
  1199. expect(snapperStub.enable.called).toBe(false);
  1200. expect(snapperStub.disable.calledOnce).toBe(true);
  1201. });
  1202. it('is not disabled again when disallowing the gesture on a wide screen', function() {
  1203. viewport.set(1280);
  1204. window.initCore();
  1205. expect(snapperStub.enable.called).toBe(false);
  1206. expect(snapperStub.disable.calledOnce).toBe(true);
  1207. expect(snapperStub.close.calledOnce).toBe(true);
  1208. OC.disallowNavigationBarSlideGesture();
  1209. expect(snapperStub.enable.called).toBe(false);
  1210. expect(snapperStub.disable.calledOnce).toBe(true);
  1211. expect(snapperStub.close.calledOnce).toBe(true);
  1212. });
  1213. it('is not enabled when allowing the gesture after disallowing it on a wide screen', function() {
  1214. viewport.set(1280);
  1215. window.initCore();
  1216. expect(snapperStub.enable.called).toBe(false);
  1217. expect(snapperStub.disable.calledOnce).toBe(true);
  1218. expect(snapperStub.close.calledOnce).toBe(true);
  1219. OC.disallowNavigationBarSlideGesture();
  1220. expect(snapperStub.enable.called).toBe(false);
  1221. expect(snapperStub.disable.calledOnce).toBe(true);
  1222. expect(snapperStub.close.calledOnce).toBe(true);
  1223. OC.allowNavigationBarSlideGesture();
  1224. expect(snapperStub.enable.called).toBe(false);
  1225. expect(snapperStub.disable.calledOnce).toBe(true);
  1226. expect(snapperStub.close.calledOnce).toBe(true);
  1227. });
  1228. it('is enabled when resizing to a narrow screen', function() {
  1229. viewport.set(1280);
  1230. window.initCore();
  1231. expect(snapperStub.enable.called).toBe(false);
  1232. expect(snapperStub.disable.calledOnce).toBe(true);
  1233. viewport.set(480);
  1234. // Setting the viewport width does not automatically trigger a
  1235. // resize.
  1236. $(window).resize();
  1237. // The resize handler is debounced to be executed a few milliseconds
  1238. // after the resize event.
  1239. clock.tick(1000);
  1240. expect(snapperStub.enable.calledOnce).toBe(true);
  1241. expect(snapperStub.disable.calledOnce).toBe(true);
  1242. });
  1243. it('is not enabled when resizing to a narrow screen after disallowing the gesture', function() {
  1244. viewport.set(1280);
  1245. window.initCore();
  1246. expect(snapperStub.enable.called).toBe(false);
  1247. expect(snapperStub.disable.calledOnce).toBe(true);
  1248. OC.disallowNavigationBarSlideGesture();
  1249. expect(snapperStub.enable.called).toBe(false);
  1250. expect(snapperStub.disable.calledOnce).toBe(true);
  1251. viewport.set(480);
  1252. // Setting the viewport width does not automatically trigger a
  1253. // resize.
  1254. $(window).resize();
  1255. // The resize handler is debounced to be executed a few milliseconds
  1256. // after the resize event.
  1257. clock.tick(1000);
  1258. expect(snapperStub.enable.called).toBe(false);
  1259. expect(snapperStub.disable.calledOnce).toBe(true);
  1260. });
  1261. it('is enabled when resizing to a narrow screen after disallowing the gesture and allowing it', function() {
  1262. viewport.set(1280);
  1263. window.initCore();
  1264. expect(snapperStub.enable.called).toBe(false);
  1265. expect(snapperStub.disable.calledOnce).toBe(true);
  1266. OC.disallowNavigationBarSlideGesture();
  1267. expect(snapperStub.enable.called).toBe(false);
  1268. expect(snapperStub.disable.calledOnce).toBe(true);
  1269. OC.allowNavigationBarSlideGesture();
  1270. expect(snapperStub.enable.called).toBe(false);
  1271. expect(snapperStub.disable.calledOnce).toBe(true);
  1272. viewport.set(480);
  1273. // Setting the viewport width does not automatically trigger a
  1274. // resize.
  1275. $(window).resize();
  1276. // The resize handler is debounced to be executed a few milliseconds
  1277. // after the resize event.
  1278. clock.tick(1000);
  1279. expect(snapperStub.enable.calledOnce).toBe(true);
  1280. expect(snapperStub.disable.calledOnce).toBe(true);
  1281. });
  1282. it('is enabled when allowing the gesture after disallowing it and resizing to a narrow screen', function() {
  1283. viewport.set(1280);
  1284. window.initCore();
  1285. expect(snapperStub.enable.called).toBe(false);
  1286. expect(snapperStub.disable.calledOnce).toBe(true);
  1287. OC.disallowNavigationBarSlideGesture();
  1288. expect(snapperStub.enable.called).toBe(false);
  1289. expect(snapperStub.disable.calledOnce).toBe(true);
  1290. viewport.set(480);
  1291. // Setting the viewport width does not automatically trigger a
  1292. // resize.
  1293. $(window).resize();
  1294. // The resize handler is debounced to be executed a few milliseconds
  1295. // after the resize event.
  1296. clock.tick(1000);
  1297. expect(snapperStub.enable.called).toBe(false);
  1298. expect(snapperStub.disable.calledOnce).toBe(true);
  1299. OC.allowNavigationBarSlideGesture();
  1300. expect(snapperStub.enable.calledOnce).toBe(true);
  1301. expect(snapperStub.disable.calledOnce).toBe(true);
  1302. });
  1303. it('is disabled when disallowing the gesture after disallowing it, resizing to a narrow screen and allowing it', function() {
  1304. viewport.set(1280);
  1305. window.initCore();
  1306. expect(snapperStub.enable.called).toBe(false);
  1307. expect(snapperStub.disable.calledOnce).toBe(true);
  1308. OC.disallowNavigationBarSlideGesture();
  1309. expect(snapperStub.enable.called).toBe(false);
  1310. expect(snapperStub.disable.calledOnce).toBe(true);
  1311. viewport.set(480);
  1312. // Setting the viewport width does not automatically trigger a
  1313. // resize.
  1314. $(window).resize();
  1315. // The resize handler is debounced to be executed a few milliseconds
  1316. // after the resize event.
  1317. clock.tick(1000);
  1318. expect(snapperStub.enable.called).toBe(false);
  1319. expect(snapperStub.disable.calledOnce).toBe(true);
  1320. OC.allowNavigationBarSlideGesture();
  1321. expect(snapperStub.enable.calledOnce).toBe(true);
  1322. expect(snapperStub.disable.calledOnce).toBe(true);
  1323. OC.disallowNavigationBarSlideGesture();
  1324. expect(snapperStub.enable.calledOnce).toBe(true);
  1325. expect(snapperStub.disable.calledTwice).toBe(true);
  1326. expect(snapperStub.disable.getCall(1).calledWithExactly(true)).toBe(true);
  1327. });
  1328. it('is disabled when resizing to a wide screen', function() {
  1329. viewport.set(480);
  1330. window.initCore();
  1331. expect(snapperStub.enable.calledOnce).toBe(true);
  1332. expect(snapperStub.disable.called).toBe(false);
  1333. expect(snapperStub.close.called).toBe(false);
  1334. viewport.set(1280);
  1335. // Setting the viewport width does not automatically trigger a
  1336. // resize.
  1337. $(window).resize();
  1338. // The resize handler is debounced to be executed a few milliseconds
  1339. // after the resize event.
  1340. clock.tick(1000);
  1341. expect(snapperStub.enable.calledOnce).toBe(true);
  1342. expect(snapperStub.disable.calledOnce).toBe(true);
  1343. expect(snapperStub.close.calledOnce).toBe(true);
  1344. });
  1345. it('is not disabled again when disallowing the gesture after resizing to a wide screen', function() {
  1346. viewport.set(480);
  1347. window.initCore();
  1348. expect(snapperStub.enable.calledOnce).toBe(true);
  1349. expect(snapperStub.disable.called).toBe(false);
  1350. expect(snapperStub.close.called).toBe(false);
  1351. viewport.set(1280);
  1352. // Setting the viewport width does not automatically trigger a
  1353. // resize.
  1354. $(window).resize();
  1355. // The resize handler is debounced to be executed a few milliseconds
  1356. // after the resize event.
  1357. clock.tick(1000);
  1358. expect(snapperStub.enable.calledOnce).toBe(true);
  1359. expect(snapperStub.disable.calledOnce).toBe(true);
  1360. expect(snapperStub.close.calledOnce).toBe(true);
  1361. OC.disallowNavigationBarSlideGesture();
  1362. expect(snapperStub.enable.calledOnce).toBe(true);
  1363. expect(snapperStub.disable.calledOnce).toBe(true);
  1364. expect(snapperStub.close.calledOnce).toBe(true);
  1365. });
  1366. it('is not enabled when allowing the gesture after disallowing it, resizing to a narrow screen and resizing to a wide screen', function() {
  1367. viewport.set(1280);
  1368. window.initCore();
  1369. expect(snapperStub.enable.called).toBe(false);
  1370. expect(snapperStub.disable.calledOnce).toBe(true);
  1371. expect(snapperStub.close.calledOnce).toBe(true);
  1372. OC.disallowNavigationBarSlideGesture();
  1373. expect(snapperStub.enable.called).toBe(false);
  1374. expect(snapperStub.disable.calledOnce).toBe(true);
  1375. expect(snapperStub.close.calledOnce).toBe(true);
  1376. viewport.set(480);
  1377. // Setting the viewport width does not automatically trigger a
  1378. // resize.
  1379. $(window).resize();
  1380. // The resize handler is debounced to be executed a few milliseconds
  1381. // after the resize event.
  1382. clock.tick(1000);
  1383. expect(snapperStub.enable.called).toBe(false);
  1384. expect(snapperStub.disable.calledOnce).toBe(true);
  1385. expect(snapperStub.close.calledOnce).toBe(true);
  1386. viewport.set(1280);
  1387. $(window).resize();
  1388. clock.tick(1000);
  1389. expect(snapperStub.enable.called).toBe(false);
  1390. expect(snapperStub.disable.calledTwice).toBe(true);
  1391. expect(snapperStub.close.calledTwice).toBe(true);
  1392. OC.allowNavigationBarSlideGesture();
  1393. expect(snapperStub.enable.called).toBe(false);
  1394. expect(snapperStub.disable.calledTwice).toBe(true);
  1395. expect(snapperStub.close.calledTwice).toBe(true);
  1396. });
  1397. });
  1398. describe('Requires password confirmation', function () {
  1399. var stubMomentNow;
  1400. var stubJsPageLoadTime;
  1401. afterEach(function () {
  1402. delete window.nc_pageLoad;
  1403. delete window.nc_lastLogin;
  1404. delete window.backendAllowsPasswordConfirmation;
  1405. stubMomentNow.restore();
  1406. stubJsPageLoadTime.restore();
  1407. });
  1408. it('should not show the password confirmation dialog when server time is earlier than local time', function () {
  1409. // add server variables
  1410. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000);
  1411. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000);
  1412. window.backendAllowsPasswordConfirmation = true;
  1413. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1414. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 20, 0).getTime());
  1415. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy();
  1416. });
  1417. it('should show the password confirmation dialog when server time is earlier than local time', function () {
  1418. // add server variables
  1419. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000);
  1420. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000);
  1421. window.backendAllowsPasswordConfirmation = true;
  1422. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1423. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 31, 0).getTime());
  1424. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy();
  1425. });
  1426. it('should not show the password confirmation dialog when server time is later than local time', function () {
  1427. // add server variables
  1428. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 23, 15, 0).getTime() / 1000);
  1429. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 23, 0, 0).getTime() / 1000);
  1430. window.backendAllowsPasswordConfirmation = true;
  1431. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1432. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 20, 0).getTime());
  1433. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy();
  1434. });
  1435. it('should show the password confirmation dialog when server time is later than local time', function () {
  1436. // add server variables
  1437. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 23, 15, 0).getTime() / 1000);
  1438. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 23, 0, 0).getTime() / 1000);
  1439. window.backendAllowsPasswordConfirmation = true;
  1440. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1441. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 31, 0).getTime());
  1442. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy();
  1443. });
  1444. });
  1445. });