Skip to content

Commit

Permalink
place.create
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlub committed Jul 19, 2017
1 parent 84fb664 commit 46535f2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/controllers/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ placeController.getById = (req, res) => {
}

placeController.create = (req, res) => {

// receives json for place in body
// adds to db
// status codes: 201 (created), 500 (server error)
const newPlace = new Place(req.body)
newPlace.save()
.then(place => {
res.status(201).send(place)
})
.catch(err => {
// Sending back 500 error, may need changing when we think about how we validate
const errorObj = { message: `Database error: ${err.message}` }
res.status(500).send(errorObj)
})
}

placeController.update = (req, res) => {
Expand Down
40 changes: 38 additions & 2 deletions tests/controllers/place.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tape('GET /places, with and without query parameters', t => {
})

// Tests for: GET /places/:id
tape('test /places/:id GET with id of something not in the database', (t) => {
tape('GET /places/:id with id of something not in the database', (t) => {
supertest(server)
.get('/places/10')
.expect(404)
Expand All @@ -60,7 +60,7 @@ tape('test /places/:id GET with id of something not in the database', (t) => {
})
})

tape('test /places/:id GET with id of something in the database', (t) => {
tape('GET /places/:id with id of something in the database', (t) => {
Place.create(validPlace1)
.then(result => {
supertest(server)
Expand All @@ -77,6 +77,42 @@ tape('test /places/:id GET with id of something in the database', (t) => {
})

// Tests for: POST /places
tape('POST /places with valid place data', t => {
supertest(server)
.post('/places')
.send(validPlace1)
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) t.fail(err)
t.equal(res.body.name, validPlace1.name, 'Correct object is added')
t.ok(res.body._id && res.body.createdAt && res.body.updatedAt, 'id and timestamp fields added')
// Now check whether it is in the database
Place.findById(res.body._id)
.then(place => {
t.equal(place.name, res.body.name, 'Place is in the database')
dropCollectionAndEnd(Place, t)
})
.catch(err => {
t.fail(err)
dropCollectionAndEnd(Place, t)
})
})
})

tape('POST /places with invalid place data', t => {
supertest(server)
.post('/places')
.send(invalidPlace1)
.expect(500)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) t.fail(err)
t.ok(res.body.message, 'A message is sent back')
t.ok(res.body.message.includes('Database error'), 'Correct message is sent back')
dropCollectionAndEnd(Place, t)
})
})

// Tests for: PUT /places/:id

Expand Down

0 comments on commit 46535f2

Please sign in to comment.