Permalink
Browse files

Added request mocking

  • Loading branch information...
1 parent 3d93235 commit 03032ffd671a5946e85cec844a4a9caf2442e0de @hakovala committed Dec 13, 2012
Showing with 39 additions and 11 deletions.
  1. +1 −1 config.json
  2. +0 −1 lib/trakt.js
  3. +1 −0 package.json
  4. +37 −9 test/testTrakt.js
View
@@ -1,4 +1,4 @@
{
"timeout" : 15,
- "api_key" : "ac55f955686886c6cffc46684d2e7563"
+ "api_key" : "<YOUR TRAKT API KEY>"
}
View
@@ -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)
}
View
@@ -17,6 +17,7 @@
"optimist": "~ 0.3.0"
},
"devDependencies": {
+ "nock": "~0.14.2",
"should": "~1.2.1",
"mocha": "~1.7.4"
},
View
@@ -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,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!')
})
})
})

0 comments on commit 03032ff

Please sign in to comment.