page.test.js 3.8 KB

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