page.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var chai = require('chai')
  2. , expect = chai.expect
  3. , sinon = require('sinon')
  4. , sinonChai = require('sinon-chai')
  5. , proxyquire = require('proxyquire')
  6. ;
  7. chai.use(sinonChai);
  8. describe('Page', function () {
  9. var conn
  10. , crowi = new (require(ROOT_DIR + '/lib/crowi'))(ROOT_DIR, process.env)
  11. , Page = proxyquire(MODEL_DIR + '/page.js', {mongoose: mongoose})(crowi)
  12. , User = proxyquire(MODEL_DIR + '/user.js', {mongoose: mongoose})(crowi)
  13. ;
  14. if (!mongoUri) {
  15. return;
  16. }
  17. before(function (done) {
  18. conn = mongoose.createConnection(mongoUri, function(err) {
  19. if (err) {
  20. done();
  21. }
  22. Page = conn.model('Page');
  23. User = conn.model('User');
  24. var fixture = [
  25. {
  26. path: '/user/anonymous/memo',
  27. grant: Page.GRANT_RESTRICTED,
  28. grantedUsers: []
  29. },
  30. {
  31. path: '/grant/public',
  32. grant: Page.GRANT_PUBLIC
  33. },
  34. {
  35. path: '/grant/restricted',
  36. grant: Page.GRANT_RESTRICTED
  37. },
  38. {
  39. path: '/grant/specified',
  40. grant: Page.GRANT_SPECIFIED
  41. },
  42. {
  43. path: '/grant/owner',
  44. grant: Page.GRANT_OWNER
  45. }
  46. ];
  47. var userFixture = [
  48. {userId: 'anonymous', email: 'anonymous@gmail.com'}
  49. ];
  50. testDBUtil.generateFixture(conn, 'Page', fixture, function() {});
  51. testDBUtil.generateFixture(conn, 'User', userFixture, done);
  52. });
  53. });
  54. after(function (done) {
  55. testDBUtil.cleanUpDb(conn, 'Page', function(err, doc) {
  56. conn.close();
  57. done();
  58. });
  59. });
  60. describe('.isPublic', function () {
  61. context('with a public page', function() {
  62. it('should return true', function(done) {
  63. Page.findOne({path: '/grant/public'}, function(err, page) {
  64. expect(err).to.be.null;
  65. expect(page.isPublic()).to.be.equal(true);
  66. done();
  67. });
  68. });
  69. });
  70. ['restricted', 'specified', 'owner'].forEach(function(grant) {
  71. context('with a ' + grant + ' page', function() {
  72. it('should return false', function(done) {
  73. Page.findOne({path: '/grant/' + grant}, function(err, page) {
  74. expect(err).to.be.null;
  75. expect(page.isPublic()).to.be.equal(false);
  76. done();
  77. });
  78. });
  79. });
  80. });
  81. });
  82. describe('.isGrantedFor', function() {
  83. context('with a granted user', function() {
  84. it('should return true', function(done) {
  85. User.find({userId: 'anonymous'}, function(err, user) {
  86. if (err) { done(err); }
  87. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  88. if (err) { done(err); }
  89. page.grantedUsers.push(user.id);
  90. page.save(function(err, newPage) {
  91. if (err) { done(err); }
  92. expect(newPage.isGrantedFor(user)).to.be.equal(true);
  93. done();
  94. });
  95. });
  96. });
  97. });
  98. });
  99. context('with a public page', function() {
  100. it('should return true', function(done) {
  101. User.find({userId: 'anonymous'}, function(err, user) {
  102. if (err) { done(err); }
  103. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  104. if (err) { done(err); }
  105. expect(page.isGrantedFor(user)).to.be.equal(true);
  106. done();
  107. });
  108. });
  109. });
  110. });
  111. context('with a restricted page and an user who has no grant', function() {
  112. it('should return false', function(done) {
  113. User.find({userId: 'anonymous'}, function(err, user) {
  114. if (err) { done(err); }
  115. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  116. if (err) { done(err); }
  117. expect(page.isGrantedFor(user)).to.be.equal(false);
  118. done();
  119. });
  120. });
  121. });
  122. });
  123. });
  124. });