from mock import patch, MagicMock
from unittest2 import TestCase
from py_selenium_server.exceptions import SeleniumJarFileNotFound
from py_selenium_server.selenium_server import SeleniumServerHandler
from util import mock_data, mock_latest_version


class TestSeleniumServerFindingFile(TestCase):
    def test_get_latest_version_of_selenium(self):
        with patch(
                "py_selenium_server.selenium_server."
                "get_latest_selenium_server_description_data",
                MagicMock(return_value=mock_data)):
            server = SeleniumServerHandler()
            self.assertEqual(server.get_latest_version_of_selenium(),
                             mock_latest_version)

    def test_get_latest_version_of_selenium_no_match(self):
        with patch(
            "py_selenium_server.selenium_server."
            "get_latest_selenium_server_description_data",
                MagicMock(return_value="<html>htfusgugfs</html>")):
            server = SeleniumServerHandler()
            self.assertRaises(
                SeleniumJarFileNotFound,
                server.get_latest_version_of_selenium)
            try:
                server.get_latest_version_of_selenium()
            except SeleniumJarFileNotFound as e:
                self.assertEqual(
                    str(e),
                    'Error could not get latest version of selenium:\n '
                    '<html>htfusgugfs</html>'
                    )