page.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. testDBUtil.generateFixture(conn, 'Page', fixture)
  55. .then(function(pages) {
  56. done();
  57. });
  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('.isCreator', function() {
  83. context('with creator', function() {
  84. it('should return true', function(done) {
  85. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  86. if (err) { done(err); }
  87. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  88. expect(page.isCreator(user)).to.be.equal(true);
  89. done();
  90. })
  91. });
  92. });
  93. });
  94. context('with non-creator', function() {
  95. it('should return false', function(done) {
  96. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  97. if (err) { done(err); }
  98. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  99. expect(page.isCreator(user)).to.be.equal(false);
  100. done();
  101. })
  102. });
  103. });
  104. });
  105. });
  106. describe('.isGrantedFor', function() {
  107. context('with a granted user', function() {
  108. it('should return true', function(done) {
  109. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  110. if (err) { done(err); }
  111. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  112. if (err) { done(err); }
  113. expect(page.isGrantedFor(user)).to.be.equal(true);
  114. done();
  115. });
  116. });
  117. });
  118. });
  119. context('with a public page', function() {
  120. it('should return true', function(done) {
  121. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  122. if (err) { done(err); }
  123. Page.findOne({path: '/grant/public'}, function(err, page) {
  124. if (err) { done(err); }
  125. expect(page.isGrantedFor(user)).to.be.equal(true);
  126. done();
  127. });
  128. });
  129. });
  130. });
  131. context('with a restricted page and an user who has no grant', function() {
  132. it('should return false', function(done) {
  133. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  134. if (err) { done(err); }
  135. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  136. if (err) { done(err); }
  137. expect(page.isGrantedFor(user)).to.be.equal(false);
  138. done();
  139. });
  140. });
  141. });
  142. });
  143. });
  144. });