Skip to content

Commit

Permalink
Merge branch 'develop' into feature/databaseMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
andela-rekemezie committed Feb 22, 2016
2 parents 30f13e1 + 4bff16a commit 02247b2
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 92 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ gulp.task('test:bend', ['test:fend'], function() {
.pipe(mocha({
reporter: 'spec'
}))
.once('error', function() {
process.exit(1);
.once('error', function(err) {
throw err;
});
});

Expand Down
2 changes: 1 addition & 1 deletion migration_ci/create-reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// id
id: {
type: Sequelize.INTEGER,
autoincrement: true
autoIncrement: true
},
review: {
type: Sequelize.TEXT,
Expand Down
2 changes: 1 addition & 1 deletion seeders/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = {
* @param queryInterface, Sequelize
*/
return queryInterface.bulkInsert('Users', [{
id: 1,
username: 'vvidaapp',
name:'vvida lavida',
password: bcrypt.hashSync('[email protected]'),
gender: 'Female',
email: '[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//Middleware to create an item
create: function(req, res) {
return Categories.create({
name: req.body.name,
type: req.body.category
}).then(function(category) {
if (!category) {
Expand Down Expand Up @@ -44,7 +45,6 @@
},

find: function(req, res) {

Categories.find({
where: {
id: req.params.id,
Expand Down
20 changes: 12 additions & 8 deletions server/controllers/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
return res.status(500).send({
error: 'Failed to create item'
});
} else {
res.json(item);
}
res.json(item);
})
.catch(function(err) {
res.status(500).send({
Expand Down Expand Up @@ -71,11 +72,12 @@
include: [Images, Reviews, Categories]
}).then(function(item) {
if (!item) {
res.status(404).send({
return res.status(404).send({
message: 'Item not found'
});
}
res.json(item);
} else {
return res.json(item);
}
}).catch(function(err) {
res.status(500).send({
error: err.message || err.errors[0].message
Expand All @@ -93,10 +95,11 @@
return res.status(500).send({
error: 'Update failed'
});
} else {
res.json({
message: 'Item has been updated.'
});
}
res.json({
message: 'Item has been updated.'
});
}).catch(function(err) {
return res.status(500).send({
error: err.message || err.errors[0].message
Expand All @@ -114,10 +117,11 @@
return res.status(500).send({
error: 'Delete failed'
});
}
} else {
res.status(200).send({
message: 'Delete successful'
});
}
}).catch(function(err) {
res.status(500).send({
error: err.message || err.errors[0].message
Expand Down
176 changes: 176 additions & 0 deletions tests/server/resources/categories.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
var request = require('superagent'),
faker = require('faker'),
_expect = require('expect.js'),
resourceApiUrl = 'http://localhost:3000/api/categories';

describe('Categories resource API tests', function() {

var generateFakeCategory = function() {
return {
// catId: faker.random.number(),
name: faker.commerce.department(),
category: 'Item'
};
},
authToken = null,
// id to be a 'string'when a resource added
id;

/**
* Store a newly created resource in storage.
* POST /user/login
*
* @return Response
*/
it('Must create session to be able to run actions on items', function(done) {
// Must log in to retain a session
request
.post('http://localhost:3000/api/users/login')
.send({
email: '[email protected]',
password: '[email protected]'
})
.end(function(err, res) {
_expect(res.status).to.be(200);
_expect(res.body.token).to.be.a('string');
_expect(res.body.token.length).to.be.greaterThan(100);
authToken = res.body.token;
done();
});
});

/**
* Display a listing of the resource.
* GET /categories
*
* @return Response
*/

it('should return empty array if DB is empty', function(done) {
request
.get(resourceApiUrl)
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
_expect(res.body).to.be.an(Array);
done();
});
});


it('should not store a newly created resource in storage.', function(done) {
var category = generateFakeCategory();
request
.post(resourceApiUrl)
.send(category)
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(500);
done();
});
});

it('should store a newly created resource in storage.', function(done) {
var category = generateFakeCategory();
request
.post(resourceApiUrl)
.set('X-Access-Token', authToken)
.send(category)
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
var newCategory = res.body;
_expect(newCategory.type).to.be(category.category);
_expect(newCategory.id).to.be.a('number');
id = newCategory.id;
done();
});
});

it('should return all categories', function(done) {
request
.get(resourceApiUrl)
.query({
'type': 'Item'
})
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
var category = res.body;
_expect(category.length).to.be.greaterThan(0);
_expect(category[0].id).to.be.a('number');
_expect(category[0].type).to.be.a('string');
done();
});
});


/**
* Show the form for creating the specified resource.
* GET /categories/:id
*
* @param int $id
* @return Response
*/
it('should show the form for creating a new resource', function() {
// Not used in an angular application
// since the angular application will route to the edit form
// and display it
_expect(true).to.be(true);
});

/**
* Display the specified resource.
* GET /categories/{id}
*
* @param int $id
* @return Response
*/
it('should display the specified resource.', function(done) {
request
.get(resourceApiUrl + '/' + id)
.query({
'model': 'Items'
})
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
_expect(res.body.id).to.be(id);
done();
});
});

/**
* Remove the specified resource from storage.
* DELETE /categories/:id
*
* @param int $id
* @return Response
*/
it('should remove the specified resource from storage.', function(done) {
request
.del(resourceApiUrl + '/' + id)
.set('X-Access-Token', authToken)
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
_expect(res.body.message).to.match(/(success)/);
done();
});
});

it('should assert the document was deleted.', function(done) {
request
.get(resourceApiUrl + '/' + id)
.query({
'model': 'Items'
})
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
_expect(res.body).to.be(null);
done();
});
});

});
29 changes: 29 additions & 0 deletions tests/server/resources/countries.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var request = require('superagent'),
_expect = require('expect.js'),
resourceApiUrl = 'http://localhost:3000/api/countries';

describe('Countries resource API tests', function() {

/**
* Display a listing of the resource.
* GET /countries
*
* @return Response
*/
it('should return countries or empty array if DB is empty', function(done) {
request
.get(resourceApiUrl)
.accept('application/json')
.end(function(err, res) {
_expect(res.status).to.be(200);
if (res.body.length === 0) {
_expect(res.body).to.be.an('array');
} else {
var country = res.body;
_expect(country.length).to.be.greaterThan(0);
_expect(country[0].name).to.be.a('string');
}
done();
});
});
});
Loading

0 comments on commit 02247b2

Please sign in to comment.