Skip to content

Commit

Permalink
Add test seed data, refactor tests to work with new jwt token
Browse files Browse the repository at this point in the history
  • Loading branch information
mallek committed Feb 8, 2018
1 parent fa035d9 commit f04e28a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 29 deletions.
1 change: 1 addition & 0 deletions config/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = {
PORT: process.env.PORT || 8081,
DB_URL: SECRETS.mongodb_test.db_url,
GOOGLE_API_KEY: SECRETS.google_maps.api_key,
JWT_KEY: SECRETS.jwt.key,
};
4 changes: 2 additions & 2 deletions controllers/saved-pins-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.getSavedPinsById = (appReq, appRes) => {

SavedPins.findById(params.id).then((pin) => {
// expect db id to be unique but just in case verifiy user._id
if (!pin || appReq.userId !== pin.user) {
if (!pin || appReq.userId !== pin.user.toString()) {
return appRes.status(404).send();
}
return appRes.send({ pin });
Expand Down Expand Up @@ -78,7 +78,7 @@ exports.postSavedPins = (appReq, appRes) => {
* @apiError 500 {server error}
*/
exports.deleteSavedPins = (appReq, appRes) => {
SavedPins.remove({}).then(() => {
SavedPins.remove({ user: appReq.userId }).then(() => {
appRes.status(200).send();
}).catch((e) => {
appRes.status(500).send(e);
Expand Down
4 changes: 2 additions & 2 deletions tests/apiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const request = require('supertest');
const app = require('../app');

describe('API Routes', () => {
describe('GET /users/current', () => {
describe('GET /users/user', () => {
it('does not send a user if not logged in', async () => {
try {
const res = await request(app)
.get('/users/current')
.get('/users/user')
.expect(401);

expect(res).to.be.an('object');
Expand Down
52 changes: 28 additions & 24 deletions tests/savedPins.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,32 @@ const { ObjectID } = require('mongodb');

const app = require('../app');
const SavedPins = require('../models/saved-pins');
const {
pins, populatePins, users, populateUsers,
} = require('./seed/seed');

const pin = new SavedPins({
_id: new ObjectID(),
lat: 1,
lng: 2,
place_id: 'testing',
});

beforeEach((done) => {
SavedPins.remove({}).then(() =>
SavedPins.insertMany(pin)).then(() =>
done());
});
beforeEach(populateUsers);
beforeEach(populatePins);

describe('POST /search/savedpins', () => {
it('should create new saved pin', (done) => {
request(app)
.post('/search/savedpins')
.send(pin)
.set('x-access-token', users[0].tokens[0].token)
.send(pins[0])
.expect(200)
.expect((res) => {
expect(res.body.pin.lat).to.equal(pin.lat);
expect(res.body.pin.lat).to.equal(pins[0].lat);
})
.end((err, res) => {
if (err) {
return done(err);
}

SavedPins.find().then((savedPins) => {
expect(savedPins.length).to.equal(2);
expect(savedPins[0].lat).to.equal(pin.lat);
expect(savedPins.length).to.equal(pins.length + 1);
expect(savedPins[0].lat).to.equal(pins[0].lat);
expect(savedPins[0].user._id).to.equal(pins[0].user._id);
done();
}).catch(e => done(e));
});
Expand All @@ -43,6 +38,7 @@ describe('POST /search/savedpins', () => {
it('should not create savedPin with invalid body data', (done) => {
request(app)
.post('/search/savedpins')
.set('x-access-token', users[0].tokens[0].token)
.send({})
.expect(500)
.end((err, res) => {
Expand All @@ -51,7 +47,7 @@ describe('POST /search/savedpins', () => {
}

SavedPins.find().then((savedPins) => {
expect(savedPins.length).to.equal(1);
expect(savedPins.length).to.equal(pins.length);
done();
}).catch(e => done(e));
});
Expand All @@ -61,10 +57,12 @@ describe('POST /search/savedpins', () => {
describe('GET /search/savedpins', () => {
it('should return savedpins doc', (done) => {
request(app)
.get(`/search/savedpins/${pin._id.toHexString()}`)
.get('/search/savedpins/')
.set('x-access-token', users[0].tokens[0].token)
.expect(200)
.expect((res) => {
expect(res.body.pin.lat).to.equal(pin.lat);
expect(res.body.savedPins[0].lat).to.equal(pins[0].lat);
expect(res.body.savedPins.length).to.equal(1);
})
.end(done);
});
Expand All @@ -73,10 +71,11 @@ describe('GET /search/savedpins', () => {
describe('GET /search/savedpins/:id', () => {
it('should return savedpins doc', (done) => {
request(app)
.get(`/search/savedpins/${pin._id.toHexString()}`)
.get(`/search/savedpins/${pins[0]._id.toHexString()}`)
.set('x-access-token', users[0].tokens[0].token)
.expect(200)
.expect((res) => {
expect(res.body.pin.lat).to.equal(pin.lat);
expect(res.body.pin.lat).to.equal(pins[0].lat);
})
.end(done);
});
Expand All @@ -86,13 +85,15 @@ describe('GET /search/savedpins/:id', () => {

request(app)
.get(`/search/savedpins/${hexId}`)
.set('x-access-token', users[0].tokens[0].token)
.expect(404)
.end(done);
});

it('should return 404 for non-object ids', (done) => {
request(app)
.get('/search/savedpins/123abc')
.set('x-access-token', users[0].tokens[0].token)
.expect(404)
.end(done);
});
Expand All @@ -102,27 +103,28 @@ describe('DELETE /search/savedpins', () => {
it('should remove all searchpins', (done) => {
request(app)
.delete('/search/savedpins')
.set('x-access-token', users[0].tokens[0].token)
.expect(200)
.end((err, res) => {
if (err) {
return done(err);
}

SavedPins.find().then((allPins) => {
expect(allPins.length).to.equal(0);
expect(allPins.length).to.equal(pins.length - 1);
done();
});
});
});
});

describe('DELETE /search/savedpins/:id', () => {

it('should remove a single searchpins', (done) => {
const hexId = pin._id.toHexString();
const hexId = pins[0]._id.toHexString();

request(app)
.delete(`/search/savedpins/${hexId}`)
.set('x-access-token', users[0].tokens[0].token)
.expect(200)
.expect((res) => {
expect(res.body.pin._id).to.equal(hexId);
Expand All @@ -144,13 +146,15 @@ describe('DELETE /search/savedpins/:id', () => {

request(app)
.delete(`/search/savedpins/${hexId}`)
.set('x-access-token', users[0].tokens[0].token)
.expect(404)
.end(done);
});

it('should return 404 if object id is invalid', (done) => {
request(app)
.delete('/search/savedpins/123abc')
.set('x-access-token', users[0].tokens[0].token)
.expect(404)
.end(done);
});
Expand Down
55 changes: 55 additions & 0 deletions tests/seed/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { ObjectID } = require('mongodb');
const jwt = require('jsonwebtoken');
const { JWT_KEY } = require('../../config');

const User = require('../../models/users');
const SavedPins = require('../../models/saved-pins');

const userOneId = new ObjectID();
const userTwoId = new ObjectID();
const users = [{
_id: userOneId,
name: 'mallek',
email: '[email protected]',
password: 'userOnePass',
tokens: [{
access: 'auth',
token: jwt.sign({ id: userOneId }, JWT_KEY).toString(),
}],
}, {
_id: userTwoId,
name: 'user',
email: '[email protected]',
password: 'userTwoPass',
}];

const pins = [{
_id: new ObjectID(),
lat: 1,
lng: 2,
place_id: 'testing pin1',
user: userOneId,
}, {
_id: new ObjectID(),
lat: 20,
lng: 30,
place_id: 'testing again',
user: userTwoId,
}];

const populatePins = (done) => {
SavedPins.remove({}).then(() => SavedPins.insertMany(pins)).then(() => done());
};

const populateUsers = (done) => {
User.remove({}).then(() => {
const userOne = new User(users[0]).save();
const userTwo = new User(users[1]).save();

return Promise.all([userOne, userTwo]);
}).then(() => done());
};

module.exports = {
pins, populatePins, users, populateUsers,
};
3 changes: 2 additions & 1 deletion tests/server.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
it('test runner should work', () => {
it('test runner should work', (done) => {
const foo = 'bar';
const beverages = { tea: ['chai', 'matcha', 'oolong'] };

expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);
done();
});

0 comments on commit f04e28a

Please sign in to comment.