 #		'should fail on nonexistent table'
 #	)
 
-	migrateTables 'permission_kind', 'uniusers'
+	clearTables 'uniusers'
 	query 'INSERT INTO uniusers (username) VALUES ( $1 )', ['Sauron']
 	
 	test.strictEqual userExists('Sauron'), true, 'should return true if user exists'

#			test.done()


exports.generateSessId =
	testNoErrors: (test) ->
		_createSalt = users.createSalt
		i = 0
		users.createSalt = (len) ->
			'someid' + (++i)

		async.series [
			(callback) ->
				mg.migrate conn, { tables: ['permission_kind', 'uniusers'] }, callback
			(callback) ->
				conn.query "INSERT INTO uniusers (sessid) VALUES ('someid1')", callback
			(callback) ->
				conn.query "INSERT INTO uniusers (sessid) VALUES ('someid2')", callback
			(callback) ->
				users.generateSessId conn, 16, callback
			(callback) ->
				conn.query 'DROP TABLE uniusers', [], callback
		],
		(error, result) ->
			users.createSalt = _createSalt
			test.ifError error
			test.strictEqual result[3], 'someid3', 'sessid should be unique'
			test.done()

	testErrors: (test) ->
		users.generateSessId conn, 16, (error, result) ->
			test.ok error, 'should return error without table'
			test.done()


exports.idBySession =
	testNoErrors: (test) ->
		async.series [
			(callback) ->
				mg.migrate conn, { tables: ['permission_kind', 'uniusers'] }, callback
			(callback) ->
				conn.query "INSERT INTO uniusers ( id, sessid ) VALUES ( 3, 'someid' )", callback
			(callback) ->
				users.idBySession conn, "someid", callback
		],
		(error, result) ->
			test.ifError error
			test.strictEqual result[2], 3, 'should return correct user id'
			test.done()

	testWrongSessid: (test) ->
		async.series [
			(callback) ->
				mg.migrate conn, { tables: ['permission_kind', 'uniusers'] }, callback
			(callback) ->
				users.idBySession conn, 'someid', callback
			(callback) ->
				conn.query 'DROP TABLE uniusers', [], callback
		],
		(error, result) ->
			test.ok error, 'should return error on wrong sessid'
			test.done()

	testQueryError: (test) ->
		users.idBySession conn, 'someid', (error, result) ->
			test.ok error, 'should return error without table'
			test.done()


exports.closeSession =
	testNoErrors: (test) ->
		async.series [
			(callback) -> #0
				mg.migrate conn, { tables: ['permission_kind', 'uniusers'] }, callback
			(callback) ->
				conn.query "INSERT INTO uniusers ( sessid, sess_time ) VALUES ( 'someid', NOW() )", [], callback
			(callback) ->
				users.closeSession conn, 'someid', callback
			(callback) ->
				users.sessionInfoRefreshing conn, 'someid', 3600, callback
			(callback) ->
				users.closeSession conn, undefined, callback
		],
		(error, result) ->
			test.ifError error
			test.strictEqual result[3].sessionIsActive, false, 'session should have expired'
			test.strictEqual result[4], 'Not closing: empty sessid', 'should not fail with empty sessid'
			test.done()

	testErrors: (test) ->
		users.closeSession conn, 'someid', (error, result) ->
			test.ok error, 'should return error without table'
			test.done()


exports.createSalt = (test) ->

			"(id, username, permissions, sessid, sess_time) " +
			"VALUES (112, '112', 'admin', '123456', NOW() - INTERVAL '3600 SECOND' )"
		
		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(query.val('SELECT sess_time FROM uniusers WHERE id = 112').sess_time)
		
		#test.ok timeBefore < timeAfter, 'should update session timestamp with asyncUpdate'
		test.done()
