Skip to content

Commit

Permalink
Added request mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
hakovala committed Dec 13, 2012
1 parent 3d93235 commit 03032ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config.json
@@ -1,4 +1,4 @@
{
"timeout" : 15,
"api_key" : "ac55f955686886c6cffc46684d2e7563"
"api_key" : "<YOUR TRAKT API KEY>"
}
1 change: 0 additions & 1 deletion lib/trakt.js
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"optimist": "~ 0.3.0"
},
"devDependencies": {
"nock": "~0.14.2",
"should": "~1.2.1",
"mocha": "~1.7.4"
},
Expand Down
46 changes: 37 additions & 9 deletions 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(){
Expand All @@ -19,39 +26,60 @@ 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)
})
})
})
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!')
})
})
})
Expand Down

0 comments on commit 03032ff

Please sign in to comment.