page.test.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. var chai = require('chai')
  2. , expect = chai.expect
  3. , sinon = require('sinon')
  4. , sinonChai = require('sinon-chai')
  5. , utils = require('../utils.js')
  6. ;
  7. chai.use(sinonChai);
  8. describe('Page', function () {
  9. var Page = utils.models.Page,
  10. User = utils.models.User,
  11. conn = utils.mongoose.connection;
  12. before(function (done) {
  13. Promise.resolve().then(function() {
  14. var userFixture = [
  15. {name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com'},
  16. {name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com'}
  17. ];
  18. return testDBUtil.generateFixture(conn, 'User', userFixture);
  19. }).then(function(testUsers) {
  20. var testUser0 = testUsers[0];
  21. var fixture = [
  22. {
  23. path: '/user/anonymous/memo',
  24. grant: Page.GRANT_RESTRICTED,
  25. grantedUsers: [testUser0],
  26. creator: testUser0
  27. },
  28. {
  29. path: '/grant/public',
  30. grant: Page.GRANT_PUBLIC,
  31. grantedUsers: [testUser0],
  32. creator: testUser0
  33. },
  34. {
  35. path: '/grant/restricted',
  36. grant: Page.GRANT_RESTRICTED,
  37. grantedUsers: [testUser0],
  38. creator: testUser0
  39. },
  40. {
  41. path: '/grant/specified',
  42. grant: Page.GRANT_SPECIFIED,
  43. grantedUsers: [testUser0],
  44. creator: testUser0
  45. },
  46. {
  47. path: '/grant/owner',
  48. grant: Page.GRANT_OWNER,
  49. grantedUsers: [testUser0],
  50. creator: testUser0,
  51. },
  52. {
  53. path: '/page/for/extended',
  54. grant: Page.GRANT_PUBLIC,
  55. creator: testUser0,
  56. extended: {hoge: 1}
  57. },
  58. ];
  59. return testDBUtil.generateFixture(conn, 'Page', fixture)
  60. .then(function(pages) {
  61. done();
  62. });
  63. });
  64. });
  65. describe('.isPublic', function () {
  66. context('with a public page', function() {
  67. it('should return true', function(done) {
  68. Page.findOne({path: '/grant/public'}, function(err, page) {
  69. expect(err).to.be.null;
  70. expect(page.isPublic()).to.be.equal(true);
  71. done();
  72. });
  73. });
  74. });
  75. ['restricted', 'specified', 'owner'].forEach(function(grant) {
  76. context('with a ' + grant + ' page', function() {
  77. it('should return false', function(done) {
  78. Page.findOne({path: '/grant/' + grant}, function(err, page) {
  79. expect(err).to.be.null;
  80. expect(page.isPublic()).to.be.equal(false);
  81. done();
  82. });
  83. });
  84. });
  85. });
  86. });
  87. describe('.getDeletedPageName', function() {
  88. it('should return trash page name', function() {
  89. expect(Page.getDeletedPageName('/hoge')).to.be.equal('/trash/hoge');
  90. expect(Page.getDeletedPageName('hoge')).to.be.equal('/trash/hoge');
  91. });
  92. });
  93. describe('.getRevertDeletedPageName', function() {
  94. it('should return reverted trash page name', function() {
  95. expect(Page.getRevertDeletedPageName('/hoge')).to.be.equal('/hoge');
  96. expect(Page.getRevertDeletedPageName('/trash/hoge')).to.be.equal('/hoge');
  97. expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).to.be.equal('/hoge/trash');
  98. });
  99. });
  100. describe('.isDeletableName', function() {
  101. it('should decide deletable or not', function() {
  102. expect(Page.isDeletableName('/hoge')).to.be.true;
  103. expect(Page.isDeletableName('/user/xxx')).to.be.false;
  104. expect(Page.isDeletableName('/user/xxx123')).to.be.false;
  105. expect(Page.isDeletableName('/user/xxx/')).to.be.true;
  106. expect(Page.isDeletableName('/user/xxx/hoge')).to.be.true;
  107. });
  108. });
  109. describe('.isCreatableName', function() {
  110. it('should decide creatable or not', function() {
  111. expect(Page.isCreatableName('/hoge')).to.be.true;
  112. // edge cases
  113. expect(Page.isCreatableName('/me')).to.be.false;
  114. expect(Page.isCreatableName('/me/')).to.be.false;
  115. expect(Page.isCreatableName('/me/x')).to.be.false;
  116. expect(Page.isCreatableName('/meeting')).to.be.true;
  117. expect(Page.isCreatableName('/meeting/x')).to.be.true;
  118. // end with "edit"
  119. expect(Page.isCreatableName('/meeting/edit')).to.be.false;
  120. // under score
  121. expect(Page.isCreatableName('/_')).to.be.false;
  122. expect(Page.isCreatableName('/_r/x')).to.be.false;
  123. expect(Page.isCreatableName('/_api')).to.be.false;
  124. expect(Page.isCreatableName('/_apix')).to.be.false;
  125. expect(Page.isCreatableName('/_api/x')).to.be.false;
  126. expect(Page.isCreatableName('/hoge/xx.md')).to.be.false;
  127. // start with https?
  128. expect(Page.isCreatableName('/http://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  129. expect(Page.isCreatableName('/https://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  130. expect(Page.isCreatableName('http://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  131. expect(Page.isCreatableName('https://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  132. expect(Page.isCreatableName('/ the / path / with / space')).to.be.false;
  133. var forbidden = ['installer', 'register', 'login', 'logout', 'admin', 'files', 'trash', 'paste', 'comments'];
  134. for (var i = 0; i < forbidden.length ; i++) {
  135. var pn = forbidden[i];
  136. expect(Page.isCreatableName('/' + pn + '')).to.be.false;
  137. expect(Page.isCreatableName('/' + pn + '/')).to.be.false;
  138. expect(Page.isCreatableName('/' + pn + '/abc')).to.be.false;
  139. }
  140. var forbidden = ['bookmarks', 'comments', 'activities', 'pages', 'recent-create', 'recent-edit'];
  141. for (var i = 0; i < forbidden.length ; i++) {
  142. var pn = forbidden[i];
  143. expect(Page.isCreatableName('/user/aoi/' + pn)).to.be.false;
  144. expect(Page.isCreatableName('/user/aoi/x/' + pn)).to.be.true;
  145. }
  146. });
  147. });
  148. describe('.isCreator', function() {
  149. context('with creator', function() {
  150. it('should return true', function(done) {
  151. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  152. if (err) { done(err); }
  153. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  154. expect(page.isCreator(user)).to.be.equal(true);
  155. done();
  156. })
  157. });
  158. });
  159. });
  160. context('with non-creator', function() {
  161. it('should return false', function(done) {
  162. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  163. if (err) { done(err); }
  164. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  165. expect(page.isCreator(user)).to.be.equal(false);
  166. done();
  167. })
  168. });
  169. });
  170. });
  171. });
  172. describe('.isGrantedFor', function() {
  173. context('with a granted user', function() {
  174. it('should return true', function(done) {
  175. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  176. if (err) { done(err); }
  177. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  178. if (err) { done(err); }
  179. expect(page.isGrantedFor(user)).to.be.equal(true);
  180. done();
  181. });
  182. });
  183. });
  184. });
  185. context('with a public page', function() {
  186. it('should return true', function(done) {
  187. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  188. if (err) { done(err); }
  189. Page.findOne({path: '/grant/public'}, function(err, page) {
  190. if (err) { done(err); }
  191. expect(page.isGrantedFor(user)).to.be.equal(true);
  192. done();
  193. });
  194. });
  195. });
  196. });
  197. context('with a restricted page and an user who has no grant', function() {
  198. it('should return false', function(done) {
  199. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  200. if (err) { done(err); }
  201. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  202. if (err) { done(err); }
  203. expect(page.isGrantedFor(user)).to.be.equal(false);
  204. done();
  205. });
  206. });
  207. });
  208. });
  209. });
  210. describe('Extended field', function () {
  211. context('Slack Channel.', function() {
  212. it('should be empty', function(done) {
  213. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  214. expect(page.extended.hoge).to.be.equal(1);
  215. expect(page.getSlackChannel()).to.be.equal('');
  216. done();
  217. })
  218. });
  219. it('set slack channel and should get it and should keep hoge ', function(done) {
  220. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  221. page.updateSlackChannel('slack-channel1')
  222. .then(function(data) {
  223. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  224. expect(page.extended.hoge).to.be.equal(1);
  225. expect(page.getSlackChannel()).to.be.equal('slack-channel1');
  226. done();
  227. });
  228. })
  229. });
  230. });
  231. });
  232. });
  233. describe('Normalize path', function () {
  234. context('Normalize', function() {
  235. it('should start with slash', function(done) {
  236. expect(Page.normalizePath('hoge/fuga')).to.equal('/hoge/fuga');
  237. done();
  238. });
  239. it('should trim spaces of slash', function(done) {
  240. expect(Page.normalizePath('/ hoge / fuga')).to.equal('/hoge/fuga');
  241. done();
  242. });
  243. });
  244. });
  245. });