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

#156100323 arrange tests and add more tests #26

Merged
merged 2 commits into from
Apr 23, 2018
Merged
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
8 changes: 4 additions & 4 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const searchFriend = (req, res) => {
if (!friend) {
return res.status(200).send({ message: 'No friends found' });
}
if (friend._id == req.decoded._id) {
if (friend._id == req.decoded.payload._id) {
return res.status(200)
.send({ message: 'You cannot search for yourself' });
}
Expand All @@ -183,7 +183,7 @@ const searchFriend = (req, res) => {
if (!friend) {
return res.status(200).send({ message: 'No friends found' });
}
if (friend._id == req.decoded._id) {
if (friend._id == req.decoded.payload._id) {
return res.status(200)
.send({ message: 'You cannot search for yourself' });
}
Expand Down Expand Up @@ -219,7 +219,7 @@ const inviteUserByEmail = (req, res) => {

transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return res.status(500).send({ error: 'an error occurred' });
return res.status(500).send({ error: 'an error occured' });
}
return res.status(200).send({
message: 'invite successfully sent',
Expand Down Expand Up @@ -344,7 +344,7 @@ exports.verifyToken = (req, res, next) => {
next();
});
} else {
return res.status(403).send({ error: 'You have to login First' });
return res.status(401).send({ error: 'You have to login First' });
}
};

Expand Down
13 changes: 1 addition & 12 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ gulp.task('dist-dep', [

gulp.task('test', () => {
gulp
.src(['test/**/*.js'])
.src(['test/user/*.js'])
.pipe(mocha({
reporter: 'spec',
exit: true,
Expand Down Expand Up @@ -110,14 +110,3 @@ gulp.task('mv-config', () => move('config/env/*.json', './dist/config/env'));

gulp.task('mv-public', () =>
move(['public/**/*', '!public/js/**'], './dist/public'));

gulp.task('test', () => {
gulp
.src(['test/**/*.js'])
.pipe(mocha({
reporter: 'spec',
exit: true,
compilers: 'babel-core/register'
}))
.pipe(exit());
});
105 changes: 105 additions & 0 deletions test/user/search_invite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import supertest from 'supertest';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to resolve path to module 'supertest' import/no-unresolved
Missing file extension for "supertest" import/extensions

import { expect } from 'chai';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to resolve path to module 'chai' import/no-unresolved
Missing file extension for "chai" import/extensions

import mongoose from 'mongoose';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to resolve path to module 'mongoose' import/no-unresolved
Missing file extension for "mongoose" import/extensions

import app from '../../dist/server';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to resolve path to module '../../dist/server' import/no-unresolved
Missing file extension for "../../dist/server" import/extensions


const request = supertest.agent(app);
let firstUserToken;
describe('POST /api/search/users', () => {
it('sign up a user first', (done) => {
request
.post('/api/auth/signup')
.set('Content-Type', 'application/json')
.send({
name: 'efosa',
email: 'test@yahoo.com',
password: 'test',
username: 'efosky',
})
.end((err, res) => {
expect(res.status).to.equal(201);
firstUserToken = res.body.token;
done();
});
});
const searchQuery = {
search: 'test@yahoo.com'
};
it('returns an error message if user is not authenticated', (done) => {
request
.post('/api/search/users')
.set('Content-Type', 'application/json')
.send(searchQuery)
.end((err, res) => {
expect(res.status).to.equal(401);
expect(res.body.error).to.equal('You have to login First');
done();
});
});
it('returns an error message if user searches for himself', (done) => {
request
.post('/api/search/users')
.set('x-access-token', firstUserToken)
.send(searchQuery)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.message).to.equal('You cannot search for yourself');
done();
});
});
it('returns a message if no user is found', (done) => {
searchQuery.search = 'tester@jfhf.com';
request
.post('/api/search/users')
.set('x-access-token', firstUserToken)
.send(searchQuery)
.end((err, res) => {
expect(res.body.message).to.equal('No friends found');
done();
});
});
it('performs a search with username', (done) => {
searchQuery.search = 'efosky';
request
.post('/api/search/users')
.set('x-access-token', firstUserToken)
.send(searchQuery)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.user.username).to.equal('efosky');
done();
});
});
it('performs a search with email', (done) => {
request
.post('/api/search/users')
.set('x-access-token', firstUserToken)
.send(searchQuery)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.user.email).to.equal('test@yahoo.com');
done();
});
});
});

describe('POST /api/invite/users', () => {
const inviteReceipt = {
emailOfUsernameOfFriend: 'efosaokpugie@gmail.com',
link: 'localhost:3000/#!/app?game=83747mdbbf',
};
it('returns an error message if user is not authenticated', (done) => {
request
.post('/api/invite/users')
.send(inviteReceipt)
.end((err, res) => {
expect(res.status).to.equal(401);
done();
});
});
});

// mongoose.connection.db.dropDatabase();
after((done) => {
mongoose.connection.db.dropDatabase(done);
});
35 changes: 1 addition & 34 deletions test/user/signup_signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ after((done) => {
mongoose.connection.db.dropDatabase(done);
});


describe('POST api/auth/signup', () => {
it('should not allow a user to sign up with no name', (done) => {
request
Expand Down Expand Up @@ -79,40 +80,6 @@ describe('POST api/auth/signup', () => {
});
});

describe('POST /api/search/users', () => {
const searchQuery = {
emailOrUsernameOfFriend: 'tester@test.com',
};
it('returns an error message if user is not authenticated', (done) => {
request
.post('/api/search/users')
.set('Content-Type', 'application/json')
.send(searchQuery)
.end((err, res) => {
expect(res.status).to.equal(403);
expect(res.body.error).to.equal('You have to login First');
done();
});
});
});
describe('POST /api/invite/users', () => {
const searchQuery = {
emailOrUsernameOfFriend: 'tester@test.com',
link: 'localhost:3000/#!/app?game=83747mdbbf',
};
it('returns an error message if user is not authenticated', (done) => {
request
.post('/api/invite/users')
.set('Content-Type', 'application/json')
.send(searchQuery)
.end((err, res) => {
expect(res.status).to.equal(403);
expect(res.body.error).to.equal('You have to login First');
done();
});
});
});

describe('POST api/auth/login', () => {
it('should not allow a user to login with no email', (done) => {
request
Expand Down