"""
open/DurusWorks/qp/pub/test/utest_common.py

"""
from durus.logger import direct_output
from qp.pub.common import get_publisher, get_request, get_hit, not_found
from qp.pub.common import get_response, get_path, redirect, clear_publisher
from qp.pub.common import get_user, get_session, redirect, not_found
from qp.pub.common import header, footer, page, respond, ensure_signed_in
from qp.pub.hit import Hit
from qp.pub.publish import DurusPublisher, RespondNow
from qp.pub.user import User
from qp.pub.session import Session
from qpy import xml
from sancho.utest import UTest
import sys

class Test(UTest):

    def _pre(self):
        direct_output(sys.stdout)
        clear_publisher()

    def _post(self):
        clear_publisher()

    def check_common(self):
        publisher = DurusPublisher()
        assert publisher == get_publisher()
        try:
            get_request()
            assert 0
        except AttributeError:
            pass
        hit = Hit(None, dict(SCRIPT_NAME='', PATH_INFO='/a', SERVER_NAME='w'))
        publisher.set_hit(hit)
        assert get_hit() is hit
        assert get_request() is hit.get_request()
        assert get_response() is hit.get_response()
        get_hit().set_session(publisher.create_session())
        assert isinstance(get_session(), Session), repr(get_session())
        assert isinstance(get_user(), User)
        assert not get_user()
        assert get_path() == '/a'
        try:
            redirect('there', permanent=True)
            assert 0
        except RespondNow:
            assert get_response().get_status_code() == 301
        try:
            not_found()
            assert 0
        except RespondNow:
            assert get_response().get_status_code() == 404
        assert isinstance(header('ok'), xml)
        assert isinstance(footer(), xml)
        assert isinstance(page('ok'), xml)
        try:
            respond('ok')
            assert 0
        except RespondNow:
            assert get_response().get_status_code() == 400
        try:
            respond('ok', status=200)
            assert 0
        except RespondNow:
            assert get_response().get_status_code() == 200
        try:
            ensure_signed_in()
            assert 0
        except RespondNow:
            assert get_response().get_status_code() == 302

if __name__ == '__main__':
    Test()
