page.test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. var forbidden = ['installer', 'register', 'login', 'logout', 'admin', 'files', 'trash', 'paste', 'comments'];
  133. for (var i = 0; i < forbidden.length ; i++) {
  134. var pn = forbidden[i];
  135. expect(Page.isCreatableName('/' + pn + '')).to.be.false;
  136. expect(Page.isCreatableName('/' + pn + '/')).to.be.false;
  137. expect(Page.isCreatableName('/' + pn + '/abc')).to.be.false;
  138. }
  139. var forbidden = ['bookmarks', 'comments', 'activities', 'pages', 'recent-create', 'recent-edit'];
  140. for (var i = 0; i < forbidden.length ; i++) {
  141. var pn = forbidden[i];
  142. expect(Page.isCreatableName('/user/aoi/' + pn)).to.be.false;
  143. expect(Page.isCreatableName('/user/aoi/x/' + pn)).to.be.true;
  144. }
  145. });
  146. });
  147. describe('.isCreator', function() {
  148. context('with creator', function() {
  149. it('should return true', function(done) {
  150. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  151. if (err) { done(err); }
  152. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  153. expect(page.isCreator(user)).to.be.equal(true);
  154. done();
  155. })
  156. });
  157. });
  158. });
  159. context('with non-creator', function() {
  160. it('should return false', function(done) {
  161. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  162. if (err) { done(err); }
  163. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  164. expect(page.isCreator(user)).to.be.equal(false);
  165. done();
  166. })
  167. });
  168. });
  169. });
  170. });
  171. describe('.isGrantedFor', function() {
  172. context('with a granted user', function() {
  173. it('should return true', function(done) {
  174. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  175. if (err) { done(err); }
  176. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  177. if (err) { done(err); }
  178. expect(page.isGrantedFor(user)).to.be.equal(true);
  179. done();
  180. });
  181. });
  182. });
  183. });
  184. context('with a public page', function() {
  185. it('should return true', function(done) {
  186. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  187. if (err) { done(err); }
  188. Page.findOne({path: '/grant/public'}, function(err, page) {
  189. if (err) { done(err); }
  190. expect(page.isGrantedFor(user)).to.be.equal(true);
  191. done();
  192. });
  193. });
  194. });
  195. });
  196. context('with a restricted page and an user who has no grant', function() {
  197. it('should return false', function(done) {
  198. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  199. if (err) { done(err); }
  200. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  201. if (err) { done(err); }
  202. expect(page.isGrantedFor(user)).to.be.equal(false);
  203. done();
  204. });
  205. });
  206. });
  207. });
  208. });
  209. describe('Extended field', function () {
  210. context('Slack Channel.', function() {
  211. it('should be empty', function(done) {
  212. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  213. expect(page.extended.hoge).to.be.equal(1);
  214. expect(page.getSlackChannel()).to.be.equal('');
  215. done();
  216. })
  217. });
  218. it('set slack channel and should get it and should keep hoge ', function(done) {
  219. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  220. page.updateSlackChannel('slack-channel1')
  221. .then(function(data) {
  222. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  223. expect(page.extended.hoge).to.be.equal(1);
  224. expect(page.getSlackChannel()).to.be.equal('slack-channel1');
  225. done();
  226. });
  227. })
  228. });
  229. });
  230. });
  231. });
  232. });