-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(search): rewrite test and edit search.js
rewrite test and edit user.id in file to test authenticated route
- Loading branch information
Victor Nwaiwu
committed
Dec 13, 2016
1 parent
d5d30a5
commit ea44a0d
Showing
2 changed files
with
43 additions
and
33 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 |
---|---|---|
@@ -1,55 +1,62 @@ | ||
/* eslint amd:true */ | ||
const should = require('should'); | ||
const app = require('../../server'); | ||
const request = require('supertest'); | ||
const mongoose = require('mongoose'); | ||
|
||
const User = mongoose.model('User'); | ||
const agent = request.agent(app); | ||
|
||
let user = ''; | ||
|
||
describe('Search api', () => { | ||
beforeEach((done) => { | ||
user = new User({ | ||
before((done) => { | ||
User.remove().exec(); | ||
const user1 = new User({ | ||
name: 'Full name', | ||
email: 'search@search.com', | ||
username: 'user', | ||
password: 'password' | ||
}); | ||
user.save(done); | ||
}); | ||
|
||
afterEach((done) => { | ||
done(); | ||
}); | ||
|
||
describe('login route', () => { | ||
it('should login for valid user', (done) => { | ||
request(app).post('/users/session') | ||
.send({ email: user.email, password: user.password }) | ||
.expect(302, done()); | ||
user1.save(); | ||
agent.post('/users/session') | ||
.send({ email: user1.email, password: user1.password }) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
res.should.have.status(302); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('Search route', () => { | ||
it('routes successfully', (done) => { | ||
request(app) | ||
.get('/api/search/users/search@search.com') | ||
.set('Accept', 'application/json') | ||
.expect(200) | ||
.end(function(err, res){ | ||
if (err) return done(err); | ||
}); | ||
done(); | ||
agent | ||
.get('/api/search/users/search@search.com') | ||
.set('Accept', 'application/json') | ||
.end(function(err, res){ | ||
if (err) return done(err); | ||
res.should.have.status(200); | ||
done(); | ||
}); | ||
}); | ||
it('returns not found when no word is entered', (done) => { | ||
agent | ||
.get('/api/search/users/') | ||
.set('Accept', 'application/json') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
res.should.have.status(404); | ||
done(); | ||
}); | ||
}); | ||
it('should not search for anything when the user is not logged in', (done) => { | ||
request(app) | ||
.get('/api/search/users/') | ||
.set('Accept', 'application/json') | ||
.expect(404) | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
}); | ||
done(); | ||
.get('/api/search/users/') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
res.should.have.status(404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |