diff --git a/cell/organelles/express-server/index.js b/cell/organelles/express-server/index.js index 4c0ee72..abb2010 100644 --- a/cell/organelles/express-server/index.js +++ b/cell/organelles/express-server/index.js @@ -2,6 +2,7 @@ const url = require('url') const express = require('express') const Organelle = require('../organelle') const User = require('../../models/user') +var bodyParser = require('body-parser') class ExpressServer extends Organelle { @@ -12,6 +13,9 @@ class ExpressServer extends Organelle { app.set('x-powered-by', false) app.use('/static', express.static('public')) + + // parse application/json + app.use(bodyParser.json()) app.use((req, res, next) => { diff --git a/cell/organelles/http-api/users/index.js b/cell/organelles/http-api/users/index.js new file mode 100644 index 0000000..630a7d6 --- /dev/null +++ b/cell/organelles/http-api/users/index.js @@ -0,0 +1,25 @@ +module.exports = (plasma, dna, helpers) => { + return { + 'POST /:userId': (req, res, next) => { + // check for invalid requests + if(req.user._id.toString() !== req.params.userId) { + return next('User not matched') + } + + const chemical = { + type: 'save-picture', + userId: req.user._id, + pictureUrl: req.body.pictureUrl + } + plasma.emit(chemical, (err) => { + if (err) return next(err) + + res.body = { + status: 'OK', + } + + return next() + }) + } + } +} \ No newline at end of file diff --git a/cell/organelles/user-manager/index.js b/cell/organelles/user-manager/index.js new file mode 100644 index 0000000..449e853 --- /dev/null +++ b/cell/organelles/user-manager/index.js @@ -0,0 +1,27 @@ +const Organelle = require('../organelle') +const User = require('../../models/user') + +class UserManager extends Organelle { + + constructor(plasma, dna) { + super(plasma, dna) + + this.on('save-picture', (c, done) => { + this.savePicture(c.userId, c.pictureUrl, done) + }) + } + + savePicture(userId, pictureUrl, done) { + User + .findOneAndUpdate({ + _id: userId + }, { + picture: pictureUrl + }, (err, pictures) => { + if (err) return done(err) + return done(null, pictures) + }) + } +} + +module.exports = UserManager \ No newline at end of file diff --git a/dna/processes/index.json b/dna/processes/index.json index 7a516bc..887f438 100644 --- a/dna/processes/index.json +++ b/dna/processes/index.json @@ -24,6 +24,10 @@ "picture-manager": { "source": "../../cell/organelles/picture-manager", "name": "PictureManager" + }, + "user-manager": { + "source": "../../cell/organelles/user-manager", + "name": "UserManager" } }, "membrane": { diff --git a/package.json b/package.json index 6172266..83edbf7 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "license": "ISC", "homepage": "https://github.com/VarnaLab/node-organic", "dependencies": { + "body-parser": "^1.15.2", "express": "^4.13.4", "jasmine": "^2.4.1", "mongoose": "^4.5.3", diff --git a/spec/http-api/users/index.spec.js b/spec/http-api/users/index.spec.js new file mode 100644 index 0000000..b03e7df --- /dev/null +++ b/spec/http-api/users/index.spec.js @@ -0,0 +1,48 @@ +const request = require('request') + +describe('POST /users', () => { + beforeAll(global.startApi) + + afterAll(global.stopApi) + + it('works with pictureUrl', done => { + request({ + url: `${apiRoot}/users/${global.user._id}`, + headers: { + 'Authorization': global.user.authToken + }, + method: 'POST', + body: { + pictureUrl: 'test-pic.jpeg' + }, + json: true + }, (err, res, body) => { + expect(err).toBeNull() + expect(res.statusCode).toEqual(200) + expect(res.body.status).toEqual('OK') + + return done() + }) + }) + + it('doesn\'t save a picture when user is not you', done => { + const notMyUserId = 'imjusthackingtryingstuffhere' + request({ + url: `${apiRoot}/users/${notMyUserId}`, + headers: { + 'Authorization': global.user.authToken + }, + method: 'POST', + body: { + pictureUrl: 'test-pic.jpeg' + }, + json: true + }, (err, res, body) => { + expect(err).toBeNull() + expect(res.statusCode).toEqual(500) + expect(res.body.trim()).toEqual('User not matched') + + return done() + }) + }) +}) \ No newline at end of file diff --git a/spec/user-manager/index.spec.js b/spec/user-manager/index.spec.js new file mode 100644 index 0000000..601fb76 --- /dev/null +++ b/spec/user-manager/index.spec.js @@ -0,0 +1,32 @@ +const request = require('request') +const User = require('../../cell/models/user') + +describe('user-manager', () => { + beforeAll(global.startApi) + + afterAll(global.stopApi) + + it('saves a picture for a user', (done) => { + // Make sure user's picture is not already our test value + expect(global.user.picture).not.toBe('test-url') + + const chemical = { + type: 'save-picture', + userId: global.user._id, + pictureUrl: 'test-url' + } + // Emit to plasma to save a user's picture + global.cell.plasma.emit(chemical, (err) => { + // Check if the picture is properly updated in the database + User.findOne({ + _id: global.user._id + }, (err, user) => { + if(err) return done(err) + + expect(user.picture).toBe('test-url') + + return done() + }) + }) + }) +}) \ No newline at end of file