Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature / User Picture Save #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cell/organelles/express-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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) => {

Expand Down
25 changes: 25 additions & 0 deletions cell/organelles/http-api/users/index.js
Original file line number Diff line number Diff line change
@@ -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()
})
}
}
}
27 changes: 27 additions & 0 deletions cell/organelles/user-manager/index.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions dna/processes/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"picture-manager": {
"source": "../../cell/organelles/picture-manager",
"name": "PictureManager"
},
"user-manager": {
"source": "../../cell/organelles/user-manager",
"name": "UserManager"
}
},
"membrane": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions spec/http-api/users/index.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
32 changes: 32 additions & 0 deletions spec/user-manager/index.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
})