Skip to content

Instantly share code, notes, and snippets.

@livestreamer
Created November 21, 2013 13:24
Show Gist options
  • Save livestreamer/7581503 to your computer and use it in GitHub Desktop.
Save livestreamer/7581503 to your computer and use it in GitHub Desktop.
var assert = require('assert')
var util = require('util')
var testUtils = require('./utils')
var vows = require('vows')
var should = require('should')
var APIeasy = require('api-easy')
var _ = require('underscore')
var Logger = require('../lib/logger')
var settings = require('../lib/global').settings
var global = require('../lib/global')
var r = require('mersenne')
var setupUsingModel = testUtils.newSetupUsingModel
var Event = require('../models').Event
var async = require('async')
var Account = require('../models').Account
var should = require('chai').should
var testAccount = {
full_name: 'mark' + r.rand(10000000000000000),
password: 'foofoofoo',
email:r.rand(10000000000000000)+'abc' + r.rand(10000000000000000) + '@xyz.com',
plan_id: 9
}
var testEvent = {
short_name: "vasanth" + r.rand(10000000000000000),
full_name: "vasanth's event",
draft: false,
start_time : new Date().toISOString(),
end_time : new Date(Date.now() + (1000 * 60 * 60 * 60)).toISOString()
}
var enterpriseAccount, pwpEvent
vows.describe('Event Password Grant').addBatch({
'When setting an enterprise account': testUtils.setupUsingModel(
Account,
function() {return _.clone(testAccount)},
function(resp){
enterpriseAccount = resp
testEvent.owner_account_id = enterpriseAccount.id
},
true)
}).addBatch({
'When setting an password protected event': testUtils.setupUsingModel(
Event,
function() {return _.clone(testEvent)},
function(resp){
pwpEvent = resp
},
true)
}).addBatch({
'When getting access token for enterpriseAccount ': {
topic: function() {
var suite = APIeasy.describe('Event Password Grant API')
if (vows && vows.options && vows.options.reporter) {
suite.suite.reporter = vows.options.reporter
}
suite.use(settings.getAuthSettings().host, settings.getAuthSettings().port)
.discuss('When auth')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.path('/oauth/access_token')
.post({client_id: '289ef33f7caa0c346c3025ff518ada99',
client_secret: '511901d55797644f2bf78716518adaa3',
username: testAccount.email, password: testAccount.password,
grant_type: 'password'})
.expect(200)
.expect.apply(suite, testUtils.checkResponse(
'It should respond with a valid token',
function(result){
enterPriseAccountAcccessToken = result.access_token
return (null !== enterPriseAccountAcccessToken)}))
.unpath()
.undiscuss()
suite.run(this.callback.bind(null, null));
},
'shuld get the access token for the user': function (err, results) {
assert.equal(results.errored, 0);
assert.equal(results.broken, 0);
}
}
}).addBatch({
'While creating a password for the event as event owner': {
topic: function() {
var suite = APIeasy.describe('Event API')
if (vows && vows.options && vows.options.reporter) {
suite.suite.reporter = vows.options.reporter
}
suite.use(testUtils.config.host, testUtils.config.port)
.discuss('When auth')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.setHeader('Authorization', 'Bearer ' + enterPriseAccountAcccessToken )
.path('/accounts/' + enterpriseAccount.id + '/events/' + pwpEvent.id)
.put({password: 'password', is_password_protected: 'true'})
.expect(200)
.undiscuss()
suite.run(this.callback.bind(null, null));
},
'Should create the password for the event': function (err, results) {
assert.equal(results.errored, 0);
assert.equal(results.broken, 0);
}
}
}).addBatch({
'While generating a password token for the event as the event owner': {
topic: function() {
var suite = APIeasy.describe('Event Password Grant API')
if (vows && vows.options && vows.options.reporter) {
suite.suite.reporter = vows.options.reporter
}
suite.use(testUtils.config.host, testUtils.config.port)
.discuss('When auth')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.setHeader('Authorization', 'Bearer ' + enterPriseAccountAcccessToken )
.path('/accounts/' + enterpriseAccount.id + '/events/' + pwpEvent.id + '/password_tokens')
.post()
.expect(200)
.undiscuss()
suite.run(this.callback.bind(null, null));
},
'Should create the password for the event': function (err, results) {
assert.equal(results.errored, 0);
assert.equal(results.broken, 0);
}
},
'While generating a password token for the event as the non event owner': {
topic: function() {
var suite = APIeasy.describe('Event Password Grant API')
if (vows && vows.options && vows.options.reporter) {
suite.suite.reporter = vows.options.reporter
}
suite.use(testUtils.config.host, testUtils.config.port)
.discuss('When auth')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.setHeader('Authorization', 'Bearer blah-blah-access-tokens' )
.path('/accounts/' + enterpriseAccount.id + '/events/' + pwpEvent.id + '/password_tokens')
.post()
.expect(401)
.undiscuss()
suite.run(this.callback.bind(null, null));
},
'Should not create the password for the event': function (err, results) {
assert.equal(results.errored, 0);
assert.equal(results.broken, 0);
}
}
})
.addBatch({
'A POST request to create password token with correct password': {
topic: function() {
var suite = APIeasy.describe('Event Password Grant API')
if (vows && vows.options && vows.options.reporter) {
suite.suite.reporter = vows.options.reporter
}
suite.use(testUtils.config.host, testUtils.config.port)
.discuss('When auth')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.path('/accounts/' + enterpriseAccount.id + '/events/' + pwpEvent.id + '/password_tokens')
.post({password: 'password'})
.expect(200)
.expect('Not this time', function (err, resp, body) {
var passwordToeknResponse
try {
passwordToeknResponse = JSON.parse(body)
}
catch(e) {return assertError(e)}
passwordToeknResponse.should.have.property('password_token')
passwordToeknResponse.should.have.property('password_token_expires_at')
passwordToeknResponse.should.have.property('refresh_token')
passwordToeknResponse.should.have.property('refresh_token_expires_at')
passwordToeknResponse.should.have.property('recommended_refresh_interval')
passwordToeknResponse.should.have.property('generated_at')
})
.unpath()
.undiscuss()
suite.run(this.callback.bind(null, null));
},
'should generate the password token ': function (err, results) {
assert.equal(results.errored, 0);
assert.equal(results.broken, 0);
}
},
'A POST request to create password token with incorrect password': {
topic: function() {
var suite = APIeasy.describe('Event Password Grant API')
if (vows && vows.options && vows.options.reporter) {
suite.suite.reporter = vows.options.reporter
}
suite.use(testUtils.config.host, testUtils.config.port)
.discuss('When auth')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.path('/accounts/' + enterpriseAccount.id + '/events/' + pwpEvent.id + '/password_tokens')
.post({password: 'bananapassword'})
.expect(403)
.expect('Should return in a invalid password error', function(err, res, body) {
var errorResponse
try {
errorResponse = JSON.parse(body)
}
catch(e) { return assertError(e) }
errorResponse.name.should.equal('InvalidEventPasswordError')
errorResponse.message.should.equal('invalid password')
})
.unpath()
.undiscuss()
suite.run(this.callback.bind(null, null));
},
'should not generate the password token': function (err, results) {
assert.equal(results.errored, 0);
assert.equal(results.broken, 0);
}
}
}).export(module)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment