Skip to content

Commit f2e6ea3

Browse files
committed
Merge branch 'UnitTest', near perfect mongoose schemes coverage
2 parents 7952cde + 4d41631 commit f2e6ea3

6 files changed

Lines changed: 390 additions & 32 deletions

File tree

INIT_DEV.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ function app_reload ()
175175
function app_tests ()
176176
{
177177
NODE_ENV='development'
178+
TEST='ok'
178179
MONGO='mongodb://localhost:27017/unittests'
179180
mongod -f conf/mongodb.conf &
180181
mongo < test/before_db.js

coin_board/schemas/user.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ UserSchema.statics.authenticate = function (username, password, callback) {
188188
if (result === true) {
189189
return callback(null, user);
190190
} else {
191-
return callback();
191+
let err = new Error('Bad password.');
192+
return callback(err);
192193
}
193194
});
194195
});
@@ -201,6 +202,8 @@ UserSchema.statics.authenticate = function (username, password, callback) {
201202
* @param {String} apikey the new api service key
202203
* @param {String} apisecret the new api service secret
203204
* @param {function} callback to get the result data or error
205+
* @return {Error} error if something wrong
206+
* @return {String} sucess if its all ok
204207
* @memberof module:models~UserSchema
205208
*/
206209
UserSchema.statics.addapi = function (id,
@@ -210,18 +213,24 @@ UserSchema.statics.addapi = function (id,
210213
key: apikey,
211214
secret: apisecret,
212215
};
213-
Apis.create(newapi, (error, api) => {
214-
let elemtype = {};
215-
elemtype['Apis.' + apitype] = api;
216-
User.findOneAndUpdate({_id: id}, {$push: elemtype},
217-
(error, success) => {
218-
if (error) {
219-
callback && callback(error);
220-
} else {
221-
callback && callback(null, success);
222-
}
223-
});
224-
});
216+
if (['Bank', 'Crypto', 'Markets'].includes(apitype)) {
217+
Apis.create(newapi, (error, api) => {
218+
let elemtype = {};
219+
elemtype['Apis.' + apitype] = api;
220+
User.findOneAndUpdate({_id: id}, {$push: elemtype},
221+
(error, success) => {
222+
if (error) {
223+
return callback && callback(error);
224+
} else {
225+
let rt = 'Api ' + api.name + ' successfully added.';
226+
return callback && callback(null, rt);
227+
}
228+
});
229+
});
230+
} else {
231+
let rt = new Error('No such api type.');
232+
return callback && callback(rt, undefined);
233+
}
225234
};
226235

227236
/** Add a new Asset object to a User
@@ -231,6 +240,8 @@ UserSchema.statics.addapi = function (id,
231240
* @param {String} assetticker the ticker / symbol
232241
* @param {String} assetqtt the qtt to parsed in float
233242
* @param {function} callback to get the result data or error
243+
* @return {Error} error if something wrong
244+
* @return {String} sucess if its all ok
234245
* @memberof module:models~UserSchema
235246
*/
236247
UserSchema.statics.addasset = (id, assettype, assetid,
@@ -240,24 +251,32 @@ UserSchema.statics.addasset = (id, assettype, assetid,
240251
ticker: assetticker,
241252
qtt: assetqtt,
242253
};
243-
Assets.create(newasset, (error, asset) => {
244-
let elemtype = {};
245-
elemtype['Assets.' + assettype] = asset;
246-
User.findOneAndUpdate({_id: id}, {$push: elemtype},
247-
(error, success) => {
248-
if (error) {
249-
callback && callback(error);
250-
} else {
251-
callback && callback(null, success);
252-
}
253-
});
254-
});
254+
if (['Bank', 'Crypto', 'Markets'].includes(assettype)) {
255+
Assets.create(newasset, (error, asset) => {
256+
let elemtype = {};
257+
elemtype['Assets.' + assettype] = asset;
258+
User.findOneAndUpdate({_id: id}, {$push: elemtype},
259+
(error, success) => {
260+
if (error) {
261+
return callback && callback(error, undefined);
262+
} else {
263+
let rt = 'Asset ' + asset.name + ' successfully added.';
264+
return callback && callback(null, rt);
265+
}
266+
});
267+
});
268+
} else {
269+
let rt = new Error('No such asset type.');
270+
return callback && callback(rt, undefined);
271+
}
255272
};
256273

257274
let Apis = mongoose.model('Apis', ApiSchema);
258275
let Assets = mongoose.model('Assets', AssetsSchema);
259276
let User = mongoose.model('User', UserSchema);
260-
261277
module.exports = User;
262278
module.exports.Apis = Apis;
263279
module.exports.Assets = Assets;
280+
if (process.env.TEST === 'ok') {
281+
module.exports.InFuncs = {toLower, toFloat};
282+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"mocha": "^5.2.0",
2828
"mocha-junit-reporter": "^1.17.0",
2929
"nyc": "^12.0.2",
30+
"rewire": "^4.0.1",
3031
"sinon": "^5.0.10",
3132
"sinon-chai": "^3.1.0",
3233
"supertest": "^3.1.0",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @file Unit test file for reqmodels var and functs
3+
* @author Trevis Gulby
4+
*/
5+
6+
const expect = require('chai').expect;
7+
const reqs = require('../../coin_board/controllers/djunk/reqmodels');
8+
9+
describe('MOQREQUEST properties test', function () {
10+
it('should have a ctaf property', function (done) {
11+
expect(reqs).to.have.property('ctaf');
12+
expect(reqs.ctaf).to.be.an('object');
13+
done();
14+
});
15+
it('should have a crnews property', function (done) {
16+
expect(reqs).to.have.property('crnews');
17+
expect(reqs.crnews).to.be.an('object');
18+
done();
19+
});
20+
it('should have a crcomp property', function (done) {
21+
expect(reqs).to.have.property('crcomp');
22+
expect(reqs.crcomp).to.be.an('object');
23+
done();
24+
});
25+
});
26+
27+
describe('MOQREQUEST.ctaf properties test', function () {
28+
it('should have an id property', function (done) {
29+
let exp = 'n.ctaf';
30+
expect(reqs.ctaf).to.have.property('id');
31+
expect(reqs.ctaf.id).to.be.a('string');
32+
expect(reqs.ctaf.id).to.be.deep.equal(exp);
33+
done();
34+
});
35+
it('should have an url property', function (done) {
36+
let exp = 'https://cointelegraph.com/rss';
37+
expect(reqs.ctaf).to.have.property('url');
38+
expect(reqs.ctaf.url).to.be.a('string');
39+
expect(reqs.ctaf.url).to.be.deep.equal(exp);
40+
done();
41+
});
42+
it('should have a req property', function (done) {
43+
expect(reqs.ctaf).to.have.property('req');
44+
expect(reqs.ctaf.req).to.be.an('object');
45+
expect(reqs.ctaf.req).to.have.property('host');
46+
expect(reqs.ctaf.req).to.have.property('path');
47+
done();
48+
});
49+
it('should have an fname property', function (done) {
50+
let exp = './DTAFOOD/news/ctaf-';
51+
expect(reqs.ctaf).to.have.property('fname');
52+
expect(reqs.ctaf.fname).to.be.a('string');
53+
expect(reqs.ctaf.fname).to.contain(exp);
54+
done();
55+
});
56+
it('should have a clean property', function (done) {
57+
expect(reqs.ctaf).to.have.property('clean');
58+
expect(reqs.ctaf.clean).to.be.a('function');
59+
done();
60+
});
61+
it('should have a get property', function (done) {
62+
expect(reqs.ctaf).to.have.property('get');
63+
expect(reqs.ctaf.get).to.be.a('function');
64+
done();
65+
});
66+
});

test/schemas/assets_schema-test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
const expect = require('chai').expect;
77
const Asset = require('../../coin_board/schemas/user').Assets;
8+
let mockasset = {
9+
name: 'MockAsset',
10+
ticker: 'MOCK',
11+
qtt: 33.33,
12+
};
813

9-
describe('Asset schema', function () {
14+
describe('Asset schema validation', function () {
1015
it('should be invalid if name is empty', function (done) {
1116
let m = new Asset();
1217

@@ -33,3 +38,34 @@ describe('Asset schema', function () {
3338
});
3439
});
3540
});
41+
42+
describe('Asset schema creation', function () {
43+
let m = new Asset(mockasset);
44+
it('should have a name', function (done) {
45+
let exp = 'MockAsset';
46+
expect(m).to.have.property('name');
47+
expect(m.name).to.be.a('string');
48+
expect(m.name).to.be.deep.equal(exp);
49+
done();
50+
});
51+
it('should have a ticker', function (done) {
52+
let exp = 'MOCK';
53+
expect(m).to.have.property('ticker');
54+
expect(m.ticker).to.be.a('string');
55+
expect(m.ticker).to.be.deep.equal(exp);
56+
done();
57+
});
58+
it('should have a qtt', function (done) {
59+
let exp = 33.33;
60+
expect(m).to.have.property('qtt');
61+
expect(m.qtt).to.be.a('number');
62+
expect(m.qtt).to.be.deep.equal(exp);
63+
done();
64+
});
65+
it('should be invalid if qtt is not a number', function (done) {
66+
mockasset.qtt = 'invalid';
67+
m = new Asset(mockasset);
68+
expect(m.qtt).to.be.an('undefined');
69+
done();
70+
});
71+
});

0 commit comments

Comments
 (0)