import os
import unittest
import sys
sys.path = ['../web'] + sys.path

import db

db.create()

import web

def input_hack():
    return dict()
web.input = input_hack
web.ctx.environ = dict()

# TODO: the following import triggers an exception for useexisting=True I cannot resolve
import admin

web.ctx.path = ""
admin.session_hook()
web.ctx.orm = db.connect()

import web.webapi

web.webapi.ctx.path = ""
web.webapi.ctx.home = ""
web.webapi.ctx.headers = []

# add client computers
from main.model import Computer, OrganizativeUnit
from config_client1 import client_daemon_port as c1_port
from config_client2 import client_daemon_port as c2_port
from config_client3 import client_daemon_port as c3_port

ou = OrganizativeUnit()
ou.name = "TestOU"
web.ctx.orm.add(ou)
web.ctx.orm.commit()

c1 = Computer("c1", "DE:AD:BE:EF:DE:AD")
c1.ip = "127.0.0.1"
c1.port = c1_port
c1.online = True
c1.ou = ou

c2 = Computer("c2", "DE:AD:BE:EF:DE:AE")
c2.ip = "127.0.0.1"
c2.port = c2_port
c2.online = True
c2.ou = ou

c3 = Computer("c3", "DE:AD:BE:EF:DE:AF")
c3.ip = "127.0.0.1"
c3.port = c3_port
c3.online = True
c3.ou = ou

web.ctx.orm.add(c1)
web.ctx.orm.add(c2)
web.ctx.orm.add(c3)
web.ctx.orm.commit()

suite = unittest.TestSuite()
tests = filter(lambda x: x.startswith('test') and x.endswith('.py'), os.listdir(os.path.dirname(__file__)))
for test in tests:
    tm = __import__(test[:-3])
    suite.addTest(tm.suite())

unittest.TextTestRunner(verbosity=2).run(suite)
