implemented changelog
This commit is contained in:
		@@ -596,6 +596,64 @@
 | 
			
		||||
        "key": "000000000000000000001004",
 | 
			
		||||
        "__v": 0
 | 
			
		||||
      }
 | 
			
		||||
    ],
 | 
			
		||||
    "changelogs": [
 | 
			
		||||
      {
 | 
			
		||||
        "_id" : {"$oid": "120000010000000000000000"},
 | 
			
		||||
        "action" : "PUT /sample/400000000000000000000001",
 | 
			
		||||
        "collectionName" : "samples",
 | 
			
		||||
        "conditions" : {
 | 
			
		||||
          "_id" : {"$oid": "400000000000000000000001"}
 | 
			
		||||
        },
 | 
			
		||||
        "data" : {
 | 
			
		||||
          "type" : "part",
 | 
			
		||||
          "status" : 0
 | 
			
		||||
        },
 | 
			
		||||
        "user_id" : {"$oid": "000000000000000000000003"},
 | 
			
		||||
        "__v" : 0
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "_id" : {"$oid": "120000020000000000000000"},
 | 
			
		||||
        "action" : "PUT /sample/400000000000000000000001",
 | 
			
		||||
        "collectionName" : "samples",
 | 
			
		||||
        "conditions" : {
 | 
			
		||||
          "_id" : {"$oid": "400000000000000000000001"}
 | 
			
		||||
        },
 | 
			
		||||
        "data" : {
 | 
			
		||||
          "type" : "part",
 | 
			
		||||
          "status" : 0
 | 
			
		||||
        },
 | 
			
		||||
        "user_id" : {"$oid": "000000000000000000000003"},
 | 
			
		||||
        "__v" : 0
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "_id" : {"$oid": "120000030000000000000000"},
 | 
			
		||||
        "action" : "PUT /sample/400000000000000000000001",
 | 
			
		||||
        "collectionName" : "samples",
 | 
			
		||||
        "conditions" : {
 | 
			
		||||
          "_id" : {"$oid": "400000000000000000000001"}
 | 
			
		||||
        },
 | 
			
		||||
        "data" : {
 | 
			
		||||
          "type" : "part",
 | 
			
		||||
          "status" : 0
 | 
			
		||||
        },
 | 
			
		||||
        "user_id" : {"$oid": "000000000000000000000003"},
 | 
			
		||||
        "__v" : 0
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "_id" : {"$oid": "120000040000000000000000"},
 | 
			
		||||
        "action" : "PUT /sample/400000000000000000000001",
 | 
			
		||||
        "collectionName" : "samples",
 | 
			
		||||
        "conditions" : {
 | 
			
		||||
          "_id" : {"$oid": "400000000000000000000001"}
 | 
			
		||||
        },
 | 
			
		||||
        "data" : {
 | 
			
		||||
          "type" : "part",
 | 
			
		||||
          "status" : 0
 | 
			
		||||
        },
 | 
			
		||||
        "user_id" : {"$oid": "000000000000000000000003"},
 | 
			
		||||
        "__v" : 0
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +1,17 @@
 | 
			
		||||
import supertest from 'supertest';
 | 
			
		||||
import should from 'should/as-function';
 | 
			
		||||
import db from "../db";
 | 
			
		||||
import _ from 'lodash';
 | 
			
		||||
import db from '../db';
 | 
			
		||||
import ChangelogModel from '../models/changelog';
 | 
			
		||||
import IdValidate from '../routes/validate/id';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export default class TestHelper {
 | 
			
		||||
  public static auth = {  // test user credentials
 | 
			
		||||
    admin: {pass: 'Abc123!#', key: '000000000000000000001003'},
 | 
			
		||||
    janedoe: {pass: 'Xyz890*)', key: '000000000000000000001002'},
 | 
			
		||||
    user: {pass: 'Xyz890*)', key: '000000000000000000001001'},
 | 
			
		||||
    johnnydoe: {pass: 'Xyz890*)', key: '000000000000000000001004'}
 | 
			
		||||
    admin: {pass: 'Abc123!#', key: '000000000000000000001003', id: '000000000000000000000003'},
 | 
			
		||||
    janedoe: {pass: 'Xyz890*)', key: '000000000000000000001002', id: '000000000000000000000002'},
 | 
			
		||||
    user: {pass: 'Xyz890*)', key: '000000000000000000001001', id: '000000000000000000000001'},
 | 
			
		||||
    johnnydoe: {pass: 'Xyz890*)', key: '000000000000000000001004', id: '000000000000000000000004'}
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static res = {  // default responses
 | 
			
		||||
@@ -92,6 +95,35 @@ export default class TestHelper {
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
    else if (options.hasOwnProperty('log')) {  // check changelog, takes log: {collection, skip, data/(dataAdd, dataIgn)}
 | 
			
		||||
      return st.end(err => {
 | 
			
		||||
        if (err) return done (err);
 | 
			
		||||
        ChangelogModel.findOne({}).sort({_id: -1}).skip(options.log.skip? options.log.skip : 0).lean().exec((err, data) => {  // latest entry
 | 
			
		||||
          if (err) return done(err);
 | 
			
		||||
          should(data).have.only.keys('_id', 'action', 'collectionName', 'conditions', 'data', 'user_id', '__v');
 | 
			
		||||
          should(data).have.property('action', options.method.toUpperCase() + ' ' + options.url);
 | 
			
		||||
          should(data).have.property('collectionName', options.log.collection);
 | 
			
		||||
          if (options.log.hasOwnProperty('data')) {
 | 
			
		||||
            should(data).have.property('data', options.log.data);
 | 
			
		||||
          }
 | 
			
		||||
          else {
 | 
			
		||||
            const ignore = ['_id', '__v'];
 | 
			
		||||
            if (options.log.hasOwnProperty('dataIgn')) {
 | 
			
		||||
              ignore.push(...options.log.dataIgn);
 | 
			
		||||
            }
 | 
			
		||||
            let tmp = options.req ? options.req : {};
 | 
			
		||||
            if (options.log.hasOwnProperty('dataAdd')) {
 | 
			
		||||
              _.assign(tmp, options.log.dataAdd)
 | 
			
		||||
            }
 | 
			
		||||
            should(IdValidate.stringify(_.omit(data.data, ignore))).be.eql(_.omit(tmp, ignore));
 | 
			
		||||
          }
 | 
			
		||||
          if (data.user_id) {
 | 
			
		||||
            should(data.user_id.toString()).be.eql(this.auth[options.auth.basic].id);
 | 
			
		||||
          }
 | 
			
		||||
          done();
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
    else {  // return object to do .end() manually
 | 
			
		||||
      return st;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user