-
Notifications
You must be signed in to change notification settings - Fork 1
Programming Phase I
It has been 2 weeks since the coding phase has started and our focus has been mainly on the upgradation of the Server APIs. The first challenge was to implement the authentication mechanism in order to make the API more secure. After doing some research and some discussion with my mentor, we have finalized and implemented token-based authentication. More details can be found on the blog).
There was a major upgrade in the way unit test cases were defined. Earlier, we used to call the routes internally using the fake response to test the backend logic. But now we are following a much flexible method to test API using Mocha and Chai. Chai is an assertion library for node and it helps us write test cases in a beautiful and more realistic manner using actual API requests. Following are some code snippets just to show the difference.
// BEFORE
var res = {send: function(value) {
this.value = value;
}};
activities.findAll({params: {},query: {}}, res);
// Start test
describe('#findAll()', function() {
it('should return all activities', function() {
assert.equal(28, res.value.length);
});
it('should return all fields', function() {
for (var i = 0 ; i < res.value.length ; i++) {
var item = res.value[i];
assert.notEqual(item.id, undefined);
assert.notEqual(item.name, undefined);
assert.notEqual(item.version, undefined);
assert.notEqual(item.directory, undefined);
assert.notEqual(item.favorite, undefined);
assert.equal(item.activityId, null);
assert.notEqual(item.index, undefined);
}
});
});
// AFTER
it('it should return all fields', (done) => {
chai.request(server)
.get('/api/v1/activities')
.set('x-access-token', fakeUser.token)
.set('x-key', fakeUser.user.name)
.end((err, res) => {
res.should.have.status(200);
for (var i = 0; i < res.body.length; i++) {
res.body[i].should.be.a('object');
res.body[i].should.have.property('id').not.eql(undefined);
res.body[i].should.have.property('name').not.eql(undefined);
res.body[i].should.have.property('version').not.eql(undefined);
res.body[i].should.have.property('directory').not.eql(undefined);
res.body[i].should.have.property('favorite').not.eql(undefined);
res.body[i].should.have.property('activityId').eql(null);
res.body[i].should.have.property('index').not.eql(undefined);
}
done();
});
});The most important update in the API was the introduction of pagination to limit large results. Following call (/api/v1/users?offset=10&limit=2) gives us a glimpse of the response with pagination.
{
"users": [
{
"_id": "5940355ad9f05af46bc2f418",
"name": "tasa1d121",
"color": {
"stroke": "#FF0000",
"fill": "#0000FF"
},
"role": "student",
"password": "pass",
"language": "fr",
"timestamp": 1497380186442,
"private_journal": "5940355ad9f05af46bc2f417",
"shared_journal": "58c8ac3a28a2aa0153f58e0d"
},
{
"_id": "59403558d9f05af46bc2f416",
"name": "tasad121",
"color": {
"stroke": "#FF0000",
"fill": "#0000FF"
},
"role": "student",
"password": "pass",
"language": "fr",
"timestamp": 1497380184297,
"private_journal": "59403558d9f05af46bc2f415",
"shared_journal": "58c8ac3a28a2aa0153f58e0d"
}
],
"offset": 10,
"limit": 2,
"total": 24,
"sort": "name(asc)",
"links": {
"prev_page": "/api/v1/users?offset=8&limit=2",
"next_page": "/api/v1/users?offset=12&limit=2"
}
}Next steps would be complete all the modification for journal APIs and then finally, work on the documentation. Stay tuned for more updates.