 			"(id, username, permissions, sessid, sess_time) " +
 			"VALUES (112, '112', 'admin', '123456', NOW() - INTERVAL '3600 SECOND' )"
 		
-		timeBefore = new Date(queryOne('SELECT sess_time FROM uniusers WHERE id = 112').sess_time)
+		timeBefore = new Date(query.val('SELECT sess_time FROM uniusers WHERE id = 112').sess_time)
 		users.sessionInfoRefreshing.sync null, conn, '123456', 7200, true
 		sync.sleep 100
-		timeAfter = new Date(queryOne('SELECT sess_time FROM uniusers WHERE id = 112').sess_time)
+		timeAfter = new Date(query.val('SELECT sess_time FROM uniusers WHERE id = 112').sess_time)
 		
 		#test.ok timeBefore < timeAfter, 'should update session timestamp with asyncUpdate'
 		test.done()

#		'should fail on nonexistent table'
#	)

	migrateTables 'permission_kind', 'uniusers'
	query 'INSERT INTO uniusers (username) VALUES ( $1 )', ['Sauron']
	
	test.strictEqual userExists('Sauron'), true, 'should return true if user exists'

	test.done()


exports.accessGranted = (test) ->
	clearTables 'uniusers', 'characters', 'locations'
	query 'INSERT INTO locations (id, initial) VALUES (2, 1)'
	users.registerUser.sync null, conn, 'TheUser', 'password', 'user'
	
	[
		{name:'TheUser',   pass:'password',  ok:true,  msg:'should return true for valid data'}
		{name:'WrongUser', pass:'password',  ok:false, msg:'should return false if user does not exist'}
		{name:'WrongUser', pass:'wrongpass', ok:false, msg:'should return false if password is wrong'}
		{name:'THEUSER',   pass:'password',  ok:true,  msg:'should ignore capitalization'}
		{name:'theuser',   pass:'password',  ok:true,  msg:'should ignore capitalization (2)'}
	].forEach (t) ->
		granted = users.accessGranted.sync null, conn, t.name, t.pass
		test.strictEqual granted, t.ok, t.msg
	test.done()


exports.createSession = (test) ->
	clearTables 'uniusers', 'locations', 'characters'
	query 'INSERT INTO locations (id, initial) VALUES (2, 1)'
	users.registerUser.sync null, conn, 'Мохнатый Ангел', 'password', 'user'
	user0 = query.row 'SELECT sessid FROM uniusers'
	users.createSession.sync null, conn, 'МОХНАТЫЙ ангел'
	user1 = query.row 'SELECT sessid, sess_time FROM uniusers'
	
	test.ok user0.sessid isnt user1.sessid, 'should change sessid'
	test.ok user1.sess_time.getTime() > Date.now() - 60000, 'should update session timestamp'
	test.done()

