Просмотр исходного кода

add a unittest for Page model.

Shinya Yamaoka 11 лет назад
Родитель
Сommit
b72138ec66
1 измененных файлов с 145 добавлено и 0 удалено
  1. 145 0
      test/models/page.test.js

+ 145 - 0
test/models/page.test.js

@@ -0,0 +1,145 @@
+var chai = require('chai')
+  , expect = chai.expect
+  , sinon = require('sinon')
+  , sinonChai = require('sinon-chai')
+  , proxyquire = require('proxyquire')
+  ;
+chai.use(sinonChai);
+
+describe('Page', function () {
+  var conn
+    , app = new express()
+    , models = proxyquire(MODEL_DIR, {mongoose: mongoose})(app)
+    , Page = proxyquire(MODEL_DIR + '/page.js', {mongoose: mongoose})(app, models)
+    , User = proxyquire(MODEL_DIR + '/user.js', {mongoose: mongoose})(app, models)
+    ;
+
+  if (!mongoUri) {
+    return;
+  }
+
+  before(function (done) {
+    conn = mongoose.createConnection(mongoUri, function(err) {
+      if (err) {
+        done();
+      }
+
+      Page = conn.model('Page');
+      User = conn.model('User');
+
+      var fixture = [
+        {
+          path: '/user/anonymous/memo',
+          grant: Page.GRANT_RESTRICTED,
+          grantedUsers: []
+        },
+        {
+          path: '/grant/public',
+          grant: Page.GRANT_PUBLIC
+        },
+        {
+          path: '/grant/restricted',
+          grant: Page.GRANT_RESTRICTED
+        },
+        {
+          path: '/grant/specified',
+          grant: Page.GRANT_SPECIFIED
+        },
+        {
+          path: '/grant/owner',
+          grant: Page.GRANT_OWNER
+        }
+      ];
+      var userFixture = [
+        {userId: 'anonymous', email: 'anonymous@gmail.com'}
+      ];
+
+      testDBUtil.generateFixture(conn, 'Page', fixture, function() {});
+      testDBUtil.generateFixture(conn, 'User', userFixture, done);
+    });
+  });
+
+  after(function (done) {
+    testDBUtil.cleanUpDb(conn, 'Page', function(err, doc) {
+      conn.close();
+      done();
+    });
+  });
+
+  describe('.isPublic', function () {
+    context('with a public page', function() {
+      it('should return true', function(done) {
+        Page.findOne({path: '/grant/public'}, function(err, page) {
+          expect(err).to.be.null;
+          expect(page.isPublic()).to.be.equal(true);
+          done();
+        });
+      });
+    });
+
+    ['restricted', 'specified', 'owner'].forEach(function(grant) {
+      context('with a ' + grant + ' page', function() {
+        it('should return false', function(done) {
+          Page.findOne({path: '/grant/' + grant}, function(err, page) {
+            expect(err).to.be.null;
+            expect(page.isPublic()).to.be.equal(false);
+            done();
+          });
+        });
+      });
+    });
+  });
+
+  describe('.isGrantedFor', function() {
+    context('with a granted user', function() {
+      it('should return true', function(done) {
+        User.find({userId: 'anonymous'}, function(err, user) {
+          if (err) { done(err); }
+
+          Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
+            if (err) { done(err); }
+
+            page.grantedUsers.push(user.id);
+
+            page.save(function(err, newPage) {
+              if (err) { done(err); }
+
+              expect(newPage.isGrantedFor(user)).to.be.equal(true);
+              done();
+            });
+          });
+        });
+      });
+    });
+
+    context('with a public page', function() {
+      it('should return true', function(done) {
+        User.find({userId: 'anonymous'}, function(err, user) {
+          if (err) { done(err); }
+
+          Page.findOne({path: '/user/anonymous/memo'}, function(err, page) {
+            if (err) { done(err); }
+
+            expect(page.isGrantedFor(user)).to.be.equal(true);
+            done();
+          });
+        });
+      });
+    });
+
+    context('with a restricted page and an user who has no grant', function() {
+      it('should return false', function(done) {
+        User.find({userId: 'anonymous'}, function(err, user) {
+          if (err) { done(err); }
+
+          Page.findOne({path: '/grant/restricted'}, function(err, page) {
+            if (err) { done(err); }
+
+            expect(page.isGrantedFor(user)).to.be.equal(false);
+            done();
+          });
+        });
+      });
+    });
+  });
+});