ソースを参照

Merge remote-tracking branch 'origin/api-page-get-by-id' into wip-v1.6.3

Sotaro KARASAWA 8 年 前
コミット
c2a844e371
2 ファイル変更56 行追加6 行削除
  1. 15 5
      lib/routes/page.js
  2. 41 1
      test/models/page.test.js

+ 15 - 5
lib/routes/page.js

@@ -652,12 +652,22 @@ module.exports = function(crowi, app) {
    * @apiParam {String} revision_id
    */
   api.get = function(req, res){
-    var pagePath = req.query.path || null;
-    var pageId = req.query.page_id || null; // TODO: handling
-    var revisionId = req.query.revision_id || null;
+    const pagePath = req.query.path || null;
+    const pageId = req.query.page_id || null; // TODO: handling
+    const revisionId = req.query.revision_id || null;
 
-    Page.findPage(pagePath, req.user, revisionId)
-    .then(function(pageData) {
+    if (!pageId && !pagePath) {
+      return res.json(ApiResponse.error(new Error('Parameter path or page_id is required.')));
+    }
+
+    let pageFinder;
+    if (pageId) { // prioritized
+      pageFinder = Page.findPageByIdAndGrantedUser(pageId, req.user);
+    } else if (pagePath) {
+      pageFinder = Page.findPage(pagePath, req.user, revisionId);
+    }
+
+    pageFinder.then(function(pageData) {
       var result = {};
       result.page = pageData;
 

+ 41 - 1
test/models/page.test.js

@@ -9,7 +9,9 @@ chai.use(sinonChai);
 describe('Page', function () {
   var Page = utils.models.Page,
     User   = utils.models.User,
-    conn   = utils.mongoose.connection;
+    conn   = utils.mongoose.connection,
+    createdPages,
+    createdUsers;
 
   before(function (done) {
     Promise.resolve().then(function() {
@@ -20,6 +22,7 @@ describe('Page', function () {
 
       return testDBUtil.generateFixture(conn, 'User', userFixture);
     }).then(function(testUsers) {
+      createdUsers = testUsers;
       var testUser0 = testUsers[0];
 
       var fixture = [
@@ -63,6 +66,7 @@ describe('Page', function () {
 
       return testDBUtil.generateFixture(conn, 'Page', fixture)
       .then(function(pages) {
+        createdPages = pages;
         done();
       });
     });
@@ -279,4 +283,40 @@ describe('Page', function () {
     });
   });
 
+  describe('.findPage', function () {
+    context('findPageById', () => {
+      it('should find page', function(done) {
+        const pageToFind = createdPages[0];
+        Page.findPageById(pageToFind._id)
+        .then(pageData => {
+          expect(pageData.path).to.equal(pageToFind.path);
+          done();
+        });
+      });
+    });
+
+    context('findPageByIdAndGrantedUser', function() {
+      it('should find page', function(done) {
+        const pageToFind = createdPages[0];
+        const grantedUser = createdUsers[0];
+        Page.findPageByIdAndGrantedUser(pageToFind._id, grantedUser)
+        .then(pageData => {
+          expect(pageData.path).to.equal(pageToFind.path);
+          done();
+        });
+      });
+
+      it('should error by grant', done => {
+        const pageToFind = createdPages[0];
+        const grantedUser = createdUsers[1];
+        Page.findPageByIdAndGrantedUser(pageToFind._id, grantedUser)
+        .then(pageData => {
+        }).catch(err => {
+          expect(err).to.instanceof(Error);
+          done();
+        });
+      });
+    });
+  });
+
 });