-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22327ea
commit cdeb73e
Showing
9 changed files
with
354 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const express = require('express'); | ||
const Authorize = require('../../utils/authorize'); | ||
const UserModel = require('../db/user'); | ||
const User = UserModel.User; | ||
const router = express.Router(); | ||
|
||
Authorize(router); | ||
|
||
router | ||
.get('/', (req, res) => { | ||
User.findById(req.payload.userId) | ||
.then(user => { | ||
if (!user) { res.send(404); return; } | ||
|
||
res.json({ user: UserModel.normalize(user) }); | ||
}) | ||
.catch(err => { | ||
res.status(500).send(err); | ||
}); | ||
}) | ||
|
||
.put('/', (req, res) => { | ||
User.findById(req.payload.userId) | ||
.then(user => { | ||
if (!user) { res.send(404); return; } | ||
|
||
UserModel.update(user, req.body) | ||
.save() | ||
.then(user => { | ||
res.json({ user: UserModel.normalize(user) }); | ||
}) | ||
.catch(err => { | ||
res.status(500).send(err); | ||
}); | ||
}) | ||
.catch(err => { | ||
res.status(500).send(err); | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
module.exports = mongoose.model('User', new Schema({ | ||
name: String, | ||
password: String | ||
})); | ||
let UserSchema = new Schema({ | ||
email: { type: String, required: true }, | ||
name: { type: String, required: true }, | ||
password: { type: String, required: true }, | ||
age: { type: Number, required: false, default: null }, | ||
bio: { type: String, required: false, default: '' }, | ||
createdAt: { type: Date, default: Date.now }, | ||
admin: { type: Boolean, default: false } | ||
}); | ||
|
||
UserSchema.pre('save', next => { | ||
now = new Date(); | ||
if (!this.createdAt) { | ||
this.createdAt = now; | ||
} | ||
next(); | ||
}); | ||
|
||
module.exports = { | ||
User: mongoose.model('User', UserSchema), | ||
normalize: (user) => { | ||
return { | ||
id: user.id, | ||
name: user.name, | ||
bio: user.bio, | ||
createdAt: user.createdAt | ||
} | ||
}, | ||
update: (user, newUser) => { | ||
delete newUser.createdAt; | ||
delete newUser.password; | ||
delete newUser.id; | ||
|
||
return Object.assign(user, newUser); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const server = require('../www'); | ||
const chai = require('chai'); | ||
const chaiHttp = require('chai-http'); | ||
const chaiJWT = require('chai-jwt'); | ||
const should = chai.should(); | ||
|
||
chai.use(chaiHttp); | ||
chai.use(chaiJWT); | ||
|
||
describe('a suite test for api route', function() { | ||
var token = { admin: null, user: null }; | ||
|
||
it('should return 200', (done) => { | ||
chai.request(server) | ||
.get('/') | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should have api route', (done) => { | ||
chai.request(server) | ||
.get('/api') | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.be.a('object'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should signup a new user as admin', (done) => { | ||
const newAdmin = { | ||
email: '[email protected]', | ||
password: 'admin', | ||
name: 'adminy', | ||
age: '31', | ||
bio: 'This is the wiazrdnet972 admin bio', | ||
admin: true | ||
} | ||
|
||
chai.request(server) | ||
.post('/api/signup') | ||
.send(newAdmin) | ||
.end((err, res) => { | ||
res.should.have.status(201); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('userId').be.a('string'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should signup a new user', (done) => { | ||
const newUser = { | ||
email: '[email protected]', | ||
password: 'user', | ||
name: 'user', | ||
bio: 'This is the user bio' | ||
} | ||
|
||
chai.request(server) | ||
.post('/api/signup') | ||
.send(newUser) | ||
.end((err, res) => { | ||
res.should.have.status(201); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('userId').be.a('string'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.