from django.core.urlresolvers import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.test import LiveServerTestCase
from django.contrib.auth.models import User
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from github.views import *
from github.models import Hiren
import os


class HomePageTest(TestCase):

    def test_root_url_resolves_to_index_page_view(self):
        found = resolve('/')
        self.assertEqual(found.func, index)

    def test_home_page_returns_correct_html(self):
        request = HttpRequest()
        response = index(request)
        self.assertTrue(response.content.startswith(b'<html>'))
        self.assertIn(b'<title>Hiren: The Bunny</title>', response.content)
        self.assertIn(
            b'<a href="/login"><i class="fa fa-sign-in"></i> Login</a>', response.content)
        self.assertTrue(response.content.endswith(b'</html>'))


class LoginFunctionalTestCase(LiveServerTestCase):

    def setUp(self):
        User.objects.create_superuser(
            username='admin', password='admin', email='admin@admin.lol')
        if 'TRAVIS' in os.environ:
            username = os.environ["SAUCE_USERNAME"]
            access_key = os.environ["SAUCE_ACCESS_KEY"]
            hub_url = "%s:%s@ondemand.saucelabs.com:80" % (username, access_key)
            capabilities = DesiredCapabilities.FIREFOX.copy()
            capabilities['platform'] = "WINDOWS"
            capabilities['version'] = "10"
            self.browser = webdriver.Remote(desired_capabilities=capabilities,
                                            command_executor="http://%s/wd/hub" % hub_url)
        else:
            self.browser = webdriver.Firefox()
            self.browser.maximize_window()
            self.browser.implicitly_wait(5)
        super(LoginFunctionalTestCase, self).setUp()

    def tearDown(self):
        self.browser.quit()
        super(LoginFunctionalTestCase, self).tearDown()

    def test_login_user(self):
        self.browser.get('%s%s' % (self.live_server_url, "/login/"))


class HirenModelTest(TestCase):

    def test_saving_and_retrieving_item(self):
        item = Hiren()
        item.access_token = "bla bla"
        item.authorized = True
        item.save()

        saved_item = Hiren.objects.all()
        self.assertEqual(saved_item.count(), 1)

        saved_item_content = saved_item[0]
        self.assertEqual(saved_item_content.access_token, "bla bla")
        self.assertEqual(saved_item_content.authorized, True)
@@ -1906,58 +1906,58 @@
 (5)%0A
+%0A
     
-    super(LoginFunctionalTestCase, self).setUp
+def tearDown(self):%0A        self.browser.quit
 ()%0A%0A
@@ -1958,38 +1958,45 @@
 it()%0A%0A    def te
-arDown
+st_login_user
 (self):%0A        
@@ -2012,128 +2012,367 @@
 ser.
-quit()%0A        super(LoginFunctionalTestCase, self).tearDown()%0A%0A    def test_login_user(self):%0A        self.browser.get(
+get('%25s%25s' %25 (self.live_server_url, %22/login/%22))%0A        username = self.browser.find_element_by_name('username')%0A        username.send_keys('admin')%0A        password = self.browser.find_element_by_name('password')%0A        password.send_keys('admin')%0A        self.browser.find_element_by_id('login').submit()%0A        self.assertEqual(self.browser.current_url, 
 '%25s%25
@@ -2399,25 +2399,25 @@
 er_url, 
-%22/login/%22
+'/hiren/'
 ))%0A%0A%0Acla
@@ -2869,8 +2869,9 @@
 d, True)
+%0A

add new functional test for login button