From 0648b2b1c81ff0a434ec89e6c8c2fa22af570554 Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 12 Dec 2016 15:38:28 -0800 Subject: [PATCH] More tests. --- client/coral-framework/actions/config.js | 18 ++++++++++++-- models/asset.js | 10 +++++++- routes/api/asset/index.js | 27 ++++++++++++--------- routes/api/comments/index.js | 2 +- tests/routes/api/assets/index.js | 31 +++++++++++++++++++++++- tests/routes/api/comments/index.js | 6 ++--- 6 files changed, 74 insertions(+), 20 deletions(-) diff --git a/client/coral-framework/actions/config.js b/client/coral-framework/actions/config.js index eb03d63ed..7850577dc 100644 --- a/client/coral-framework/actions/config.js +++ b/client/coral-framework/actions/config.js @@ -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: ''})); } }; diff --git a/models/asset.js b/models/asset.js index 09a9f49e8..ed1bf98d0 100644 --- a/models/asset.js +++ b/models/asset.js @@ -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(); }); /** diff --git a/routes/api/asset/index.js b/routes/api/asset/index.js index f61851f0c..6723d3671 100644 --- a/routes/api/asset/index.js +++ b/routes/api/asset/index.js @@ -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; diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index bf741ed2d..b54717c78 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -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; diff --git a/tests/routes/api/assets/index.js b/tests/routes/api/assets/index.js index c56e5b1ba..817f5b181 100644 --- a/tests/routes/api/assets/index.js +++ b/tests/routes/api/assets/index.js @@ -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); + }); + }); + }); + }); + }); diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 0f6e8442e..59d840e1b 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -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)