# -*- coding: utf-8 -*-
# Copyright (c) 2014 Ionuț Arțăriși <ionut@artarisi.eu>
# This file is part of shorter.

# shorter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# shorter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with shorter. If not, see <http://www.gnu.org/licenses/>.

import multiprocessing
import unittest

from selenium import webdriver
from sqlalchemy import create_engine

from shorter import config
from shorter.database import Base, db_session
from shorter.web import app


class UITests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

        engine = create_engine('sqlite://')
        db_session.configure(bind=engine)
        Base.metadata.create_all(bind=engine)

        self.flask_process = multiprocessing.Process(target=app.run)
        self.flask_process.start()

    def tearDown(self):
        self.driver.close()
        self.flask_process.terminate()

        db_session.remove()

    def test_shorter_says_hello(self):
        self.driver.get(config.base_url)

        banner = self.driver.find_element_by_id('banner')
        self.assertIn('Hi', banner.text)

    def test_index_has_form(self):
        self.driver.get(config.base_url)

        form = self.driver.find_element_by_id('shorten')
        self.assertEqual(form.tag_name, 'form')

@@ -745,16 +745,49 @@
 cessing%0A
+from urllib.parse import urljoin%0A
 import u
@@ -826,16 +826,64 @@
 bdriver%0A
+from selenium.webdriver.common.keys import Keys%0A
 from sql
@@ -1014,16 +1014,49 @@
 rt app%0A%0A
+TEST_URL = 'http://example.com'%0A%0A
 %0Aclass U
@@ -1821,16 +1821,16 @@
 orten')%0A
-
         
@@ -1869,8 +1869,569 @@
 'form')%0A
+%0A    def test_shortens_url(self):%0A        self.driver.get(config.base_url)%0A        url_field = self.driver.find_element_by_name('url')%0A        url_field.send_keys(TEST_URL)%0A        url_field.send_keys(Keys.RETURN)%0A%0A        shortened = self.driver.find_element_by_id('shorter')%0A        self.assertEqual(shortened.tag_name, 'a')%0A        self.assertEqual(urljoin(config.base_url, '1'), shortened.text)%0A%0A        original = self.driver.find_element_by_id('original')%0A        self.assertEqual(original.tag_name, 'a')%0A        self.assertEqual(TEST_URL, original.text)%0A

add a webUI test for the url shortening