Update tests

This commit is contained in:
gaba
2017-02-07 15:36:10 -08:00
parent 5b609ceb86
commit 1176a94bec
3 changed files with 50 additions and 27 deletions
+13 -11
View File
@@ -57,20 +57,22 @@ module.exports = class AssetsService {
static findOrCreateByUrl(url) {
// Check the URL to confirm that is in the domain whitelist
if (!domainlist.urlCheck(url)) {
return Promise.reject(errors.ErrInvalidAssetURL);
}
return domainlist.urlCheck(url).then((whitelisted) => {
if (!whitelisted) {
return Promise.reject(errors.ErrInvalidAssetURL);
} else {
return AssetModel.findOneAndUpdate({url}, {url}, {
return AssetModel.findOneAndUpdate({url}, {url}, {
// Ensure that if it's new, we return the new object created.
new: true,
// Ensure that if it's new, we return the new object created.
new: true,
// Perform an upsert in the event that this doesn't exist.
upsert: true,
// Perform an upsert in the event that this doesn't exist.
upsert: true,
// Set the default values if not provided based on the mongoose models.
setDefaultsOnInsert: true
// Set the default values if not provided based on the mongoose models.
setDefaultsOnInsert: true
});
}
});
}
+19 -14
View File
@@ -10,24 +10,29 @@ chai.use(require('chai-http'));
const AssetModel = require('../../../../models/asset');
const AssetsService = require('../../../../services/assets');
const SettingsService = require('../../../../services/settings');
describe('/api/v1/assets', () => {
beforeEach(() => {
return AssetModel.create([
{
url: 'https://coralproject.net/news/asset1',
title: 'Asset 1',
description: 'term1',
closedAt: Date.now()
},
{
url: 'https://coralproject.net/news/asset2',
title: 'Asset 2',
description: 'term2',
closedAt: null
}
]);
const settings = {id: '1', moderation: 'PRE', domains: {whitelist: ['test.com']}};
return SettingsService.init(settings).then(() => {
return AssetModel.create([
{
url: 'https://coralproject.net/news/asset1',
title: 'Asset 1',
description: 'term1',
closedAt: Date.now()
},
{
url: 'https://coralproject.net/news/asset2',
title: 'Asset 2',
description: 'term2',
closedAt: null
}
]);
});
});
describe('#get', () => {
+18 -2
View File
@@ -1,5 +1,6 @@
const AssetModel = require('../../models/asset');
const AssetsService = require('../../services/assets');
const SettingsService = require('../../services/settings');
const chai = require('chai');
const expect = chai.expect;
@@ -10,8 +11,12 @@ chai.should();
describe('services.AssetsService', () => {
beforeEach(() => {
const settings = {id: '1', moderation: 'PRE', domains: {whitelist: ['new.test.com', 'test.com', 'override.test.com']}};
const defaults = {url:'http://test.com'};
return AssetModel.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true});
return SettingsService.init(settings).then(() => {
return AssetModel.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true});
});
});
describe('#findById', ()=> {
@@ -54,7 +59,7 @@ describe('services.AssetsService', () => {
});
});
it('should return a new asset when the url does not exist', () => {
it('should return a new asset when the url does not exist and its domain is whitelisted', () => {
return AssetsService
.findOrCreateByUrl('http://new.test.com')
.then((asset) => {
@@ -62,6 +67,17 @@ describe('services.AssetsService', () => {
.and.to.not.equal(1);
});
});
it('should return an error when the url does not exist and its domain is not whitelisted', () => {
return AssetsService
.findOrCreateByUrl('http://bad.test.com')
.then((asset) => {
expect(asset).to.be.null;
})
.catch((error) => {
expect(error).to.not.be.null;
});
});
});
describe('#overrideSettings', () => {