2011-06-13 11:23:02 +08:00
require ( '../helper' ) ;
var _ = require ( 'underscore' )
, PSQL = require ( '../../app/models/psql' )
, assert = require ( 'assert' ) ;
2013-11-06 00:49:10 +08:00
var public _user = global . settings . db _pubuser ;
2013-11-18 18:42:43 +08:00
var dbopts _auth = {
host : global . settings . db _host ,
port : global . settings . db _port ,
user : _ . template ( global . settings . db _user , { user _id : 1 } ) ,
2013-11-18 20:31:11 +08:00
dbname : _ . template ( global . settings . db _base _name , { user _id : 1 } ) ,
pass : _ . template ( global . settings . db _user _pass , { user _id : 1 } )
2013-11-18 18:42:43 +08:00
}
var dbopts _anon = _ . clone ( dbopts _auth ) ;
dbopts _anon . user = global . settings . db _pubuser ;
2013-11-18 20:31:11 +08:00
dbopts _anon . pass = global . settings . db _pubuser _pass ;
2013-11-18 18:42:43 +08:00
2012-07-13 04:54:12 +08:00
suite ( 'psql' , function ( ) {
test ( 'test throws error if no args passed to constructor' , function ( ) {
var msg ;
2011-06-13 11:23:02 +08:00
try {
2012-06-02 04:07:22 +08:00
var pg = new PSQL ( ) ;
2011-06-13 11:23:02 +08:00
} catch ( err ) {
2012-07-13 04:54:12 +08:00
msg = err . message ;
2011-06-13 11:23:02 +08:00
}
2012-07-13 04:54:12 +08:00
assert . equal ( msg , "Incorrect access parameters. If you are accessing via OAuth, please check your tokens are correct. For public users, please ensure your table is published." ) ;
} ) ;
2011-06-13 11:23:02 +08:00
2012-07-13 04:54:12 +08:00
test ( 'test private user can execute SELECTS on db' , function ( done ) {
2013-11-18 18:42:43 +08:00
var pg = new PSQL ( dbopts _auth ) ;
2012-06-02 04:12:29 +08:00
var sql = "SELECT 1 as test_sum" ;
2011-06-13 11:23:02 +08:00
pg . query ( sql , function ( err , result ) {
2013-11-18 18:42:43 +08:00
assert . ok ( ! err , err ) ;
2011-06-13 11:23:02 +08:00
assert . equal ( result . rows [ 0 ] . test _sum , 1 ) ;
2012-07-13 04:54:12 +08:00
done ( ) ;
2011-06-13 11:23:02 +08:00
} ) ;
2012-07-13 04:54:12 +08:00
} ) ;
2011-06-13 11:23:02 +08:00
2012-07-13 04:54:12 +08:00
test ( 'test private user can execute CREATE on db' , function ( done ) {
2013-11-18 18:42:43 +08:00
var pg = new PSQL ( dbopts _auth ) ;
2012-06-02 04:12:29 +08:00
var sql = "DROP TABLE IF EXISTS distributors; CREATE TABLE distributors (id integer, name varchar(40), UNIQUE(name))" ;
2011-06-13 11:23:02 +08:00
pg . query ( sql , function ( err , result ) {
2012-07-13 04:54:12 +08:00
assert . ok ( _ . isNull ( err ) ) ;
done ( ) ;
2011-06-13 11:23:02 +08:00
} ) ;
2012-07-13 04:54:12 +08:00
} ) ;
2011-06-13 11:23:02 +08:00
2012-07-13 04:54:12 +08:00
test ( 'test private user can execute INSERT on db' , function ( done ) {
2013-11-18 18:42:43 +08:00
var pg = new PSQL ( dbopts _auth ) ;
2012-06-02 04:12:29 +08:00
var sql = "DROP TABLE IF EXISTS distributors1; CREATE TABLE distributors1 (id integer, name varchar(40), UNIQUE(name))" ;
2011-06-13 11:23:02 +08:00
pg . query ( sql , function ( err , result ) {
2012-06-02 04:12:29 +08:00
sql = "INSERT INTO distributors1 (id, name) VALUES (1, 'fish')" ;
2011-06-13 11:23:02 +08:00
pg . query ( sql , function ( err , result ) {
2012-07-13 04:54:12 +08:00
assert . deepEqual ( result . rows , [ ] ) ;
done ( ) ;
2011-06-13 11:23:02 +08:00
} ) ;
} ) ;
2012-07-13 04:54:12 +08:00
} ) ;
2011-06-13 11:23:02 +08:00
2013-11-06 00:49:10 +08:00
test ( 'test public user can execute SELECT on enabled tables' , function ( done ) {
2013-11-18 18:42:43 +08:00
var pg = new PSQL ( dbopts _auth ) ;
2013-11-06 00:49:10 +08:00
var sql = "DROP TABLE IF EXISTS distributors2; CREATE TABLE distributors2 (id integer, name varchar(40), UNIQUE(name)); GRANT SELECT ON distributors2 TO " + public _user + ";" ;
2011-06-13 11:23:02 +08:00
pg . query ( sql , function ( err , result ) {
2013-11-18 18:42:43 +08:00
pg = new PSQL ( dbopts _anon )
2011-06-13 11:23:02 +08:00
pg . query ( "SELECT count(*) FROM distributors2" , function ( err , result ) {
assert . equal ( result . rows [ 0 ] . count , 0 ) ;
2012-07-13 04:54:12 +08:00
done ( ) ;
2011-06-13 11:23:02 +08:00
} ) ;
} ) ;
2012-07-13 04:54:12 +08:00
} ) ;
2011-06-13 11:23:02 +08:00
2013-11-06 00:49:10 +08:00
test ( 'test public user cannot execute INSERT on db' , function ( done ) {
2013-11-18 18:42:43 +08:00
var pg = new PSQL ( dbopts _auth ) ;
2013-11-06 00:49:10 +08:00
var sql = "DROP TABLE IF EXISTS distributors3; CREATE TABLE distributors3 (id integer, name varchar(40), UNIQUE(name)); GRANT SELECT ON distributors3 TO " + public _user + ";" ;
2011-06-13 11:23:02 +08:00
pg . query ( sql , function ( err , result ) {
2013-11-18 18:42:43 +08:00
pg = new PSQL ( dbopts _anon ) ;
2011-06-13 11:23:02 +08:00
pg . query ( "INSERT INTO distributors3 (id, name) VALUES (1, 'fishy')" , function ( err , result ) {
2012-07-13 04:54:12 +08:00
assert . equal ( err . message , 'permission denied for relation distributors3' ) ;
done ( ) ;
2011-06-13 11:23:02 +08:00
} ) ;
} ) ;
2012-07-13 04:54:12 +08:00
} ) ;
2013-12-02 18:47:52 +08:00
test ( 'dbkey depends on dbopts' , function ( ) {
var opt1 = _ . clone ( dbopts _anon ) ;
opt1 . dbname = 'dbname1' ;
var pg1 = new PSQL ( opt1 ) ;
var opt2 = _ . clone ( dbopts _anon ) ;
opt2 . dbname = 'dbname2' ;
var pg2 = new PSQL ( opt2 ) ;
assert . ok ( pg1 . dbkey ( ) !== pg2 . dbkey ( ) ,
'both PSQL object using same dbkey ' + pg1 . dbkey ( ) ) ;
assert . ok ( _ . isString ( pg1 . dbkey ( ) ) , "pg1 dbkey is " + pg1 . dbkey ( ) ) ;
} ) ;
2014-06-02 20:48:38 +08:00
test ( 'eventedQuery provisions a cancel mechanism to abort queries' , function ( done ) {
var psql = new PSQL ( dbopts _auth ) ;
psql . eventedQuery ( "SELECT 1 as foo" , function ( err , query , queryCanceller ) {
assert . ok ( _ . isFunction ( queryCanceller ) ) ;
done ( ) ;
} ) ;
} ) ;
2012-07-13 04:54:12 +08:00
} ) ;