from pytest import raises
import webtest
import base64

from pywb.webapp.pywb_init import create_wb_router
from pywb.framework.wsgi_wrappers import init_app
from pywb.cdx.cdxobject import CDXObject

from urlparse import urlsplit


class TestProxyIPResolver(object):
    TEST_CONFIG = 'tests/test_config_proxy_ip.yaml'

    def setup(self):
        self.app = init_app(create_wb_router,
                            load_yaml=True,
                            config_file=self.TEST_CONFIG)

        self.testapp = webtest.TestApp(self.app)

    def _assert_basic_html(self, resp):
        assert resp.status_int == 200
        assert resp.content_type == 'text/html'
        assert resp.content_length > 0

    def _assert_basic_text(self, resp):
        assert resp.status_int == 200
        assert resp.content_type == 'text/plain'
        assert resp.content_length > 0

    def get_url(self, uri, addr='127.0.0.1'):
        parts = urlsplit(uri)
        env = dict(REQUEST_URI=uri, QUERY_STRING=parts.query, SCRIPT_NAME='', REMOTE_ADDR=addr)
        # 'Simulating' proxy by settings REQUEST_URI explicitly to full url with empty SCRIPT_NAME
        return self.testapp.get('/x-ignore-this-x', extra_environ=env)

    def test_proxy_ip_default_ts(self):
        resp = self.get_url('http://www.iana.org/')
        self._assert_basic_html(resp)

        assert '"20140127171238"' in resp.body
        assert 'wb.js' in resp.body

    def test_proxy_ip_get_defaults(self):
        resp = self.get_url('http://info.pywb.proxy/')
        assert resp.content_type == 'application/json'
        assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None}

    def test_proxy_ip_set_ts(self):
        resp = self.get_url('http://info.pywb.proxy/set?ts=1996')
        assert resp.content_type == 'application/json'
        assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': '1996'}

    def test_proxy_ip_set_ts_coll(self):
        resp = self.get_url('http://info.pywb.proxy/set?ts=1996&coll=all')
        assert resp.content_type == 'application/json'
        assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'}

    def test_proxy_ip_set_ts_coll_diff_ip(self):
        resp = self.get_url('http://info.pywb.proxy/set?ts=1996&coll=all', '127.0.0.2')
        assert resp.content_type == 'application/json'
        assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '1996'}

        resp = self.get_url('http://info.pywb.proxy/')
        assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None}

        resp = self.get_url('http://info.pywb.proxy/set?ip=127.0.0.2&ts=2005')
        assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2005'}

        resp = self.get_url('http://info.pywb.proxy/', '127.0.0.2')
        assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2005'}

    def test_proxy_ip_change_ts_for_ip(self):
        resp = self.get_url('http://info.pywb.proxy/set?ip=1.2.3.4&ts=20140126200624')
        assert resp.json == {'ip': '1.2.3.4', 'coll': None, 'ts': '20140126200624'}

        # different ts for this ip
        resp = self.get_url('http://www.iana.org/', '1.2.3.4')
        self._assert_basic_html(resp)

        assert '"20140126200624"' in resp.body

        # defaults for any other ip
        resp = self.get_url('http://www.iana.org/')
        self._assert_basic_html(resp)
        assert '"20140127171238"' in resp.body