Skip to content

Instantly share code, notes, and snippets.

@flockonus
Created August 17, 2011 20:27
Show Gist options
  • Save flockonus/1152521 to your computer and use it in GitHub Desktop.
Save flockonus/1152521 to your computer and use it in GitHub Desktop.
/**
* INSTRUCTIONS
*
* install: npm install expresso
*
* (on project folder) run: express -s
*
*/
var app = require('../app.js'),
async = require('async'),
qs = require('qs'),
assert = require('assert'),
cookie = "",
asserted_login = qs.stringify( {
user: {
username: 'lab',
password: '123456',
}
}),
// All tests that do not require Auth to run
unauth_tests = {},
// All tests that require cookie to be signed
authed_tests = {}
/*
* Index should respond as HTML5
*/
unauth_tests.rootUrl = function(){
console.log("RUN",1)
assert.response( app, {
url: "/",
timeout: 200,
method: 'GET',
}, {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
},
function(res){
//console.info(res.headers)
//console.info('0')
assert.match(res.body, /^\<\!DOCTYPE html/, "is HTML5?")
},
"Asserting index page"
)
}
/*
* Before loggin in client should _NOT_ be allowed to access the data
*/
unauth_tests.userIndexAccessDenied = function(){
console.log("RUN",2)
assert.response( app, {
url: "/admin/users",
timeout: 200,
method: 'GET',
}, {
status: 200,
},
function(res){
//console.info(res.body)
var res_data = JSON.parse(res.body)
assert.length(res_data, 1)
assert.type(res_data[0], 'string')
},
"Restricted area off-limits!"
)
}
/*
* Empty parameters, should make validation fail
*/
unauth_tests.postEmptyLoginFail = function(){
console.log("RUN",3)
assert.response( app, {
url: '/login',
//timeout: 300,
method: 'POST',
data: ""
},
{
status: 200,
body: '["FAIL"]',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
}
)
}
/*
* Wrong parameters, should make validation fail, alike empty params
*/
unauth_tests.postWrongLoginFail = function(){
console.log("RUN",4)
assert.response( app, {
url: '/login',
//timeout: 300,
method: 'POST',
data: asserted_login+'--notnot',
},
{
status: 200,
body: '["FAIL"]',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
}
)
}
/*
* Valid client and params, login, return a access cookie!
*/
unauth_tests.postLoginWin = function(){
console.log("RUN",5)
assert.response( app, {
url: '/login',
//timeout: 300,
data: asserted_login,
method: 'POST',
},
{
status: 200,
body: '["OK"]',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
},
function(res){
//console.info('-3')
cookie = res.headers['set-cookie'][0].split(';')[0]
append_authed_tests()
},
"Asseting the login key-value, right"
)
}
for( var prop in unauth_tests){
exports[prop] = unauth_tests[prop]
}
setTimeout(function(){
exports['test async exports'] = function(){
console.log("RUN",6)
console.log("!!! I, late appending.. will run?() RAN !!")
assert.isNull(null);
};
}, 1000);
/*
* After .postLoginWin ran, we can run the authed tests, so only then, append then
*/
function append_authed_tests(){
for( var prop in authed_tests){
console.log('appending', prop)
exports[prop] = authed_tests[prop]
}
}
/*
* Authed test, requires signed cookie
*/
authed_tests.getUsers = function(){
console.log("RUN",7)
assert.response( app, {
url: '/admin/users',
//timeout: 300,
data: asserted_login,
headers: {
'Cookie': cookie,
},
method: 'GET',
},
{
status: 200,
//body: '["OK"]',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
},
function(res){
//console.info('-3')
},
"Get a list of all users in the DB"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment