page.test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. var chai = require('chai')
  2. , expect = chai.expect
  3. , sinon = require('sinon')
  4. , sinonChai = require('sinon-chai')
  5. , Promise = require('bluebird')
  6. , utils = require('../utils.js')
  7. ;
  8. chai.use(sinonChai);
  9. describe('Page', function () {
  10. var Page = utils.models.Page,
  11. User = utils.models.User,
  12. conn = utils.mongoose.connection;
  13. before(function (done) {
  14. Promise.resolve().then(function() {
  15. var userFixture = [
  16. {name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com'},
  17. {name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com'}
  18. ];
  19. return testDBUtil.generateFixture(conn, 'User', userFixture);
  20. }).then(function(testUsers) {
  21. var testUser0 = testUsers[0];
  22. var fixture = [
  23. {
  24. path: '/user/anonymous/memo',
  25. grant: Page.GRANT_RESTRICTED,
  26. grantedUsers: [testUser0],
  27. creator: testUser0
  28. },
  29. {
  30. path: '/grant/public',
  31. grant: Page.GRANT_PUBLIC,
  32. grantedUsers: [testUser0],
  33. creator: testUser0
  34. },
  35. {
  36. path: '/grant/restricted',
  37. grant: Page.GRANT_RESTRICTED,
  38. grantedUsers: [testUser0],
  39. creator: testUser0
  40. },
  41. {
  42. path: '/grant/specified',
  43. grant: Page.GRANT_SPECIFIED,
  44. grantedUsers: [testUser0],
  45. creator: testUser0
  46. },
  47. {
  48. path: '/grant/owner',
  49. grant: Page.GRANT_OWNER,
  50. grantedUsers: [testUser0],
  51. creator: testUser0,
  52. },
  53. {
  54. path: '/page/for/extended',
  55. grant: Page.GRANT_PUBLIC,
  56. creator: testUser0,
  57. extended: {hoge: 1}
  58. },
  59. ];
  60. return testDBUtil.generateFixture(conn, 'Page', fixture)
  61. .then(function(pages) {
  62. done();
  63. });
  64. });
  65. });
  66. describe('.isPublic', function () {
  67. context('with a public page', function() {
  68. it('should return true', function(done) {
  69. Page.findOne({path: '/grant/public'}, function(err, page) {
  70. expect(err).to.be.null;
  71. expect(page.isPublic()).to.be.equal(true);
  72. done();
  73. });
  74. });
  75. });
  76. ['restricted', 'specified', 'owner'].forEach(function(grant) {
  77. context('with a ' + grant + ' page', function() {
  78. it('should return false', function(done) {
  79. Page.findOne({path: '/grant/' + grant}, function(err, page) {
  80. expect(err).to.be.null;
  81. expect(page.isPublic()).to.be.equal(false);
  82. done();
  83. });
  84. });
  85. });
  86. });
  87. });
  88. describe('.getDeletedPageName', function() {
  89. it('should return trash page name', function() {
  90. expect(Page.getDeletedPageName('/hoge')).to.be.equal('/trash/hoge');
  91. expect(Page.getDeletedPageName('hoge')).to.be.equal('/trash/hoge');
  92. });
  93. });
  94. describe('.getRevertDeletedPageName', function() {
  95. it('should return reverted trash page name', function() {
  96. expect(Page.getRevertDeletedPageName('/hoge')).to.be.equal('/hoge');
  97. expect(Page.getRevertDeletedPageName('/trash/hoge')).to.be.equal('/hoge');
  98. expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).to.be.equal('/hoge/trash');
  99. });
  100. });
  101. describe('.isDeletableName', function() {
  102. it('should decide deletable or not', function() {
  103. expect(Page.isDeletableName('/hoge')).to.be.true;
  104. expect(Page.isDeletableName('/user/xxx')).to.be.false;
  105. expect(Page.isDeletableName('/user/xxx123')).to.be.false;
  106. expect(Page.isDeletableName('/user/xxx/')).to.be.true;
  107. expect(Page.isDeletableName('/user/xxx/hoge')).to.be.true;
  108. });
  109. });
  110. describe('.isCreatableName', function() {
  111. it('should decide creatable or not', function() {
  112. expect(Page.isCreatableName('/hoge')).to.be.true;
  113. // edge cases
  114. expect(Page.isCreatableName('/me')).to.be.false;
  115. expect(Page.isCreatableName('/me/')).to.be.false;
  116. expect(Page.isCreatableName('/me/x')).to.be.false;
  117. expect(Page.isCreatableName('/meeting')).to.be.true;
  118. expect(Page.isCreatableName('/meeting/x')).to.be.true;
  119. // end with "edit"
  120. expect(Page.isCreatableName('/meeting/edit')).to.be.false;
  121. // under score
  122. expect(Page.isCreatableName('/_')).to.be.false;
  123. expect(Page.isCreatableName('/_r/x')).to.be.false;
  124. expect(Page.isCreatableName('/_api')).to.be.false;
  125. expect(Page.isCreatableName('/_apix')).to.be.false;
  126. expect(Page.isCreatableName('/_api/x')).to.be.false;
  127. expect(Page.isCreatableName('/hoge/xx.md')).to.be.false;
  128. // start with https?
  129. expect(Page.isCreatableName('/http://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  130. expect(Page.isCreatableName('/https://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  131. expect(Page.isCreatableName('http://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
  132. expect(Page.isCreatableName('https://demo.crowi.wiki/user/sotarok/hoge')).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. });