page.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. path: '/page/for/extended',
  55. grant: Page.GRANT_PUBLIC,
  56. creator: testUser0,
  57. extended: {hoge: 1}
  58. },
  59. ];
  60. testDBUtil.generateFixture(conn, 'Page', fixture)
  61. .then(function(pages) {
  62. done();
  63. });
  64. });
  65. });
  66. describe('.isPublic', function () {
  67. context('with a public page', function() {
  68. it('should return true', function(done) {
  69. Page.findOne({path: '/grant/public'}, function(err, page) {
  70. expect(err).to.be.null;
  71. expect(page.isPublic()).to.be.equal(true);
  72. done();
  73. });
  74. });
  75. });
  76. ['restricted', 'specified', 'owner'].forEach(function(grant) {
  77. context('with a ' + grant + ' page', function() {
  78. it('should return false', function(done) {
  79. Page.findOne({path: '/grant/' + grant}, function(err, page) {
  80. expect(err).to.be.null;
  81. expect(page.isPublic()).to.be.equal(false);
  82. done();
  83. });
  84. });
  85. });
  86. });
  87. });
  88. describe('.isCreator', function() {
  89. context('with creator', function() {
  90. it('should return true', function(done) {
  91. User.findOne({email: 'anonymous0@example.com'}, function(err, user) {
  92. if (err) { done(err); }
  93. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  94. expect(page.isCreator(user)).to.be.equal(true);
  95. done();
  96. })
  97. });
  98. });
  99. });
  100. context('with non-creator', function() {
  101. it('should return false', function(done) {
  102. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  103. if (err) { done(err); }
  104. Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
  105. expect(page.isCreator(user)).to.be.equal(false);
  106. done();
  107. })
  108. });
  109. });
  110. });
  111. });
  112. describe('.isGrantedFor', function() {
  113. context('with a granted user', 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. if (err) { done(err); }
  119. expect(page.isGrantedFor(user)).to.be.equal(true);
  120. done();
  121. });
  122. });
  123. });
  124. });
  125. context('with a public page', function() {
  126. it('should return true', function(done) {
  127. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  128. if (err) { done(err); }
  129. Page.findOne({path: '/grant/public'}, function(err, page) {
  130. if (err) { done(err); }
  131. expect(page.isGrantedFor(user)).to.be.equal(true);
  132. done();
  133. });
  134. });
  135. });
  136. });
  137. context('with a restricted page and an user who has no grant', function() {
  138. it('should return false', function(done) {
  139. User.findOne({email: 'anonymous1@example.com'}, function(err, user) {
  140. if (err) { done(err); }
  141. Page.findOne({path: '/grant/restricted'}, function(err, page) {
  142. if (err) { done(err); }
  143. expect(page.isGrantedFor(user)).to.be.equal(false);
  144. done();
  145. });
  146. });
  147. });
  148. });
  149. });
  150. describe('Extended field', function () {
  151. context('Slack Channel.', function() {
  152. it('should be empty', function(done) {
  153. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  154. expect(page.extended.hoge).to.be.equal(1);
  155. expect(page.getSlackChannel()).to.be.equal('');
  156. done();
  157. })
  158. });
  159. it('set slack channel and should get it and should keep hoge ', function(done) {
  160. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  161. page.updateSlackChannel('slack-channel1')
  162. .then(function(data) {
  163. Page.findOne({path: '/page/for/extended'}, function(err, page) {
  164. expect(page.extended.hoge).to.be.equal(1);
  165. expect(page.getSlackChannel()).to.be.equal('slack-channel1');
  166. done();
  167. });
  168. })
  169. });
  170. });
  171. });
  172. });
  173. });