From 84c07224a04189d42abbcd2dbfaf541fa53a90ff Mon Sep 17 00:00:00 2001 From: James Lal Date: Wed, 10 Oct 2012 11:31:20 -0700 Subject: server/capability tests --- test/servers/helper.js | 98 ++++++++++++++++++++++++++++++++++++++ test/servers/home_test.js | 13 +++++ test/servers/index.js | 66 ++++++++++++++++++++++++++ test/servers/query_test.js | 105 +++++++++++++++++++++++++++++++++++++++++ test/servers/resources_test.js | 71 ++++++++++++++++++++++++++++ test/servers/servers.json.tpl | 15 ++++++ 6 files changed, 368 insertions(+) create mode 100644 test/servers/helper.js create mode 100644 test/servers/home_test.js create mode 100755 test/servers/index.js create mode 100644 test/servers/query_test.js create mode 100644 test/servers/resources_test.js create mode 100644 test/servers/servers.json.tpl (limited to 'test') diff --git a/test/servers/helper.js b/test/servers/helper.js new file mode 100644 index 0000000..260b4cb --- /dev/null +++ b/test/servers/helper.js @@ -0,0 +1,98 @@ +// taken from mocha +const COLORS = { + 'pass': 90, + 'fail': 31, + 'bright pass': 92, + 'bright fail': 91, + 'bright yellow': 93, + 'pending': 36, + 'suite': 0, + 'error title': 0, + 'error message': 31, + 'error stack': 90, + 'checkmark': 32, + 'fast': 90, + 'medium': 33, + 'slow': 31, + 'green': 32, + 'light': 90, + 'diff gutter': 90, + 'diff added': 42, + 'diff removed': 41 +}; + +function color(type, str) { + return '\u001b[' + COLORS[type] + 'm' + str + '\u001b[0m'; +} + +var util = require('util'); +var Caldav = require('../../lib/caldav'); +var debug = require('debug'); + +var logs = { + 'normal': debug('caldav:test'), + 'detailed': debug('caldav:test') +}; + +var Helper = { + formatObject: function(obj) { + var format = util.inspect( + obj, + false, + 4, + true + ); + + return format; + }, + + log: function(message, object, level) { + var log = debug('caldav:test'); + + if (!level) + level = 'normal'; + + if (level !== 'normal') { + log = debug('caldav:test-' + level); + } + + log(message, Helper.formatObject(object)); + }, + + /** + * Return new caldav connection object. + * Gets config information from testEnv `serverConfig`. + * + * @return {CalDav.Connection} live connection. + */ + connection: function() { + var config = testEnv.serverConfig; + + return new Caldav.Connection({ + domain: config.domain, + user: config.user, + password: config.password + }); + }, + + findEnvHome: function(done) { + var config = testEnv.serverConfig; + var con = Helper.connection(); + + var home = new Caldav.Request.CalendarHome(con, { + url: config.uri + }); + + home.send(function(err, data) { + if (err) { + done(err); + return; + } + Helper.log('found home', data); + testEnv.homeUrl = data.url; + done(); + }); + } +}; + +module.exports = Helper; diff --git a/test/servers/home_test.js b/test/servers/home_test.js new file mode 100644 index 0000000..813f954 --- /dev/null +++ b/test/servers/home_test.js @@ -0,0 +1,13 @@ +var debug = require('debug')('caldav:test'); +var helper = require('./helper.js'); +var Caldav = require('../../lib/caldav'); +var assert = require('assert'); + +suite('home::', function() { + test('detect', function(done) { + helper.findEnvHome(function() { + assert.ok(testEnv.homeUrl, 'should set home uri'); + done(); + }); + }); +}); diff --git a/test/servers/index.js b/test/servers/index.js new file mode 100755 index 0000000..ae5fd02 --- /dev/null +++ b/test/servers/index.js @@ -0,0 +1,66 @@ +#! /usr/bin/env node + +/** imports */ +var Mocha = require('mocha'); +var fs = require('fs'); +var fsPath = require('path'); +var debug = require('debug')('cli'); + +/** server configurations */ +var configurations = JSON.parse(require('fs').readFileSync( + __dirname + '/servers.json', 'utf8' +)); + + +var configTypes = Object.keys(configurations); +var program = require('commander'); + +/** default tests */ +var tests = [ + 'home_test.js', + 'resources_test.js', + 'query_test.js' +]; + +/** cli::setup */ +program.option( + '-s, --server ', + 'target server: ' + + '[ ' + configTypes.join(', ') + ' ]' +); + + +program.on('--help', function() { + console.log(' Logging:'); + console.log(); + console.log(' Use DEBUG=caldav:* to show all test logs'); + console.log(); +}); + +program.parse(process.argv); + +testEnv = { + serverConfig: configurations[program.server], + server: program.server +}; + +/** load tests */ +var inputTests = program.args; + +if (inputTests.length) + tests = inputTests; + +tests = tests.map(function(test) { + if (test[0] !== '/') { + test = fsPath.join(process.cwd(), test); + } + debug('loading', test); + return test; +}); + +/** run mocha tests */ +var mocha = new Mocha(); +mocha.files = tests; +mocha.ui('tdd'); +mocha.reporter(program.reporter || 'spec'); +mocha.run(process.exit); diff --git a/test/servers/query_test.js b/test/servers/query_test.js new file mode 100644 index 0000000..2af2787 --- /dev/null +++ b/test/servers/query_test.js @@ -0,0 +1,105 @@ +var debug = require('debug')('caldav:test:detailed'); +var helper = require('./helper.js'); +var Caldav = require('../../lib/caldav'); +var assert = require('assert'); +require('../support/ical.js'); + +suite('query', function() { + var calendars; + var con; + + suiteSetup(function(done) { + if (!testEnv.homeUrl) { + helper.findEnvHome(done); + } else { + done(); + } + }); + + suiteSetup(function(done) { + con = helper.connection(); + var resources = new Caldav.Request.Resources(con, { + url: testEnv.homeUrl + }); + + resources.addResource('calendar', Caldav.Resources.Calendar); + resources.prop('displayname'); + resources.prop('resourcetype'); + + // found calendar home find calendars. + resources.send(function(err, data) { + helper.log('calendars:', data, 'detailed'); + helper.log('found calendars:', Object.keys(data.calendar)); + + calendars = data.calendar; + + if (Object.keys(calendars).length < 1) { + console.error('No calendars'); + helper.log('no calendars', true, 'fatal'); + process.exit(0); + } + + done(); + }); + }); + + function parseString(ical, callback) { + var event; + var exceptions = []; + var primary; + var build = new ICAL.ComponentParser(); + + //build.onevent = function() { + //} + + //build.oncomplete = function() { + //callback(primary); + //} + } + + test('limit query', function(done) { + var keys = Object.keys(calendars); + var cal = calendars[keys[0]]; + var query = cal.createQuery(); + + var comp = query.filter.setComp('VCALENDAR'). + comp('VEVENT'); + + + comp.setTimeRange({ + start: '20121001T000000Z', + end: '20131001T000000Z' + }); + + query.prop('getetag'); + + var comp = query.data.setComp('VCALENDAR'); + var event = comp.comp('VEVENT'); + + event.prop([ + 'DTSTART', + 'DTEND', + 'SUMMARY', + 'DESCRIPTION' + ]); + + query.send(function(err, data) { + var list = []; + + for (var key in data) { + list.push({ + url: key, + ical: data[key]['calendar-data'].value + }); + } + + + list.forEach(function(item) { + console.log(item.url, item.ical); + }); + + done(); + }); + }); + +}); diff --git a/test/servers/resources_test.js b/test/servers/resources_test.js new file mode 100644 index 0000000..840b17c --- /dev/null +++ b/test/servers/resources_test.js @@ -0,0 +1,71 @@ +var debug = require('debug')('caldav:test:detailed'); +var helper = require('./helper.js'); +var Caldav = require('../../lib/caldav'); +var assert = require('assert'); + +suite('resources', function() { + + suiteSetup(function(done) { + if (!testEnv.homeUrl) { + helper.findEnvHome(done); + } else { + done(); + } + }); + + function checkCalendarCap(url, calendar) { + + function cap(field) { + var value = calendar[field]; + + if (!value && value != 0) { + helper.log(url + ' missing ' + field, false, 'cap'); + } else { + helper.log(url + ' ' + field, value, 'cap'); + } + } + + cap('color'); + cap('ctag'); + cap('privilegeSet'); + cap('name'); + cap('description'); + } + + test('find calendars', function(done) { + var con = helper.connection(); + var resources = new Caldav.Request.Resources(con, { + url: testEnv.homeUrl + }); + + resources.addResource('calendar', Caldav.Resources.Calendar); + resources.prop(['ical', 'calendar-color']); + resources.prop(['caldav', 'calendar-description']); + resources.prop(['caldav', 'calendar-timezone']); + resources.prop('displayname'); + resources.prop('resourcetype'); + resources.prop('getlastmodified'); + resources.prop('current-user-privilege-set'); + resources.prop(['calserver', 'getctag']); + + // found calendar home find calendars. + resources.send(function(err, data) { + helper.log('calendars:', data, 'detailed'); + + if (!data || !data.calendar) { + console.log('no calendars - fatal exiting.'); + helper.log('no calendars', err, 'fatal'); + process.exit(0); + } + + helper.log('found calendars:', Object.keys(data.calendar)); + + for (var id in data.calendar) { + checkCalendarCap(id, data.calendar[id]); + } + + done(); + }); + }); + +}); diff --git a/test/servers/servers.json.tpl b/test/servers/servers.json.tpl new file mode 100644 index 0000000..ce44f42 --- /dev/null +++ b/test/servers/servers.json.tpl @@ -0,0 +1,15 @@ +{ + "google": { + "domain": "https://calendar.google.com", + "user": "user", + "password": "pass", + "uri": "/calendar/dav/" + }, + + "yahoo": { + "domain": "https://caldav.calendar.yahoo.com", + "user": "user", + "password": "pass", + "uri": "/" + } +} -- cgit