diff --git a/config.json b/config.json index 52be635..b63562f 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,4 @@ { "timeout" : 15, - "api_key" : "ac55f955686886c6cffc46684d2e7563" + "api_key" : "" } \ No newline at end of file diff --git a/lib/trakt.js b/lib/trakt.js index 3ae0202..ab09ab7 100644 --- a/lib/trakt.js +++ b/lib/trakt.js @@ -15,7 +15,6 @@ var Trakt = module.exports = function(options) { if (options && options.api_key) this.config.api_key = options.api_key if (!this.config.api_key) throw new Error('No API key specified') - this.config = JSON.parse(fs.readFileSync('config.json', 'utf8')) if (options) { this.setUser.call(this, options.username, options.password, options.pass_hash) } diff --git a/package.json b/package.json index e0bcd01..fc1ffe1 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "optimist": "~ 0.3.0" }, "devDependencies": { + "nock": "~0.14.2", "should": "~1.2.1", "mocha": "~1.7.4" }, diff --git a/test/testTrakt.js b/test/testTrakt.js index bfe6af3..15e114c 100644 --- a/test/testTrakt.js +++ b/test/testTrakt.js @@ -1,9 +1,16 @@ var should = require('should') var fs = require('fs') +var nock = require('nock') var Trakt = require('../index.js'); -var config = JSON.parse(fs.readFileSync('test/test_config.json', 'utf8')) +var config = { + user: 'username', + pass: 'password', + api_key: 'api_key' +} + +var url = 'http://api.trakt.tv' describe('Trakt init', function(){ it('should be valid Trakt object', function(){ @@ -19,14 +26,21 @@ describe('Trakt init', function(){ }) describe('Trakt requests', function() { - var trakt = new Trakt() + var trakt = new Trakt({api_key: config.api_key}) describe('Get request', function() { it('should give error, missing param', function() { + var get_req = nock(url) + .get('/search/shows.json/' + config.api_key + '/') trakt.request('search', 'shows', {}, function(err, res) { - should.exist(err) + should.exist(err, 'expecting error') + err.message.should.equal('Missing parameters') + should.not.exist(res, "expecting undefined result") }) }) it('should be ok', function() { + var get_req = nock(url) + .get('/search/shows.json/' + config.api_key + '/hello') + .reply(200, '{ "status": "success" }') trakt.request('search', 'shows', {query: 'hello'}, function(err, res) { should.not.exist(err) should.exist(res) @@ -34,24 +48,38 @@ describe('Trakt requests', function() { }) }) describe('Post request', function() { - it('need user and password, specified in test_config.json', function() { - should.exist(config) - config.should.have.property('user') - config.user.should.not.equal('TEST USER') - }) it('should give error, no auth', function() { trakt.request('account', 'test', {}, function(err, res) { should.exist(err) + err.message.should.equal('POST messages require username and password') + }) + }) + it('should give authentication error', function() { + var post = nock(url) + .post('/account/test/' + config.api_key, {username: 'wrong', password: 'wrong'}) + .reply(401, {status: 'failure', error: 'failed authentication'}) + trakt.on('error', function(err) {}) + trakt.setUser('wrong', 'wrong', true) + trakt.request('account', 'test', {}, function(err, res) { + should.exist(err) + should.exist(res) + res.should.have.property('status') + res.should.have.property('error') + res.status.should.equal('failure') }) }) it('should be ok', function() { - trakt.setUser(config.user, config.pass) + var post = nock(url) + .post('/account/test/' + config.api_key, {username: 'username', password: 'password'}) + .reply(200, {status: 'success', message: 'all good!'}) + trakt.setUser(config.user, config.pass, true) trakt.request('account', 'test', {}, function(err, res) { should.not.exist(err) should.exist(res) res.should.have.property('status') res.should.have.property('message') res.status.should.equal('success') + res.message.should.equal('all good!') }) }) })