page.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var chai = require('chai')
  2. , expect = chai.expect
  3. , sinon = require('sinon')
  4. , sinonChai = require('sinon-chai')
  5. , proxyquire = require('proxyquire')
  6. , Promise = require('bluebird')
  7. ;
  8. chai.use(sinonChai);
  9. describe('Page', function () {
  10. var conn
  11. , crowi = new (require(ROOT_DIR + '/lib/crowi'))(ROOT_DIR, process.env)
  12. , Page = proxyquire(MODEL_DIR + '/page.js', {mongoose: mongoose})(crowi)
  13. , User = proxyquire(MODEL_DIR + '/user.js', {mongoose: mongoose})(crowi)
  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. var Page = conn.model('Page'),
  24. User = conn.model('User');
  25. new Promise(function(resolve, reject) {
  26. testDBUtil.cleanUpDb(conn, 'Page')
  27. .then(function() {
  28. return testDBUtil.cleanUpDb(conn, 'User')
  29. })
  30. .then(resolve);
  31. }).then(function() {
  32. var userFixture = [
  33. {name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com'},
  34. {name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com'}
  35. ];
  36. return new Promise(function(resolve, reject) {
  37. testDBUtil.generateFixture(conn, 'User', userFixture)
  38. .then(function(users) {
  39. resolve(users);
  40. });
  41. });
  42. }).then(function(testUsers) {
  43. var testUser0 = testUsers[0];
  44. var fixture = [
  45. {
  46. path: '/user/anonymous/memo',
  47. grant: Page.GRANT_RESTRICTED,
  48. grantedUsers: [testUser0],
  49. creator: testUser0
  50. },
  51. {
  52. path: '/grant/public',
  53. grant: Page.GRANT_PUBLIC,
  54. grantedUsers: [testUser0],
  55. creator: testUser0
  56. },
  57. {
  58. path: '/grant/restricted',
  59. grant: Page.GRANT_RESTRICTED,
  60. grantedUsers: [testUser0],
  61. creator: testUser0
  62. },
  63. {
  64. path: '/grant/specified',
  65. grant: Page.GRANT_SPECIFIED,
  66. grantedUsers: [testUser0],
  67. creator: testUser0
  68. },
  69. {
  70. path: '/grant/owner',
  71. grant: Page.GRANT_OWNER,
  72. grantedUsers: [testUser0],
  73. creator: testUser0
  74. }
  75. ];
  76. testDBUtil.generateFixture(conn, 'Page', fixture)
  77. .then(function(pages) {
  78. done();
  79. });
  80. });
  81. });
  82. });
  83. after(function (done) {
  84. testDBUtil.cleanUpDb(conn, 'Page')
  85. .then(function() {
  86. return testDBUtil.cleanUpDb(conn, 'User')
  87. })
  88. .then(done);
  89. });
  90. describe('.isPublic', function () {
  91. context('with a public page', function() {
  92. it('should return true', function(done) {
  93. Page.findOne({path: '/grant/public'}, function(err, page) {
  94. expect(err).to.be.null;
  95. expect(page.isPublic()).to.be.equal(true);
  96. done();
  97. });
  98. });
  99. });
  100. ['restricted', 'specified', 'owner'].forEach(function(grant) {
  101. context('with a ' + grant + ' page', function() {
  102. it('should return false', function(done) {
  103. Page.findOne({path: '/grant/' + grant}, function(err, page) {
  104. expect(err).to.be.null;
  105. expect(page.isPublic()).to.be.equal(false);
  106. done();
  107. });
  108. });
  109. });
  110. });
  111. });
  112. describe('.isCreator', function() {
  113. context('with creator', function() {
  114. it('should return true', function(done) {
  115. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  116. if (err) { done(err); }
  117. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  118. expect(page.isCreator(user)).to.be.equal(true);
  119. done();
  120. })
  121. });
  122. });
  123. });
  124. context('with non-creator', function() {
  125. it('should return false', function(done) {
  126. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  127. if (err) { done(err); }
  128. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  129. expect(page.isCreator(user)).to.be.equal(false);
  130. done();
  131. })
  132. });
  133. });
  134. });
  135. });
  136. describe('.isGrantedFor', function() {
  137. context('with a granted user', function() {
  138. it('should return true', function(done) {
  139. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  140. if (err) { done(err); }
  141. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  142. if (err) { done(err); }
  143. expect(page.isGrantedFor(user)).to.be.equal(true);
  144. done();
  145. });
  146. });
  147. });
  148. });
  149. context('with a public page', function() {
  150. it('should return true', function(done) {
  151. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  152. if (err) { done(err); }
  153. Page.findOne({path: '/grant/public'}, function(err, page) {
  154. if (err) { done(err); }
  155. expect(page.isGrantedFor(user)).to.be.equal(true);
  156. done();
  157. });
  158. });
  159. });
  160. });
  161. context('with a restricted page and an user who has no grant', function() {
  162. it('should return false', function(done) {
  163. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  164. if (err) { done(err); }
  165. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  166. if (err) { done(err); }
  167. expect(page.isGrantedFor(user)).to.be.equal(false);
  168. done();
  169. });
  170. });
  171. });
  172. });
  173. });
  174. });