Skip to content

Commit

Permalink
Add test scripts + fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
wanasit committed Sep 30, 2013
1 parent 4616cd1 commit 7e11a8d
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 6 deletions.
10 changes: 5 additions & 5 deletions GoogleCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ CalendarList.prototype.patch = function(calendarId, calendarList, option, callba
// Calendars
function Calendars(request){ this.request = request; }

Calendars.prototype.clear = function() {
Calendars.prototype.clear = function(calendarId, option, callback) {
if(!callback){ callback = option; option = {}; }
calendarId = encodeURIComponent(calendarId);
this.request('POST', '/calendars/'+calendarId+'/clear', option, {}, null, callback);
}

Calendars.prototype.delete = function() {
Calendars.prototype.delete = function(calendarId, option, callback) {
if(!callback){ callback = option; option = {}; }
calendarId = encodeURIComponent(calendarId);
this.request('DELETE', '/calendars/'+calendarId, option, {}, null, callback);
Expand All @@ -133,19 +133,19 @@ Calendars.prototype.get = function(calendarId, option, callback) {

Calendars.prototype.insert = function(calendar, option, callback) {
if(!callback){ callback = option; option = {}; }
this.request('POST', '/calendars', option, calendar, null, callback);
this.request('POST', '/calendars', option, {}, calendar, callback);
}

Calendars.prototype.update = function(calendarId, calendar, option, callback) {
if(!callback){ callback = option; option = {}; }
calendarId = encodeURIComponent(calendarId);
this.request('PUT', '/calendars/'+calendarId, option, calendar, null, callback);
this.request('PUT', '/calendars/'+calendarId, option, {}, calendar, callback);
}

Calendars.prototype.patch = function() {
if(!callback){ callback = option; option = {}; }
calendarId = encodeURIComponent(calendarId);
this.request('PATCH', '/calendars/'+calendarId, option, calendar, null, callback);
this.request('PATCH', '/calendars/'+calendarId, option, {}, calendar, callback);
}


Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"passport": "0.1.x",
"passport-google-oauth": "0.1.x",
"needle": "0.5.x",
"express": "3.x.x"
"express": "3.x.x",

"should" :"latest",
"mocha" :"latest",
"async" :"latest"
}
}
87 changes: 87 additions & 0 deletions specs/calendar.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var config = global.config = require(__dirname + '/config.js');
var util = require('util');
var should = require('should');
var async = require('async');
var needle = require('needle');

var google_calendar = require(__dirname +'/../GoogleCalendar.js');


function getAccessToken(callback){

var url = 'https://accounts.google.com/o/oauth2/token';
var content = {
refresh_token: config.refresh_token,
client_id: config.consumer_key,
client_secret: config.consumer_secret,
grant_type:'refresh_token',
}

needle.post(url, content, function(err, resp, body) {
if(resp.statusCode != 200){
return callback(body, null, null);
}

return callback(null, body.access_token, new Date().getTime() + (body.expires_in-3) * 1000);
})
}

describe('google_calendar.calendar',function() {

var gcal = null;
before(function(done) {
getAccessToken(function(err, access_token) {
if(err) return done(err);
gcal = google_calendar(access_token);
done();
})
})

describe('#get()',function() {

it('return the calendar' , function(done){

gcal.calendarList.list(function(err, _result) {

should.not.exist(err);
should.exist(_result);
should.exist(_result.items[0]);

gcal.calendars.get(_result.items[0].id,function(err, result) {

should.not.exist(err);
should.exist(result);
result.id.should.equal(_result.items[0].id)
result.kind.should.equal('calendar#calendar')
done()
})
})
})
})


describe('insert and delete',function() {

var inserted_calendar_id = null;
var calendar = { summary: 'Test' }

it('return create a new calendar and delete it' , function(done){
gcal.calendars.insert(calendar, function(err, result) {

should.not.exist(err);
should.exist(result);
inserted_calendar_id = result.id

gcal.calendars.delete(inserted_calendar_id, function(err, result) {
should.not.exist(err);
should.exist(result);

done()
})
})
})

})

})

69 changes: 69 additions & 0 deletions specs/calendar_list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var config = global.config = require(__dirname + '/config.js');
var util = require('util');
var should = require('should');
var async = require('async');
var needle = require('needle');

var google_calendar = require(__dirname +'/../GoogleCalendar.js');


function getAccessToken(callback){

var url = 'https://accounts.google.com/o/oauth2/token';
var content = {
refresh_token: config.refresh_token,
client_id: config.consumer_key,
client_secret: config.consumer_secret,
grant_type:'refresh_token',
}

needle.post(url, content, function(err, resp, body) {
if(resp.statusCode != 200){
return callback(body, null, null);
}

return callback(null, body.access_token, new Date().getTime() + (body.expires_in-3) * 1000);
})
}

describe('google_calendar.calendarList',function() {

var gcal = null;
before(function(done) {
getAccessToken(function(err, access_token) {
if(err) return done(err);
gcal = google_calendar(access_token);
done();
})
})

describe('#list()',function() {

it('return the calendar list' , function(done){

gcal.calendarList.list(function(err, result) {

should.not.exist(err);
should.exist(result);
should.exist(result.items);
should.exist(result.items.length);
done();
})
})

it('return the calendar list with parameters' , function(done){

gcal.calendarList.list({maxResults:1}, function(err, result) {

should.not.exist(err);
should.exist(result);
should.exist(result.items);
should.exist(result.items.length);
result.items.should.have.length(1);
done();
})
})
})

})

94 changes: 94 additions & 0 deletions specs/event.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
var config = global.config = require(__dirname + '/config.js');
var util = require('util');
var should = require('should');
var async = require('async');
var needle = require('needle');

var google_calendar = require(__dirname +'/../GoogleCalendar.js');


function getAccessToken(callback){

var url = 'https://accounts.google.com/o/oauth2/token';
var content = {
refresh_token: config.refresh_token,
client_id: config.consumer_key,
client_secret: config.consumer_secret,
grant_type:'refresh_token',
}

needle.post(url, content, function(err, resp, body) {
if(resp.statusCode != 200){
return callback(body, null, null);
}

return callback(null, body.access_token, new Date().getTime() + (body.expires_in-3) * 1000);
})
}

describe('google_calendar.events',function() {

var gcal = null;
var calendar_id = null;

before(function(done) {
getAccessToken(function(err, access_token) {
if(err) return done(err);
gcal = google_calendar(access_token);
gcal.calendars.insert({summary:'Test'}, function(err, result) {

should.not.exist(err);
should.exist(result);
calendar_id = result.id

done();
})
})
})

after(function(done) {
gcal.calendars.delete(calendar_id, function(err, result) {
done();
})
})

describe('#list()',function() {

it('return the events list' , function(done){
gcal.events.list(calendar_id, function(err, result) {

should.not.exist(err);
should.exist(result);
should.exist(result.items);
should.exist(result.items.length);
done()
})
})
})



describe('#insert()',function() {

var event = { summary:'Hello World' ,
start: { dateTime: new Date().toISOString() },
end: { dateTime: new Date().toISOString() }
}

it('return an event' , function(done){
gcal.events.insert(calendar_id, event, function(err, result) {

should.not.exist(err);
should.exist(result);
should.exist(result.id);

result.summary.should.equal(event.summary);
done()
})
})
})



})

0 comments on commit 7e11a8d

Please sign in to comment.