More tests.

This commit is contained in:
gaba
2016-12-12 15:38:28 -08:00
parent 83a7ffee6f
commit 0648b2b1c8
6 changed files with 74 additions and 20 deletions
+16 -2
View File
@@ -24,15 +24,29 @@ export const updateConfiguration = newConfig => (dispatch, getState) => {
.catch(error => dispatch(updateConfigFailure(error)));
};
export const updateOpenStream = newConfig => (dispatch, getState) => {
const assetId = getState().items.get('assets')
.keySeq()
.toArray()[0];
dispatch(updateConfigRequest());
coralApi(`/asset/${assetId}/status`, {method: 'PUT', body: newConfig})
.then(() => {
dispatch(addNotification('success', lang.t('successUpdateSettings')));
dispatch(updateConfigSuccess(newConfig));
})
.catch(error => dispatch(updateConfigFailure(error)));
};
const openStream = () => ({type: actions.OPEN_COMMENTS});
const closeStream = () => ({type: actions.CLOSE_COMMENTS});
export const updateOpenStatus = status => dispatch => {
if (status === 'open') {
dispatch(openStream());
dispatch(updateConfiguration({closedAt: null}));
dispatch(updateOpenStream({closedAt: null}));
} else {
dispatch(closeStream());
dispatch(updateConfiguration({closedAt: Date.getTime()}));
dispatch(updateOpenStream({closedAt: Date.now(), closedMessage: ''}));
}
};
+9 -1
View File
@@ -29,6 +29,14 @@ const AssetSchema = new Schema({
type: Schema.Types.Mixed,
default: null
},
closedAt: {
type: Date,
default: null
},
closedMessage: {
type: String,
default: null
},
title: String,
description: String,
image: String,
@@ -69,7 +77,7 @@ AssetSchema.statics.findById = (id) => Asset.findOne({id});
AssetSchema.statics.findByUrl = (url) => Asset.findOne({url});
AssetSchema.virtual('isClosed').get(function() {
return this.settings && this.settings.closedAt && this.settings.closedAt <= new Date().getTime();
return this.closedAt && this.closedAt.getTime() <= new Date().getTime();
});
/**
+16 -11
View File
@@ -89,16 +89,21 @@ router.put('/:asset_id/settings', (req, res, next) => {
.catch((err) => next(err));
});
// router.put('/:asset_id/status', (req, res, next) => {
// Asset
// .findOneAndUpdate(req.params.asset_id, {
// $set: {
// closedAt: req.query.closedAt,
// closedMessage: req.query.closedMessage
// }
// })
// .then(() => res.status(204).end())
// .catch((err) => next(err));
// });
router.put('/:asset_id/status', (req, res, next) => {
const id = req.params.asset_id;
const {
closedAt = null,
closedMessage = null
} = req.query;
Asset
.update(id, {
closedAt: closedAt,
closedMessage: closedMessage
})
.then((asset) => {
res.status(201).json(asset);
})
.catch((err) => next(err));
});
module.exports = router;
+1 -1
View File
@@ -88,7 +88,7 @@ router.post('/', wordlist.filter('body'), (req, res, next) => {
// Check to see if the asset has closed commenting...
if (asset.isClosed) {
// They have, ensure that we send back an error.
return Promise.reject(new Error(`asset has commenting closed because: ${asset.settings.closedMessage}`));
return Promise.reject(new Error(`asset has commenting closed because: ${asset.closedMessage}`));
}
return asset;
+30 -1
View File
@@ -17,7 +17,8 @@ describe('/api/v1/assets', () => {
{
url: 'https://coralproject.net/news/asset1',
title: 'Asset 1',
description: 'term1'
description: 'term1',
id: '1'
},
{
url: 'https://coralproject.net/news/asset2',
@@ -82,4 +83,32 @@ describe('/api/v1/assets', () => {
});
describe('#put', () => {
it('should close the asset', function() {
const asset = Asset.findByUrl('http://test.com')
.then((asset) => {
expect(asset).to.have.property('closedAt')
.and.to.equal(null);
});
const today = Date.now();
return chai.request(app)
.put(`/api/v1/asset/${asset.id}/status`)
.set(passport.inject({roles: ['admin']}))
.send({closedAt: today})
.then((res) => {
expect(res).to.have.status(201);
Asset.findByUrl('http://test.com')
.then((asset) => {
expect(asset).to.have.property('isClosed')
.and.to.equal(true);
expect(asset).to.have.property('closedAt')
.and.to.not.equal(null);
});
});
});
});
});
+2 -4
View File
@@ -202,10 +202,8 @@ describe('/api/v1/comments', () => {
it('shouldn\'t create a comment when the asset has expired commenting', () => {
return Asset.create({
settings: {
closedAt: new Date().setDate(0),
closedMessage: 'tests said expired!'
}
closedAt: new Date().setDate(0),
closedMessage: 'tests said expired!'
})
.then((asset) => {
return chai.request(app)