page.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return testDBUtil.cleanUpDb(conn, 'Page', function(err, doc) {
  56. return conn.close(done);
  57. });
  58. });
  59. describe('.isPublic', function () {
  60. context('with a public page', function() {
  61. it('should return true', function(done) {
  62. Page.findOne({path: '/grant/public'}, function(err, page) {
  63. expect(err).to.be.null;
  64. expect(page.isPublic()).to.be.equal(true);
  65. done();
  66. });
  67. });
  68. });
  69. ['restricted', 'specified', 'owner'].forEach(function(grant) {
  70. context('with a ' + grant + ' page', function() {
  71. it('should return false', function(done) {
  72. Page.findOne({path: '/grant/' + grant}, function(err, page) {
  73. expect(err).to.be.null;
  74. expect(page.isPublic()).to.be.equal(false);
  75. done();
  76. });
  77. });
  78. });
  79. });
  80. });
  81. describe('.isGrantedFor', function() {
  82. context('with a granted user', function() {
  83. it('should return true', function(done) {
  84. User.find({userId: 'anonymous'}, function(err, user) {
  85. if (err) { done(err); }
  86. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  87. if (err) { done(err); }
  88. page.grantedUsers.push(user.id);
  89. page.save(function(err, newPage) {
  90. if (err) { done(err); }
  91. expect(newPage.isGrantedFor(user)).to.be.equal(true);
  92. done();
  93. });
  94. });
  95. });
  96. });
  97. });
  98. context('with a public page', function() {
  99. it('should return true', function(done) {
  100. User.find({userId: 'anonymous'}, function(err, user) {
  101. if (err) { done(err); }
  102. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  103. if (err) { done(err); }
  104. expect(page.isGrantedFor(user)).to.be.equal(true);
  105. done();
  106. });
  107. });
  108. });
  109. });
  110. context('with a restricted page and an user who has no grant', function() {
  111. it('should return false', function(done) {
  112. User.find({userId: 'anonymous'}, function(err, user) {
  113. if (err) { done(err); }
  114. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  115. if (err) { done(err); }
  116. expect(page.isGrantedFor(user)).to.be.equal(false);
  117. done();
  118. });
  119. });
  120. });
  121. });
  122. });
  123. });